-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_authorization.go
183 lines (157 loc) · 4.06 KB
/
http_authorization.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
package ohauth
import (
"net/http"
"net/url"
"strconv"
"time"
)
func (c *context) redirectAuthorization() {
next := c.provider.URL.Clone()
next.Path += "/dialog"
next.RawQuery = c.request.URL.RawQuery
c.redirect(next.String())
}
type authorizationRequest struct {
client *Client
session *TokenClaims
redirect *StrictURL
scope Scope
state string
prompted bool
}
var authorizeHandlers = map[string]func(*context, *authorizationRequest) error{
"code": authorizeWithCode,
"token": authorizeWithToken,
}
func authorizeWithCode(ctx *context, r *authorizationRequest) error {
p := ctx.provider
c := r.client
cid := r.client.ID
uid := r.session.Subject
v := url.Values{}
v.Set("state", r.state)
if c.GrantType != AuthorizationCode {
ctx.fail(r.redirect, ErrWrongGrant, r.state)
return nil
}
tc := NewTokenClaims(RoleCode, ctx.timestamp, ctx.timestamp.Add(p.Issuer.ExpiryForCode()))
tc.ID = randID()
tc.Audience = cid
tc.Subject = r.session.Subject
tc.Issuer = p.URL.String()
tc.Scope = r.scope
tc.Grant = "authorization_code"
a, err := p.Store.FetchAuthorization(cid, uid)
if err != nil {
return err
}
authorized := a != nil && a.Scope.Equals(r.scope)
if !authorized && !r.prompted {
ctx.redirectAuthorization()
return nil
}
if r.prompted {
err := p.Store.StoreAuthorization(NewAuthorization(cid, uid, r.scope))
if err != nil {
return err
}
}
code, err := p.Tokenizer.Tokenize(tc, r.client.Keys.Sign)
if err != nil {
return err
}
v.Set("code", code)
ctx.redirect(r.redirect.StringWithParams(v))
return nil
}
func authorizeWithToken(ctx *context, r *authorizationRequest) error {
p := ctx.provider
c := r.client
v := url.Values{}
v.Set("state", r.state)
if c.GrantType != Implicit {
ctx.fail(r.redirect, ErrWrongGrant, r.state)
return nil
}
cid := r.client.ID
uid := r.session.Subject
a, err := p.Store.FetchAuthorization(cid, uid)
if err != nil {
return err
}
authorized := a != nil && a.Scope.Equals(r.scope)
if !authorized && !r.prompted {
ctx.redirectAuthorization()
return nil
}
if r.prompted {
err := p.Store.StoreAuthorization(NewAuthorization(cid, uid, r.scope))
if err != nil {
return err
}
}
tc := NewTokenClaims(RoleAccessToken, ctx.timestamp, ctx.timestamp.Add(p.Issuer.ExpiryForToken(c.GrantType)))
tc.ID = randID()
tc.Audience = cid
tc.Subject = r.session.Subject
tc.Issuer = p.URL.String()
tc.Scope = r.scope
tc.Grant = "implicit"
at, err := p.Tokenizer.Tokenize(tc, r.client.Keys.Sign)
if err != nil {
return err
}
v.Set("access_token", at)
v.Set("expires_in", strconv.FormatInt(tc.Expires-time.Now().Unix(), 10))
ctx.redirect(r.redirect.StringWithFragment(v))
return nil
}
func handleAuthorize(ctx *context) error {
p := ctx.provider
err := ctx.request.ParseForm()
if err != nil {
ctx.abort(http.StatusBadRequest, "Bad request")
return nil
}
q := ctx.request.Form
if ctx.request.Method == "POST" {
q = ctx.request.PostForm
}
state := q.Get("state")
scope := ParseScope(q.Get("scope"))
prompted := ctx.request.Method == "POST"
v := url.Values{}
v.Set("state", state)
ru, err := ParseURL(q.Get("redirect_uri"))
if err != nil {
ctx.abort(http.StatusBadRequest, "Bad redirect uri")
return nil
}
handler, found := authorizeHandlers[q.Get("response_type")]
if !found {
ctx.redirect(ru.StringWithParams(mergeValues(ErrUnsupportResponseType.Values(), v)))
return nil
}
client, err := p.Store.FetchClient(q.Get("client_id"))
if err != nil {
return err
}
if client == nil || client.Status != ClientActive {
ctx.redirect(ru.StringWithParams(mergeValues(ErrClientNotFound.Values(), v)))
return nil
}
if client.RedirectURI.String() != ru.String() {
ctx.redirect(ru.StringWithParams(mergeValues(ErrBadRedirect.Values(), v)))
return nil
}
if !client.Scope.Contains(scope) {
ctx.fail(ru, ErrScopeNotAllowed, state)
return nil
}
sc, err := p.Authenticator.AuthenticateRequest(ctx.request, client)
if err != nil {
panic(err) // TODO redirect and then spew
}
req := &authorizationRequest{client, sc, ru, scope, state, prompted}
return handler(ctx, req)
}