diff --git a/index.ts b/index.ts index 7387513..363be43 100644 --- a/index.ts +++ b/index.ts @@ -7,9 +7,6 @@ * file that was distributed with this source code. */ +export * as errors from './src/errors.js' export { HttpClient } from './src/http_client.js' export { UrlBuilder } from './src/url_builder.js' -export { Oauth1Client } from './src/clients/oauth1/main.js' -export { Oauth2Client } from './src/clients/oauth2/main.js' -export { MissingTokenException } from './src/exceptions/missing_token.js' -export { StateMisMatchException } from './src/exceptions/state_mismatch.js' diff --git a/package.json b/package.json index 795bacf..746bf59 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,8 @@ ], "exports": { ".": "./build/index.js", + "./oauth1": "./build/src/clients/oauth1/main.js", + "./oauth2": "./build/src/clients/oauth2/main.js", "./types": "./build/src/types.js" }, "engines": { diff --git a/src/clients/oauth1/main.ts b/src/clients/oauth1/main.ts index b56ba3c..1f6c821 100644 --- a/src/clients/oauth1/main.ts +++ b/src/clients/oauth1/main.ts @@ -22,8 +22,7 @@ import debug from '../../debug.js' import { Oauth1Signature } from './signature.js' import { HttpClient } from '../../http_client.js' import { UrlBuilder } from '../../url_builder.js' -import { MissingTokenException } from '../../exceptions/missing_token.js' -import { StateMisMatchException } from '../../exceptions/state_mismatch.js' +import { E_OAUTH_MISSING_TOKEN, E_OAUTH_STATE_MISMATCH } from '../../errors.js' /** * Generic implementation of Oauth1 three leged authorization flow. @@ -181,7 +180,7 @@ export class Oauth1Client { */ verifyState(state: string, inputValue?: string) { if (!state || state !== inputValue) { - throw new StateMisMatchException() + throw new E_OAUTH_STATE_MISMATCH() } } @@ -226,7 +225,7 @@ export class Oauth1Client { * We expect the response to have "oauth_token" and "oauth_token_secret" */ if (!oauthToken || !oauthTokenSecret) { - throw new MissingTokenException(MissingTokenException.oauth1Message, { cause: parsed }) + throw new E_OAUTH_MISSING_TOKEN(E_OAUTH_MISSING_TOKEN.oauth1Message, { cause: parsed }) } return { @@ -343,7 +342,7 @@ export class Oauth1Client { * We expect the response to have "oauth_token" and "oauth_token_secret" */ if (!accessOauthToken || !accessOauthTokenSecret) { - throw new MissingTokenException(MissingTokenException.oauth1Message, { cause: parsed }) + throw new E_OAUTH_MISSING_TOKEN(E_OAUTH_MISSING_TOKEN.oauth1Message, { cause: parsed }) } return { diff --git a/src/clients/oauth2/main.ts b/src/clients/oauth2/main.ts index d39720b..9dfab8b 100644 --- a/src/clients/oauth2/main.ts +++ b/src/clients/oauth2/main.ts @@ -21,8 +21,7 @@ import { import debug from '../../debug.js' import { HttpClient } from '../../http_client.js' import { UrlBuilder } from '../../url_builder.js' -import { MissingTokenException } from '../../exceptions/missing_token.js' -import { StateMisMatchException } from '../../exceptions/state_mismatch.js' +import { E_OAUTH_MISSING_TOKEN, E_OAUTH_STATE_MISMATCH } from '../../errors.js' /** * Generic implementation of OAuth2. @@ -140,7 +139,7 @@ export class Oauth2Client { */ verifyState(state: string, inputValue?: string) { if (!state || state !== inputValue) { - throw new StateMisMatchException() + throw new E_OAUTH_STATE_MISMATCH() } } @@ -216,7 +215,7 @@ export class Oauth2Client { * We expect the response to have "access_token" */ if (!accessToken) { - throw new MissingTokenException(MissingTokenException.oauth2Message, { cause: parsed }) + throw new E_OAUTH_MISSING_TOKEN(E_OAUTH_MISSING_TOKEN.oauth2Message, { cause: parsed }) } return { diff --git a/src/exceptions/missing_token.ts b/src/errors.ts similarity index 60% rename from src/exceptions/missing_token.ts rename to src/errors.ts index 9359e72..e0e5900 100644 --- a/src/exceptions/missing_token.ts +++ b/src/errors.ts @@ -7,15 +7,24 @@ * file that was distributed with this source code. */ -import { Exception } from '@poppinss/utils' +import { createError, Exception } from '@poppinss/utils' /** * Raised when unable to get access token for oauth2 or oauth token and secret * for oauth1 */ -export class MissingTokenException extends Exception { +export const E_OAUTH_MISSING_TOKEN = class MissingTokenException extends Exception { static status = 400 static code = 'E_OAUTH_MISSING_TOKEN' static oauth2Message = 'Invalid oauth2 response. Missing "access_token"' static oauth1Message = 'Invalid oauth1 response. Missing "oauth_token" and "oauth_token_secret"' } + +/** + * Raised when unable to verify the CSRF state post redirect + */ +export const E_OAUTH_STATE_MISMATCH = createError( + 'Unable to verify re-redirect state', + 'E_OAUTH_STATE_MISMATCH', + 400 +) diff --git a/src/exceptions/state_mismatch.ts b/src/exceptions/state_mismatch.ts deleted file mode 100644 index a9d6b39..0000000 --- a/src/exceptions/state_mismatch.ts +++ /dev/null @@ -1,19 +0,0 @@ -/* - * @poppinss/oauth-client - * - * (c) Poppinss - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -import { Exception } from '@poppinss/utils' - -/** - * Raised when unable to verify the CSRF state post redirect - */ -export class StateMisMatchException extends Exception { - static status = 400 - static code = 'E_OAUTH_STATE_MISMATCH' - static message = 'Unable to verify re-redirect state' -} diff --git a/src/http_client.ts b/src/http_client.ts index 5273c61..a028d17 100644 --- a/src/http_client.ts +++ b/src/http_client.ts @@ -9,8 +9,8 @@ import got, { type CancelableRequest, type Response } from 'got' -import type { ApiRequestContract } from './types.js' import debug from './debug.js' +import type { ApiRequestContract } from './types.js' /** * An HTTP client abstraction we need for making OAuth requests