@authly/sdk 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/dist/globals/clients/AuthlyClient.d.ts +27 -70
  2. package/dist/globals/clients/AuthlyClient.js +28 -17
  3. package/dist/globals/configuration/AuthlyConfiguration.d.ts +28 -0
  4. package/dist/globals/configuration/AuthlyConfiguration.js +31 -0
  5. package/dist/globals/{clients → internal}/HttpClient.d.ts +7 -7
  6. package/dist/globals/{clients → internal}/HttpClient.js +7 -7
  7. package/dist/globals/internal/JWTVerifier.d.ts +37 -0
  8. package/dist/globals/internal/JWTVerifier.js +71 -0
  9. package/dist/index.d.ts +22 -2
  10. package/dist/index.js +22 -2
  11. package/dist/models/builders/process-errors/base/AuthlyError.d.ts +11 -0
  12. package/dist/models/builders/process-errors/base/AuthlyError.js +18 -0
  13. package/dist/models/builders/process-errors/base/AuthlyTokenError.d.ts +12 -0
  14. package/dist/models/builders/process-errors/base/AuthlyTokenError.js +19 -0
  15. package/dist/models/builders/process-errors/tokens/AuthlyTokenExpiredError.d.ts +13 -0
  16. package/dist/models/builders/process-errors/tokens/AuthlyTokenExpiredError.js +20 -0
  17. package/dist/models/builders/process-errors/tokens/AuthlyTokenInvalidError.d.ts +16 -0
  18. package/dist/models/builders/process-errors/tokens/AuthlyTokenInvalidError.js +23 -0
  19. package/dist/models/globals/clients/interfaces/IAuthlyClientOptions.d.ts +39 -0
  20. package/dist/models/globals/clients/interfaces/IAuthorizeUrlOptions.d.ts +36 -0
  21. package/dist/models/globals/clients/interfaces/IAuthorizeUrlOptions.js +2 -0
  22. package/dist/models/globals/clients/interfaces/IDecodedTokenClaim.d.ts +68 -0
  23. package/dist/models/globals/clients/interfaces/IDecodedTokenClaim.js +2 -0
  24. package/dist/models/globals/clients/internal/interfaces/IJWTVerifierOptions.d.ts +33 -0
  25. package/dist/models/globals/clients/internal/interfaces/IJWTVerifierOptions.js +2 -0
  26. package/package.json +3 -3
  27. package/dist/config.d.ts +0 -3
  28. package/dist/config.js +0 -6
  29. package/dist/exceptions/index.d.ts +0 -27
  30. package/dist/exceptions/index.js +0 -46
  31. package/dist/globals/clients/internal/JWTVerifier.d.ts +0 -26
  32. package/dist/globals/clients/internal/JWTVerifier.js +0 -55
  33. package/dist/models/Claims.d.ts +0 -48
  34. /package/dist/models/{Claims.js → globals/clients/interfaces/IAuthlyClientOptions.js} +0 -0
@@ -1,92 +1,49 @@
1
- import type { Claims } from "../../models/Claims";
1
+ import type { IAuthlyClientOptions } from "../../models/globals/clients/interfaces/IAuthlyClientOptions";
2
+ import type { IAuthorizeUrlOptions } from "../../models/globals/clients/interfaces/IAuthorizeUrlOptions";
3
+ import type { IDecodedTokenClaim } from "../../models/globals/clients/interfaces/IDecodedTokenClaim";
2
4
  /**
3
- * Options for initializing the AuthlyClient.
4
- */
5
- export interface AuthlyClientOptions {
6
- /**
7
- * The base URL of the identity provider (e.g., "https://auth.example.com").
8
- */
9
- issuer: string;
10
- /**
11
- * The expected audience claim (aud) in the token.
12
- */
13
- audience: string;
14
- /**
15
- * The ID of the service in Authly.
16
- */
17
- serviceId: string;
18
- /**
19
- * The path to the JWKS endpoint relative to the issuer. Defaults to "/.well-known/jwks.json".
20
- */
21
- jwksPath?: string;
22
- /**
23
- * The path to the authorize endpoint relative to the issuer. Defaults to "/v1/oauth/authorize".
24
- */
25
- authorizePath?: string;
26
- /**
27
- * A list of allowed signing algorithms. If omitted, defaults to ["RS256"].
28
- */
29
- algorithms?: string[];
30
- }
31
- /**
32
- * Options for generating an authorization URL.
5
+ * @summary A client for interacting with Authly.
6
+ * @description This client handles the validation of tokens against a specific issuer and audience,
7
+ * fetching the public keys (JWKS) automatically, and provides utilities for
8
+ * starting the OAuth2 flow.
33
9
  */
34
- export interface AuthorizeUrlOptions {
35
- /**
36
- * The URI to redirect back to after authentication.
37
- */
38
- redirectUri: string;
10
+ declare class AuthlyClient {
39
11
  /**
40
- * A unique state string to prevent CSRF.
12
+ * @summary The JWT verifier for the client.
41
13
  */
42
- state: string;
14
+ private readonly verifier;
43
15
  /**
44
- * The PKCE code challenge.
16
+ * @summary The service ID of the client.
45
17
  */
46
- codeChallenge: string;
18
+ private readonly serviceId;
47
19
  /**
48
- * The PKCE code challenge method. Defaults to "S256".
20
+ * @summary The issuer of the client.
49
21
  */
50
- codeChallengeMethod?: "S256" | "plain";
22
+ private readonly issuer;
51
23
  /**
52
- * The requested scopes. Defaults to "openid profile email".
24
+ * @summary The authorize path of the client.
53
25
  */
54
- scope?: string;
26
+ private readonly authorizePath;
55
27
  /**
56
- * The OAuth2 response type. Defaults to "code".
28
+ * @summary Constructs a new AuthlyClient.
29
+ * @param options - The options for the client.
57
30
  */
58
- responseType?: string;
59
- }
60
- /**
61
- * A client for interacting with Authly.
62
- *
63
- * This client handles the validation of tokens against a specific issuer and audience,
64
- * fetching the public keys (JWKS) automatically, and provides utilities for
65
- * starting the OAuth2 flow.
66
- */
67
- export declare class AuthlyClient {
68
- private readonly verifier;
69
- private readonly serviceId;
70
- private readonly issuer;
71
- private readonly authorizePath;
72
- constructor(options: AuthlyClientOptions);
31
+ constructor(options: IAuthlyClientOptions);
73
32
  /**
74
- * Generate the authorization URL to redirect the user to.
75
- *
33
+ * @summary Generate the authorization URL to redirect the user to.
76
34
  * @param options - Options for generating the URL.
77
35
  * @returns The full authorization URL.
78
36
  */
79
- getAuthorizeUrl(options: AuthorizeUrlOptions): string;
37
+ getAuthorizeUrl(options: IAuthorizeUrlOptions): string;
80
38
  /**
81
- * Verify a JWT token and return its decoded claims.
82
- *
83
- * This method verifies the token's signature using the provider's JWKS,
39
+ * @summary Verify a JWT token and return its decoded claims.
40
+ * @description This method verifies the token's signature using the provider's JWKS,
84
41
  * and validates standard claims like expiration, issuer, and audience.
85
- *
86
42
  * @param token - The encoded JWT token string.
87
43
  * @returns A promise that resolves to the token claims (e.g., sub, iss, aud).
88
- * @throws {TokenExpiredError} If the token has expired.
89
- * @throws {TokenInvalidError} If the token is invalid (e.g., bad signature, invalid audience).
44
+ * @throws {AuthlyTokenExpiredError} If the token has expired.
45
+ * @throws {AuthlyTokenInvalidError} If the token is invalid (e.g., bad signature, invalid audience).
90
46
  */
91
- verify(token: string): Promise<Claims>;
47
+ verify(token: string): Promise<IDecodedTokenClaim>;
92
48
  }
49
+ export { AuthlyClient };
@@ -1,35 +1,48 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AuthlyClient = void 0;
4
- const config_1 = require("../../config");
5
- const JWTVerifier_1 = require("./internal/JWTVerifier");
4
+ const JWTVerifier_1 = require("../internal/JWTVerifier");
5
+ const AuthlyConfiguration_1 = require("../configuration/AuthlyConfiguration");
6
6
  /**
7
- * A client for interacting with Authly.
8
- *
9
- * This client handles the validation of tokens against a specific issuer and audience,
7
+ * @summary A client for interacting with Authly.
8
+ * @description This client handles the validation of tokens against a specific issuer and audience,
10
9
  * fetching the public keys (JWKS) automatically, and provides utilities for
11
10
  * starting the OAuth2 flow.
12
11
  */
13
12
  class AuthlyClient {
13
+ /**
14
+ * @summary The JWT verifier for the client.
15
+ */
14
16
  verifier;
17
+ /**
18
+ * @summary The service ID of the client.
19
+ */
15
20
  serviceId;
21
+ /**
22
+ * @summary The issuer of the client.
23
+ */
16
24
  issuer;
25
+ /**
26
+ * @summary The authorize path of the client.
27
+ */
17
28
  authorizePath;
29
+ /**
30
+ * @summary Constructs a new AuthlyClient.
31
+ * @param options - The options for the client.
32
+ */
18
33
  constructor(options) {
19
34
  this.issuer = options.issuer.replace(/\/$/, "");
20
35
  this.serviceId = options.serviceId;
21
- const jwksPath = options.jwksPath || config_1.DEFAULT_JWKS_PATH;
22
- this.authorizePath = options.authorizePath || config_1.DEFAULT_AUTHORIZE_PATH;
36
+ this.authorizePath = options.authorizePath || AuthlyConfiguration_1.AuthlyConfiguration.DEFAULT_AUTHORIZE_PATH;
23
37
  this.verifier = new JWTVerifier_1.JWTVerifier({
24
38
  issuer: this.issuer,
25
39
  audience: options.audience,
26
- jwksUrl: `${this.issuer}${jwksPath}`,
40
+ jwksUrl: `${this.issuer}${options.jwksPath || AuthlyConfiguration_1.AuthlyConfiguration.DEFAULT_JWKS_PATH}`,
27
41
  algorithms: options.algorithms,
28
42
  });
29
43
  }
30
44
  /**
31
- * Generate the authorization URL to redirect the user to.
32
- *
45
+ * @summary Generate the authorization URL to redirect the user to.
33
46
  * @param options - Options for generating the URL.
34
47
  * @returns The full authorization URL.
35
48
  */
@@ -41,19 +54,17 @@ class AuthlyClient {
41
54
  url.searchParams.set("scope", options.scope || "openid profile email");
42
55
  url.searchParams.set("state", options.state);
43
56
  url.searchParams.set("code_challenge", options.codeChallenge);
44
- url.searchParams.set("code_challenge_method", options.codeChallengeMethod || "S256");
57
+ url.searchParams.set("code_challenge_method", options.codeChallengeMethod || "s256");
45
58
  return url.toString();
46
59
  }
47
60
  /**
48
- * Verify a JWT token and return its decoded claims.
49
- *
50
- * This method verifies the token's signature using the provider's JWKS,
61
+ * @summary Verify a JWT token and return its decoded claims.
62
+ * @description This method verifies the token's signature using the provider's JWKS,
51
63
  * and validates standard claims like expiration, issuer, and audience.
52
- *
53
64
  * @param token - The encoded JWT token string.
54
65
  * @returns A promise that resolves to the token claims (e.g., sub, iss, aud).
55
- * @throws {TokenExpiredError} If the token has expired.
56
- * @throws {TokenInvalidError} If the token is invalid (e.g., bad signature, invalid audience).
66
+ * @throws {AuthlyTokenExpiredError} If the token has expired.
67
+ * @throws {AuthlyTokenInvalidError} If the token is invalid (e.g., bad signature, invalid audience).
57
68
  */
58
69
  async verify(token) {
59
70
  return this.verifier.verify(token);
@@ -0,0 +1,28 @@
1
+ /**
2
+ * @summary Configuration for the Authly library.
3
+ */
4
+ declare class AuthlyConfiguration {
5
+ /**
6
+ * @summary Private constructor to prevent instantiation.
7
+ */
8
+ private constructor();
9
+ /**
10
+ * @summary The default JWKS path.
11
+ * @readonly This property is readonly and cannot be changed.
12
+ * @default "/.well-known/jwks.json"
13
+ */
14
+ static readonly DEFAULT_JWKS_PATH: string;
15
+ /**
16
+ * @summary The default authorize path.
17
+ * @readonly This property is readonly and cannot be changed.
18
+ * @default "/authorize"
19
+ */
20
+ static readonly DEFAULT_AUTHORIZE_PATH: string;
21
+ /**
22
+ * @summary The default algorithms.
23
+ * @readonly This property is readonly and cannot be changed.
24
+ * @default ["RS256"]
25
+ */
26
+ static readonly DEFAULT_ALGORITHMS: readonly string[];
27
+ }
28
+ export { AuthlyConfiguration };
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthlyConfiguration = void 0;
4
+ /**
5
+ * @summary Configuration for the Authly library.
6
+ */
7
+ class AuthlyConfiguration {
8
+ /**
9
+ * @summary Private constructor to prevent instantiation.
10
+ */
11
+ constructor() { }
12
+ /**
13
+ * @summary The default JWKS path.
14
+ * @readonly This property is readonly and cannot be changed.
15
+ * @default "/.well-known/jwks.json"
16
+ */
17
+ static DEFAULT_JWKS_PATH = "/.well-known/jwks.json";
18
+ /**
19
+ * @summary The default authorize path.
20
+ * @readonly This property is readonly and cannot be changed.
21
+ * @default "/authorize"
22
+ */
23
+ static DEFAULT_AUTHORIZE_PATH = "/authorize";
24
+ /**
25
+ * @summary The default algorithms.
26
+ * @readonly This property is readonly and cannot be changed.
27
+ * @default ["RS256"]
28
+ */
29
+ static DEFAULT_ALGORITHMS = ["RS256"];
30
+ }
31
+ exports.AuthlyConfiguration = AuthlyConfiguration;
@@ -1,46 +1,46 @@
1
1
  import type { IRequestResponseClient } from "../../models/globals/clients/interfaces/IRequestResponseClient";
2
2
  /**
3
- * A class that provides a static method for making HTTP requests.
3
+ * @summary A class that provides a static method for making HTTP requests.
4
4
  */
5
5
  declare class HttpClient {
6
6
  private constructor();
7
7
  /**
8
- * Makes an HTTP request to the specified URL.
8
+ * @summary Makes an HTTP request to the specified URL.
9
9
  * @param url - The URL to make the request to.
10
10
  * @param options - The options for the request.
11
11
  * @returns A promise that resolves to the response data.
12
12
  */
13
13
  private static request;
14
14
  /**
15
- * Makes a GET request to the specified URL.
15
+ * @summary Makes a GET request to the specified URL.
16
16
  * @param url - The URL to make the request to.
17
17
  * @param options - The options for the request.
18
18
  * @returns A promise that resolves to the response data.
19
19
  */
20
20
  static get<D>(url: string, options?: Omit<RequestInit, "method">): Promise<IRequestResponseClient<D>>;
21
21
  /**
22
- * Makes a POST request to the specified URL.
22
+ * @summary Makes a POST request to the specified URL.
23
23
  * @param url - The URL to make the request to.
24
24
  * @param options - The options for the request.
25
25
  * @returns A promise that resolves to the response data.
26
26
  */
27
27
  static post<D>(url: string, options?: Omit<RequestInit, "method">): Promise<IRequestResponseClient<D>>;
28
28
  /**
29
- * Makes a PUT request to the specified URL.
29
+ * @summary Makes a PUT request to the specified URL.
30
30
  * @param url - The URL to make the request to.
31
31
  * @param options - The options for the request.
32
32
  * @returns A promise that resolves to the response data.
33
33
  */
34
34
  static put<D>(url: string, options?: Omit<RequestInit, "method">): Promise<IRequestResponseClient<D>>;
35
35
  /**
36
- * Makes a PATCH request to the specified URL.
36
+ * @summary Makes a PATCH request to the specified URL.
37
37
  * @param url - The URL to make the request to.
38
38
  * @param options - The options for the request.
39
39
  * @returns A promise that resolves to the response data.
40
40
  */
41
41
  static patch<D>(url: string, options?: Omit<RequestInit, "method">): Promise<IRequestResponseClient<D>>;
42
42
  /**
43
- * Makes a DELETE request to the specified URL.
43
+ * @summary Makes a DELETE request to the specified URL.
44
44
  * @param url - The URL to make the request to.
45
45
  * @param options - The options for the request.
46
46
  * @returns A promise that resolves to the response data.
@@ -2,12 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HttpClient = void 0;
4
4
  /**
5
- * A class that provides a static method for making HTTP requests.
5
+ * @summary A class that provides a static method for making HTTP requests.
6
6
  */
7
7
  class HttpClient {
8
8
  constructor() { }
9
9
  /**
10
- * Makes an HTTP request to the specified URL.
10
+ * @summary Makes an HTTP request to the specified URL.
11
11
  * @param url - The URL to make the request to.
12
12
  * @param options - The options for the request.
13
13
  * @returns A promise that resolves to the response data.
@@ -35,7 +35,7 @@ class HttpClient {
35
35
  }
36
36
  }
37
37
  /**
38
- * Makes a GET request to the specified URL.
38
+ * @summary Makes a GET request to the specified URL.
39
39
  * @param url - The URL to make the request to.
40
40
  * @param options - The options for the request.
41
41
  * @returns A promise that resolves to the response data.
@@ -47,7 +47,7 @@ class HttpClient {
47
47
  });
48
48
  }
49
49
  /**
50
- * Makes a POST request to the specified URL.
50
+ * @summary Makes a POST request to the specified URL.
51
51
  * @param url - The URL to make the request to.
52
52
  * @param options - The options for the request.
53
53
  * @returns A promise that resolves to the response data.
@@ -59,7 +59,7 @@ class HttpClient {
59
59
  });
60
60
  }
61
61
  /**
62
- * Makes a PUT request to the specified URL.
62
+ * @summary Makes a PUT request to the specified URL.
63
63
  * @param url - The URL to make the request to.
64
64
  * @param options - The options for the request.
65
65
  * @returns A promise that resolves to the response data.
@@ -71,7 +71,7 @@ class HttpClient {
71
71
  });
72
72
  }
73
73
  /**
74
- * Makes a PATCH request to the specified URL.
74
+ * @summary Makes a PATCH request to the specified URL.
75
75
  * @param url - The URL to make the request to.
76
76
  * @param options - The options for the request.
77
77
  * @returns A promise that resolves to the response data.
@@ -83,7 +83,7 @@ class HttpClient {
83
83
  });
84
84
  }
85
85
  /**
86
- * Makes a DELETE request to the specified URL.
86
+ * @summary Makes a DELETE request to the specified URL.
87
87
  * @param url - The URL to make the request to.
88
88
  * @param options - The options for the request.
89
89
  * @returns A promise that resolves to the response data.
@@ -0,0 +1,37 @@
1
+ import type { IDecodedTokenClaim } from "../../models/globals/clients/interfaces/IDecodedTokenClaim";
2
+ import type { IJWTVerifierOptions } from "../../models/globals/clients/internal/interfaces/IJWTVerifierOptions";
3
+ /**
4
+ * @summary Internal class for verifying JWT tokens using jose.
5
+ */
6
+ declare class JWTVerifier {
7
+ /**
8
+ * @summary The issuer of the token.
9
+ */
10
+ private readonly issuer;
11
+ /**
12
+ * @summary The audience of the token.
13
+ */
14
+ private readonly audience;
15
+ /**
16
+ * @summary The algorithms to use for the token.
17
+ */
18
+ private readonly algorithms;
19
+ /**
20
+ * @summary The JWKS to use for the token.
21
+ */
22
+ private readonly JWKS;
23
+ /**
24
+ * @summary Constructs a new JWTVerifier.
25
+ * @param params - The options for the JWTVerifier.
26
+ */
27
+ constructor(params: IJWTVerifierOptions);
28
+ /**
29
+ * @summary Verify the JWT token and return its claims.
30
+ * @param token - The encoded JWT token string.
31
+ * @returns The decoded claims from the token.
32
+ * @throws {AuthlyTokenExpiredError} If the token's exp claim is in the past.
33
+ * @throws {AuthlyTokenInvalidError} If the token is otherwise invalid.
34
+ */
35
+ verify(token: string): Promise<IDecodedTokenClaim>;
36
+ }
37
+ export { JWTVerifier };
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JWTVerifier = void 0;
4
+ const jose_1 = require("jose");
5
+ const AuthlyTokenExpiredError_1 = require("../../models/builders/process-errors/tokens/AuthlyTokenExpiredError");
6
+ const AuthlyTokenInvalidError_1 = require("../../models/builders/process-errors/tokens/AuthlyTokenInvalidError");
7
+ const AuthlyConfiguration_1 = require("../configuration/AuthlyConfiguration");
8
+ /**
9
+ * @summary Internal class for verifying JWT tokens using jose.
10
+ */
11
+ class JWTVerifier {
12
+ /**
13
+ * @summary The issuer of the token.
14
+ */
15
+ issuer;
16
+ /**
17
+ * @summary The audience of the token.
18
+ */
19
+ audience;
20
+ /**
21
+ * @summary The algorithms to use for the token.
22
+ */
23
+ algorithms;
24
+ /**
25
+ * @summary The JWKS to use for the token.
26
+ */
27
+ JWKS;
28
+ /**
29
+ * @summary Constructs a new JWTVerifier.
30
+ * @param params - The options for the JWTVerifier.
31
+ */
32
+ constructor(params) {
33
+ this.issuer = params.issuer;
34
+ this.audience = params.audience;
35
+ this.algorithms = params.algorithms || AuthlyConfiguration_1.AuthlyConfiguration.DEFAULT_ALGORITHMS;
36
+ this.JWKS = params.jwks || (0, jose_1.createRemoteJWKSet)(new URL(params.jwksUrl));
37
+ }
38
+ /**
39
+ * @summary Verify the JWT token and return its claims.
40
+ * @param token - The encoded JWT token string.
41
+ * @returns The decoded claims from the token.
42
+ * @throws {AuthlyTokenExpiredError} If the token's exp claim is in the past.
43
+ * @throws {AuthlyTokenInvalidError} If the token is otherwise invalid.
44
+ */
45
+ async verify(token) {
46
+ try {
47
+ const { payload } = await (0, jose_1.jwtVerify)(token, this.JWKS, {
48
+ issuer: this.issuer,
49
+ audience: this.audience,
50
+ algorithms: this.algorithms,
51
+ });
52
+ return payload;
53
+ }
54
+ catch (error) {
55
+ if (error instanceof Error) {
56
+ const code = error.code;
57
+ if (code === "ERR_JWT_EXPIRED") {
58
+ throw new AuthlyTokenExpiredError_1.AuthlyTokenExpiredError("Token has expired");
59
+ }
60
+ if (code === "ERR_JWT_CLAIM_VALIDATION_FAILED" ||
61
+ code === "ERR_JWS_SIGNATURE_VERIFICATION_FAILED" ||
62
+ code === "ERR_JWS_INVALID" ||
63
+ code === "ERR_JWT_INVALID") {
64
+ throw new AuthlyTokenInvalidError_1.AuthlyTokenInvalidError(error.message ?? "Token validation failed");
65
+ }
66
+ }
67
+ throw new AuthlyTokenInvalidError_1.AuthlyTokenInvalidError("Invalid token");
68
+ }
69
+ }
70
+ }
71
+ exports.JWTVerifier = JWTVerifier;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,23 @@
1
- export * from "./models/Claims";
2
- export * from "./exceptions";
1
+ /**
2
+ * @authly/sdk
3
+ *
4
+ * A library for building authentication systems using Authly.
5
+ *
6
+ * @author Anvoria
7
+ * @license MIT
8
+ */
9
+ /**
10
+ * Clients.
11
+ */
3
12
  export * from "./globals/clients/AuthlyClient";
13
+ export * from "./globals/configuration/AuthlyConfiguration";
14
+ /**
15
+ * Models.
16
+ */
17
+ export * from "./models/builders/process-errors/base/AuthlyError";
18
+ export * from "./models/builders/process-errors/base/AuthlyTokenError";
19
+ export * from "./models/builders/process-errors/tokens/AuthlyTokenExpiredError";
20
+ export * from "./models/builders/process-errors/tokens/AuthlyTokenInvalidError";
21
+ export * from "./models/globals/clients/interfaces/IAuthlyClientOptions";
22
+ export * from "./models/globals/clients/interfaces/IAuthorizeUrlOptions";
23
+ export * from "./models/globals/clients/interfaces/IDecodedTokenClaim";
package/dist/index.js CHANGED
@@ -1,4 +1,12 @@
1
1
  "use strict";
2
+ /**
3
+ * @authly/sdk
4
+ *
5
+ * A library for building authentication systems using Authly.
6
+ *
7
+ * @author Anvoria
8
+ * @license MIT
9
+ */
2
10
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
11
  if (k2 === undefined) k2 = k;
4
12
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -14,6 +22,18 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
22
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
23
  };
16
24
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./models/Claims"), exports);
18
- __exportStar(require("./exceptions"), exports);
25
+ /**
26
+ * Clients.
27
+ */
19
28
  __exportStar(require("./globals/clients/AuthlyClient"), exports);
29
+ __exportStar(require("./globals/configuration/AuthlyConfiguration"), exports);
30
+ /**
31
+ * Models.
32
+ */
33
+ __exportStar(require("./models/builders/process-errors/base/AuthlyError"), exports);
34
+ __exportStar(require("./models/builders/process-errors/base/AuthlyTokenError"), exports);
35
+ __exportStar(require("./models/builders/process-errors/tokens/AuthlyTokenExpiredError"), exports);
36
+ __exportStar(require("./models/builders/process-errors/tokens/AuthlyTokenInvalidError"), exports);
37
+ __exportStar(require("./models/globals/clients/interfaces/IAuthlyClientOptions"), exports);
38
+ __exportStar(require("./models/globals/clients/interfaces/IAuthorizeUrlOptions"), exports);
39
+ __exportStar(require("./models/globals/clients/interfaces/IDecodedTokenClaim"), exports);
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @summary Base exception for all Authly errors.
3
+ */
4
+ declare abstract class AuthlyError extends Error {
5
+ /**
6
+ * @summary Constructs a new AuthlyError.
7
+ * @param message - The error message.
8
+ */
9
+ constructor(message: string);
10
+ }
11
+ export { AuthlyError };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthlyError = void 0;
4
+ /**
5
+ * @summary Base exception for all Authly errors.
6
+ */
7
+ class AuthlyError extends Error {
8
+ /**
9
+ * @summary Constructs a new AuthlyError.
10
+ * @param message - The error message.
11
+ */
12
+ constructor(message) {
13
+ super(message);
14
+ this.name = "AuthlyError";
15
+ Object.setPrototypeOf(this, AuthlyError.prototype);
16
+ }
17
+ }
18
+ exports.AuthlyError = AuthlyError;
@@ -0,0 +1,12 @@
1
+ import { AuthlyError } from "./AuthlyError";
2
+ /**
3
+ * @summary Base exception for all Authly token errors.
4
+ */
5
+ declare abstract class AuthlyTokenError extends AuthlyError {
6
+ /**
7
+ * @summary Constructs a new AuthlyTokenError.
8
+ * @param message - The error message.
9
+ */
10
+ constructor(message: string);
11
+ }
12
+ export { AuthlyTokenError };
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthlyTokenError = void 0;
4
+ const AuthlyError_1 = require("./AuthlyError");
5
+ /**
6
+ * @summary Base exception for all Authly token errors.
7
+ */
8
+ class AuthlyTokenError extends AuthlyError_1.AuthlyError {
9
+ /**
10
+ * @summary Constructs a new AuthlyTokenError.
11
+ * @param message - The error message.
12
+ */
13
+ constructor(message) {
14
+ super(message);
15
+ this.name = "AuthlyTokenError";
16
+ Object.setPrototypeOf(this, AuthlyTokenError.prototype);
17
+ }
18
+ }
19
+ exports.AuthlyTokenError = AuthlyTokenError;
@@ -0,0 +1,13 @@
1
+ import { AuthlyTokenError } from "../base/AuthlyTokenError";
2
+ /**
3
+ * @summary Exception raised when a token is expired.
4
+ * @throws This error is thrown when the token's expiration time is in the past.
5
+ */
6
+ declare class AuthlyTokenExpiredError extends AuthlyTokenError {
7
+ /**
8
+ * @summary Constructs a new AuthlyTokenExpiredError.
9
+ * @param message - The error message.
10
+ */
11
+ constructor(message: string);
12
+ }
13
+ export { AuthlyTokenExpiredError };
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthlyTokenExpiredError = void 0;
4
+ const AuthlyTokenError_1 = require("../base/AuthlyTokenError");
5
+ /**
6
+ * @summary Exception raised when a token is expired.
7
+ * @throws This error is thrown when the token's expiration time is in the past.
8
+ */
9
+ class AuthlyTokenExpiredError extends AuthlyTokenError_1.AuthlyTokenError {
10
+ /**
11
+ * @summary Constructs a new AuthlyTokenExpiredError.
12
+ * @param message - The error message.
13
+ */
14
+ constructor(message) {
15
+ super(message);
16
+ this.name = "AuthlyTokenExpiredError";
17
+ Object.setPrototypeOf(this, AuthlyTokenExpiredError.prototype);
18
+ }
19
+ }
20
+ exports.AuthlyTokenExpiredError = AuthlyTokenExpiredError;
@@ -0,0 +1,16 @@
1
+ import { AuthlyTokenError } from "../base/AuthlyTokenError";
2
+ /**
3
+ * @summary Exception raised when a token is invalid.
4
+ * @throws This error is thrown when:
5
+ * - The token has an invalid signature.
6
+ * - The token has an invalid format.
7
+ * - The token has an invalid issuer or audience.
8
+ */
9
+ declare class AuthlyTokenInvalidError extends AuthlyTokenError {
10
+ /**
11
+ * @summary Constructs a new AuthlyTokenInvalidError.
12
+ * @param message - The error message.
13
+ */
14
+ constructor(message: string);
15
+ }
16
+ export { AuthlyTokenInvalidError };
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthlyTokenInvalidError = void 0;
4
+ const AuthlyTokenError_1 = require("../base/AuthlyTokenError");
5
+ /**
6
+ * @summary Exception raised when a token is invalid.
7
+ * @throws This error is thrown when:
8
+ * - The token has an invalid signature.
9
+ * - The token has an invalid format.
10
+ * - The token has an invalid issuer or audience.
11
+ */
12
+ class AuthlyTokenInvalidError extends AuthlyTokenError_1.AuthlyTokenError {
13
+ /**
14
+ * @summary Constructs a new AuthlyTokenInvalidError.
15
+ * @param message - The error message.
16
+ */
17
+ constructor(message) {
18
+ super(message);
19
+ this.name = "AuthlyTokenInvalidError";
20
+ Object.setPrototypeOf(this, AuthlyTokenInvalidError.prototype);
21
+ }
22
+ }
23
+ exports.AuthlyTokenInvalidError = AuthlyTokenInvalidError;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * @summary Options for initializing the AuthlyClient.
3
+ */
4
+ interface IAuthlyClientOptions {
5
+ /**
6
+ * @summary The base URL of the identity provider.
7
+ * @example "https://auth.example.com"
8
+ */
9
+ readonly issuer: string;
10
+ /**
11
+ * @summary The expected audience claim (aud) in the token.
12
+ * @example "https://app.example.com"
13
+ */
14
+ readonly audience: string;
15
+ /**
16
+ * @summary The ID of the service in Authly.
17
+ * @example "1234567890"
18
+ */
19
+ readonly serviceId: string;
20
+ /**
21
+ * @summary The path to the JWKS endpoint relative to the issuer.
22
+ * @example "/.well-known/jwks.json"
23
+ * @default "/.well-known/jwks.json"
24
+ */
25
+ readonly jwksPath?: string;
26
+ /**
27
+ * @summary The path to the authorize endpoint relative to the issuer.
28
+ * @example "/v1/oauth/authorize"
29
+ * @default "/authorize"
30
+ */
31
+ readonly authorizePath?: string;
32
+ /**
33
+ * @summary A list of allowed signing algorithms.
34
+ * @example ["RS256"]
35
+ * @default ["RS256"]
36
+ */
37
+ readonly algorithms?: string[];
38
+ }
39
+ export type { IAuthlyClientOptions };
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @summary Options for generating an authorization URL.
3
+ */
4
+ interface IAuthorizeUrlOptions {
5
+ /**
6
+ * @summary The URI to redirect back to after authentication.
7
+ * @example "https://app.example.com/callback"
8
+ */
9
+ readonly redirectUri: string;
10
+ /**
11
+ * @summary A unique state string to prevent CSRF.
12
+ * @example "1234567890"
13
+ */
14
+ readonly state: string;
15
+ /**
16
+ * @summary The PKCE code challenge.
17
+ * @example "1234567890"
18
+ */
19
+ readonly codeChallenge: string;
20
+ /**
21
+ * @summary The PKCE code challenge method.
22
+ * @example "S256"
23
+ */
24
+ readonly codeChallengeMethod?: "S256" | "plain";
25
+ /**
26
+ * @summary The requested scopes.
27
+ * @example "openid profile email"
28
+ */
29
+ readonly scope?: string;
30
+ /**
31
+ * @summary The OAuth2 response type.
32
+ * @example "code"
33
+ */
34
+ readonly responseType?: string;
35
+ }
36
+ export type { IAuthorizeUrlOptions };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,68 @@
1
+ /**
2
+ * @summary Interface for decoded token claims.
3
+ */
4
+ interface IDecodedTokenClaim {
5
+ /**
6
+ * @summary Subject identifier - the unique ID of the user.
7
+ * @example "1234567890"
8
+ */
9
+ readonly sub: string;
10
+ /**
11
+ * @summary Issuer identifier - the URL of the identity provider.
12
+ * @example "https://auth.example.com"
13
+ */
14
+ readonly iss: string;
15
+ /**
16
+ * @summary Audience(s) for which the token is intended.
17
+ * @example "https://app.example.com"
18
+ * @example ["https://app.example.com", "https://app.example.com/api"]
19
+ */
20
+ readonly aud: string | string[];
21
+ /**
22
+ * @summary Expiration time (Unix timestamp).
23
+ * @example 1715145600
24
+ */
25
+ readonly exp: number;
26
+ /**
27
+ * @summary Issued at time (Unix timestamp).
28
+ * @example 1715145600
29
+ */
30
+ readonly iat: number;
31
+ /**
32
+ * @summary Session ID identifier.
33
+ * @example "1234567890"
34
+ */
35
+ readonly sid: string;
36
+ /**
37
+ * @summary Dictionary of permissions granted to the user, where keys are resource names and values are permission levels.
38
+ * @example
39
+ * ```json
40
+ * {
41
+ * "read": 1,
42
+ * "write": 0
43
+ * }
44
+ * ```
45
+ */
46
+ readonly permissions: Record<string, number>;
47
+ /**
48
+ * @summary Permission version.
49
+ * @example 1
50
+ */
51
+ readonly pver?: number;
52
+ /**
53
+ * @summary Space-separated list of scopes.
54
+ * @example "openid profile email"
55
+ */
56
+ readonly scope?: string;
57
+ /**
58
+ * @summary Allow additional claims.
59
+ * @example
60
+ * ```json
61
+ * {
62
+ * "custom": "custom-value"
63
+ * }
64
+ * ```
65
+ */
66
+ readonly [key: string]: unknown;
67
+ }
68
+ export type { IDecodedTokenClaim };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,33 @@
1
+ import type { JWTVerifyGetKey } from "jose";
2
+ /**
3
+ * @summary Options for the JWTVerifier class.
4
+ */
5
+ interface IJWTVerifierOptions {
6
+ /**
7
+ * @summary The issuer of the token.
8
+ * @example "https://auth.example.com"
9
+ */
10
+ readonly issuer: string;
11
+ /**
12
+ * @summary The audience of the token.
13
+ * @example "https://app.example.com"
14
+ */
15
+ readonly audience: string;
16
+ /**
17
+ * @summary The URL of the JWKS endpoint.
18
+ * @example "https://auth.example.com/.well-known/jwks.json"
19
+ */
20
+ readonly jwksUrl: string;
21
+ /**
22
+ * @summary The algorithms to use for the token.
23
+ * @example ["RS256", "ES256"]
24
+ * @default ["RS256"]
25
+ */
26
+ readonly algorithms?: string[];
27
+ /**
28
+ * @summary The JWKS to use for the token.
29
+ * @example async () => publicKey
30
+ */
31
+ readonly jwks?: JWTVerifyGetKey;
32
+ }
33
+ export type { IJWTVerifierOptions };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@authly/sdk",
3
3
  "description": "A library for building authentication systems using Authly.",
4
- "version": "1.1.1",
4
+ "version": "1.1.2",
5
5
  "author": {
6
6
  "name": "Anvoria",
7
7
  "url": "https://github.com/Anvoria"
@@ -59,7 +59,7 @@
59
59
  "@types/bun": "^1.3.4",
60
60
  "@types/node": "^25.0.3",
61
61
  "eslint": "^9.39.2",
62
- "globals": "^16.5.0",
62
+ "globals": "^17.0.0",
63
63
  "husky": "^9.1.7",
64
64
  "jiti": "^2.6.1",
65
65
  "lint-staged": "^16.2.7",
@@ -71,4 +71,4 @@
71
71
  "dependencies": {
72
72
  "jose": "^6.1.3"
73
73
  }
74
- }
74
+ }
package/dist/config.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export declare const DEFAULT_JWKS_PATH = "/.well-known/jwks.json";
2
- export declare const DEFAULT_AUTHORIZE_PATH = "/authorize";
3
- export declare const DEFAULT_ALGORITHMS: string[];
package/dist/config.js DELETED
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_ALGORITHMS = exports.DEFAULT_AUTHORIZE_PATH = exports.DEFAULT_JWKS_PATH = void 0;
4
- exports.DEFAULT_JWKS_PATH = "/.well-known/jwks.json";
5
- exports.DEFAULT_AUTHORIZE_PATH = "/authorize";
6
- exports.DEFAULT_ALGORITHMS = ["RS256"];
@@ -1,27 +0,0 @@
1
- /**
2
- * Base exception for all Authly errors.
3
- */
4
- export declare class AuthlyError extends Error {
5
- constructor(message: string);
6
- }
7
- /**
8
- * Base exception for all token errors.
9
- */
10
- export declare class TokenError extends AuthlyError {
11
- constructor(message: string);
12
- }
13
- /**
14
- * Exception raised when a token is invalid:
15
- * - bad signature
16
- * - bad format
17
- * - bad iss / aud
18
- */
19
- export declare class TokenInvalidError extends TokenError {
20
- constructor(message: string);
21
- }
22
- /**
23
- * Exception raised when a token is expired.
24
- */
25
- export declare class TokenExpiredError extends TokenError {
26
- constructor(message: string);
27
- }
@@ -1,46 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TokenExpiredError = exports.TokenInvalidError = exports.TokenError = exports.AuthlyError = void 0;
4
- /**
5
- * Base exception for all Authly errors.
6
- */
7
- class AuthlyError extends Error {
8
- constructor(message) {
9
- super(message);
10
- this.name = "AuthlyError";
11
- }
12
- }
13
- exports.AuthlyError = AuthlyError;
14
- /**
15
- * Base exception for all token errors.
16
- */
17
- class TokenError extends AuthlyError {
18
- constructor(message) {
19
- super(message);
20
- this.name = "TokenError";
21
- }
22
- }
23
- exports.TokenError = TokenError;
24
- /**
25
- * Exception raised when a token is invalid:
26
- * - bad signature
27
- * - bad format
28
- * - bad iss / aud
29
- */
30
- class TokenInvalidError extends TokenError {
31
- constructor(message) {
32
- super(message);
33
- this.name = "TokenInvalidError";
34
- }
35
- }
36
- exports.TokenInvalidError = TokenInvalidError;
37
- /**
38
- * Exception raised when a token is expired.
39
- */
40
- class TokenExpiredError extends TokenError {
41
- constructor(message) {
42
- super(message);
43
- this.name = "TokenExpiredError";
44
- }
45
- }
46
- exports.TokenExpiredError = TokenExpiredError;
@@ -1,26 +0,0 @@
1
- import type { JWTVerifyGetKey } from "jose";
2
- import type { Claims } from "../../../models/Claims";
3
- /**
4
- * Internal class for verifying JWT tokens using jose.
5
- */
6
- export declare class JWTVerifier {
7
- private readonly issuer;
8
- private readonly audience;
9
- private readonly algorithms;
10
- private readonly JWKS;
11
- constructor(params: {
12
- issuer: string;
13
- audience: string;
14
- jwksUrl: string;
15
- algorithms?: string[];
16
- jwks?: JWTVerifyGetKey;
17
- });
18
- /**
19
- * Verify the JWT token and return its claims.
20
- * @param token - The encoded JWT token string.
21
- * @returns The decoded claims from the token.
22
- * @throws {TokenExpiredError} If the token's exp claim is in the past.
23
- * @throws {TokenInvalidError} If the token is otherwise invalid.
24
- */
25
- verify(token: string): Promise<Claims>;
26
- }
@@ -1,55 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.JWTVerifier = void 0;
4
- const jose_1 = require("jose");
5
- const config_1 = require("../../../config");
6
- const exceptions_1 = require("../../../exceptions");
7
- /**
8
- * Internal class for verifying JWT tokens using jose.
9
- */
10
- class JWTVerifier {
11
- issuer;
12
- audience;
13
- algorithms;
14
- JWKS;
15
- constructor(params) {
16
- this.issuer = params.issuer;
17
- this.audience = params.audience;
18
- this.algorithms = params.algorithms || config_1.DEFAULT_ALGORITHMS;
19
- this.JWKS = params.jwks || (0, jose_1.createRemoteJWKSet)(new URL(params.jwksUrl));
20
- }
21
- /**
22
- * Verify the JWT token and return its claims.
23
- * @param token - The encoded JWT token string.
24
- * @returns The decoded claims from the token.
25
- * @throws {TokenExpiredError} If the token's exp claim is in the past.
26
- * @throws {TokenInvalidError} If the token is otherwise invalid.
27
- */
28
- async verify(token) {
29
- try {
30
- const options = {
31
- issuer: this.issuer,
32
- audience: this.audience,
33
- algorithms: this.algorithms,
34
- };
35
- const { payload } = await (0, jose_1.jwtVerify)(token, this.JWKS, options);
36
- return payload;
37
- }
38
- catch (error) {
39
- if (error instanceof Error) {
40
- const code = error.code;
41
- if (code === "ERR_JWT_EXPIRED") {
42
- throw new exceptions_1.TokenExpiredError("Token has expired");
43
- }
44
- if (code === "ERR_JWT_CLAIM_VALIDATION_FAILED" ||
45
- code === "ERR_JWS_SIGNATURE_VERIFICATION_FAILED" ||
46
- code === "ERR_JWS_INVALID" ||
47
- code === "ERR_JWT_INVALID") {
48
- throw new exceptions_1.TokenInvalidError(error.message || "Token validation failed");
49
- }
50
- }
51
- throw new exceptions_1.TokenInvalidError("Invalid token");
52
- }
53
- }
54
- }
55
- exports.JWTVerifier = JWTVerifier;
@@ -1,48 +0,0 @@
1
- /**
2
- * Decoded JWT claims found in an Authly token.
3
- *
4
- * This interface contains both standard OIDC claims and Authly-specific claims
5
- * like session ID (sid) and permissions.
6
- */
7
- export interface Claims {
8
- /**
9
- * Subject identifier - the unique ID of the user.
10
- */
11
- sub: string;
12
- /**
13
- * Issuer identifier - the URL of the identity provider.
14
- */
15
- iss: string;
16
- /**
17
- * Audience(s) for which the token is intended.
18
- */
19
- aud: string | string[];
20
- /**
21
- * Expiration time (Unix timestamp).
22
- */
23
- exp: number;
24
- /**
25
- * Issued at time (Unix timestamp).
26
- */
27
- iat: number;
28
- /**
29
- * Session ID identifier.
30
- */
31
- sid: string;
32
- /**
33
- * Dictionary of permissions granted to the user, where keys are resource names and values are permission levels.
34
- */
35
- permissions: Record<string, number>;
36
- /**
37
- * Permission version.
38
- */
39
- pver?: number;
40
- /**
41
- * Space-separated list of scopes.
42
- */
43
- scope?: string;
44
- /**
45
- * Allow additional claims.
46
- */
47
- [key: string]: unknown;
48
- }