This repository has been archived by the owner on Mar 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
oauth2.go
310 lines (268 loc) · 10 KB
/
oauth2.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Package oauth2 implements the OAuth2 HTTP dancing in accordance with http://tools.ietf.org/html/rfc6749
// and leaves the rest of the implementation to its users by requiring them
// to implement oauth2.Provider interface.
package oauth2
import (
"html/template"
"log"
"net/http"
"net/url"
"strings"
"time"
"github.com/hooklift/oauth2/internal/render"
"github.com/hooklift/oauth2/types"
)
// Provider defines functions required by the oauth2 package to properly work.
// Users of this package are required to implement them.
type Provider interface {
// AuthenticateClient authenticates a previously registered client.
AuthenticateClient(username, password string) (types.Client, error)
// AuthenticateUser authenticates resource owner.
AuthenticateUser(username, password string) (valid bool)
// ClientInfo returns 3rd-party client information
ClientInfo(clientID string) (info types.Client, err error)
// GrantInfo returns information about the authorization grant code.
GrantInfo(code string) (types.Grant, error)
// TokenInfo returns information about one specific token.
TokenInfo(token string) (types.Token, error)
// ScopesInfo parses the list of scopes requested by the client and
// returns its descriptions for the resource owner to fully understand
// what she is authorizing the client to access to. An error is returned
// if the scopes list does not comply with http://tools.ietf.org/html/rfc6749#section-3.3
//
// Unrecognized or non-existent scopes are ignored.
ScopesInfo(scopes string) (types.Scopes, error)
// ResourceScopes returns the scopes associated with a given resource
ResourceScopes(url *url.URL) (types.Scopes, error)
// GenGrant issues and stores an authorization grant code, in a persistent storage.
// The authorization code MUST expire shortly after it is issued to mitigate
// the risk of leaks. A maximum authorization code lifetime of 10 minutes is
// RECOMMENDED. If an authorization code is used more than once, the authorization
// server MUST deny the request and SHOULD revoke (when possible) all tokens
// previously issued based on that authorization code. The authorization
// code is bound to the client identifier and redirection URI.
// -- http://tools.ietf.org/html/rfc6749#section-4.1.2
GenGrant(client types.Client, scopes types.Scopes, expiration time.Duration) (code types.Grant, err error)
// GenToken generates and stores access and refresh tokens with the given
// client information and authorization scope.
GenToken(grant types.Grant, client types.Client, refreshToken bool, expiration time.Duration) (token types.Token, err error)
// RevokeToken expires a specific token.
RevokeToken(token string) error
// RefreshToken refreshes an access token.
RefreshToken(refreshToken types.Token, scopes types.Scopes) (accessToken types.Token, err error)
// IsUserAuthenticated checks whether or not the resource owner has a valid session
// with the system. If not, it redirects the user to the login URL.
IsUserAuthenticated() bool
}
// http://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html
type option func(*config)
// Config defines the configuration struct for the oauth2 provider.
type config struct {
authzEndpoint string
tokenEndpoint string
loginURL struct {
url *url.URL
redirectParam string
}
stsMaxAge time.Duration
authzForm *template.Template
provider Provider
authzExpiration time.Duration
tokenExpiration time.Duration
}
// TokenEndpoint allows setting token endpoint. Defaults to "/oauth2/tokens".
//
// The token endpoint is used by the client to obtain an access token by
// presenting its authorization grant or refresh token. The token
// endpoint is used with every authorization grant except for the
// implicit grant type (since an access token is issued directly).
//
// Since requests to the token endpoint result in the transmission of
// clear-text credentials (in the HTTP request and response), the
// authorization server MUST require the use of TLS as described in
// Section 1.6 when sending requests to the token endpoint.
//
// -- http://tools.ietf.org/html/rfc6749#section-3.2
func SetTokenEndpoint(endpoint string) option {
return func(c *config) {
c.tokenEndpoint = endpoint
}
}
// AuthzEndpoint allows setting authorization endpoint. Defaults to "/oauth2/authzs"
//
// The authorization endpoint is used to interact with the resource owner and
// obtain an authorization grant.
//
// Since requests to the authorization endpoint result in user authentication
// and the transmission of clear-text credentials (in the HTTP response), the
// authorization server MUST require the use of TLS as described in Section 1.6
// when sending requests to the authorization endpoint.
//
// -- http://tools.ietf.org/html/rfc6749#section-3.1.1
func SetAuthzEndpoint(endpoint string) option {
return func(c *config) {
c.authzEndpoint = endpoint
}
}
// SetSTSMaxAge sets Strict Transport Security maximum age. Defaults to 1yr.
func SetSTSMaxAge(maxAge time.Duration) option {
return func(c *config) {
c.stsMaxAge = maxAge
}
}
// SetAuthzForm sets authorization form to show to the resource owner.
func SetAuthzForm(form string) option {
return func(c *config) {
t := template.New("authzform")
tpl, err := t.Parse(form)
if err != nil {
log.Fatalf("Error parsing authorization form: %v", err)
}
c.authzForm = tpl
}
}
// SetTokenExpiration allows setting expiration time for access tokens.
func SetTokenExpiration(e time.Duration) option {
return func(c *config) {
c.tokenExpiration = e
}
}
// SetAuthzExpiration allows setting expiration time for authorization grant codes.
func SetAuthzExpiration(e time.Duration) option {
return func(c *config) {
c.authzExpiration = e
}
}
// SetProvider sets backend provider
func SetProvider(p Provider) option {
return func(c *config) {
c.provider = p
}
}
// SetLoginURL allows to set a login URL to redirect users to when they don't
// have valid sessions. The authentication system should send back the user
// to the referer URL in order to complete the OAuth2 authorization process.
func SetLoginURL(u, redirectParam string) option {
loginURL, err := url.Parse(u)
if err != nil {
log.Fatalln("[ERROR] Invalid URL: %v", err)
}
return func(c *config) {
c.loginURL.url = loginURL
c.loginURL.redirectParam = redirectParam
}
}
// AuthzHandler is intended to be used at the resource server side to protect and validate
// access to its resources. In accordance with http://tools.ietf.org/html/rfc6749#section-7
// and http://tools.ietf.org/html/rfc6750
func AuthzHandler(next http.Handler, provider Provider) http.Handler {
if provider == nil {
log.Fatalln("An implementation of the oauth2.Provider interface is expected")
}
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
var token string
auth := req.Header.Get("Authorization")
if auth == "" {
token = req.FormValue("access_token")
} else {
if !strings.HasPrefix(auth, "Bearer ") {
render.Unauthorized(w, render.Options{
Status: http.StatusUnauthorized,
Data: ErrUnsupportedTokenType,
})
return
}
token = strings.TrimPrefix(auth, "Bearer ")
}
// If the request lacks any authentication information (e.g., the client
// was unaware that authentication is necessary or attempted using an
// unsupported authentication method), the resource server SHOULD NOT
// include an error code or other error information.
if token == "" {
render.Unauthorized(w, render.Options{
Status: http.StatusUnauthorized,
})
return
}
// Get token info from Authorizer
tokenInfo, err := provider.TokenInfo(token)
if err != nil {
render.Unauthorized(w, render.Options{
Status: http.StatusUnauthorized,
Data: ErrServerError("", err),
})
return
}
if tokenInfo.Status == types.TokenExpired || tokenInfo.Status == types.TokenRevoked {
render.Unauthorized(w, render.Options{
Status: http.StatusUnauthorized,
Data: ErrInvalidToken,
})
return
}
// Get scopes information for the given resource
scopes, err := provider.ResourceScopes(req.URL)
if err != nil {
render.Unauthorized(w, render.Options{
Status: http.StatusUnauthorized,
Data: ErrServerError("", err),
})
return
}
// Check that token's scope covers the requested resource
resourceScopes := scopes.Encode()
for _, scope := range tokenInfo.Scopes {
if !strings.Contains(resourceScopes, scope.ID) {
render.Unauthorized(w, render.Options{
Status: http.StatusForbidden,
Data: ErrInsufficientScope,
})
return
}
}
next.ServeHTTP(w, req)
})
}
// Handler handles OAuth2 requests for getting authorization grants as well as
// access and refresh tokens.
func Handler(next http.Handler, opts ...option) http.Handler {
// Default configuration options.
cfg := config{
tokenEndpoint: "/oauth2/tokens",
authzEndpoint: "/oauth2/authzs",
stsMaxAge: time.Duration(31536000) * time.Second, // 1yr
}
// Applies user's configuration.
for _, opt := range opts {
opt(&cfg)
}
if cfg.authzForm == nil {
log.Fatalln("Authorization form is required")
}
if cfg.provider == nil {
log.Fatalln("An implementation of the oauth2.Provider interface is expected")
}
// Keeps a registry of path function handlers for OAuth2 requests.
registry := map[string]map[string]func(http.ResponseWriter, *http.Request, config){
cfg.authzEndpoint: AuthzHandlers,
cfg.tokenEndpoint: TokenHandlers,
}
// Locates and runs specific OAuth2 handler for request's method
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
for p, handlers := range registry {
if strings.HasPrefix(req.URL.Path, p) {
if handlerFn, ok := handlers[req.Method]; ok {
handlerFn(w, req, cfg)
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("Method Not Allowed"))
return
}
}
next.ServeHTTP(w, req)
})
}