-
Notifications
You must be signed in to change notification settings - Fork 0
/
middlewares.go
48 lines (39 loc) · 1.18 KB
/
middlewares.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
package contexes
import (
"github.com/orange-cloudfoundry/aggregadantur/jwtclaim"
"net/http"
)
const (
ScopeContextKey MiddlewareContextKey = iota
UsernameContextKey
JwtClaimContextKey
)
type MiddlewareContextKey int
// Set the username to a request context
func SetUsername(req *http.Request, username string) {
AddContextValue(req, UsernameContextKey, username)
}
// Retrieve username from a request context
func Username(req *http.Request) string {
return GetContextValue(req, UsernameContextKey, "").(string)
}
// set scope to a request context
func SetScopes(req *http.Request, scopes []string) {
AddContextValue(req, ScopeContextKey, scopes)
}
// retrieve scope from request context
func Scopes(req *http.Request) []string {
return GetContextValue(req, ScopeContextKey, []string{}).([]string)
}
// set jwt claim to a request context
func SetJwtClaim(req *http.Request, jwtClaim *jwtclaim.ScopeClaims) {
AddContextValue(req, JwtClaimContextKey, jwtClaim)
}
// retrieve jwt claim from request context
func JwtClaim(req *http.Request) *jwtclaim.ScopeClaims {
val := GetContextValue(req, JwtClaimContextKey, nil)
if val == nil {
return nil
}
return val.(*jwtclaim.ScopeClaims)
}