-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
40 lines (33 loc) · 940 Bytes
/
auth.go
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
package main
import (
"encoding/base64"
"errors"
"fmt"
"golang.org/x/crypto/bcrypt"
"strings"
)
type AuthConfig struct {
AuthType string `yaml:"type"`
Passwdfile string
BasicAuthUsers map[string]string
}
// Authenticate receives the Authorization header content. Extracts the username/password
// and compares it to the password from AuthConfig for location.
func (ac *AuthConfig) Authenticate(authorizationHeader string) error {
if authorizationHeader == "" {
return errors.New("No authorization header")
}
hash := strings.Split(authorizationHeader, " ")
decoded, err := base64.StdEncoding.DecodeString(hash[1])
if err != nil {
fmt.Println(err)
}
creds := strings.Split(string(decoded), ":")
username := creds[0]
password := creds[1]
err = bcrypt.CompareHashAndPassword([]byte(ac.BasicAuthUsers[username]), []byte(password))
if err != nil {
return errors.New("Wrong credentials")
}
return nil
}