/
errors.ts
74 lines (65 loc) · 1.98 KB
/
errors.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Errors per https://tools.ietf.org/html/rfc6750#section-3.1
*/
/**
* If the request lacks any authentication information,
* the resource server SHOULD NOT include an error code or
* other error information.
*/
export class UnauthorizedError extends Error {
status = 401;
statusCode = 401;
headers = { 'WWW-Authenticate': 'Bearer realm="api"' };
constructor(message = 'Unauthorized') {
super(message);
this.name = this.constructor.name;
}
}
/**
* The request is missing a required parameter, includes an
* unsupported parameter or parameter value, repeats the same
* parameter, uses more than one method for including an access
* token, or is otherwise malformed.
*/
export class InvalidRequestError extends UnauthorizedError {
code = 'invalid_request';
status = 400;
statusCode = 400;
constructor(message = 'Invalid Request') {
super(message);
this.headers = getHeaders(this.code, this.message);
}
}
/**
* The access token provided is expired, revoked, malformed, or
* invalid for other reasons.
*/
export class InvalidTokenError extends UnauthorizedError {
code = 'invalid_token';
status = 401;
statusCode = 401;
constructor(message = 'Invalid Token') {
super(message);
this.headers = getHeaders(this.code, this.message);
}
}
/**
* The request requires higher privileges than provided by the
* access token.
*/
export class InsufficientScopeError extends UnauthorizedError {
code = 'insufficient_scope';
status = 403;
statusCode = 403;
constructor(scopes?: string[], message = 'Insufficient Scope') {
super(message);
this.headers = getHeaders(this.code, this.message, scopes);
}
}
// Generate a response header per https://tools.ietf.org/html/rfc6750#section-3
const getHeaders = (error: string, description: string, scopes?: string[]) => ({
'WWW-Authenticate': `Bearer realm="api", error="${error}", error_description="${description.replace(
/"/g,
"'"
)}"${(scopes && `, scope="${scopes.join(' ')}"`) || ''}`,
});