This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payload.go
120 lines (113 loc) · 2.93 KB
/
payload.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Copyright 2018 Johannes Donath <johannesd@torchmind.com>
* and other copyright owners as documented in the project's IP log.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jwt
import (
"encoding/json"
"errors"
"time"
)
type TokenPayload struct {
Issuer string
Audience string
AuthorizedParty string
Subject string
IssuedAt time.Time
ExpiresAt time.Time
NotBefore time.Time
claims map[string]interface{}
}
func (p TokenPayload) MarshalJSON() ([]byte, error) {
c := make(map[string]interface{})
if p.Issuer != "" {
c["iss"] = p.Issuer
}
if p.Audience != "" {
c["aud"] = p.Audience
}
if p.AuthorizedParty != "" {
c["azp"] = p.AuthorizedParty
}
if p.Subject != "" {
c["sub"] = p.Subject
}
if p.IssuedAt.Unix() != 0 {
c["iat"] = p.IssuedAt.Unix()
}
if p.IssuedAt.Unix() != 0 {
c["exp"] = p.ExpiresAt.Unix()
}
if p.IssuedAt.Unix() != 0 {
c["nbf"] = p.NotBefore.Unix()
}
for key, val := range p.claims {
c[key] = val
}
return json.Marshal(c)
}
func (p *TokenPayload) UnmarshalJSON(data []byte) error {
var c map[string]interface{}
err := json.Unmarshal(data, &c)
if err != nil {
return err
}
if raw, ok := c["iss"]; ok {
p.Issuer, ok = raw.(string)
if !ok {
return errors.New("malformed issuer: not a string")
}
}
if raw, ok := c["aud"]; ok {
p.Audience, ok = raw.(string)
if !ok {
return errors.New("malformed audience: not a string")
}
}
if raw, ok := c["azp"]; ok {
p.AuthorizedParty, ok = raw.(string)
if !ok {
return errors.New("malformed authorized party: not a string")
}
}
if raw, ok := c["sub"]; ok {
p.Subject, ok = raw.(string)
if !ok {
return errors.New("malformed subject: not a string")
}
}
if raw, ok := c["iat"]; ok {
epoch, ok := raw.(float64)
if !ok {
return errors.New("malformed issuance timestamp: not an integer")
}
p.IssuedAt = time.Unix(int64(epoch), 0)
}
if raw, ok := c["exp"]; ok {
epoch, ok := raw.(float64)
if !ok {
return errors.New("malformed expiration timestamp: not an integer")
}
p.ExpiresAt = time.Unix(int64(epoch), 0)
}
if raw, ok := c["nbf"]; ok {
epoch, ok := raw.(float64)
if !ok {
return errors.New("malformed not before timestamp: not an integer")
}
p.NotBefore = time.Unix(int64(epoch), 0)
}
return nil
}