Skip to content

Commit 1c1c9aa

Browse files
committed
Split command authorization between aggregate boundaries
1 parent 6109fad commit 1c1c9aa

3 files changed

+114
-81
lines changed

authorization_command_handler.go renamed to client_application_command_authorization.go

+10-80
Original file line numberDiff line numberDiff line change
@@ -2,88 +2,22 @@ package goauth2
22

33
import (
44
"github.com/inklabs/rangedb"
5-
"github.com/inklabs/rangedb/pkg/clock"
65
)
76

8-
type authorizationCommandHandler struct {
9-
store rangedb.Store
10-
pendingEvents []rangedb.Event
11-
tokenGenerator TokenGenerator
12-
clock clock.Clock
7+
type clientApplicationCommandAuthorization struct {
8+
store rangedb.Store
9+
pendingEvents []rangedb.Event
1310
}
1411

15-
func newAuthorizationCommandHandler(
16-
store rangedb.Store,
17-
tokenGenerator TokenGenerator,
18-
clock clock.Clock,
19-
) *authorizationCommandHandler {
20-
return &authorizationCommandHandler{
21-
store: store,
22-
tokenGenerator: tokenGenerator,
23-
clock: clock,
12+
func newClientApplicationCommandAuthorization(store rangedb.Store) *clientApplicationCommandAuthorization {
13+
return &clientApplicationCommandAuthorization{
14+
store: store,
2415
}
2516
}
2617

27-
func (h *authorizationCommandHandler) Handle(command Command) bool {
18+
func (h *clientApplicationCommandAuthorization) Handle(command Command) bool {
2819
switch c := command.(type) {
2920

30-
case GrantUserAdministratorRole:
31-
grantingUser := h.loadResourceOwnerAggregate(c.GrantingUserID)
32-
33-
if !grantingUser.IsOnBoarded {
34-
h.emit(GrantUserAdministratorRoleWasRejectedDueToMissingGrantingUser{
35-
UserID: c.UserID,
36-
GrantingUserID: c.GrantingUserID,
37-
})
38-
return false
39-
}
40-
41-
if !grantingUser.IsAdministrator {
42-
h.emit(GrantUserAdministratorRoleWasRejectedDueToNonAdministrator{
43-
UserID: c.UserID,
44-
GrantingUserID: c.GrantingUserID,
45-
})
46-
return false
47-
}
48-
49-
case AuthorizeUserToOnBoardClientApplications:
50-
authorizingUser := h.loadResourceOwnerAggregate(c.AuthorizingUserID)
51-
52-
if !authorizingUser.IsOnBoarded {
53-
h.emit(AuthorizeUserToOnBoardClientApplicationsWasRejectedDueToMissingAuthorizingUser{
54-
UserID: c.UserID,
55-
AuthorizingUserID: c.AuthorizingUserID,
56-
})
57-
return false
58-
}
59-
60-
if !authorizingUser.IsAdministrator {
61-
h.emit(AuthorizeUserToOnBoardClientApplicationsWasRejectedDueToNonAdministrator{
62-
UserID: c.UserID,
63-
AuthorizingUserID: c.AuthorizingUserID,
64-
})
65-
return false
66-
}
67-
68-
case OnBoardClientApplication:
69-
resourceOwner := h.loadResourceOwnerAggregate(c.UserID)
70-
71-
if !resourceOwner.IsOnBoarded {
72-
h.emit(OnBoardClientApplicationWasRejectedDueToUnAuthorizeUser{
73-
ClientID: c.ClientID,
74-
UserID: c.UserID,
75-
})
76-
return false
77-
}
78-
79-
if !resourceOwner.IsAuthorizedToOnboardClientApplications {
80-
h.emit(OnBoardClientApplicationWasRejectedDueToUnAuthorizeUser{
81-
ClientID: c.ClientID,
82-
UserID: c.UserID,
83-
})
84-
return false
85-
}
86-
8721
case RequestAccessTokenViaImplicitGrant:
8822
clientApplication := h.loadClientApplicationAggregate(c.ClientID)
8923

@@ -195,18 +129,14 @@ func (h *authorizationCommandHandler) Handle(command Command) bool {
195129
return true
196130
}
197131

198-
func (h *authorizationCommandHandler) emit(events ...rangedb.Event) {
132+
func (h *clientApplicationCommandAuthorization) emit(events ...rangedb.Event) {
199133
h.pendingEvents = append(h.pendingEvents, events...)
200134
}
201135

202-
func (h *authorizationCommandHandler) loadResourceOwnerAggregate(userID string) *resourceOwner {
203-
return newResourceOwner(h.store.AllEventsByStream(resourceOwnerStream(userID)), h.tokenGenerator, h.clock)
204-
}
205-
206-
func (h *authorizationCommandHandler) loadClientApplicationAggregate(clientID string) *clientApplication {
136+
func (h *clientApplicationCommandAuthorization) loadClientApplicationAggregate(clientID string) *clientApplication {
207137
return newClientApplication(h.store.AllEventsByStream(clientApplicationStream(clientID)))
208138
}
209139

210-
func (h *authorizationCommandHandler) GetPendingEvents() []rangedb.Event {
140+
func (h *clientApplicationCommandAuthorization) GetPendingEvents() []rangedb.Event {
211141
return h.pendingEvents
212142
}

goauth2.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ func New(options ...Option) *App {
5252
}
5353

5454
app.preCommandHandlers = []PreCommandHandler{
55-
newAuthorizationCommandHandler(app.store, app.tokenGenerator, app.clock),
55+
newResourceOwnerCommandAuthorization(app.store, app.tokenGenerator, app.clock),
56+
newClientApplicationCommandAuthorization(app.store),
5657
}
5758

5859
return app
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package goauth2
2+
3+
import (
4+
"github.com/inklabs/rangedb"
5+
"github.com/inklabs/rangedb/pkg/clock"
6+
)
7+
8+
type resourceOwnerCommandAuthorization struct {
9+
store rangedb.Store
10+
clock clock.Clock
11+
tokenGenerator TokenGenerator
12+
pendingEvents []rangedb.Event
13+
}
14+
15+
func newResourceOwnerCommandAuthorization(
16+
store rangedb.Store,
17+
tokenGenerator TokenGenerator,
18+
clock clock.Clock,
19+
) *resourceOwnerCommandAuthorization {
20+
return &resourceOwnerCommandAuthorization{
21+
store: store,
22+
tokenGenerator: tokenGenerator,
23+
clock: clock,
24+
}
25+
}
26+
27+
func (a *resourceOwnerCommandAuthorization) Handle(command Command) bool {
28+
switch c := command.(type) {
29+
30+
case GrantUserAdministratorRole:
31+
grantingUser := a.loadResourceOwnerAggregate(c.GrantingUserID)
32+
33+
if !grantingUser.IsOnBoarded {
34+
a.emit(GrantUserAdministratorRoleWasRejectedDueToMissingGrantingUser{
35+
UserID: c.UserID,
36+
GrantingUserID: c.GrantingUserID,
37+
})
38+
return false
39+
}
40+
41+
if !grantingUser.IsAdministrator {
42+
a.emit(GrantUserAdministratorRoleWasRejectedDueToNonAdministrator{
43+
UserID: c.UserID,
44+
GrantingUserID: c.GrantingUserID,
45+
})
46+
return false
47+
}
48+
49+
case AuthorizeUserToOnBoardClientApplications:
50+
authorizingUser := a.loadResourceOwnerAggregate(c.AuthorizingUserID)
51+
52+
if !authorizingUser.IsOnBoarded {
53+
a.emit(AuthorizeUserToOnBoardClientApplicationsWasRejectedDueToMissingAuthorizingUser{
54+
UserID: c.UserID,
55+
AuthorizingUserID: c.AuthorizingUserID,
56+
})
57+
return false
58+
}
59+
60+
if !authorizingUser.IsAdministrator {
61+
a.emit(AuthorizeUserToOnBoardClientApplicationsWasRejectedDueToNonAdministrator{
62+
UserID: c.UserID,
63+
AuthorizingUserID: c.AuthorizingUserID,
64+
})
65+
return false
66+
}
67+
68+
case OnBoardClientApplication:
69+
resourceOwner := a.loadResourceOwnerAggregate(c.UserID)
70+
71+
if !resourceOwner.IsOnBoarded {
72+
a.emit(OnBoardClientApplicationWasRejectedDueToUnAuthorizeUser{
73+
ClientID: c.ClientID,
74+
UserID: c.UserID,
75+
})
76+
return false
77+
}
78+
79+
if !resourceOwner.IsAuthorizedToOnboardClientApplications {
80+
a.emit(OnBoardClientApplicationWasRejectedDueToUnAuthorizeUser{
81+
ClientID: c.ClientID,
82+
UserID: c.UserID,
83+
})
84+
return false
85+
}
86+
87+
}
88+
89+
return true
90+
}
91+
92+
func (a *resourceOwnerCommandAuthorization) emit(events ...rangedb.Event) {
93+
a.pendingEvents = append(a.pendingEvents, events...)
94+
}
95+
96+
func (a *resourceOwnerCommandAuthorization) loadResourceOwnerAggregate(userID string) *resourceOwner {
97+
return newResourceOwner(a.store.AllEventsByStream(resourceOwnerStream(userID)), a.tokenGenerator, a.clock)
98+
}
99+
100+
func (a *resourceOwnerCommandAuthorization) GetPendingEvents() []rangedb.Event {
101+
return a.pendingEvents
102+
}

0 commit comments

Comments
 (0)