-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclient_application.go
127 lines (103 loc) · 2.92 KB
/
client_application.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
package goauth2
import (
"net/url"
"time"
"github.com/inklabs/rangedb"
"github.com/inklabs/rangedb/pkg/clock"
)
const clientApplicationGrantLifetime = 1 * time.Hour
// ClientApplicationCommandTypes returns all command types goauth2.clientApplication supports.
func ClientApplicationCommandTypes() []string {
return []string{
OnBoardClientApplication{}.CommandType(),
RequestAccessTokenViaClientCredentialsGrant{}.CommandType(),
}
}
type clientApplication struct {
IsOnBoarded bool
ClientID string
ClientSecret string
RedirectURI string
pendingEvents []rangedb.Event
clock clock.Clock
}
func newClientApplication(iter rangedb.RecordIterator, clock clock.Clock) *clientApplication {
aggregate := &clientApplication{
clock: clock,
}
for iter.Next() {
if event, ok := iter.Record().Data.(rangedb.Event); ok {
aggregate.apply(event)
}
}
return aggregate
}
func (a *clientApplication) apply(event rangedb.Event) {
switch e := event.(type) {
case *ClientApplicationWasOnBoarded:
a.IsOnBoarded = true
a.ClientID = e.ClientID
a.ClientSecret = e.ClientSecret
a.RedirectURI = e.RedirectURI
}
}
func (a *clientApplication) GetPendingEvents() []rangedb.Event {
return a.pendingEvents
}
func (a *clientApplication) Handle(command Command) {
switch c := command.(type) {
case OnBoardClientApplication:
a.OnBoardClientApplication(c)
case RequestAccessTokenViaClientCredentialsGrant:
a.RequestAccessTokenViaClientCredentialsGrant(c)
}
}
func (a *clientApplication) OnBoardClientApplication(c OnBoardClientApplication) {
uri, err := url.Parse(c.RedirectURI)
if err != nil {
a.raise(OnBoardClientApplicationWasRejectedDueToInvalidRedirectURI{
ClientID: c.ClientID,
RedirectURI: c.RedirectURI,
})
return
}
if uri.Scheme != "https" {
a.raise(OnBoardClientApplicationWasRejectedDueToInsecureRedirectURI{
ClientID: c.ClientID,
RedirectURI: c.RedirectURI,
})
return
}
a.raise(ClientApplicationWasOnBoarded{
ClientID: c.ClientID,
ClientSecret: c.ClientSecret,
RedirectURI: c.RedirectURI,
UserID: c.UserID,
})
}
func (a *clientApplication) RequestAccessTokenViaClientCredentialsGrant(c RequestAccessTokenViaClientCredentialsGrant) {
if !a.IsOnBoarded {
a.raise(RequestAccessTokenViaClientCredentialsGrantWasRejectedDueToInvalidClientApplicationID{
ClientID: c.ClientID,
})
return
}
if a.ClientSecret != c.ClientSecret {
a.raise(RequestAccessTokenViaClientCredentialsGrantWasRejectedDueToInvalidClientApplicationSecret{
ClientID: c.ClientID,
})
return
}
expiresAt := a.clock.Now().Add(clientApplicationGrantLifetime).Unix()
a.raise(AccessTokenWasIssuedToClientApplicationViaClientCredentialsGrant{
ClientID: c.ClientID,
ExpiresAt: expiresAt,
Scope: c.Scope,
})
}
func (a *clientApplication) raise(events ...rangedb.Event) {
for _, event := range events {
a.apply(event)
}
a.pendingEvents = append(a.pendingEvents, events...)
}