|
| 1 | +import type { OAuthErrorResponse } from '@modelcontextprotocol/core'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Base class for all OAuth errors |
| 5 | + */ |
| 6 | +export class OAuthError extends Error { |
| 7 | + static errorCode: string; |
| 8 | + |
| 9 | + constructor( |
| 10 | + message: string, |
| 11 | + public readonly errorUri?: string |
| 12 | + ) { |
| 13 | + super(message); |
| 14 | + this.name = this.constructor.name; |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * Converts the error to a standard OAuth error response object |
| 19 | + */ |
| 20 | + toResponseObject(): OAuthErrorResponse { |
| 21 | + const response: OAuthErrorResponse = { |
| 22 | + error: this.errorCode, |
| 23 | + error_description: this.message |
| 24 | + }; |
| 25 | + |
| 26 | + if (this.errorUri) { |
| 27 | + response.error_uri = this.errorUri; |
| 28 | + } |
| 29 | + |
| 30 | + return response; |
| 31 | + } |
| 32 | + |
| 33 | + get errorCode(): string { |
| 34 | + return (this.constructor as typeof OAuthError).errorCode; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Invalid request error - The request is missing a required parameter, |
| 40 | + * includes an invalid parameter value, includes a parameter more than once, |
| 41 | + * or is otherwise malformed. |
| 42 | + */ |
| 43 | +export class InvalidRequestError extends OAuthError { |
| 44 | + static override errorCode = 'invalid_request'; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Invalid client error - Client authentication failed (e.g., unknown client, no client |
| 49 | + * authentication included, or unsupported authentication method). |
| 50 | + */ |
| 51 | +export class InvalidClientError extends OAuthError { |
| 52 | + static override errorCode = 'invalid_client'; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Invalid grant error - The provided authorization grant or refresh token is |
| 57 | + * invalid, expired, revoked, does not match the redirection URI used in the |
| 58 | + * authorization request, or was issued to another client. |
| 59 | + */ |
| 60 | +export class InvalidGrantError extends OAuthError { |
| 61 | + static override errorCode = 'invalid_grant'; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Unauthorized client error - The authenticated client is not authorized to use |
| 66 | + * this authorization grant type. |
| 67 | + */ |
| 68 | +export class UnauthorizedClientError extends OAuthError { |
| 69 | + static override errorCode = 'unauthorized_client'; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Unsupported grant type error - The authorization grant type is not supported |
| 74 | + * by the authorization server. |
| 75 | + */ |
| 76 | +export class UnsupportedGrantTypeError extends OAuthError { |
| 77 | + static override errorCode = 'unsupported_grant_type'; |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Invalid scope error - The requested scope is invalid, unknown, malformed, or |
| 82 | + * exceeds the scope granted by the resource owner. |
| 83 | + */ |
| 84 | +export class InvalidScopeError extends OAuthError { |
| 85 | + static override errorCode = 'invalid_scope'; |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Access denied error - The resource owner or authorization server denied the request. |
| 90 | + */ |
| 91 | +export class AccessDeniedError extends OAuthError { |
| 92 | + static override errorCode = 'access_denied'; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Server error - The authorization server encountered an unexpected condition |
| 97 | + * that prevented it from fulfilling the request. |
| 98 | + */ |
| 99 | +export class ServerError extends OAuthError { |
| 100 | + static override errorCode = 'server_error'; |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Temporarily unavailable error - The authorization server is currently unable to |
| 105 | + * handle the request due to a temporary overloading or maintenance of the server. |
| 106 | + */ |
| 107 | +export class TemporarilyUnavailableError extends OAuthError { |
| 108 | + static override errorCode = 'temporarily_unavailable'; |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Unsupported response type error - The authorization server does not support |
| 113 | + * obtaining an authorization code using this method. |
| 114 | + */ |
| 115 | +export class UnsupportedResponseTypeError extends OAuthError { |
| 116 | + static override errorCode = 'unsupported_response_type'; |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Unsupported token type error - The authorization server does not support |
| 121 | + * the requested token type. |
| 122 | + */ |
| 123 | +export class UnsupportedTokenTypeError extends OAuthError { |
| 124 | + static override errorCode = 'unsupported_token_type'; |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Invalid token error - The access token provided is expired, revoked, malformed, |
| 129 | + * or invalid for other reasons. |
| 130 | + */ |
| 131 | +export class InvalidTokenError extends OAuthError { |
| 132 | + static override errorCode = 'invalid_token'; |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * Method not allowed error - The HTTP method used is not allowed for this endpoint. |
| 137 | + * (Custom, non-standard error) |
| 138 | + */ |
| 139 | +export class MethodNotAllowedError extends OAuthError { |
| 140 | + static override errorCode = 'method_not_allowed'; |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * Too many requests error - Rate limit exceeded. |
| 145 | + * (Custom, non-standard error based on RFC 6585) |
| 146 | + */ |
| 147 | +export class TooManyRequestsError extends OAuthError { |
| 148 | + static override errorCode = 'too_many_requests'; |
| 149 | +} |
| 150 | + |
| 151 | +/** |
| 152 | + * Invalid client metadata error - The client metadata is invalid. |
| 153 | + * (Custom error for dynamic client registration - RFC 7591) |
| 154 | + */ |
| 155 | +export class InvalidClientMetadataError extends OAuthError { |
| 156 | + static override errorCode = 'invalid_client_metadata'; |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Insufficient scope error - The request requires higher privileges than provided by the access token. |
| 161 | + */ |
| 162 | +export class InsufficientScopeError extends OAuthError { |
| 163 | + static override errorCode = 'insufficient_scope'; |
| 164 | +} |
| 165 | + |
| 166 | +/** |
| 167 | + * Invalid target error - The requested resource is invalid, missing, unknown, or malformed. |
| 168 | + * (Custom error for resource indicators - RFC 8707) |
| 169 | + */ |
| 170 | +export class InvalidTargetError extends OAuthError { |
| 171 | + static override errorCode = 'invalid_target'; |
| 172 | +} |
| 173 | + |
| 174 | +/** |
| 175 | + * A utility class for defining one-off error codes |
| 176 | + */ |
| 177 | +export class CustomOAuthError extends OAuthError { |
| 178 | + constructor( |
| 179 | + private readonly customErrorCode: string, |
| 180 | + message: string, |
| 181 | + errorUri?: string |
| 182 | + ) { |
| 183 | + super(message, errorUri); |
| 184 | + } |
| 185 | + |
| 186 | + override get errorCode(): string { |
| 187 | + return this.customErrorCode; |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +/** |
| 192 | + * A full list of all OAuthErrors, enabling parsing from error responses |
| 193 | + */ |
| 194 | +export const OAUTH_ERRORS = { |
| 195 | + [InvalidRequestError.errorCode]: InvalidRequestError, |
| 196 | + [InvalidClientError.errorCode]: InvalidClientError, |
| 197 | + [InvalidGrantError.errorCode]: InvalidGrantError, |
| 198 | + [UnauthorizedClientError.errorCode]: UnauthorizedClientError, |
| 199 | + [UnsupportedGrantTypeError.errorCode]: UnsupportedGrantTypeError, |
| 200 | + [InvalidScopeError.errorCode]: InvalidScopeError, |
| 201 | + [AccessDeniedError.errorCode]: AccessDeniedError, |
| 202 | + [ServerError.errorCode]: ServerError, |
| 203 | + [TemporarilyUnavailableError.errorCode]: TemporarilyUnavailableError, |
| 204 | + [UnsupportedResponseTypeError.errorCode]: UnsupportedResponseTypeError, |
| 205 | + [UnsupportedTokenTypeError.errorCode]: UnsupportedTokenTypeError, |
| 206 | + [InvalidTokenError.errorCode]: InvalidTokenError, |
| 207 | + [MethodNotAllowedError.errorCode]: MethodNotAllowedError, |
| 208 | + [TooManyRequestsError.errorCode]: TooManyRequestsError, |
| 209 | + [InvalidClientMetadataError.errorCode]: InvalidClientMetadataError, |
| 210 | + [InsufficientScopeError.errorCode]: InsufficientScopeError, |
| 211 | + [InvalidTargetError.errorCode]: InvalidTargetError |
| 212 | +} as const; |
0 commit comments