-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
sign.ts
52 lines (46 loc) · 1.46 KB
/
sign.ts
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
import { FlattenedSign } from '../flattened/sign.ts'
import type { CompactJWSHeaderParameters, KeyLike, SignOptions } from '../../types.d.ts'
/**
* The CompactSign class is a utility for creating Compact JWS strings.
*
* @example Usage
*
* ```js
* const jws = await new jose.CompactSign(
* new TextEncoder().encode('It’s a dangerous business, Frodo, going out your door.'),
* )
* .setProtectedHeader({ alg: 'ES256' })
* .sign(privateKey)
*
* console.log(jws)
* ```
*/
export class CompactSign {
private _flattened: FlattenedSign
/** @param payload Binary representation of the payload to sign. */
constructor(payload: Uint8Array) {
this._flattened = new FlattenedSign(payload)
}
/**
* Sets the JWS Protected Header on the Sign object.
*
* @param protectedHeader JWS Protected Header.
*/
setProtectedHeader(protectedHeader: CompactJWSHeaderParameters) {
this._flattened.setProtectedHeader(protectedHeader)
return this
}
/**
* Signs and resolves the value of the Compact JWS string.
*
* @param key Private Key or Secret to sign the JWS with.
* @param options JWS Sign options.
*/
async sign(key: KeyLike | Uint8Array, options?: SignOptions): Promise<string> {
const jws = await this._flattened.sign(key, options)
if (jws.payload === undefined) {
throw new TypeError('use the flattened module for creating JWS with b64: false')
}
return `${jws.protected}.${jws.payload}.${jws.signature}`
}
}