-
Notifications
You must be signed in to change notification settings - Fork 9
/
basicauth.go
78 lines (65 loc) · 2.23 KB
/
basicauth.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package basicauth
import (
"context"
"crypto/subtle"
"encoding/base64"
"github.com/mikeydub/go-gallery/env"
"github.com/mikeydub/go-gallery/service/logger"
"github.com/mikeydub/go-gallery/util"
)
type AuthTokenType string
const (
AuthTokenTypeRetool AuthTokenType = "Retool"
AuthTokenTypeMonitoring AuthTokenType = "Monitoring"
)
// AuthorizeHeader checks if the request has a Basic Auth header matching the specified
// username and password. Username is optional and will be ignored if nil.
func AuthorizeHeader(ctx context.Context, username *string, password string) bool {
gc := util.MustGetGinContext(ctx)
headerUsername, headerPassword, ok := gc.Request.BasicAuth()
if !ok {
return false
}
// Username is optional, but has to match if not nil
if username != nil {
if cmp := subtle.ConstantTimeCompare([]byte(*username), []byte(headerUsername)); cmp != 1 {
return false
}
}
// Password is required
if cmp := subtle.ConstantTimeCompare([]byte(password), []byte(headerPassword)); cmp != 1 {
return false
}
return true
}
// AuthorizeHeaderForAllowedTypes checks whether the request has a Basic Auth header matching one of the
// known token types. If the request has a valid token, it returns true. Otherwise, it returns false.
func AuthorizeHeaderForAllowedTypes(ctx context.Context, allowedTypes []AuthTokenType) bool {
authTokens := map[AuthTokenType]string{
AuthTokenTypeRetool: env.GetString("BASIC_AUTH_TOKEN_RETOOL"),
AuthTokenTypeMonitoring: env.GetString("BASIC_AUTH_TOKEN_MONITORING"),
}
for _, authType := range allowedTypes {
authToken, ok := authTokens[authType]
if !ok {
logger.For(ctx).Errorf("Basic auth: unknown type %s", authType)
continue
}
if AuthorizeHeader(ctx, nil, authToken) {
return true
}
}
return false
}
// MakeHeader takes a password and an optional username and base64 encodes them in the
// format required for a basic auth header. The output can be used as the value for the
// "Authorization" header.
func MakeHeader(username *string, password string) string {
usernameValue := ""
if username != nil {
usernameValue = *username
}
data := usernameValue + ":" + password
encoded := base64.StdEncoding.EncodeToString([]byte(data))
return "Basic " + encoded
}