-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
251 lines (217 loc) · 9.48 KB
/
server.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
package oauth
import (
"net/http"
"time"
"github.com/gofrs/uuid"
)
type GrantType string
const (
PasswordGrant GrantType = "password"
ClientCredentialsGrant GrantType = "client_credentials"
AuthCodeGrant GrantType = "authorization_code"
RefreshTokenGrant GrantType = "refresh_token"
)
// CredentialsVerifier defines the interface of the user and client credentials verifier.
type CredentialsVerifier interface {
// Validate username and password returning an error if the user credentials are wrong
ValidateUser(username, password, scope string, r *http.Request) error
// Validate clientID and secret returning an error if the client credentials are wrong
ValidateClient(clientID, clientSecret, scope string, r *http.Request) error
// Provide additional claims to the token
AddClaims(tokenType TokenType, credential, tokenID, scope string, r *http.Request) (map[string]string, error)
// Provide additional information to the authorization server response
AddProperties(tokenType TokenType, credential, tokenID, scope string, r *http.Request) (map[string]string, error)
// Optionally validate previously stored tokenID during refresh request
ValidateTokenID(tokenType TokenType, credential, tokenID, refreshTokenID string) error
// Optionally store the tokenID generated for the user
StoreTokenID(tokenType TokenType, credential, tokenID, refreshTokenID string) error
}
// AuthorizationCodeVerifier defines the interface of the Authorization Code verifier
type AuthorizationCodeVerifier interface {
// ValidateCode checks the authorization code and returns the user credential
ValidateCode(clientID, clientSecret, code, redirectURI string, r *http.Request) (string, error)
}
// BearerServer is the OAuth 2 bearer server implementation.
type BearerServer struct {
secretKey string
TokenTTL time.Duration
verifier CredentialsVerifier
provider *TokenProvider
}
// NewBearerServer creates new OAuth 2 bearer server
func NewBearerServer(secretKey string, ttl time.Duration, verifier CredentialsVerifier, formatter TokenSecureFormatter) *BearerServer {
if formatter == nil {
formatter = NewSHA256RC4TokenSecurityProvider([]byte(secretKey))
}
return &BearerServer{
secretKey: secretKey,
TokenTTL: ttl,
verifier: verifier,
provider: NewTokenProvider(formatter)}
}
// UserCredentials manages password grant type requests
func (bs *BearerServer) UserCredentials(w http.ResponseWriter, r *http.Request) {
grantType := r.FormValue("grant_type")
username := r.FormValue("username")
password := r.FormValue("password")
scope := r.FormValue("scope")
if username == "" || password == "" {
// get username and password from basic authorization header
var err error
username, password, err = GetBasicAuthentication(r)
if err != nil {
renderJSON(w, "Not authorized", http.StatusUnauthorized)
return
}
}
refreshToken := r.FormValue("refresh_token")
resp, statusCode := bs.generateTokenResponse(GrantType(grantType), username, password, refreshToken, scope, "", "", r)
renderJSON(w, resp, statusCode)
}
// ClientCredentials manages client credentials grant type requests
func (bs *BearerServer) ClientCredentials(w http.ResponseWriter, r *http.Request) {
grantType := r.FormValue("grant_type")
// grant_type client_credentials variables
clientID := r.FormValue("client_id")
clientSecret := r.FormValue("client_secret")
if clientID == "" || clientSecret == "" {
// get clientID and secret from basic authorization header
var err error
clientID, clientSecret, err = GetBasicAuthentication(r)
if err != nil {
renderJSON(w, "Not authorized", http.StatusUnauthorized)
return
}
}
scope := r.FormValue("scope")
refreshToken := r.FormValue("refresh_token")
resp, statusCode := bs.generateTokenResponse(GrantType(grantType), clientID, clientSecret, refreshToken, scope, "", "", r)
renderJSON(w, resp, statusCode)
}
// AuthorizationCode manages authorization code grant type requests for the phase two of the authorization process
func (bs *BearerServer) AuthorizationCode(w http.ResponseWriter, r *http.Request) {
grantType := r.FormValue("grant_type")
// grant_type client_credentials variables
clientID := r.FormValue("client_id")
clientSecret := r.FormValue("client_secret") // not mandatory
code := r.FormValue("code")
redirectURI := r.FormValue("redirect_uri") // not mandatory
scope := r.FormValue("scope") // not mandatory
if clientID == "" {
var err error
clientID, clientSecret, err = GetBasicAuthentication(r)
if err != nil {
renderJSON(w, "Not authorized", http.StatusUnauthorized)
return
}
}
resp, status := bs.generateTokenResponse(GrantType(grantType), clientID, clientSecret, "", scope, code, redirectURI, r)
renderJSON(w, resp, status)
}
// Generate token response
func (bs *BearerServer) generateTokenResponse(grantType GrantType, credential string, secret string, refreshToken string, scope string, code string, redirectURI string, r *http.Request) (interface{}, int) {
var resp *TokenResponse
switch grantType {
case PasswordGrant:
if err := bs.verifier.ValidateUser(credential, secret, scope, r); err != nil {
return "Not authorized", http.StatusUnauthorized
}
token, refresh, err := bs.generateTokens(UserToken, credential, scope, r)
if err != nil {
return "Token generation failed, check claims", http.StatusInternalServerError
}
if err = bs.verifier.StoreTokenID(token.TokenType, credential, token.ID, refresh.RefreshTokenID); err != nil {
return "Storing Token ID failed", http.StatusInternalServerError
}
if resp, err = bs.cryptTokens(token, refresh, r); err != nil {
return "Token generation failed, check security provider", http.StatusInternalServerError
}
case ClientCredentialsGrant:
if err := bs.verifier.ValidateClient(credential, secret, scope, r); err != nil {
return "Not authorized", http.StatusUnauthorized
}
token, refresh, err := bs.generateTokens(ClientToken, credential, scope, r)
if err != nil {
return "Token generation failed, check claims", http.StatusInternalServerError
}
if err = bs.verifier.StoreTokenID(token.TokenType, credential, token.ID, refresh.RefreshTokenID); err != nil {
return "Storing Token ID failed", http.StatusInternalServerError
}
if resp, err = bs.cryptTokens(token, refresh, r); err != nil {
return "Token generation failed, check security provider", http.StatusInternalServerError
}
case AuthCodeGrant:
codeVerifier, ok := bs.verifier.(AuthorizationCodeVerifier)
if !ok {
return "Not authorized, grant type not supported", http.StatusUnauthorized
}
user, err := codeVerifier.ValidateCode(credential, secret, code, redirectURI, r)
if err != nil {
return "Not authorized", http.StatusUnauthorized
}
token, refresh, err := bs.generateTokens(AuthToken, user, scope, r)
if err != nil {
return "Token generation failed, check claims", http.StatusInternalServerError
}
err = bs.verifier.StoreTokenID(token.TokenType, user, token.ID, refresh.RefreshTokenID)
if err != nil {
return "Storing Token ID failed", http.StatusInternalServerError
}
if resp, err = bs.cryptTokens(token, refresh, r); err != nil {
return "Token generation failed, check security provider", http.StatusInternalServerError
}
case RefreshTokenGrant:
refresh, err := bs.provider.DecryptRefreshTokens(refreshToken)
if err != nil {
return "Not authorized", http.StatusUnauthorized
}
if err = bs.verifier.ValidateTokenID(refresh.TokenType, refresh.Credential, refresh.TokenID, refresh.RefreshTokenID); err != nil {
return "Not authorized invalid token", http.StatusUnauthorized
}
token, refresh, err := bs.generateTokens(refresh.TokenType, refresh.Credential, refresh.Scope, r)
if err != nil {
return "Token generation failed", http.StatusInternalServerError
}
err = bs.verifier.StoreTokenID(token.TokenType, refresh.Credential, token.ID, refresh.RefreshTokenID)
if err != nil {
return "Storing Token ID failed", http.StatusInternalServerError
}
if resp, err = bs.cryptTokens(token, refresh, r); err != nil {
return "Token generation failed", http.StatusInternalServerError
}
default:
return "Invalid grant_type", http.StatusBadRequest
}
return resp, http.StatusOK
}
func (bs *BearerServer) generateTokens(tokenType TokenType, username, scope string, r *http.Request) (*Token, *RefreshToken, error) {
token := &Token{ID: uuid.Must(uuid.NewV4()).String(), Credential: username, ExpiresIn: bs.TokenTTL, CreationDate: time.Now().UTC(), TokenType: tokenType, Scope: scope}
if bs.verifier != nil {
claims, err := bs.verifier.AddClaims(token.TokenType, username, token.ID, token.Scope, r)
if err != nil {
return nil, nil, err
}
token.Claims = claims
}
refreshToken := &RefreshToken{RefreshTokenID: uuid.Must(uuid.NewV4()).String(), TokenID: token.ID, CreationDate: time.Now().UTC(), Credential: username, TokenType: tokenType, Scope: scope}
return token, refreshToken, nil
}
func (bs *BearerServer) cryptTokens(token *Token, refresh *RefreshToken, r *http.Request) (*TokenResponse, error) {
cToken, err := bs.provider.CryptToken(token)
if err != nil {
return nil, err
}
cRefreshToken, err := bs.provider.CryptRefreshToken(refresh)
if err != nil {
return nil, err
}
tokenResponse := &TokenResponse{Token: cToken, RefreshToken: cRefreshToken, TokenType: BearerToken, ExpiresIn: (int64)(bs.TokenTTL / time.Second)}
if bs.verifier != nil {
props, err := bs.verifier.AddProperties(token.TokenType, token.Credential, token.ID, token.Scope, r)
if err != nil {
return nil, err
}
tokenResponse.Properties = props
}
return tokenResponse, nil
}