-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.go
106 lines (85 loc) · 3 KB
/
common.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
package pvx
import (
"errors"
)
// ErrWrongKey is occurred when given key is not intended for specified version of PASETO.
var ErrWrongKey = errors.New("the given key is not intended for this version of PASETO")
type purpose string
const (
purposeLocal purpose = "local"
purposePublic purpose = "public"
)
// Version denotes PASETO version which will be used.
type Version int32
const (
Version2 Version = 2
Version3 Version = 3
Version4 Version = 4
)
// key abstracts raw key material for extra safety.
type key struct {
keyMaterial []byte
version Version
}
// SymKey is a symmetric key abstraction for usage inside PASETO.
type SymKey struct {
key
}
// AsymSecretKey is an asymmetric key abstraction for usage inside PASETO on sign.
type AsymSecretKey struct {
key
}
// AsymPublicKey is an asymmetric key abstraction for usage inside PASETO on verify.
type AsymPublicKey struct {
key
}
func (k *AsymPublicKey) isValidFor(v Version, p purpose) bool {
return k.version == v && p == purposePublic
}
// NewAsymmetricPublicKey is a constructor-like function for AsymPublicKey which is a wrapper for raw key material used inside PASETO.
func NewAsymmetricPublicKey(keyMaterial []byte, version Version) *AsymPublicKey {
return &AsymPublicKey{key: key{keyMaterial: keyMaterial, version: version}}
}
// NewAsymmetricSecretKey is a constructor-like function for AsymSecretKey which is a wrapper for raw key material used inside PASETO.
func NewAsymmetricSecretKey(keyMaterial []byte, version Version) *AsymSecretKey {
return &AsymSecretKey{key{keyMaterial: keyMaterial, version: version}}
}
// NewSymmetricKey is a constructor-like function for SymKey which is a wrapper for raw key material used inside PASETO
func NewSymmetricKey(keyMaterial []byte, version Version) *SymKey {
return &SymKey{key: key{keyMaterial: keyMaterial, version: version}}
}
func (k *AsymSecretKey) isValidFor(v Version, p purpose) bool {
return k.version == v && p == purposePublic
}
func (k *SymKey) isValidFor(v Version, p purpose) bool {
return k.version == v && p == purposeLocal
}
// optional includes optional arguments which is non-mandatory to PASETO.
type optional struct {
footer interface{}
assertion []byte
}
// ProvidedOption is the type of constructor options.
type ProvidedOption func(*optional) error
// WithFooter adds PASETO footer to the token.
func WithFooter(footer interface{}) ProvidedOption {
return func(o *optional) error {
if footer == nil {
return errors.New("nil footer was passed to WithFooter function")
}
o.footer = footer
return nil
}
}
// WithAssert adds implicit assertion to PASETO token
// Implicit assertion is an unencrypted but authenticated data (like the optional footer), but is NOT stored in the PASETO token (thus, implicit)
// and MUST be asserted when verifying a token.
func WithAssert(assertion []byte) ProvidedOption {
return func(o *optional) error {
if assertion == nil {
return errors.New("nil assertion was passed to WuthAssert function")
}
o.assertion = assertion
return nil
}
}