-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjwt_claims.go
More file actions
executable file
·36 lines (29 loc) · 890 Bytes
/
jwt_claims.go
File metadata and controls
executable file
·36 lines (29 loc) · 890 Bytes
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
package main
import (
"context"
"net/http"
jwt "github.com/dgrijalva/jwt-go"
)
const jwtClaimsKey contextKeyType = "jwtClaims"
type jwtClaims struct {
OpaqueUserID string `json:"opaque_user_id,omitempty"`
UserID string `json:"user_id"`
ChannelID string `json:"channel_id,omitempty"`
Role string `json:"role"`
Permissions jwtPermissions `json:"pubsub_perms"`
jwt.StandardClaims
}
type jwtPermissions struct {
Send []string `json:"send,omitempty"`
Listen []string `json:"listen,omitempty"`
}
func setClaims(r *http.Request, claims *jwtClaims) *http.Request {
ctx := context.WithValue(r.Context(), jwtClaimsKey, claims)
return r.WithContext(ctx)
}
func getClaims(r *http.Request) *jwtClaims {
if claims, ok := r.Context().Value(jwtClaimsKey).(*jwtClaims); ok {
return claims
}
return &jwtClaims{} // empty default
}