-
Notifications
You must be signed in to change notification settings - Fork 9
/
authorization_code_refresh_tokens_projection.go
74 lines (58 loc) · 2.61 KB
/
authorization_code_refresh_tokens_projection.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
package goauth2
import (
"fmt"
"github.com/inklabs/rangedb"
)
// AuthorizationCodeRefreshTokens is a projection mapping authorization codes to refresh tokens.
type AuthorizationCodeRefreshTokens struct {
refreshTokensByAuthorizationCode map[string][]string
authorizationCodeByRefreshToken map[string]string
}
// NewAuthorizationCodeRefreshTokens constructs an AuthorizationCodeRefreshTokens projection.
func NewAuthorizationCodeRefreshTokens() *AuthorizationCodeRefreshTokens {
return &AuthorizationCodeRefreshTokens{
refreshTokensByAuthorizationCode: make(map[string][]string),
authorizationCodeByRefreshToken: make(map[string]string),
}
}
// Accept receives a rangedb.Record.
func (a *AuthorizationCodeRefreshTokens) Accept(record *rangedb.Record) {
switch event := record.Data.(type) {
case *RefreshTokenWasIssuedToUserViaAuthorizationCodeGrant:
a.addRefreshToken(event.AuthorizationCode, event.RefreshToken)
case *RefreshTokenWasIssuedToUserViaRefreshTokenGrant:
if authorizationCode, ok := a.authorizationCodeByRefreshToken[event.RefreshToken]; ok {
a.addRefreshToken(authorizationCode, event.NextRefreshToken)
}
case *RefreshTokenWasRevokedFromUser:
if authorizationCode, ok := a.authorizationCodeByRefreshToken[event.RefreshToken]; ok {
a.removeRefreshTokens(authorizationCode)
}
}
}
// GetTokens returns all refresh tokens by authorizationCode.
func (a *AuthorizationCodeRefreshTokens) GetTokens(authorizationCode string) []string {
return a.refreshTokensByAuthorizationCode[authorizationCode]
}
// GetAuthorizationCode returns a single authorization code from a refresh token.
func (a *AuthorizationCodeRefreshTokens) GetAuthorizationCode(refreshToken string) (string, error) {
if authorizationCode, ok := a.authorizationCodeByRefreshToken[refreshToken]; ok {
return authorizationCode, nil
}
return "", ErrAuthorizationCodeNotFound
}
func (a *AuthorizationCodeRefreshTokens) addRefreshToken(authorizationCode, refreshToken string) {
a.authorizationCodeByRefreshToken[refreshToken] = authorizationCode
a.refreshTokensByAuthorizationCode[authorizationCode] = append(
a.refreshTokensByAuthorizationCode[authorizationCode],
refreshToken,
)
}
func (a *AuthorizationCodeRefreshTokens) removeRefreshTokens(authorizationCode string) {
for _, refreshToken := range a.refreshTokensByAuthorizationCode[authorizationCode] {
delete(a.authorizationCodeByRefreshToken, refreshToken)
}
delete(a.refreshTokensByAuthorizationCode, authorizationCode)
}
// ErrAuthorizationCodeNotFound is a defined error for missing authorization code.
var ErrAuthorizationCodeNotFound = fmt.Errorf("authorization code not found")