forked from inklabs/goauth2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrefresh_token.go
146 lines (124 loc) · 3.4 KB
/
refresh_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
package goauth2
import (
"github.com/inklabs/rangedb"
)
type refreshToken struct {
tokenGenerator TokenGenerator
Token string
Scope string
PendingEvents []rangedb.Event
Username string
IsLoaded bool
HasBeenPreviouslyUsed bool
HasBeenRevoked bool
IsForUser bool
UserID string
IsForClientApplication bool
ClientID string
}
func newRefreshToken(records <-chan *rangedb.Record, generator TokenGenerator) *refreshToken {
aggregate := &refreshToken{
tokenGenerator: generator,
}
for record := range records {
if event, ok := record.Data.(rangedb.Event); ok {
aggregate.apply(event)
}
}
return aggregate
}
func (a *refreshToken) apply(event rangedb.Event) {
switch e := event.(type) {
case *RefreshTokenWasIssuedToUser:
a.IsLoaded = true
a.IsForUser = true
a.UserID = e.UserID
a.Scope = e.Scope
case *RefreshTokenWasIssuedToClientApplication:
a.IsLoaded = true
a.IsForClientApplication = true
a.ClientID = e.ClientID
case *RefreshTokenWasIssuedToUserViaRefreshTokenGrant:
a.HasBeenPreviouslyUsed = true
case *RefreshTokenWasRevoked:
a.HasBeenRevoked = true
}
}
func (a *refreshToken) Handle(command Command) {
switch c := command.(type) {
case RequestAccessTokenViaRefreshTokenGrant:
if !a.IsLoaded {
a.emit(RequestAccessTokenViaRefreshTokenGrantWasRejectedDueToInvalidRefreshToken{
RefreshToken: c.RefreshToken,
ClientID: c.ClientID,
})
return
}
if a.HasBeenPreviouslyUsed {
a.emit(RequestAccessTokenViaRefreshTokenGrantWasRejectedDueToPreviouslyUsedRefreshToken{
RefreshToken: c.RefreshToken,
})
return
}
if a.HasBeenRevoked {
a.emit(RequestAccessTokenViaRefreshTokenGrantWasRejectedDueToRevokedRefreshToken{
RefreshToken: c.RefreshToken,
})
return
}
if c.Scope != "" && a.Scope != c.Scope {
a.emit(RequestAccessTokenViaRefreshTokenGrantWasRejectedDueToInvalidScope{
RefreshToken: c.RefreshToken,
ClientID: c.ClientID,
Scope: a.Scope,
RequestedScope: c.Scope,
})
return
}
nextRefreshToken := a.tokenGenerator.New()
if a.IsForUser {
a.emit(
AccessTokenWasIssuedToUserViaRefreshTokenGrant{
RefreshToken: c.RefreshToken,
UserID: a.UserID,
ClientID: c.ClientID,
},
RefreshTokenWasIssuedToUserViaRefreshTokenGrant{
RefreshToken: c.RefreshToken,
UserID: a.UserID,
ClientID: c.ClientID,
NextRefreshToken: nextRefreshToken,
Scope: a.Scope,
},
)
} else if a.IsForClientApplication {
a.emit(
AccessTokenWasIssuedToClientApplicationViaRefreshTokenGrant{
RefreshToken: c.RefreshToken,
ClientID: c.ClientID,
},
RefreshTokenWasIssuedToClientApplicationViaRefreshTokenGrant{
RefreshToken: c.RefreshToken,
ClientID: a.ClientID,
NextRefreshToken: nextRefreshToken,
},
)
}
case IssueRefreshTokenToUser:
a.emit(RefreshTokenWasIssuedToUser{
RefreshToken: c.RefreshToken,
UserID: c.UserID,
ClientID: c.ClientID,
Scope: c.Scope,
})
}
}
func (a *refreshToken) emit(events ...rangedb.Event) {
for _, event := range events {
a.apply(event)
}
a.PendingEvents = append(a.PendingEvents, events...)
}
func (a *refreshToken) GetPendingEvents() []rangedb.Event {
return a.PendingEvents
}