From b60d757ef407cd7afee507e16436d01210a033d1 Mon Sep 17 00:00:00 2001 From: Matthias Kunnen Date: Tue, 14 May 2019 00:21:38 +0200 Subject: [PATCH] [oauth2orize] Typed errors (#35251) See: - https://github.com/jaredhanson/oauth2orize/blob/70608ed5384a0f57e4a840f733db6e75a998057b/lib/errors/authorizationerror.js - https://github.com/jaredhanson/oauth2orize/blob/70608ed5384a0f57e4a840f733db6e75a998057b/lib/errors/oauth2error.js - https://github.com/jaredhanson/oauth2orize/blob/70608ed5384a0f57e4a840f733db6e75a998057b/lib/errors/tokenerror.js --- types/oauth2orize/index.d.ts | 56 ++++++++++++++++++++++++++ types/oauth2orize/oauth2orize-tests.ts | 6 +++ 2 files changed, 62 insertions(+) diff --git a/types/oauth2orize/index.d.ts b/types/oauth2orize/index.d.ts index b3a62d01e4..b5962237ee 100644 --- a/types/oauth2orize/index.d.ts +++ b/types/oauth2orize/index.d.ts @@ -58,6 +58,62 @@ export interface ErrorHandlerOptions { mode?: string; } +export class OAuth2Error extends Error { + code: string; + status: number; + uri?: string; + + /** + * @param code Defaults to *server_error*. + * @param status Defaults to 500. + */ + constructor(message?: string, code?: string, uri?: string, status?: number); +} + +export type AuthorizationErrorCode = 'invalid_request' + | 'unauthorized_client' + | 'access_denied' + | 'unsupported_response_type' + | 'invalid_scope' + | 'temporarily_unavailable'; + +export class AuthorizationError extends OAuth2Error { + /** + * @param code The code sets the status unless status is present. Mapping: + * invalid_request = 400 + * unauthorized_client = 403 + * access_denied = 403 + * unsupported_response_type = 501 + * invalid_scope = 400 + * temporarily_unavailable = 503 + * Defaults to *server_error*. + * @param status Defaults to 500 if code is not specified. + */ + constructor(message?: string, code?: AuthorizationErrorCode | string, uri?: string, status?: number); +} + +export type TokenErrorCode = 'invalid_request' + | 'invalid_client' + | 'invalid_grant' + | 'unauthorized_client' + | 'unsupported_grant_type' + | 'invalid_scope'; + +export class TokenError extends OAuth2Error { + /** + * @param code The code sets the status unless status is present. Mapping: + * invalid_request = 400 + * invalid_client = 401 + * invalid_grant = 403 + * unauthorized_client = 403 + * unsupported_grant_type = 501 + * invalid_scope = 400 + * Defaults to server_error. + * @param status Defaults to 500 if code is not specified. + */ + constructor(message?: string, code?: TokenErrorCode | string, uri?: string, status?: number); +} + export type MiddlewareFunction = (req: MiddlewareRequest, res: ServerResponse, next: MiddlewareNextFunction) => void; export type MiddlewareErrorFunction = (err: Error, req: MiddlewareRequest, res: ServerResponse, next: MiddlewareNextFunction) => void; diff --git a/types/oauth2orize/oauth2orize-tests.ts b/types/oauth2orize/oauth2orize-tests.ts index 84d9fe95e9..9abcfc0fa6 100644 --- a/types/oauth2orize/oauth2orize-tests.ts +++ b/types/oauth2orize/oauth2orize-tests.ts @@ -91,3 +91,9 @@ server.deserializeClient((id, done) => { server.token(); server.errorHandler(); // ); + +// Test errors +const tokenError = new oauth2orize.TokenError('Incorrect token', 'invalid_grant'); +const code: string = tokenError.code; +new oauth2orize.AuthorizationError('Incorrect token', 'access_denied'); +new oauth2orize.OAuth2Error();