-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathauth.go
More file actions
41 lines (37 loc) · 847 Bytes
/
Copy pathauth.go
File metadata and controls
41 lines (37 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"encoding/base64"
"fmt"
"net/http"
"strings"
)
type Credentials struct {
User string
Pass string
}
func ParseCredentials(s string) (*Credentials, error) {
parts := strings.SplitN(s, ":", 2)
if len(parts) != 2 || parts[0] == "" {
return nil, fmt.Errorf("expected user:password, got %q", s)
}
return &Credentials{User: parts[0], Pass: parts[1]}, nil
}
func (c *Credentials) Check(r *http.Request) bool {
auth := r.Header.Get("Proxy-Authorization")
if auth == "" {
return false
}
const prefix = "Basic "
if !strings.HasPrefix(auth, prefix) {
return false
}
decoded, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
if err != nil {
return false
}
parts := strings.SplitN(string(decoded), ":", 2)
if len(parts) != 2 {
return false
}
return parts[0] == c.User && parts[1] == c.Pass
}