forked from gbrlsnchs/jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raw_token.go
59 lines (49 loc) · 1.31 KB
/
raw_token.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
package jwt
import (
"encoding/json"
"github.com/gbrlsnchs/jwt/v3/internal"
)
// ErrMalformed indicates a token doesn't have a valid format, as per the RFC 7519.
var ErrMalformed = internal.NewError("jwt: malformed token")
// RawToken is a representation of a parsed JWT string.
type RawToken struct {
token []byte
sep1, sep2 int
hd Header
alg Algorithm
pl *Payload
vds []Validator
}
func (rt *RawToken) header() []byte { return rt.token[:rt.sep1] }
func (rt *RawToken) headerPayload() []byte { return rt.token[:rt.sep2] }
func (rt *RawToken) payload() []byte { return rt.token[rt.sep1+1 : rt.sep2] }
func (rt *RawToken) sig() []byte { return rt.token[rt.sep2+1:] }
func (rt *RawToken) setToken(token []byte, sep1, sep2 int) {
rt.sep1 = sep1
rt.sep2 = sep1 + 1 + sep2
rt.token = token
}
func (rt *RawToken) decode(payload interface{}) (err error) {
pb, err := internal.DecodeToBytes(rt.payload())
if err != nil {
return err
}
if !isJSONObject(pb) {
return ErrNotJSONObject
}
if err = json.Unmarshal(pb, payload); err != nil {
return err
}
for _, vd := range rt.vds {
if err = vd(rt.pl); err != nil {
return err
}
}
return nil
}
func (rt *RawToken) decodeHeader() error {
if err := internal.Decode(rt.header(), &rt.hd); err != nil {
return err
}
return nil
}