-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth_service.go
500 lines (430 loc) · 13.7 KB
/
oauth_service.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package ssso
import (
"context"
"crypto/subtle"
"fmt"
"strings"
"time"
"github.com/google/uuid"
"github.com/pilab-dev/shadow-sso/api"
"github.com/pilab-dev/shadow-sso/client"
"golang.org/x/crypto/bcrypt"
)
type OAuthService struct {
oauthRepo OAuthRepository
userRepo UserStore
clientRepo client.ClientStore
tokenService *TokenService
keyID string
issuer string
}
// NewOAuthService creates a new instance of the OAuthService.
func NewOAuthService(
oauthRepo OAuthRepository,
userRepo UserStore,
tokenService *TokenService,
issuer string,
) *OAuthService {
return &OAuthService{
oauthRepo: oauthRepo,
userRepo: userRepo,
keyID: uuid.NewString(), // ! This must be refactored to keystore or something.
tokenService: tokenService,
issuer: issuer,
}
}
func (s *OAuthService) RegisterUser(ctx context.Context, username, password string) (*User, error) {
// Hashing password
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return nil, fmt.Errorf("failed to hash password: %w", err)
}
// Create the user
user, err := s.userRepo.CreateUser(ctx, username, string(hashedPassword))
if err != nil {
return nil, fmt.Errorf("failed to create user: %w", err)
}
return user, nil
}
// func (s *OAuthService) Login(ctx context.Context, username, password, deviceInfo string) (*TokenResponse, error) {
// // Search for the user
// user, err := s.userRepo.GetUserByUsername(ctx, username)
// if err != nil {
// return nil, fmt.Errorf("user not found: %w", err)
// }
// // Check password
// if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil {
// return nil, ErrInvalidCredentials
// }
// // Generate token and session
// accessToken := uuid.NewString()
// refreshToken := uuid.NewString()
// expiresAt := time.Now().Add(time.Hour)
// session := &UserSession{
// ID: uuid.NewString(),
// UserID: user.ID,
// AccessToken: accessToken,
// RefreshToken: refreshToken,
// ExpiresAt: expiresAt,
// CreatedAt: time.Now(),
// LastUsedAt: time.Now(),
// DeviceInfo: deviceInfo,
// IsRevoked: false,
// }
// // Save the generated session
// if err := s.userRepo.CreateSession(ctx, user.ID, session); err != nil {
// return nil, fmt.Errorf("failed to create session: %w", err)
// }
// // Store token in the database
// token := &Token{
// ID: uuid.NewString(),
// TokenType: "access_token",
// TokenValue: accessToken,
// ClientID: "",
// UserID: user.ID,
// ExpiresAt: expiresAt,
// CreatedAt: time.Now(),
// LastUsedAt: time.Now(),
// }
// if err := s.oauthRepo.StoreToken(ctx, token); err != nil {
// return nil, fmt.Errorf("failed to store token: %w", err)
// }
// return &TokenResponse{
// AccessToken: accessToken,
// TokenType: "Bearer",
// ExpiresIn: 3600,
// RefreshToken: refreshToken,
// }, nil
// }
// GetUserSessions retrieves all active sessions for a user.
func (s *OAuthService) GetUserSessions(ctx context.Context, userID string) ([]UserSession, error) {
return s.userRepo.GetUserSessions(ctx, userID)
}
// TokenRefresh refreshes the access token with the refresh token.
func (s *OAuthService) RefreshToken(ctx context.Context, refreshToken string, clientID string) (*api.TokenResponse, error) {
session, err := s.userRepo.GetSessionByToken(ctx, refreshToken)
if err != nil {
return nil, ErrInvalidRefreshToken
}
if session.IsRevoked || time.Now().After(session.ExpiresAt) {
return nil, ErrTokenExpiredOrRevoked
}
if err := s.userRepo.UpdateSessionLastUsed(ctx, session.ID); err != nil {
return nil, fmt.Errorf("failed to update session: %w", err)
}
return s.tokenService.GenerateTokenPair(ctx, clientID, session.UserID, session.Scope, time.Hour)
}
// GetJWKS returns the JSON Web Key Set (JWKS) containing the public key used to sign tokens.
func (s *OAuthService) GetJWKS() JSONWebKeySet {
keyset := JSONWebKeySet{
Keys: make([]JSONWebKey, 0),
}
if false {
// The public key of the signing key. This must be set in the constructor.
// publicKey, _ := s.signingKey.Public().(*rsa.PublicKey)
// Encode the public key components in base64. This is required by the JWKS spec.
// mod := base64.RawURLEncoding.EncodeToString(publicKey.N.Bytes())
// exp := base64.RawURLEncoding.EncodeToString(big.NewInt(int64(publicKey.E)).Bytes())
// []JSONWebKey{
// {
// Kid: s.keyID, // Unique Key ID
// Kty: "RSA", // Key Type
// Alg: "RS256", // Algorithm
// Use: "sig", // Usage (signature)
// N: mod, // Modulus
// E: exp, // Exponent
// },
// }
}
return keyset
}
// Additional methods for OAuthService
func (s *OAuthService) ValidateClient(ctx context.Context, clientID, clientSecret string) (*client.Client, error) {
cli, err := s.clientRepo.GetClient(ctx, clientID)
if err != nil {
return nil, fmt.Errorf("client not found: %w", err)
}
// Compare client secret using constant-time comparison
if subtle.ConstantTimeCompare([]byte(cli.Secret), []byte(clientSecret)) != 1 {
return nil, ErrInvalidCredentials
}
return cli, nil
}
// DirectGrant implements the Resource Owner Password Credentials flow
func (s *OAuthService) DirectGrant(ctx context.Context,
clientID, clientSecret, username, password, scope string,
) (*api.TokenResponse, error) {
// Validate cli
cli, err := s.ValidateClient(ctx, clientID, clientSecret)
if err != nil {
return nil, err
}
// Check if password grant is allowed for this client
if !contains(cli.AllowedGrantTypes, "password") {
return nil, fmt.Errorf("%w: grant type not allowed for this client", ErrInvalidConfig)
}
// Validate user credentials
user, err := s.userRepo.GetUserByUsername(ctx, username)
if err != nil {
return nil, ErrInvalidCredentials
}
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil {
return nil, ErrInvalidCredentials
}
// Validate requested scope
if !s.validateScope(scope, cli.AllowedScopes) {
return nil, ErrInvalidScopeRequest
}
// Generate tokens
accessToken := uuid.NewString()
refreshToken := uuid.NewString()
expiresAt := time.Now().Add(time.Hour)
// Create session
session := &UserSession{
ID: uuid.NewString(),
UserID: user.ID,
AccessToken: accessToken,
RefreshToken: refreshToken,
ExpiresAt: expiresAt,
CreatedAt: time.Now(),
LastUsedAt: time.Now(),
DeviceInfo: "Direct Grant",
IsRevoked: false,
Scope: scope,
}
if err := s.userRepo.CreateSession(ctx, user.ID, session); err != nil {
return nil, fmt.Errorf("failed to create session: %w", err)
}
return &api.TokenResponse{
IDToken: "",
AccessToken: accessToken,
TokenType: "Bearer",
ExpiresIn: 3600,
RefreshToken: refreshToken,
}, nil
}
// ClientCredentials implements the Client Credentials flow
func (s *OAuthService) ClientCredentials(ctx context.Context,
clientID, clientSecret, scope string,
) (*api.TokenResponse, error) {
// Validate cli
cli, err := s.ValidateClient(ctx, clientID, clientSecret)
if err != nil {
return nil, err
}
// Check if client_credentials grant is allowed for this client
if !contains(cli.AllowedGrantTypes, "client_credentials") {
return nil, fmt.Errorf("%w: grant type not allowed for this client", ErrInvalidConfig)
}
// Validate requested scope
if !s.validateScope(scope, cli.AllowedScopes) {
return nil, ErrInvalidScopeRequest
}
// Generate the access token
token, err := s.tokenService.CreateToken(ctx, CreateTokenOptions{
TokenID: uuid.NewString(),
Scope: scope,
ClientID: clientID,
UserID: "",
TokenType: api.TokenTypeAccessToken,
ExpireIn: time.Hour,
SigningKeyID: "",
}, nil)
if err != nil {
return nil, err
}
return &api.TokenResponse{
IDToken: "",
AccessToken: token.TokenValue,
TokenType: "Bearer",
ExpiresIn: 3600,
}, nil
}
// Helper function to validate scopes
func (s *OAuthService) validateScope(requestedScope string, allowedScopes []string) bool {
if requestedScope == "" {
return true
}
requested := strings.Split(requestedScope, " ")
for _, req := range requested {
found := false
for _, allowed := range allowedScopes {
if req == allowed {
found = true
break
}
}
if !found {
return false
}
}
return true
}
// Helper function to check if a slice contains a string
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
func (s *OAuthService) PasswordGrant(ctx context.Context,
username, password, scope string, cli *client.Client,
) (*api.TokenResponse, error) {
// Validate user credentials
user, err := s.userRepo.GetUserByUsername(ctx, username)
if err != nil {
return nil, ErrInvalidCredentials
}
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil {
return nil, ErrInvalidCredentials
}
// TODO: check if the user request for an id token
// TODO: check the user if requests for a refresh token (offline_access)
// Generate tokens
return s.tokenService.GenerateTokenPair(ctx, cli.ID, user.ID, scope, time.Hour)
}
//nolint:funlen
func (s *OAuthService) ExchangeAuthorizationCode(ctx context.Context,
code, clientID, clientSecret, redirectURI string,
) (*api.TokenResponse, error) {
// Validate client
if err := s.validateClient(ctx, clientID, clientSecret); err != nil {
return nil, err
}
// Validate authorization code
authCode, err := s.oauthRepo.GetAuthCode(ctx, code)
if err != nil {
return nil, fmt.Errorf("invalid authorization code")
}
if authCode.Used || time.Now().After(authCode.ExpiresAt) {
return nil, fmt.Errorf("authorization code expired or already used")
}
if authCode.ClientID != clientID || authCode.RedirectURI != redirectURI {
return nil, fmt.Errorf("invalid client or redirect URI")
}
// Mark code as used
if err := s.oauthRepo.MarkAuthCodeAsUsed(ctx, code); err != nil {
return nil, err
}
tokenResponse := new(api.TokenResponse)
tokenResponse.TokenType = "Bearer"
tokenResponse.ExpiresIn = 3600
// Generate refresh token
accessToken, err := s.tokenService.CreateToken(ctx, CreateTokenOptions{
TokenID: uuid.NewString(),
Scope: authCode.Scope,
ClientID: clientID,
UserID: authCode.UserID,
TokenType: api.TokenTypeRefreshToken,
ExpireIn: time.Hour, // 1 hour
SigningKeyID: "",
}, nil)
if err != nil {
return nil, err
}
tokenResponse.AccessToken = accessToken.TokenValue
if contains(strings.Fields(authCode.Scope), "offline_access") {
// Generate refresh token
refreshToken, err := s.tokenService.CreateToken(ctx, CreateTokenOptions{
TokenID: uuid.NewString(),
Scope: authCode.Scope,
ClientID: clientID,
UserID: authCode.UserID,
TokenType: api.TokenTypeRefreshToken,
ExpireIn: time.Hour * 24 * 30, // 30 days
SigningKeyID: "",
}, nil)
if err != nil {
return nil, err
}
tokenResponse.RefreshToken = refreshToken.TokenValue
}
// Generate tokens
return tokenResponse, nil
}
func (s *OAuthService) validateClient(ctx context.Context, clientID, clientSecret string) error {
cli, err := s.clientRepo.GetClient(ctx, clientID)
if err != nil {
return fmt.Errorf("invalid client: %w", err)
}
if cli.Secret != clientSecret {
return ErrInvalidClientCredentials
}
return nil
}
// TokenIntrospection represents the response format defined in RFC 7662
//
//nolint:tagliatelle
type TokenIntrospection struct {
Active bool `json:"active"`
Scope string `json:"scope,omitempty"`
ClientID string `json:"client_id,omitempty"`
Username string `json:"username,omitempty"`
TokenType string `json:"token_type,omitempty"`
Exp int64 `json:"exp,omitempty"`
Iat int64 `json:"iat,omitempty"`
Nbf int64 `json:"nbf,omitempty"`
Sub string `json:"sub,omitempty"`
Aud string `json:"aud,omitempty"`
Iss string `json:"iss,omitempty"`
Jti string `json:"jti,omitempty"`
}
// IntrospectToken implements RFC 7662 Token Introspection
func (s *OAuthService) IntrospectToken(ctx context.Context,
token, tokenTypeHint, clientID, clientSecret string,
) (*TokenIntrospection, error) {
// Validate the requesting client
if err := s.validateClient(ctx, clientID, clientSecret); err != nil {
return nil, fmt.Errorf("invalid client: %w", err)
}
var tokenInfo *TokenInfo
var err error
// Use token_type_hint to optimize lookup
switch tokenTypeHint {
case "refresh_token":
tokenInfo, err = s.tokenService.GetRefreshTokenInfo(ctx, token)
case "access_token", "":
tokenInfo, err = s.tokenService.GetAccessTokenInfo(ctx, token)
if err != nil && tokenTypeHint == "" {
// If no hint was provided, try refresh token as fallback
tokenInfo, err = s.tokenService.GetRefreshTokenInfo(ctx, token)
}
default:
// Unknown token type hint, try both
tokenInfo, err = s.tokenService.GetAccessTokenInfo(ctx, token)
if err != nil {
tokenInfo, err = s.tokenService.GetRefreshTokenInfo(ctx, token)
}
}
if err != nil {
return &TokenIntrospection{Active: false}, nil
}
// Check if token is expired
if time.Now().After(tokenInfo.ExpiresAt) {
return &TokenIntrospection{Active: false}, nil
}
// If we have user info, get username
var username string
if tokenInfo.UserID != "" {
user, err := s.userRepo.GetUserByID(ctx, tokenInfo.UserID)
if err == nil {
username = user.Username
}
}
return &TokenIntrospection{
Active: true,
Scope: tokenInfo.Scope,
ClientID: tokenInfo.ClientID,
Username: username,
TokenType: tokenInfo.TokenType,
Exp: tokenInfo.ExpiresAt.Unix(),
Iat: tokenInfo.IssuedAt.Unix(),
Sub: tokenInfo.UserID,
Iss: s.issuer,
Jti: tokenInfo.ID,
Nbf: tokenInfo.IssuedAt.Unix(),
Aud: tokenInfo.ClientID,
}, nil
}