1package jws
2
3import (
4	"encoding/json"
5	"time"
6
7	"github.com/briankassouf/jose"
8	"github.com/briankassouf/jose/jwt"
9)
10
11// Claims represents a set of JOSE Claims.
12type Claims jwt.Claims
13
14// Get retrieves the value corresponding with key from the Claims.
15func (c Claims) Get(key string) interface{} {
16	return jwt.Claims(c).Get(key)
17}
18
19// Set sets Claims[key] = val. It'll overwrite without warning.
20func (c Claims) Set(key string, val interface{}) {
21	jwt.Claims(c).Set(key, val)
22}
23
24// Del removes the value that corresponds with key from the Claims.
25func (c Claims) Del(key string) {
26	jwt.Claims(c).Del(key)
27}
28
29// Has returns true if a value for the given key exists inside the Claims.
30func (c Claims) Has(key string) bool {
31	return jwt.Claims(c).Has(key)
32}
33
34// MarshalJSON implements json.Marshaler for Claims.
35func (c Claims) MarshalJSON() ([]byte, error) {
36	return jwt.Claims(c).MarshalJSON()
37}
38
39// Base64 implements the Encoder interface.
40func (c Claims) Base64() ([]byte, error) {
41	return jwt.Claims(c).Base64()
42}
43
44// UnmarshalJSON implements json.Unmarshaler for Claims.
45func (c *Claims) UnmarshalJSON(b []byte) error {
46	if b == nil {
47		return nil
48	}
49
50	b, err := jose.DecodeEscaped(b)
51	if err != nil {
52		return err
53	}
54
55	// Since json.Unmarshal calls UnmarshalJSON,
56	// calling json.Unmarshal on *p would be infinitely recursive
57	// A temp variable is needed because &map[string]interface{}(*p) is
58	// invalid Go.
59
60	tmp := map[string]interface{}(*c)
61	if err = json.Unmarshal(b, &tmp); err != nil {
62		return err
63	}
64	*c = Claims(tmp)
65	return nil
66}
67
68// Issuer retrieves claim "iss" per its type in
69// https://tools.ietf.org/html/rfc7519#section-4.1.1
70func (c Claims) Issuer() (string, bool) {
71	return jwt.Claims(c).Issuer()
72}
73
74// Subject retrieves claim "sub" per its type in
75// https://tools.ietf.org/html/rfc7519#section-4.1.2
76func (c Claims) Subject() (string, bool) {
77	return jwt.Claims(c).Subject()
78}
79
80// Audience retrieves claim "aud" per its type in
81// https://tools.ietf.org/html/rfc7519#section-4.1.3
82func (c Claims) Audience() ([]string, bool) {
83	return jwt.Claims(c).Audience()
84}
85
86// Expiration retrieves claim "exp" per its type in
87// https://tools.ietf.org/html/rfc7519#section-4.1.4
88func (c Claims) Expiration() (time.Time, bool) {
89	return jwt.Claims(c).Expiration()
90}
91
92// NotBefore retrieves claim "nbf" per its type in
93// https://tools.ietf.org/html/rfc7519#section-4.1.5
94func (c Claims) NotBefore() (time.Time, bool) {
95	return jwt.Claims(c).NotBefore()
96}
97
98// IssuedAt retrieves claim "iat" per its type in
99// https://tools.ietf.org/html/rfc7519#section-4.1.6
100func (c Claims) IssuedAt() (time.Time, bool) {
101	return jwt.Claims(c).IssuedAt()
102}
103
104// JWTID retrieves claim "jti" per its type in
105// https://tools.ietf.org/html/rfc7519#section-4.1.7
106func (c Claims) JWTID() (string, bool) {
107	return jwt.Claims(c).JWTID()
108}
109
110// RemoveIssuer deletes claim "iss" from c.
111func (c Claims) RemoveIssuer() {
112	jwt.Claims(c).RemoveIssuer()
113}
114
115// RemoveSubject deletes claim "sub" from c.
116func (c Claims) RemoveSubject() {
117	jwt.Claims(c).RemoveIssuer()
118}
119
120// RemoveAudience deletes claim "aud" from c.
121func (c Claims) RemoveAudience() {
122	jwt.Claims(c).Audience()
123}
124
125// RemoveExpiration deletes claim "exp" from c.
126func (c Claims) RemoveExpiration() {
127	jwt.Claims(c).RemoveExpiration()
128}
129
130// RemoveNotBefore deletes claim "nbf" from c.
131func (c Claims) RemoveNotBefore() {
132	jwt.Claims(c).NotBefore()
133}
134
135// RemoveIssuedAt deletes claim "iat" from c.
136func (c Claims) RemoveIssuedAt() {
137	jwt.Claims(c).IssuedAt()
138}
139
140// RemoveJWTID deletes claim "jti" from c.
141func (c Claims) RemoveJWTID() {
142	jwt.Claims(c).RemoveJWTID()
143}
144
145// SetIssuer sets claim "iss" per its type in
146// https://tools.ietf.org/html/rfc7519#section-4.1.1
147func (c Claims) SetIssuer(issuer string) {
148	jwt.Claims(c).SetIssuer(issuer)
149}
150
151// SetSubject sets claim "iss" per its type in
152// https://tools.ietf.org/html/rfc7519#section-4.1.2
153func (c Claims) SetSubject(subject string) {
154	jwt.Claims(c).SetSubject(subject)
155}
156
157// SetAudience sets claim "aud" per its type in
158// https://tools.ietf.org/html/rfc7519#section-4.1.3
159func (c Claims) SetAudience(audience ...string) {
160	jwt.Claims(c).SetAudience(audience...)
161}
162
163// SetExpiration sets claim "exp" per its type in
164// https://tools.ietf.org/html/rfc7519#section-4.1.4
165func (c Claims) SetExpiration(expiration time.Time) {
166	jwt.Claims(c).SetExpiration(expiration)
167}
168
169// SetNotBefore sets claim "nbf" per its type in
170// https://tools.ietf.org/html/rfc7519#section-4.1.5
171func (c Claims) SetNotBefore(notBefore time.Time) {
172	jwt.Claims(c).SetNotBefore(notBefore)
173}
174
175// SetIssuedAt sets claim "iat" per its type in
176// https://tools.ietf.org/html/rfc7519#section-4.1.6
177func (c Claims) SetIssuedAt(issuedAt time.Time) {
178	jwt.Claims(c).SetIssuedAt(issuedAt)
179}
180
181// SetJWTID sets claim "jti" per its type in
182// https://tools.ietf.org/html/rfc7519#section-4.1.7
183func (c Claims) SetJWTID(uniqueID string) {
184	jwt.Claims(c).SetJWTID(uniqueID)
185}
186
187var (
188	_ json.Marshaler   = (Claims)(nil)
189	_ json.Unmarshaler = (*Claims)(nil)
190)
191