-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
claims.go
54 lines (48 loc) · 1.52 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
package magiclinksdev
import (
"encoding/json"
"fmt"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/tidwall/gjson"
)
const (
AttrIss = "iss"
AttrSub = "sub"
AttrAud = "aud"
AttrExp = "exp"
AttrNbf = "nbf"
AttrIat = "iat"
AttrJti = "jti"
)
// SigningBytesClaims is a JWT claims type that allows for signing claims represented in bytes.
type SigningBytesClaims struct {
Claims json.RawMessage
}
func (s SigningBytesClaims) GetExpirationTime() (*jwt.NumericDate, error) {
return jwt.NewNumericDate(time.Unix(gjson.GetBytes(s.Claims, AttrExp).Int(), 0)), nil
}
func (s SigningBytesClaims) GetIssuedAt() (*jwt.NumericDate, error) {
return jwt.NewNumericDate(time.Unix(gjson.GetBytes(s.Claims, AttrIat).Int(), 0)), nil
}
func (s SigningBytesClaims) GetNotBefore() (*jwt.NumericDate, error) {
return jwt.NewNumericDate(time.Unix(gjson.GetBytes(s.Claims, AttrNbf).Int(), 0)), nil
}
func (s SigningBytesClaims) GetIssuer() (string, error) {
return gjson.GetBytes(s.Claims, AttrIss).String(), nil
}
func (s SigningBytesClaims) GetSubject() (string, error) {
return gjson.GetBytes(s.Claims, AttrSub).String(), nil
}
func (s SigningBytesClaims) GetAudience() (jwt.ClaimStrings, error) {
var aud jwt.ClaimStrings
err := json.Unmarshal([]byte(gjson.GetBytes(s.Claims, AttrAud).Raw), &aud)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal audience: %w", err)
}
return aud, nil
}
// MarshalJSON helps implement the json.Marshaler interface.
func (s SigningBytesClaims) MarshalJSON() ([]byte, error) {
return s.Claims, nil
}