-
Notifications
You must be signed in to change notification settings - Fork 70
/
ginoauth2.go
383 lines (341 loc) · 11.1 KB
/
ginoauth2.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Package ginoauth2 implements an OAuth2 based authorization
// middleware for the Gin https://github.com/gin-gonic/gin
// webframework.
//
// Example:
//
// package main
// import (
// "flag"
// "time"
// "github.com/gin-gonic/gin"
// "github.com/golang/glog"
// "github.com/szuecs/gin-glog"
// "github.com/zalando/gin-oauth2"
// "golang.org/x/oauth2"
// )
//
// var OAuth2Endpoint = oauth2.Endpoint{
// AuthURL: "https://token.oauth2.corp.com/access_token",
// TokenURL: "https://oauth2.corp.com/corp/oauth2/tokeninfo",
// }
//
// func UidCheck(tc *TokenContainer, ctx *gin.Context) bool {
// uid := tc.Scopes["uid"].(string)
// if uid != "sszuecs" {
// return false
// }
// ctx.Set("uid", uid)
// return true
// }
//
// func main() {
// flag.Parse()
// router := gin.New()
// router.Use(ginglog.Logger(3 * time.Second))
// router.Use(gin.Recovery())
//
// ginoauth2.VarianceTimer = 300 * time.Millisecond // defaults to 30s
//
// public := router.Group("/api")
// public.GET("/", func(c *gin.Context) {
// c.JSON(200, gin.H{"message": "Hello to public world"})
// })
//
// private := router.Group("/api/private")
// private.Use(ginoauth2.Auth(UidCheck, OAuth2Endpoint))
// private.GET("/", func(c *gin.Context) {
// c.JSON(200, gin.H{"message": "Hello from private"})
// })
//
// glog.Info("bootstrapped application")
// router.Run(":8081")
package ginoauth2
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strings"
"time"
"github.com/gin-gonic/gin"
"golang.org/x/oauth2"
)
// VarianceTimer controls the max runtime of Auth() and AuthChain() middleware
var VarianceTimer time.Duration = 30000 * time.Millisecond
// AuthInfoURL is the URL to get information of your token
var AuthInfoURL string
// Transport to use for client http connections to AuthInfoURL
var Transport = http.Transport{}
// TokenContainer stores all relevant token information
type TokenContainer struct {
Token *oauth2.Token
Scopes map[string]interface{} // LDAP record vom Benutzer (cn, ..
GrantType string // password, ??
Realm string // services, employees
}
// AccessCheckFunction is a function that checks if a given token grants
// access.
type AccessCheckFunction func(tc *TokenContainer, ctx *gin.Context) bool
type Options struct {
Endpoint oauth2.Endpoint
AccessTokenInHeader bool
}
var accessTokenMask = regexp.MustCompile("[?&]access_token=[^&]+")
func maskAccessToken(a interface{}) string {
s := fmt.Sprint(a)
s = accessTokenMask.ReplaceAllString(s, "<MASK>")
return s
}
func extractToken(r *http.Request) (*oauth2.Token, error) {
hdr := r.Header.Get("Authorization")
if hdr == "" {
return nil, errors.New("no authorization header")
}
th := strings.Split(hdr, " ")
if len(th) != 2 {
return nil, errors.New("incomplete authorization header")
}
return &oauth2.Token{AccessToken: th[1], TokenType: th[0]}, nil
}
func requestAuthInfo(o Options, t *oauth2.Token) ([]byte, error) {
var infoURL string
if o.AccessTokenInHeader {
infoURL = AuthInfoURL
} else {
var uv = make(url.Values)
uv.Set("access_token", t.AccessToken)
infoURL = AuthInfoURL + "?" + uv.Encode()
}
client := &http.Client{Transport: &Transport}
req, err := http.NewRequest("GET", infoURL, nil)
if err != nil {
return nil, err
}
if o.AccessTokenInHeader {
req.Header.Set("Authorization", "Bearer "+t.AccessToken)
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
func RequestAuthInfo(t *oauth2.Token) ([]byte, error) {
return requestAuthInfo(Options{}, t)
}
func ParseTokenContainer(t *oauth2.Token, data map[string]interface{}) (*TokenContainer, error) {
tdata := make(map[string]interface{})
ttype := data["token_type"].(string)
gtype := data["grant_type"].(string)
realm := data["realm"].(string)
exp := data["expires_in"].(float64)
tok := data["access_token"].(string)
if ttype != t.TokenType {
return nil, errors.New("token type mismatch")
}
if tok != t.AccessToken {
return nil, errors.New("mismatch between verify request and answer")
}
scopes := data["scope"].([]interface{})
for _, scope := range scopes {
sscope := scope.(string)
sval, ok := data[sscope]
if ok {
tdata[sscope] = sval
}
}
return &TokenContainer{
Token: &oauth2.Token{
AccessToken: tok,
TokenType: ttype,
Expiry: time.Now().Add(time.Duration(exp) * time.Second),
},
Scopes: tdata,
Realm: realm,
GrantType: gtype,
}, nil
}
func getTokenContainerForToken(o Options, token *oauth2.Token) (*TokenContainer, error) {
body, err := requestAuthInfo(o, token)
if err != nil {
errorf("[Gin-OAuth] RequestAuthInfo failed caused by: %s", err)
return nil, err
}
// extract AuthInfo
var data map[string]interface{}
err = json.Unmarshal(body, &data)
if err != nil {
errorf("[Gin-OAuth] JSON.Unmarshal failed caused by: %s", err)
return nil, err
}
if si, ok := data["error_description"]; ok {
s, ok := si.(string)
if !ok {
s = ""
}
errorf("[Gin-OAuth] RequestAuthInfo returned an error: %s", s)
return nil, errors.New(s)
}
return ParseTokenContainer(token, data)
}
func GetTokenContainer(token *oauth2.Token) (*TokenContainer, error) {
return getTokenContainerForToken(Options{}, token)
}
func getTokenContainer(o Options, ctx *gin.Context) (*TokenContainer, bool) {
var oauthToken *oauth2.Token
var tc *TokenContainer
var err error
if oauthToken, err = extractToken(ctx.Request); err != nil {
errorf("[Gin-OAuth] Can not extract oauth2.Token, caused by: %s", err)
return nil, false
}
if !oauthToken.Valid() {
infof("[Gin-OAuth] Invalid Token - nil or expired")
return nil, false
}
if tc, err = getTokenContainerForToken(o, oauthToken); err != nil {
errorf("[Gin-OAuth] Can not extract TokenContainer, caused by: %s", err)
return nil, false
}
return tc, true
}
// Valid validates that the AccessToken within TokenContainer is not
// expired and not empty.
func (t *TokenContainer) Valid() bool {
if t.Token == nil {
return false
}
return t.Token.Valid()
}
// Auth implements a router middleware that can be used to get an
// authenticated and authorized service for the whole router group.
// Example:
//
// var endpoints oauth2.Endpoint = oauth2.Endpoint{
// AuthURL: "https://token.oauth2.corp.com/access_token",
// TokenURL: "https://oauth2.corp.com/corp/oauth2/tokeninfo",
// }
// var acl []ginoauth2.AccessTuple = []ginoauth2.AccessTuple{{"employee", 1070, "sszuecs"}, {"employee", 1114, "njuettner"}}
// router := gin.Default()
// private := router.Group("")
// private.Use(ginoauth2.Auth(ginoauth2.UidCheck, ginoauth2.endpoints))
// private.GET("/api/private", func(c *gin.Context) {
// c.JSON(200, gin.H{"message": "Hello from private"})
// })
func Auth(accessCheckFunction AccessCheckFunction, endpoints oauth2.Endpoint) gin.HandlerFunc {
return AuthChain(endpoints, accessCheckFunction)
}
// AuthChain is a router middleware that can be used to get an authenticated
// and authorized service for the whole router group. Similar to Auth, but
// takes a chain of AccessCheckFunctions and only fails if all of them fails.
// Example:
//
// var endpoints oauth2.Endpoint = oauth2.Endpoint{
// AuthURL: "https://token.oauth2.corp.com/access_token",
// TokenURL: "https://oauth2.corp.com/corp/oauth2/tokeninfo",
// }
// var acl []ginoauth2.AccessTuple = []ginoauth2.AccessTuple{{"employee", 1070, "sszuecs"}, {"employee", 1114, "njuettner"}}
// router := gin.Default()
// private := router.Group("")
// checkChain := []AccessCheckFunction{
// ginoauth2.UidCheck,
// ginoauth2.GroupCheck,
// }
// private.Use(ginoauth2.AuthChain(checkChain, ginoauth2.endpoints))
// private.GET("/api/private", func(c *gin.Context) {
// c.JSON(200, gin.H{"message": "Hello from private"})
// })
func AuthChain(endpoint oauth2.Endpoint, accessCheckFunctions ...AccessCheckFunction) gin.HandlerFunc {
return AuthChainOptions(Options{Endpoint: endpoint}, accessCheckFunctions...)
}
func AuthChainOptions(o Options, accessCheckFunctions ...AccessCheckFunction) gin.HandlerFunc {
// init
AuthInfoURL = o.Endpoint.TokenURL
// middleware
return func(ctx *gin.Context) {
t := time.Now()
varianceControl := make(chan bool, 1)
go func() {
tokenContainer, ok := getTokenContainer(o, ctx)
if !ok {
// set LOCATION header to auth endpoint such that the user can easily get a new access-token
ctx.Writer.Header().Set("Location", o.Endpoint.AuthURL)
ctx.AbortWithError(http.StatusUnauthorized, errors.New("no token in context"))
varianceControl <- false
return
}
if !tokenContainer.Valid() {
// set LOCATION header to auth endpoint such that the user can easily get a new access-token
ctx.Writer.Header().Set("Location", o.Endpoint.AuthURL)
ctx.AbortWithError(http.StatusUnauthorized, errors.New("invalid Token"))
varianceControl <- false
return
}
for i, fn := range accessCheckFunctions {
if fn(tokenContainer, ctx) {
varianceControl <- true
break
}
if len(accessCheckFunctions)-1 == i {
ctx.AbortWithError(http.StatusForbidden, errors.New("access to the Resource is forbidden"))
varianceControl <- false
return
}
}
}()
select {
case ok := <-varianceControl:
if !ok {
infofv2("[Gin-OAuth] %12v %s access not allowed", time.Since(t), ctx.Request.URL.Path)
return
}
case <-time.After(VarianceTimer):
ctx.AbortWithError(http.StatusGatewayTimeout, errors.New("authorization check overtime"))
infofv2("[Gin-OAuth] %12v %s overtime", time.Since(t), ctx.Request.URL.Path)
return
}
infofv2("[Gin-OAuth] %12v %s access allowed", time.Since(t), ctx.Request.URL.Path)
}
}
// RequestLogger is a middleware that logs all the request and prints
// relevant information. This can be used for logging all the
// requests that contain important information and are authorized.
// The assumption is that the request to log has a content and an Id
// identifiying who made the request uIdKey string to use as key to
// get the uid from the context contentKey string to use as key to get
// the content to be logged from the context.
//
// Example:
//
// var endpoints oauth2.Endpoint = oauth2.Endpoint{
// AuthURL: "https://token.oauth2.corp.com/access_token",
// TokenURL: "https://oauth2.corp.com/corp/oauth2/tokeninfo",
// }
// var acl []ginoauth2.AccessTuple = []ginoauth2.AccessTuple{{"employee", 1070, "sszuecs"}, {"employee", 1114, "njuettner"}}
// router := gin.Default()
// router.Use(ginoauth2.RequestLogger([]string{"uid"}, "data"))
func RequestLogger(keys []string, contentKey string) gin.HandlerFunc {
return func(c *gin.Context) {
request := c.Request
c.Next()
err := c.Errors
if request.Method != "GET" && err == nil {
if data, ok := c.Get(contentKey); !ok {
values := make([]string, 0)
for _, key := range keys {
val, keyPresent := c.Get(key)
if keyPresent {
values = append(values, val.(string))
}
}
infof("[Gin-OAuth] Request: %+v for %s", data, strings.Join(values, "-"))
}
}
}
}
// vim: ts=4 sw=4 noexpandtab nolist syn=go