-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.v
135 lines (120 loc) · 2.63 KB
/
jwt.v
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
module jwt
import x.json2
import encoding.base64
import crypto.sha256
import crypto.sha512
import crypto.hmac
const (
algorithms = {
Algorithm.hs256: sha256.sum
Algorithm.hs384: sha512.sum384
Algorithm.hs512: sha512.sum512
}
block_sizes = {
Algorithm.hs256: sha256.block_size
Algorithm.hs384: sha512.block_size
Algorithm.hs512: sha512.block_size
}
)
pub enum Algorithm {
hs256
hs384
hs512
}
pub fn (a Algorithm) str() string {
return match a {
.hs256 {
'HS256'
}
.hs384 {
'HS384'
}
.hs512 {
'HS512'
}
}
}
pub struct JWT {
algorithm Algorithm
key string
}
pub struct JwtClaims {
iss string
sub string
aud string
exp int
nbf int
iat int
jti string
}
fn (j JWT) sign(message string) string {
hash_fn := jwt.algorithms[j.algorithm]
block_size := jwt.block_sizes[j.algorithm]
signature := hmac.new(j.key.bytes(), message.bytes(), hash_fn, block_size)
url_encoded := base64.url_encode_str(signature.bytestr())
return url_encoded
}
fn (j JWT) verify(token string) bool {
split_token := token.split('.')
signing_input := split_token[..2].join('.')
signature := split_token[2]
return j.sign(signing_input) == signature
}
pub struct EncodeOptions[T] {
payload T
key string
algorithm Algorithm = .hs256
headers map[string]json2.Any
}
pub fn encode[T](options EncodeOptions[T]) !string {
if options.algorithm !in jwt.algorithms {
return error('algorithm ${options.algorithm} is not supported')
}
jwt := JWT{
algorithm: options.algorithm
key: options.key
}
mut headers := options.headers.clone()
headers['alg'] = options.algorithm.str()
headers['typ'] = 'JWT'
json_header := json2.encode(headers)
json_payload := json2.encode(options.payload)
mut segments := [base64.url_encode_str(json_header), base64.url_encode_str(json_payload)]
signing_input := segments.join('.')
segments << jwt.sign(signing_input)
return segments.join('.')
}
pub struct DecodeOptions {
token string
key string
algorithm Algorithm = .hs256
verify bool = true
}
pub fn decode[T](options DecodeOptions) !T {
jwt := JWT{
algorithm: options.algorithm
key: options.key
}
split_token := options.token.split('.')
if options.verify {
if !jwt.verify(options.token) {
return error('invalid signature')
}
}
payload_segment := split_token[1]
payload_data := base64.decode_str(payload_segment)
payload := json2.decode[T](payload_data)!
return payload
}
pub struct VerifyOptions {
token string
key string
algorithm Algorithm = .hs256
}
pub fn verify(options VerifyOptions) bool {
jwt := JWT{
algorithm: options.algorithm
key: options.key
}
return jwt.verify(options.token)
}