forked from inklabs/goauth2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
goauth2.go
185 lines (146 loc) · 4.76 KB
/
goauth2.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
package goauth2
import (
"log"
"github.com/inklabs/rangedb"
"github.com/inklabs/rangedb/pkg/clock"
"github.com/inklabs/rangedb/provider/inmemorystore"
"github.com/inklabs/goauth2/provider/uuidtoken"
)
//App is the OAuth2 CQRS application.
type App struct {
clock clock.Clock
store rangedb.Store
tokenGenerator TokenGenerator
preCommandHandlers []PreCommandHandler
logger *log.Logger
}
// Option defines functional option parameters for App.
type Option func(*App)
//WithClock is a functional option to inject a clock.
func WithClock(clock clock.Clock) Option {
return func(app *App) {
app.clock = clock
}
}
//WithStore is a functional option to inject a RangeDB Event Store.
func WithStore(store rangedb.Store) Option {
return func(app *App) {
app.store = store
}
}
//WithTokenGenerator is a functional option to inject a token generator.
func WithTokenGenerator(generator TokenGenerator) Option {
return func(app *App) {
app.tokenGenerator = generator
}
}
// WithLogger is a functional option to inject a Logger.
func WithLogger(logger *log.Logger) Option {
return func(app *App) {
app.logger = logger
}
}
//New constructs an OAuth2 CQRS application.
func New(options ...Option) *App {
app := &App{
store: inmemorystore.New(),
tokenGenerator: uuidtoken.NewGenerator(),
}
for _, option := range options {
option(app)
}
BindEvents(app.store)
app.preCommandHandlers = []PreCommandHandler{
newResourceOwnerCommandAuthorization(app.store, app.tokenGenerator, app.clock),
newClientApplicationCommandAuthorization(app.store),
}
app.store.Subscribe(
newRefreshTokenProcessManager(app.Dispatch),
)
return app
}
func (a *App) Dispatch(command Command) []rangedb.Event {
var events []rangedb.Event
for _, handler := range a.preCommandHandlers {
shouldContinue := handler.Handle(command)
events = append(events, a.savePendingEvents(handler)...)
if !shouldContinue {
return events
}
}
switch command.(type) {
case RequestAccessTokenViaClientCredentialsGrant:
events = a.handleWithClientApplicationAggregate(command)
case OnBoardUser:
events = a.handleWithResourceOwnerAggregate(command)
case GrantUserAdministratorRole:
events = a.handleWithResourceOwnerAggregate(command)
case AuthorizeUserToOnBoardClientApplications:
events = a.handleWithResourceOwnerAggregate(command)
case OnBoardClientApplication:
events = a.handleWithClientApplicationAggregate(command)
case RequestAccessTokenViaImplicitGrant:
events = a.handleWithResourceOwnerAggregate(command)
case RequestAccessTokenViaROPCGrant:
events = a.handleWithResourceOwnerAggregate(command)
case RequestAccessTokenViaRefreshTokenGrant:
events = a.handleWithRefreshTokenAggregate(command)
case RequestAuthorizationCodeViaAuthorizationCodeGrant:
events = a.handleWithResourceOwnerAggregate(command)
case RequestAccessTokenViaAuthorizationCodeGrant:
events = a.handleWithAuthorizationCodeAggregate(command)
case IssueRefreshTokenToUser:
events = a.handleWithRefreshTokenAggregate(command)
}
return events
}
func (a *App) handleWithClientApplicationAggregate(command Command) []rangedb.Event {
aggregate := newClientApplication(a.store.AllEventsByStream(rangedb.GetEventStream(command)))
aggregate.Handle(command)
return a.savePendingEvents(aggregate)
}
func (a *App) handleWithResourceOwnerAggregate(command Command) []rangedb.Event {
aggregate := newResourceOwner(
a.store.AllEventsByStream(rangedb.GetEventStream(command)),
a.tokenGenerator,
a.clock,
)
aggregate.Handle(command)
return a.savePendingEvents(aggregate)
}
func (a *App) handleWithRefreshTokenAggregate(command Command) []rangedb.Event {
aggregate := newRefreshToken(
a.store.AllEventsByStream(rangedb.GetEventStream(command)),
a.tokenGenerator,
)
aggregate.Handle(command)
return a.savePendingEvents(aggregate)
}
func (a *App) handleWithAuthorizationCodeAggregate(command Command) []rangedb.Event {
aggregate := newAuthorizationCode(
a.store.AllEventsByStream(rangedb.GetEventStream(command)),
a.tokenGenerator,
a.clock,
)
aggregate.Handle(command)
return a.savePendingEvents(aggregate)
}
func (a *App) savePendingEvents(events PendingEvents) []rangedb.Event {
pendingEvents := events.GetPendingEvents()
for _, event := range pendingEvents {
err := a.store.Save(event, nil)
if err != nil {
a.logger.Printf("unable to save event: %v", err)
}
}
return pendingEvents
}
func (a *App) SubscribeAndReplay(subscribers ...rangedb.RecordSubscriber) {
a.store.SubscribeAndReplay(subscribers...)
}
func resourceOwnerStream(userID string) string {
return rangedb.GetEventStream(UserWasOnBoarded{UserID: userID})
}
func clientApplicationStream(clientID string) string {
return rangedb.GetEventStream(ClientApplicationWasOnBoarded{ClientID: clientID})
}