forked from mohabusama/identityserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_token.go
268 lines (230 loc) · 8.08 KB
/
access_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
package oauthservice
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"net/http"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/itsyouonline/identityserver/db/user/apikey"
)
//AccessTokenExpiration is the time in seconds an access token expires
var AccessTokenExpiration = time.Second * 3600 * 24 //Tokens expire after 1 day
//AccessToken is an oauth2 accesstoken together with the access information it stands for
type AccessToken struct {
AccessToken string
Type string
Username string
GlobalID string //The organization that granted the token (in case of a client credentials flow)
Scope string
ClientID string //The client_id of the organization that was granted the token
CreatedAt time.Time
}
//IsExpiredAt checks if the token is expired at a specific time
func (at *AccessToken) IsExpiredAt(testtime time.Time) bool {
return testtime.After(at.ExpirationTime())
}
//IsExpired is a convenience method for IsExpired(time.Now())
func (at *AccessToken) IsExpired() bool {
return at.IsExpiredAt(time.Now())
}
//ExpirationTime return the time at which this token expires
func (at *AccessToken) ExpirationTime() time.Time {
return at.CreatedAt.Add(AccessTokenExpiration)
}
func newAccessToken(username, globalID, clientID, scope string) *AccessToken {
var at AccessToken
randombytes := make([]byte, 21) //Multiple of 3 to make sure no padding is added
rand.Read(randombytes)
at.AccessToken = base64.URLEncoding.EncodeToString(randombytes)
at.CreatedAt = time.Now()
at.Username = username
at.GlobalID = globalID
at.ClientID = clientID
at.Scope = scope
at.Type = "bearer"
return &at
}
//AccessTokenHandler is the handler of the /v1/oauth/access_token endpoint
func (service *Service) AccessTokenHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")
err := r.ParseForm()
if err != nil {
log.Debug("ERROR parsing form: ", err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
var clientID, clientSecret string
code := r.FormValue("code")
grantType := r.FormValue("grant_type")
clientSecret = r.FormValue("client_secret")
clientID = r.FormValue("client_id")
//If clientSecret if missing from form data check if its available as basicauth
//See https://tools.ietf.org/html/rfc6749#section-2.3.1
if clientSecret == "" {
var ok bool
clientID, clientSecret, ok = r.BasicAuth()
if !ok {
log.Debug("clientSecret not found in form data nor basicauth")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
}
//Also accept some alternatives
if grantType == "authorization_code" {
grantType = ""
}
if clientSecret == "" || clientID == "" || (grantType == "" && code == "") {
log.Debug("Required parameter missing in the request")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
var at *AccessToken
httpStatusCode := http.StatusOK
mgr := NewManager(r)
if grantType != "" {
if grantType == ClientCredentialsGrantCodeType {
at, httpStatusCode = clientCredentialsTokenHandler(clientID, clientSecret, mgr, r)
} else {
log.Debug("Invalid grant_type")
httpStatusCode = http.StatusBadRequest
}
} else {
redirectURI := r.FormValue("redirect_uri")
at, httpStatusCode = convertCodeToAccessTokenHandler(code, clientID, clientSecret, redirectURI, mgr, r)
}
if httpStatusCode != http.StatusOK {
http.Error(w, http.StatusText(httpStatusCode), httpStatusCode)
return
}
//It is also possible to immediately get a JWT by specifying 'id_token' as the response type
// In this case, the scope parameter needs to be given to prevent consumers to accidentially handing out too powerful tokens to third party services
// It is also possible to specify additional audiences
responseType := r.FormValue("response_type")
if responseType == "id_token" {
requestedScopeParameter := r.FormValue("scope")
extraAudiences := r.FormValue("aud")
tokenString, err := service.convertAccessTokenToJWT(r, at, requestedScopeParameter, extraAudiences)
if err == errUnauthorized {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
if err != nil {
log.Error(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
// if client could accept JSON we give the token as JSON string
// otherwise in plain text
if strings.Index(r.Header.Get("Accept"), "application/json") >= 0 {
w.Header().Set("Content-type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"access_token": tokenString})
} else {
w.Header().Set("Content-type", "application/jwt")
w.Write([]byte(tokenString))
}
return
}
mgr.saveAccessToken(at)
response := struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
ExpiresIn int64 `json:"expires_in"`
Info interface{} `json:"info"`
}{
AccessToken: at.AccessToken,
TokenType: at.Type,
Scope: at.Scope,
ExpiresIn: int64(at.ExpirationTime().Sub(time.Now()).Seconds() - 600),
Info: struct {
Username string `json:"username"`
}{
Username: at.Username,
},
}
w.Header().Set("Content-type", "application/json")
json.NewEncoder(w).Encode(&response)
}
func clientCredentialsTokenHandler(clientID string, secret string, mgr *Manager, r *http.Request) (at *AccessToken, httpStatusCode int) {
httpStatusCode = http.StatusOK
var scopes string
username := ""
client, err := mgr.getClientByCredentials(clientID, secret)
if err != nil {
log.Error("Error getting the oauth client: ", err)
httpStatusCode = http.StatusInternalServerError
return
}
if client == nil || !client.ClientCredentialsGrantType {
log.Info("Checking user api")
apikeyMgr := apikey.NewManager(r)
apikey, err := apikeyMgr.GetByApplicationAndSecret(clientID, secret)
if err != nil || apikey.ApiKey != secret {
log.Error("Error getting the user api key: ", err)
httpStatusCode = http.StatusBadRequest
return
}
log.Info("apikey", apikey)
scopes = strings.Join(apikey.Scopes, " ")
log.Info("scopes ", scopes)
username = apikey.Username
} else {
scopes = "organization:owner"
}
at = newAccessToken(username, clientID, clientID, scopes)
return
}
func convertCodeToAccessTokenHandler(code string, clientID string, secret string, redirectURI string, mgr *Manager, r *http.Request) (at *AccessToken, httpStatusCode int) {
httpStatusCode = http.StatusOK
ar, err := mgr.Get(code)
if err != nil {
log.Error("ERROR getting the original authorization request:", err)
httpStatusCode = http.StatusInternalServerError
return
}
if ar == nil {
log.Debug("No original authorization request found with this authorization code")
httpStatusCode = http.StatusBadRequest
return
}
state := r.FormValue("state")
if ar.ClientID != clientID || ar.State != state || ar.RedirectURL != redirectURI {
log.Info("Bad client or hacking attempt, state, client_id or redirect_uri is different from the original authorization request")
httpStatusCode = http.StatusBadRequest
return
}
if ar.IsExpiredAt(time.Now()) {
log.Info("Token request for an expired authorizationrequest")
httpStatusCode = http.StatusBadRequest
return
}
client, err := mgr.getClientByCredentials(clientID, secret)
if err != nil {
log.Error("Error getting the oauth client: ", err)
httpStatusCode = http.StatusInternalServerError
return
}
if client == nil {
log.Info("(client_id - secret) combination not found")
httpStatusCode = http.StatusBadRequest
return
}
if !strings.HasPrefix(redirectURI, client.CallbackURL) {
log.Debug("return_uri does not match the callback uri")
httpStatusCode = http.StatusBadRequest
return
}
at = newAccessToken(ar.Username, "", ar.ClientID, ar.Scope)
return
}
func (service *Service) createItsYouOnlineAdminToken(username string, r *http.Request) (token string, err error) {
at := newAccessToken(username, "", "itsyouonline", "admin")
mgr := NewManager(r)
err = mgr.saveAccessToken(at)
if err == nil {
token = at.AccessToken
}
return
}