-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvalidation.test.js
173 lines (153 loc) · 4.77 KB
/
validation.test.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { test } from 'uvu'
import * as assert from 'uvu/assert'
import { base64url, utf8 } from '../src/encoding.js'
import { validate, KeyPair } from '../src/ucan.js'
import { serialize } from '../src/utils.js'
test('should fail with string "test"', async () => {
try {
await validate('test')
assert.unreachable('should have thrown')
} catch (error) {
assert.instance(error, Error)
assert.match(
error.message,
`Can't parse UCAN: test: Expected JWT format: 3 dot-separated base64url-encoded values.`
)
}
})
test('should fail with string "test.test"', async () => {
try {
await validate('test.test')
assert.unreachable('should have thrown')
} catch (error) {
assert.instance(error, Error)
assert.match(
error.message,
`Can't parse UCAN: test.test: Expected JWT format: 3 dot-separated base64url-encoded values.`
)
}
})
test('should fail with string bad json header', async () => {
try {
await validate('test.test.test')
assert.unreachable('should have thrown')
} catch (error) {
assert.instance(error, Error)
assert.match(
error.message,
`Can't parse: test: Can't parse base64url encoded JSON inside.`
)
}
})
test('should fail with non base64url header', async () => {
try {
await validate('test*.test.test')
assert.unreachable('should have thrown')
} catch (error) {
assert.instance(error, Error)
assert.match(error.message, `Can't parse: test*: Can't parse as base64url.`)
}
})
test('should fail with non base64url payload', async () => {
const header = serialize({ ss: 1 })
try {
await validate(`${header}.test*.test`)
assert.unreachable('should have thrown')
} catch (error) {
assert.instance(error, Error)
assert.match(error.message, `Can't parse: test*: Can't parse as base64url.`)
}
})
test('should fail with bad json payload', async () => {
const header = serialize({ ss: 1 })
try {
await validate(`${header}.test.test`)
assert.unreachable('should have thrown')
} catch (error) {
assert.instance(error, Error)
assert.match(
error.message,
`Can't parse: test: Can't parse base64url encoded JSON inside.`
)
}
})
test('should fail with non base64url signature', async () => {
const header = serialize({ ss: 1 })
const payload = serialize({ ss: 1 })
try {
await validate(`${header}.${payload}.test*`)
assert.unreachable('should have thrown')
} catch (error) {
assert.instance(error, SyntaxError)
assert.match(error.message, `Non-base64url character`)
}
})
test('should fail with bad alg', async () => {
const kp = await KeyPair.create()
const header = serialize({ alg: 'EdDSAa' })
const payload = serialize({ iss: kp.did() })
const sig = await kp.sign(utf8.decode(`${header}.${payload}`))
const jwt = `${header}.${payload}.${base64url.encode(sig)}`
try {
await validate(jwt)
assert.unreachable('should have thrown')
} catch (error) {
assert.instance(error, Error)
assert.match(
error.message,
`Invalid UCAN: ${jwt}: Issuer key type does not match UCAN's alg property.`
)
}
})
test('should fail with bad signature', async () => {
const kp = await KeyPair.create()
const header = serialize({ alg: 'EdDSA' })
const payload = serialize({ iss: kp.did() })
const sig = await kp.sign(utf8.decode(`${header}.${payload}BAD`))
const jwt = `${header}.${payload}.${base64url.encode(sig)}`
try {
await validate(jwt)
assert.unreachable('should have thrown')
} catch (error) {
assert.instance(error, Error)
assert.match(error.message, `Invalid UCAN: ${jwt}: Signature invalid.`)
}
})
test('should fail with expired jwt', async () => {
const kp = await KeyPair.create()
const header = serialize({ alg: 'EdDSA' })
const payload = serialize({
iss: kp.did(),
exp: Math.floor(Date.now() / 1000) - 1000,
})
const sig = await kp.sign(utf8.decode(`${header}.${payload}`))
const jwt = `${header}.${payload}.${base64url.encode(sig)}`
try {
await validate(jwt)
assert.unreachable('should have thrown')
} catch (error) {
assert.instance(error, Error)
assert.match(error.message, `Invalid UCAN: ${jwt}: Expired.`)
}
})
test('should fail with not yet active jwt', async () => {
const kp = await KeyPair.create()
const header = serialize({ alg: 'EdDSA' })
const payload = serialize({
iss: kp.did(),
nbf: Math.floor(Date.now() / 1000) + 1000,
})
const sig = await kp.sign(utf8.decode(`${header}.${payload}`))
const jwt = `${header}.${payload}.${base64url.encode(sig)}`
try {
await validate(jwt)
assert.unreachable('should have thrown')
} catch (error) {
assert.instance(error, Error)
assert.match(
error.message,
`Invalid UCAN: ${jwt}: Not active yet (too early).`
)
}
})
test.run()