/
base.ts
53 lines (49 loc) · 1.49 KB
/
base.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
53
/**
* Interface that should follow a class to implement a crypto helper.
*
* It's useful because we have different implementations in browser and NodeJS.
*/
export interface ICryptoHelper {
/**
* Return the validation hash of a value.
*
* Useful to check the validity `c_hash` and `at_hash` claims.
*
* @param value - The value to hash.
*
* @returns The hashed value.
*/
getValidationHash: (value: string) => Promise<string>;
/**
* Check if a hash corresponds to the provided value.
*
* Useful to check the validity `c_hash` and `at_hash` claims.
*
* @param value - The plain value to challenge.
* @param hash - The hash to compare with.
*
* @returns If the hash is valid.
*/
isValidHash: (value: string, hash: string) => Promise<boolean>;
/**
* Generate a cryptographic-safe value suitable for PKCE.
*
* @returns A code verifier to use for PKCE.
*
* @see [PKCE](https://docs.fief.dev/going-further/pkce/)
*/
generateCodeVerifier: () => Promise<string>;
/**
* Generate a code challenge from a code verifier for PKCE.
*
* @param code - The code verifier.
* @param method - The hashing method.
* Can either be `plain` or `S256`. For maximum security, prefer `S256`.
*
* @returns A code challenge to use for PKCE.
*
* @see [PKCE](https://docs.fief.dev/going-further/pkce/)
*/
getCodeChallenge: (code: string, method: 'plain' | 'S256') => Promise<string>;
}
export class CryptoHelperError extends Error {}