...
1 package jwtx
2
3 import (
4 "time"
5
6 "github.com/pkg/errors"
7
8 "github.com/ory/x/mapx"
9 )
10
11
12 type Claims struct {
13
14 Audience []string `json:"aud"`
15
16
17 Issuer string `json:"iss"`
18
19
20 Subject string `json:"sub"`
21
22
23 ExpiresAt time.Time `json:"exp"`
24
25
26 IssuedAt time.Time `json:"iat"`
27
28
29 NotBefore time.Time `json:"nbf"`
30
31
32 JTI string `json:"jti"`
33 }
34
35
36 func ParseMapStringInterfaceClaims(claims map[string]interface{}) *Claims {
37 c := make(map[interface{}]interface{})
38 for k, v := range claims {
39 c[k] = v
40 }
41 return ParseMapInterfaceInterfaceClaims(c)
42 }
43
44
45 func ParseMapInterfaceInterfaceClaims(claims map[interface{}]interface{}) *Claims {
46 result := &Claims{
47 Issuer: mapx.GetStringDefault(claims, "iss", ""),
48 Subject: mapx.GetStringDefault(claims, "sub", ""),
49 JTI: mapx.GetStringDefault(claims, "jti", ""),
50 }
51
52 if aud, err := mapx.GetString(claims, "aud"); err == nil {
53 result.Audience = []string{aud}
54 } else if errors.Cause(err) == mapx.ErrKeyCanNotBeTypeAsserted {
55 if aud, err := mapx.GetStringSlice(claims, "aud"); err == nil {
56 result.Audience = aud
57 } else {
58 result.Audience = []string{}
59 }
60 } else {
61 result.Audience = []string{}
62 }
63
64 if exp, err := mapx.GetTime(claims, "exp"); err == nil {
65 result.ExpiresAt = exp
66 }
67
68 if iat, err := mapx.GetTime(claims, "iat"); err == nil {
69 result.IssuedAt = iat
70 }
71
72 if nbf, err := mapx.GetTime(claims, "nbf"); err == nil {
73 result.NotBefore = nbf
74 }
75
76 return result
77 }
78
View as plain text