-
Notifications
You must be signed in to change notification settings - Fork 0
/
claims.go
97 lines (86 loc) · 2.19 KB
/
claims.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
package jwtlib
import (
"context"
"github.com/dgrijalva/jwt-go"
"github.com/pestanko/gouthy/app/domain/apps"
"github.com/pestanko/gouthy/app/domain/users"
"github.com/pestanko/gouthy/app/shared"
log "github.com/sirupsen/logrus"
"strconv"
"time"
)
type Claims struct {
Audience string `json:"aud,omitempty"`
ExpiresAt int64 `json:"exp,omitempty"`
Id string `json:"jti,omitempty"`
IssuedAt int64 `json:"iat,omitempty"`
Issuer string `json:"iss,omitempty"`
Subject string `json:"sub,omitempty"`
Additional map[string]interface{}
Scopes shared.Scopes `json:"scope,omitempty"`
}
func (claims *Claims) Serialize() jwt.MapClaims {
mapClaims := jwt.MapClaims{
"aud": claims.Audience,
"exp": claims.ExpiresAt,
"jti": claims.Id,
"iss": claims.Issuer,
"iat": claims.IssuedAt,
"sub": claims.Subject,
"scope": claims.Scopes,
}
for key, value := range claims.Additional {
mapClaims[key] = value
}
return mapClaims
}
type claimsParams struct {
User *users.User
Application *apps.Application
ExpirationAdd int64
Issuer string
Scopes shared.Scopes
TokenType string
CorrelationId string
Iat int64
}
const PasswordLogin = "pwd-login"
func makeClaims(ctx context.Context, params claimsParams) Claims {
iat := time.Now().Unix()
id := JtiPartsToString(JtiParts{
CorrelationId: params.CorrelationId,
Type: params.TokenType,
Offset: makeOffset(params),
})
shared.GetLogger(ctx).WithFields(log.Fields{
"id": id,
"iat": iat,
}).Debug("Making token claims")
audience := PasswordLogin
if params.Application != nil {
audience = params.Application.ClientId
}
return Claims{
Audience: audience,
IssuedAt: iat,
ExpiresAt: iat + params.ExpirationAdd,
Id: id,
Issuer: params.Issuer,
Subject: params.User.ID.String(),
Scopes: params.Scopes,
Additional: make(map[string]interface{}),
}
}
func makeOffset(params claimsParams) string {
switch params.TokenType {
case TokenTypeAccess:
return strconv.FormatInt(params.Iat, 10)
case TokenTypeRefresh:
return "0"
case TokenTypeId:
return "0"
case TokenTypeSession:
return "0"
}
return TokenTypeUndefined
}