This repository has been archived by the owner on Apr 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
authenticators.go
306 lines (248 loc) · 11 KB
/
authenticators.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package stormpath
import (
"fmt"
"net/http"
"time"
)
//AuthResult is implemented by any authentication result for any of the posible authenticators to retrieve
//the authentication result account.
type AuthResult interface {
GetAccount() *Account
}
//AuthenticationResult base authentication result for all authenticators
type AuthenticationResult struct {
Account *Account
}
//OAuthAccessTokenResult is the default OAuth response
type OAuthAccessTokenResult OAuthResponse
//OAuthClientCredentialsAuthenticationResult is the authentication result for the OAuthClientCredentialsAuthenticator
type OAuthClientCredentialsAuthenticationResult OAuthResponse
//StormpathAssertionAuthenticationResult is the authentication result for the StormpathAssertionAuthenticator
type StormpathAssertionAuthenticationResult CallbackResult
//Authenticator is the base authenticator type
//
//See https://github.com/stormpath/stormpath-sdk-spec/blob/master/specifications/authenticators.md
type Authenticator struct {
Application *Application
}
//BasicAuthenticator will authenticate the API Key and Secret of a Stormpath Account object. Authentication should succeed only if the following are true:
//
// * The provided API Key and Secret exist for an account that is reachable by the application.
// * The API Key is not disabled.
// * The Account is not disabled.
type BasicAuthenticator Authenticator
//OAuthRequestAuthenticator should authenticate OAuth2 requests. It will eventually support authenticating all 4 OAuth2 grant types.
//
//Specifically, right now, this class will authenticate OAuth2 access tokens, as well as handle API key for access token exchanges using the OAuth2 client credentials grant type.
type OAuthRequestAuthenticator struct {
Authenticator
ScopeFactory ScopeFactoryFunc
TTL time.Duration
}
//OAuthBearerAuthenticator should authenticate OAuth2 bearer tokens only. The token is an access token JWT that has been created by Stormpath. The token may have been created by the client_credential or password_grant flow. This can be determined by looking at the kid property in the header of the JWT. Password grant JWTs will have a kid, but client credential JWTs will not.
type OAuthBearerAuthenticator Authenticator
//OAuthClientCredentialsAuthenticator this authenticator accepts an Account's API Key and Secret, and gives back an access token in response. The authenticator should follow the same authentication rules as the BasicAuthenticator. The end-user (account) can request scope, if the scope factory determines that this scope is permitted, then the scope should be added to the access token.
//
//This authenticator is responsible for creating the access token. The Stormpath REST API does not yet provide the client_credential grant on the appplication's /oauth/token endpoint.
type OAuthClientCredentialsAuthenticator struct {
Authenticator
ScopeFactory ScopeFactoryFunc
TTL time.Duration
}
//ScopeFactoryFunc defines a function to valide scope for the cient credentials OAuth authentication
type ScopeFactoryFunc func(string) bool
//OAuthPasswordAuthenticator this authenticator accepts an account's username and password, and returns an access token response that is obtained by posting the username and password to the application's /oauth/token endpoint with the grant_type=password parameter.
type OAuthPasswordAuthenticator Authenticator
//OAuthRefreshTokenAuthenticator this authenticator accepts a previously-issued refresh token and post's it to the application's /oauth/token endpoint with the grant_type=refresh_token parameter. The response is a new access token response.
type OAuthRefreshTokenAuthenticator Authenticator
//OAuthStormpathTokenAuthenticator this authenticator takes a Stormpath Token JWT and posts it to the application's /oauth/token endpoint, as grant_type=stormpath_token. The result is an OAuthAccessTokenResult.
type OAuthStormpathTokenAuthenticator Authenticator
//StormpathAssertionAuthenticator this authenticator will verify the a JWT from an ID Site or SAML callback. It should verify that:
//
// * The token is not expired
// * The signature can be verified
// * The claims body does not contain an err property.
type StormpathAssertionAuthenticator Authenticator
//NewBasicAuthenticator returns a BasicAuthenticator for the given application
func NewBasicAuthenticator(application *Application) BasicAuthenticator {
return BasicAuthenticator{application}
}
//Authenticate authenticates the given account APIKey and APISecret
func (a BasicAuthenticator) Authenticate(accountAPIKey, accountAPISecret string) (*AuthenticationResult, error) {
apiKey, err := a.Application.GetAPIKey(accountAPIKey, MakeAPIKeysCriteria().WithAccount())
if err != nil {
return nil, err
}
if apiKey.Secret != accountAPISecret {
return nil, fmt.Errorf("Invalid API Key Secret")
}
if apiKey.Status == Disabled {
return nil, fmt.Errorf("API Key disabled")
}
if apiKey.Account.Status == Disabled {
return nil, fmt.Errorf("Account is disable")
}
return &AuthenticationResult{Account: apiKey.Account}, nil
}
//NewOAuthRequestAuthenticator creates a new OAuthRequestAuthenticator for a given Application
func NewOAuthRequestAuthenticator(application *Application) OAuthRequestAuthenticator {
authenticator := OAuthRequestAuthenticator{}
authenticator.Application = application
return authenticator
}
//Authenticate authenticates an http.Request value using the possible grant types that Stormpath supports,
//it returns an error if the request is not authenticated.
//
//Supported grant types:
// - password
// - client_credentials
// - refresh_token
func (a OAuthRequestAuthenticator) Authenticate(r *http.Request) (*OAuthAccessTokenResult, error) {
err := r.ParseForm()
if err != nil {
return nil, err
}
grantType := r.Form.Get("grant_type")
switch grantType {
case "password":
authResult, err := NewOAuthPasswordAuthenticator(a.Application).Authenticate(r.Form.Get("username"), r.Form.Get("password"))
if err != nil {
return nil, err
}
return authResult, nil
case "client_credentials":
accountAPIKeyID, accountAPIKeyScret, ok := r.BasicAuth()
if !ok {
return nil, fmt.Errorf("invalid_client")
}
oauthResponse, err := NewOAuthClientCredentialsAuthenticator(a.Application).Authenticate(accountAPIKeyID, accountAPIKeyScret, r.Form.Get("scope"))
if err != nil {
return nil, err
}
result := OAuthAccessTokenResult(*oauthResponse)
return &result, nil
case "refresh_token":
authResult, err := NewOAuthRefreshTokenAuthenticator(a.Application).Authenticate(r.Form.Get("refresh_token"))
if err != nil {
return nil, err
}
return authResult, nil
case "stormpath_social":
oauthResponse, err := a.Application.GetOAuthTokenSocialGrantType(r.Form.Get("providerId"), r.Form.Get("accessToken"), "")
if err != nil {
return nil, err
}
result := OAuthAccessTokenResult(*oauthResponse)
return &result, nil
}
return nil, fmt.Errorf("unsupported_grant_type")
}
func NewOAuthClientCredentialsAuthenticator(application *Application) OAuthClientCredentialsAuthenticator {
authenticator := OAuthClientCredentialsAuthenticator{}
authenticator.Application = application
authenticator.TTL = 3600 * time.Second
return authenticator
}
func (a OAuthClientCredentialsAuthenticator) Authenticate(accountAPIKeyID, accountAPIKeySecret, scope string) (*OAuthClientCredentialsAuthenticationResult, error) {
if a.ScopeFactory != nil {
if !a.ScopeFactory(scope) {
return nil, fmt.Errorf("invalid_scope")
}
}
oAuthResponse, err := a.Application.GetOAuthTokenClientCredentialsGrantType(accountAPIKeyID, accountAPIKeySecret)
if err != nil {
return nil, fmt.Errorf("invalid_client")
}
oauthResult := OAuthClientCredentialsAuthenticationResult(*oAuthResponse)
return &oauthResult, nil
}
func NewOAuthPasswordAuthenticator(application *Application) OAuthPasswordAuthenticator {
return OAuthPasswordAuthenticator{application}
}
func (a OAuthPasswordAuthenticator) Authenticate(username, password string) (*OAuthAccessTokenResult, error) {
oauthResponse, err := a.Application.GetOAuthToken(username, password)
if err != nil {
return nil, err
}
authResult := OAuthAccessTokenResult(*oauthResponse)
return &authResult, nil
}
func NewOAuthRefreshTokenAuthenticator(application *Application) OAuthRefreshTokenAuthenticator {
return OAuthRefreshTokenAuthenticator{application}
}
func (a OAuthRefreshTokenAuthenticator) Authenticate(refreshToken string) (*OAuthAccessTokenResult, error) {
oauthResponse, err := a.Application.RefreshOAuthToken(refreshToken)
if err != nil {
return nil, err
}
authResult := OAuthAccessTokenResult(*oauthResponse)
return &authResult, nil
}
func NewOAuthStormpathTokenAuthenticator(application *Application) OAuthStormpathTokenAuthenticator {
return OAuthStormpathTokenAuthenticator{application}
}
func (a OAuthStormpathTokenAuthenticator) Authenticate(stormpathJWT string) (*OAuthAccessTokenResult, error) {
oauthResponse, err := a.Application.GetOAuthTokenStormpathGrantType(stormpathJWT)
if err != nil {
return nil, err
}
authResult := OAuthAccessTokenResult(*oauthResponse)
return &authResult, nil
}
func NewStormpathAssertionAuthenticator(application *Application) StormpathAssertionAuthenticator {
return StormpathAssertionAuthenticator{application}
}
func (a StormpathAssertionAuthenticator) Authenticate(stormpathJWT string) (*StormpathAssertionAuthenticationResult, error) {
callbackResponse, err := a.Application.HandleCallback("http://fake?jwtResponse=" + stormpathJWT)
if err != nil {
return nil, err
}
authResult := StormpathAssertionAuthenticationResult(*callbackResponse)
return &authResult, nil
}
func NewOAuthBearerAuthenticator(application *Application) OAuthBearerAuthenticator {
return OAuthBearerAuthenticator{application}
}
func (a OAuthBearerAuthenticator) Authenticate(accessTokenJWT string) (*AuthenticationResult, error) {
oauthToken, err := a.Application.ValidateToken(accessTokenJWT)
if err != nil {
return nil, err
}
if oauthToken.ExpandedJWT.Header.STT != "access" {
//This is a refresh token
return nil, fmt.Errorf("can't use refresh token as access token")
}
return &AuthenticationResult{oauthToken.Account}, nil
}
func (ar *AuthenticationResult) GetAccount() *Account {
account, err := GetAccount(ar.Account.Href, MakeAccountCriteria().WithProviderData().WithDirectory())
if err != nil {
return nil
}
return account
}
func (ar *OAuthAccessTokenResult) GetAccount() *Account {
claims := &AccessTokenClaims{}
ParseJWT(ar.AccessToken, claims)
account, err := GetAccount(claims.Subject, MakeAccountCriteria().WithProviderData().WithDirectory())
if err != nil {
return nil
}
return account
}
func (ar *OAuthClientCredentialsAuthenticationResult) GetAccount() *Account {
claims := &AccessTokenClaims{}
ParseJWT(ar.AccessToken, claims)
account, err := GetAccount(claims.Subject, MakeAccountCriteria().WithProviderData().WithDirectory())
if err != nil {
return nil
}
return account
}
func (ar *StormpathAssertionAuthenticationResult) GetAccount() *Account {
account, err := GetAccount(ar.Account.Href, MakeAccountCriteria().WithProviderData().WithDirectory())
if err != nil {
return nil
}
return account
}