UNPKG

@oa2/core

Version:

A comprehensive, RFC-compliant OAuth 2.0 authorization server implementation in TypeScript

130 lines (127 loc) 6.08 kB
Object.defineProperty(exports, '__esModule', { value: true }); /** * Default Descriptions */ const defaultErrorDescriptions = { invalid_request: 'The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed.', unauthorized_client: 'The client is not authorized to request an authorization code using this method.', access_denied: 'The resource owner or authorization server denied the request.', unsupported_response_type: 'The authorization server does not support obtaining an authorization code using this response type.', invalid_scope: 'The requested scope is invalid, unknown, or malformed.', server_error: 'The authorization server encountered an unexpected condition that prevented it from fulfilling the request.', temporarily_unavailable: 'The authorization server is currently unable to handle the request due to a temporary overloading or maintenance of the server.', invalid_grant: 'The provided authorization grant (e.g., authorization code, refresh token) or the refresh token is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.', unsupported_grant_type: 'The authorization grant type is not supported by the authorization server.' }; /** * Base class for all OAuth2 related errors. */ class OAuth2Error extends Error { constructor(code, description, statusCode = 400){ super(`[${code}] ${description || defaultErrorDescriptions[code] || 'Unknown error'}`); this.name = 'OAuth2Error'; this.code = code; this.description = description || defaultErrorDescriptions[code] || 'Unknown error'; this.statusCode = statusCode; } } /** * Represents an 'invalid_request' error as per OAuth2 specification. * This error indicates that the request is missing a required parameter, * includes an invalid parameter value, includes a parameter more than once, * or is otherwise malformed. */ class InvalidRequestError extends OAuth2Error { constructor(description){ super('invalid_request', description, 400); this.name = 'InvalidRequestError'; } } /** * Represents an 'unauthorized_client' error as per OAuth2 specification. * This error indicates that the client is not authorized to request an authorization * code using this method. */ class UnauthorizedClientError extends OAuth2Error { constructor(description){ super('unauthorized_client', description, 400); this.name = 'UnauthorizedClientError'; } } /** * Represents an 'access_denied' error as per OAuth2 specification. * This error indicates that the resource owner or authorization server denied the request. */ class AccessDeniedError extends OAuth2Error { constructor(description){ super('access_denied', description, 400); this.name = 'AccessDeniedError'; } } /** * Represents an 'unsupported_response_type' error as per OAuth2 specification. * This error indicates that the authorization server does not support * obtaining an authorization code using this response type. */ class UnsupportedResponseTypeError extends OAuth2Error { constructor(description){ super('unsupported_response_type', description, 400); this.name = 'UnsupportedResponseTypeError'; } } /** * Represents an 'invalid_scope' error as per OAuth2 specification. * This error indicates that the requested scope is invalid, unknown, or malformed. */ class InvalidScopeError extends OAuth2Error { constructor(description){ super('invalid_scope', description, 400); this.name = 'InvalidScopeError'; } } /** * Represents a 'server_error' as per OAuth2 specification. * This error indicates that the authorization server encountered an unexpected * condition that prevented it from fulfilling the request. */ class ServerError extends OAuth2Error { constructor(description){ super('server_error', description, 500); this.name = 'ServerError'; } } /** * Represents a 'temporarily_unavailable' error as per OAuth2 specification. * This error indicates that the authorization server is currently unable to handle * the request due to a temporary overloading or maintenance of the server. */ class TemporarilyUnavailableError extends OAuth2Error { constructor(description){ super('temporarily_unavailable', description, 503); this.name = 'TemporarilyUnavailableError'; } } /** * Represents an 'invalid_grant' error as per OAuth2 specification. * This error indicates that the provided authorization grant (e.g., authorization code, * refresh token) or the refresh token is invalid, expired, revoked, does not match the * redirection URI used in the authorization request, or was issued to another client. */ class InvalidGrantError extends OAuth2Error { constructor(description){ super('invalid_grant', description, 400); this.name = 'InvalidGrantError'; } } /** * Represents an 'unsupported_grant_type' error as per OAuth2 specification. * This error indicates that the authorization grant type is not supported by the * authorization server. * @see RFC 6749, Section 5.2 Error Response */ class UnsupportedGrantTypeError extends OAuth2Error { constructor(description){ super('unsupported_grant_type', description, 400); this.name = 'UnsupportedGrantTypeError'; } } exports.AccessDeniedError = AccessDeniedError; exports.InvalidGrantError = InvalidGrantError; exports.InvalidRequestError = InvalidRequestError; exports.InvalidScopeError = InvalidScopeError; exports.OAuth2Error = OAuth2Error; exports.ServerError = ServerError; exports.TemporarilyUnavailableError = TemporarilyUnavailableError; exports.UnauthorizedClientError = UnauthorizedClientError; exports.UnsupportedGrantTypeError = UnsupportedGrantTypeError; exports.UnsupportedResponseTypeError = UnsupportedResponseTypeError; exports.defaultErrorDescriptions = defaultErrorDescriptions;