/
basic_auth.go
154 lines (125 loc) · 4.58 KB
/
basic_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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// go-aah/security source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package scheme
import (
"fmt"
"aahframework.org/ahttp.v0"
"aahframework.org/config.v0"
"aahframework.org/essentials.v0"
"aahframework.org/log.v0"
"aahframework.org/security.v0/authc"
"aahframework.org/security.v0/authz"
)
var _ Schemer = (*BasicAuth)(nil)
type (
// BasicAuth struct provides aah's OOTB Basic Auth scheme.
BasicAuth struct {
BaseAuth
RealmName string
isFileRealm bool
subjectMap map[string]*basicSubjectInfo
}
basicSubjectInfo struct {
AuthcInfo *authc.AuthenticationInfo
AuthzInfo *authz.AuthorizationInfo
}
)
// Init method initializes the Basic authentication scheme from `security.auth_schemes`.
func (b *BasicAuth) Init(cfg *config.Config, keyName string) error {
b.AppConfig = cfg
b.KeyName = keyName
b.KeyPrefix = "security.auth_schemes." + b.KeyName
b.Name, _ = b.AppConfig.String(b.ConfigKey("scheme"))
b.RealmName = b.AppConfig.StringDefault(b.ConfigKey("realm_name"), "Authentication Required")
fileRealmPath := b.AppConfig.StringDefault(b.ConfigKey("file_realm"), "")
b.isFileRealm = !ess.IsStrEmpty(fileRealmPath)
// Basic auth configured to use file based user source
if b.isFileRealm {
fileRealmCfg, err := config.LoadFile(fileRealmPath)
if err != nil {
return err
}
b.subjectMap = make(map[string]*basicSubjectInfo)
for _, username := range fileRealmCfg.Keys() {
password := fileRealmCfg.StringDefault(username+".password", "")
if ess.IsStrEmpty(password) {
return fmt.Errorf("basicauth: '%v' key is required", username+".password")
}
authcInfo := authc.NewAuthenticationInfo()
authcInfo.Principals = append(authcInfo.Principals, &authc.Principal{Realm: "Basic", Claim: "Username", Value: username, IsPrimary: true})
authcInfo.Credential = []byte(password)
authzInfo := authz.NewAuthorizationInfo()
if roles, found := fileRealmCfg.StringList(username + ".roles"); found {
authzInfo.AddRole(roles...)
}
if permissions, found := fileRealmCfg.StringList(username + ".permissions"); found {
authzInfo.AddPermissionString(permissions...)
}
b.subjectMap[username] = &basicSubjectInfo{AuthcInfo: authcInfo, AuthzInfo: authzInfo}
}
}
var err error
b.passwordEncoder, err = passwordAlgorithm(b.AppConfig, b.KeyPrefix)
return err
}
// DoAuthenticate method calls the registered `Authenticator` with authentication token.
func (b *BasicAuth) DoAuthenticate(authcToken *authc.AuthenticationToken) (*authc.AuthenticationInfo, error) {
log.Info(authcToken)
var authcInfo *authc.AuthenticationInfo
var err error
if b.isFileRealm {
if subject, found := b.subjectMap[authcToken.Identity]; found {
ai := *subject.AuthcInfo
authcInfo = &ai
} else {
err = authc.ErrSubjectNotExists
}
} else {
if b.authenticator == nil {
log.Warnf("%s: basic auth is not properly configured in security.conf, possibly file realm or authenticator", b.Scheme())
return nil, authc.ErrAuthenticatorIsNil
}
authcInfo, err = b.authenticator.GetAuthenticationInfo(authcToken)
}
if err != nil {
log.Error(err)
return nil, err
}
// Compare passwords
isPasswordOk := b.passwordEncoder.Compare(authcInfo.Credential, []byte(authcToken.Credential))
if !isPasswordOk {
log.Errorf("Subject [%s] credentials do not match", authcToken.Identity)
return nil, authc.ErrAuthenticationFailed
}
if authcInfo.IsLocked || authcInfo.IsExpired {
log.Errorf("Subject [%s] is locked or expired", authcToken.Identity)
return nil, authc.ErrAuthenticationFailed
}
return authcInfo, nil
}
// DoAuthorizationInfo method calls registered `Authorizer` with authentication information.
func (b *BasicAuth) DoAuthorizationInfo(authcInfo *authc.AuthenticationInfo) *authz.AuthorizationInfo {
if b.isFileRealm {
return b.subjectMap[authcInfo.PrimaryPrincipal().Value].AuthzInfo
}
if b.authorizer == nil {
log.Warnf("%s: '%s' is not properly configured in security.conf", b.Scheme(), b.ConfigKey("authorizer"))
return authz.NewAuthorizationInfo()
}
authzInfo := b.authorizer.GetAuthorizationInfo(authcInfo)
if authzInfo == nil {
authzInfo = authz.NewAuthorizationInfo()
}
return authzInfo
}
// ExtractAuthenticationToken method extracts the authentication token information
// from the HTTP request.
func (b *BasicAuth) ExtractAuthenticationToken(r *ahttp.Request) *authc.AuthenticationToken {
username, password, _ := r.Unwrap().BasicAuth()
return &authc.AuthenticationToken{
Scheme: b.Scheme(),
Identity: username,
Credential: password,
}
}