-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth2_token.go
379 lines (340 loc) · 13.1 KB
/
oauth2_token.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package heimdall
import (
"crypto/rand"
"encoding/json"
"fmt"
"mime"
"net/http"
"strings"
"time"
)
func genUUIDv4() string {
u := make([]byte, 16)
rand.Read(u)
//Set the version to 4
u[6] = (u[6] | 0x40) & 0x4F
u[8] = (u[8] | 0x80) & 0xBF
return fmt.Sprintf("%x-%x-%x-%x-%x", u[0:4], u[4:6], u[6:8], u[8:10], u[10:])
}
type tokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int64 `json:"expires_in"`
Scope []string `json:"scope"`
RefreshToken string `json:"refresh_token,omitempty"`
}
type tokenError struct {
Code string `json:"error"`
Description string `json:"error_description"`
URI string `json:"error_uri"`
}
func writeTokenErrorResponse(w http.ResponseWriter, r *http.Request, errorString, errorDescription, errorURI string) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Pragma", "no-cache")
if errorString == "invalid_client" {
if r.Header.Get("Authorization") != "" {
w.Header().Set("WWW-Authenticate", `Basic realm="Token"`)
}
w.WriteHeader(http.StatusUnauthorized)
} else {
w.WriteHeader(http.StatusBadRequest)
}
te := tokenError{Code: errorString, Description: errorDescription, URI: errorURI}
e := json.NewEncoder(w)
err := e.Encode(&te)
if err != nil {
fmt.Println(err)
}
}
func (h *Heimdall) OAuth2Token(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "The Token endpoint only supports POST requests", http.StatusNotFound)
return
}
if mediaType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")); mediaType != "application/x-www-form-urlencoded" {
//writeTokenErrorResponse("invalid_request", "Token endpoint only supports a content-type of application/x-www-form-urlencoded", "https://tools.ietf.org/html/rfc6749", w, r)
http.Error(w, "Token endpoint only supports a content-type of application/x-www-form-urlencoded", http.StatusNotFound)
return
}
clientId, clientSecret, basicAuth := r.BasicAuth()
if !basicAuth {
clientId = r.PostFormValue("client_id")
clientSecret = r.PostFormValue("client_secret")
}
grantType := r.PostFormValue("grant_type")
if grantType != TokenGrantTypeAuthCode &&
grantType != TokenGrantTypeClientCredentials &&
grantType != TokenGrantTypeRefreshToken &&
grantType != TokenGrantTypeRefreshToken {
writeTokenErrorResponse(w, r, "invalid_grant", "Grant Type must be one of authorization_code, client_credentials, refresh_token, or password", "https://tools.ietf.org/html/rfc6749")
return
}
switch grantType {
case "authorization_code":
authorizationCode := r.PostFormValue("code")
redirectURI := r.PostFormValue("redirect_uri")
if clientId == "" {
writeTokenErrorResponse(w, r, "invalid_client", "Required param client_id is missing", "https://tools.ietf.org/html/rfc6749")
return
}
if authorizationCode == "" {
writeTokenErrorResponse(w, r, "invalid_request", "Required param code is missing", "https://tools.ietf.org/html/rfc6749")
return
}
if redirectURI == "" {
writeTokenErrorResponse(w, r, "invalid_request", "Required param redirect_uri is missing", "https://tools.ietf.org/html/rfc6749")
return
}
//Grab the client
client, err := h.DB.GetClient(clientId)
if err != nil {
writeTokenErrorResponse(w, r, "invalid_client", "Unknown Client", "https://tools.ietf.org/html/rfc6749")
return
}
setValuesOnContext(r.Context(), clientId, clientId)
//r.Header.Set("X-User-Id", clientId)
//r.Header.Set("X-Client-Id", clientId)
//The big question now is whether I should _force_ the client to auth, the spec recommends that any client that is confidential should
if client.GetType() == "confidential" {
if clientSecret != "" {
if client.GetSecret() != clientSecret {
writeTokenErrorResponse(w, r, "invalid_client", "A confidential client is required to authenticate (client_id and client_secret)", "https://tools.ietf.org/html/rfc6749")
return
}
}
}
//Is the code valid?
code, err := h.DB.GetToken(authorizationCode)
if err != nil {
writeTokenErrorResponse(w, r, "invalid_grant", "Invalid or Expired Authorization Code", "https://tools.ietf.org/html/rfc6749")
return
}
//Does the client_id match the code?
if code.GetClientId() != clientId {
writeTokenErrorResponse(w, r, "invalid_request", "client_id does not match authorization code grant", "https://tools.ietf.org/html/rfc6749")
return
}
//Is the redirect_uri valid
valid := false
redirectURIs := client.GetRedirectURIs()
for _, ruri := range redirectURIs {
if ruri == r.PostFormValue("redirect_uri") {
valid = true
}
}
if !valid {
writeTokenErrorResponse(w, r, "invalid_grant", "The provided redirect_uri does not match the redirection URI used in the authorization request, or was issued to another client", "https://tools.ietf.org/html/rfc6749")
return
}
setValuesOnContext(r.Context(), code.GetUserId(), clientId)
//r.Header.Set("X-User-Id", code.GetUserId())
//Coolness all is in order to give away the access token requested
tokenId := genUUIDv4()
token := h.DB.NewToken()
token.SetId(tokenId)
token.SetType(TokenTypeBearer)
token.SetScope(code.GetScope())
token.SetUserId(code.GetUserId())
token.SetClientId(code.GetClientId())
token.SetExpires(time.Now().UTC().Add(h.AccessTokenDuration))
h.DB.CreateToken(token)
//Finally remove the code so it can't be reused
h.DB.DeleteToken(authorizationCode)
//Maybe not always create a refresh token?
refreshTokenId := ""
if code.GetAccessType() == TokenAccessTypeOffline {
refreshTokenId = genUUIDv4()
refreshToken := h.DB.NewToken()
refreshToken.SetId(refreshTokenId)
refreshToken.SetType(TokenTypeRefresh)
refreshToken.SetScope(code.GetScope())
refreshToken.SetUserId(code.GetUserId())
refreshToken.SetClientId(code.GetClientId())
refreshToken.SetExpires(time.Now().UTC().Add(h.RefreshTokenDuration))
h.DB.CreateToken(refreshToken)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Pragma", "no-cache")
w.WriteHeader(http.StatusOK)
tr := tokenResponse{AccessToken: token.GetId(), TokenType: token.GetType(), ExpiresIn: int64(token.GetExpires().Sub(time.Now()).Seconds()), Scope: token.GetScope(), RefreshToken: refreshTokenId}
e := json.NewEncoder(w)
err = e.Encode(tr)
if err != nil {
fmt.Println(err)
}
case "client_credentials":
if clientId == "" || clientSecret == "" {
writeTokenErrorResponse(w, r, "invalid_client", "Client is required to authenticate, client_id/client_secret is missing", "https://tools.ietf.org/html/rfc6749")
return
}
client, err := h.DB.VerifyClient(clientId, clientSecret)
if err != nil {
writeTokenErrorResponse(w, r, "invalid_client", "Unknown Client", "https://tools.ietf.org/html/rfc6749")
return
}
asked_scope := strings.Split(r.PostFormValue("scope"), " ")
scope := make([]string, 0)
for _, s := range asked_scope {
if z, _ := h.PreAuthZFunction(r, s, client, nil); z == Permit {
scope = append(scope, s)
}
}
setValuesOnContext(r.Context(), clientId, clientId)
//r.Header.Set("X-User-Id", clientId)
//r.Header.Set("X-Client-Id", clientId)
//Coolness, all is in order to give away the access token requested
tokenId := genUUIDv4()
token := h.DB.NewToken()
token.SetId(tokenId)
token.SetType(TokenTypeBearer)
token.SetScope(scope)
token.SetClientId(clientId)
token.SetExpires(time.Now().UTC().Add(h.AccessTokenDuration))
h.DB.CreateToken(token)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Pragma", "no-cache")
w.WriteHeader(http.StatusOK)
tr := tokenResponse{AccessToken: token.GetId(), TokenType: token.GetType(), ExpiresIn: int64(token.GetExpires().Sub(time.Now()).Seconds()), Scope: token.GetScope()}
e := json.NewEncoder(w)
err = e.Encode(tr)
if err != nil {
fmt.Println(err)
}
case "refresh_token":
refreshTokenId := r.PostFormValue("refresh_token")
if refreshTokenId == "" {
writeTokenErrorResponse(w, r, "invalid_request", "Required param refresh_token is missing", "https://tools.ietf.org/html/rfc6749")
return
}
refreshToken, err := h.DB.GetToken(refreshTokenId)
if err != nil {
writeTokenErrorResponse(w, r, "invalid_grant", "Refresh Token is invalid, expired, or revoked", "https://tools.ietf.org/html/rfc6749")
return
}
client, err := h.DB.GetClient(refreshToken.GetClientId())
if err != nil {
writeTokenErrorResponse(w, r, "invalid_client", "The refresh_token provided does not tie to a valid client", "https://tools.ietf.org/html/rfc6749")
return
}
setValuesOnContext(r.Context(), clientId, clientId)
//r.Header.Set("X-User-Id", clientId)
//r.Header.Set("X-Client-Id", clientId)
if client.GetType() == "confidential" {
if clientId == "" || clientSecret == "" {
writeTokenErrorResponse(w, r, "invalid_client", "Required param client_id/client_secret is missing (Or basic Auth with client credentials)", "https://tools.ietf.org/html/rfc6749")
return
}
}
//They get a subset of the original scope
scopeRequest := strings.Split(r.PostFormValue("scope"), " ")
scope := make([]string, 0)
if len(scopeRequest) == 0 {
scope = refreshToken.GetScope()
} else {
for _, sco := range refreshToken.GetScope() {
for _, sc := range scopeRequest {
if sc == sco {
scope = append(scope, sc)
}
}
}
}
userId := refreshToken.GetUserId()
setValuesOnContext(r.Context(), userId, clientId)
//r.Header.Set("X-User-Id", userId)
//Coolness all is in order to give away the access token requested
tokenId := genUUIDv4()
token := h.DB.NewToken()
token.SetId(tokenId)
token.SetType(TokenTypeBearer)
token.SetScope(scope)
token.SetClientId(clientId)
token.SetUserId(userId)
token.SetRefreshToken(refreshTokenId)
token.SetExpires(time.Now().UTC().Add(h.AccessTokenDuration))
h.DB.CreateToken(token)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Pragma", "no-cache")
w.WriteHeader(http.StatusOK)
tr := tokenResponse{AccessToken: token.GetId(), TokenType: token.GetType(), ExpiresIn: int64(token.GetExpires().Sub(time.Now()).Seconds()), Scope: token.GetScope()}
e := json.NewEncoder(w)
err = e.Encode(tr)
if err != nil {
fmt.Println(err)
}
case "password":
if clientId == "" || clientSecret == "" {
writeTokenErrorResponse(w, r, "invalid_request", "Required param client_id/client_secret is missing", "https://tools.ietf.org/html/rfc6749")
return
}
client, err := h.DB.VerifyClient(clientId, clientSecret)
if err != nil {
writeTokenErrorResponse(w, r, "invalid_client", "Unknown Client", "https://tools.ietf.org/html/rfc6749")
return
}
setValuesOnContext(r.Context(), clientId, clientId)
//r.Header.Set("X-User-Id", clientId)
//r.Header.Set("X-Client-Id", clientId)
clientInternal := client.GetInternal()
clientType := client.GetType()
if !clientInternal || clientType != "confidential" {
writeTokenErrorResponse(w, r, "unauthorized_client", "Unauthorized Client", "https://tools.ietf.org/html/rfc6749")
return
}
username := r.PostFormValue("username")
password := r.PostFormValue("password")
user, err := h.DB.VerifyUser(username, password)
if err != nil {
//TODO Maybe rate limit for the requested user, or something
writeTokenErrorResponse(w, r, "invalid_grant", "Invalid User Credentials", "https://tools.ietf.org/html/rfc6749")
return
}
userId := user.GetId()
setValuesOnContext(r.Context(), userId, clientId)
//r.Header.Set("X-User-Id", userId)
asked_scope := strings.Split(r.PostFormValue("scope"), " ")
scope := make([]string, 0)
for _, s := range asked_scope {
if z, _ := h.PreAuthZFunction(r, s, client, user); z == Permit {
scope = append(scope, s)
}
}
//Coolness all is in order to give away the access token requested
tokenId := genUUIDv4()
token := h.DB.NewToken()
token.SetId(tokenId)
token.SetType(TokenTypeBearer)
token.SetScope(scope)
token.SetClientId(clientId)
token.SetUserId(userId)
token.SetExpires(time.Now().UTC().Add(h.AccessTokenDuration))
h.DB.CreateToken(token)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Pragma", "no-cache")
w.WriteHeader(http.StatusOK)
//Maybe not always create a refresh token?
refreshTokenId := ""
if r.PostFormValue("access_type") == TokenAccessTypeOffline {
refreshTokenId = genUUIDv4()
refreshToken := h.DB.NewToken()
refreshToken.SetId(refreshTokenId)
refreshToken.SetType(TokenTypeRefresh)
refreshToken.SetScope(scope)
refreshToken.SetUserId(userId)
refreshToken.SetClientId(clientId)
refreshToken.SetExpires(time.Now().UTC().Add(h.RefreshTokenDuration))
h.DB.CreateToken(refreshToken)
}
tr := tokenResponse{AccessToken: token.GetId(), TokenType: token.GetType(), ExpiresIn: int64(token.GetExpires().Sub(time.Now()).Seconds()), Scope: token.GetScope(), RefreshToken: refreshTokenId}
e := json.NewEncoder(w)
err = e.Encode(tr)
if err != nil {
fmt.Println(err)
}
}
}