@authly/sdk 1.0.1 → 1.1.0

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.
@@ -0,0 +1,3 @@
1
+ export declare const DEFAULT_JWKS_PATH = "/.well-known/jwks.json";
2
+ export declare const DEFAULT_AUTHORIZE_PATH = "/v1/oauth/authorize";
3
+ export declare const DEFAULT_ALGORITHMS: string[];
package/dist/config.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_ALGORITHMS = exports.DEFAULT_JWKS_PATH = void 0;
3
+ exports.DEFAULT_ALGORITHMS = exports.DEFAULT_AUTHORIZE_PATH = exports.DEFAULT_JWKS_PATH = void 0;
4
4
  exports.DEFAULT_JWKS_PATH = "/.well-known/jwks.json";
5
+ exports.DEFAULT_AUTHORIZE_PATH = "/v1/oauth/authorize";
5
6
  exports.DEFAULT_ALGORITHMS = ["RS256"];
@@ -0,0 +1,27 @@
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
+ }
@@ -0,0 +1,92 @@
1
+ import type { Claims } from "../../models/Claims";
2
+ /**
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.
33
+ */
34
+ export interface AuthorizeUrlOptions {
35
+ /**
36
+ * The URI to redirect back to after authentication.
37
+ */
38
+ redirectUri: string;
39
+ /**
40
+ * A unique state string to prevent CSRF.
41
+ */
42
+ state: string;
43
+ /**
44
+ * The PKCE code challenge.
45
+ */
46
+ codeChallenge: string;
47
+ /**
48
+ * The PKCE code challenge method. Defaults to "S256".
49
+ */
50
+ codeChallengeMethod?: "S256" | "plain";
51
+ /**
52
+ * The requested scopes. Defaults to "openid profile email".
53
+ */
54
+ scope?: string;
55
+ /**
56
+ * The OAuth2 response type. Defaults to "code".
57
+ */
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);
73
+ /**
74
+ * Generate the authorization URL to redirect the user to.
75
+ *
76
+ * @param options - Options for generating the URL.
77
+ * @returns The full authorization URL.
78
+ */
79
+ getAuthorizeUrl(options: AuthorizeUrlOptions): string;
80
+ /**
81
+ * Verify a JWT token and return its decoded claims.
82
+ *
83
+ * This method verifies the token's signature using the provider's JWKS,
84
+ * and validates standard claims like expiration, issuer, and audience.
85
+ *
86
+ * @param token - The encoded JWT token string.
87
+ * @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).
90
+ */
91
+ verify(token: string): Promise<Claims>;
92
+ }
@@ -4,19 +4,22 @@ exports.AuthlyClient = void 0;
4
4
  const config_1 = require("../../config");
5
5
  const JWTVerifier_1 = require("./internal/JWTVerifier");
6
6
  /**
7
- * A client for verifying Authly JWT tokens.
7
+ * A client for interacting with Authly.
8
8
  *
9
9
  * This client handles the validation of tokens against a specific issuer and audience,
10
- * fetching the public keys (JWKS) automatically.
10
+ * fetching the public keys (JWKS) automatically, and provides utilities for
11
+ * starting the OAuth2 flow.
11
12
  */
12
13
  class AuthlyClient {
13
14
  verifier;
14
15
  serviceId;
15
16
  issuer;
17
+ authorizePath;
16
18
  constructor(options) {
17
19
  this.issuer = options.issuer.replace(/\/$/, "");
18
20
  this.serviceId = options.serviceId;
19
21
  const jwksPath = options.jwksPath || config_1.DEFAULT_JWKS_PATH;
22
+ this.authorizePath = options.authorizePath || config_1.DEFAULT_AUTHORIZE_PATH;
20
23
  this.verifier = new JWTVerifier_1.JWTVerifier({
21
24
  issuer: this.issuer,
22
25
  audience: options.audience,
@@ -24,6 +27,23 @@ class AuthlyClient {
24
27
  algorithms: options.algorithms,
25
28
  });
26
29
  }
30
+ /**
31
+ * Generate the authorization URL to redirect the user to.
32
+ *
33
+ * @param options - Options for generating the URL.
34
+ * @returns The full authorization URL.
35
+ */
36
+ getAuthorizeUrl(options) {
37
+ const url = new URL(`${this.issuer}${this.authorizePath}`);
38
+ url.searchParams.set("client_id", this.serviceId);
39
+ url.searchParams.set("redirect_uri", options.redirectUri);
40
+ url.searchParams.set("response_type", options.responseType || "code");
41
+ url.searchParams.set("scope", options.scope || "openid profile email");
42
+ url.searchParams.set("state", options.state);
43
+ url.searchParams.set("code_challenge", options.codeChallenge);
44
+ url.searchParams.set("code_challenge_method", options.codeChallengeMethod || "S256");
45
+ return url.toString();
46
+ }
27
47
  /**
28
48
  * Verify a JWT token and return its decoded claims.
29
49
  *
@@ -0,0 +1,50 @@
1
+ import type { IRequestResponseClient } from "../../models/globals/clients/interfaces/IRequestResponseClient";
2
+ /**
3
+ * A class that provides a static method for making HTTP requests.
4
+ */
5
+ declare class HttpClient {
6
+ private constructor();
7
+ /**
8
+ * Makes an HTTP request to the specified URL.
9
+ * @param url - The URL to make the request to.
10
+ * @param options - The options for the request.
11
+ * @returns A promise that resolves to the response data.
12
+ */
13
+ private static request;
14
+ /**
15
+ * Makes a GET request to the specified URL.
16
+ * @param url - The URL to make the request to.
17
+ * @param options - The options for the request.
18
+ * @returns A promise that resolves to the response data.
19
+ */
20
+ static get<D>(url: string, options?: Omit<RequestInit, "method">): Promise<IRequestResponseClient<D>>;
21
+ /**
22
+ * Makes a POST request to the specified URL.
23
+ * @param url - The URL to make the request to.
24
+ * @param options - The options for the request.
25
+ * @returns A promise that resolves to the response data.
26
+ */
27
+ static post<D>(url: string, options?: Omit<RequestInit, "method">): Promise<IRequestResponseClient<D>>;
28
+ /**
29
+ * Makes a PUT request to the specified URL.
30
+ * @param url - The URL to make the request to.
31
+ * @param options - The options for the request.
32
+ * @returns A promise that resolves to the response data.
33
+ */
34
+ static put<D>(url: string, options?: Omit<RequestInit, "method">): Promise<IRequestResponseClient<D>>;
35
+ /**
36
+ * Makes a PATCH request to the specified URL.
37
+ * @param url - The URL to make the request to.
38
+ * @param options - The options for the request.
39
+ * @returns A promise that resolves to the response data.
40
+ */
41
+ static patch<D>(url: string, options?: Omit<RequestInit, "method">): Promise<IRequestResponseClient<D>>;
42
+ /**
43
+ * Makes a DELETE request to the specified URL.
44
+ * @param url - The URL to make the request to.
45
+ * @param options - The options for the request.
46
+ * @returns A promise that resolves to the response data.
47
+ */
48
+ static delete<D>(url: string, options?: Omit<RequestInit, "method">): Promise<IRequestResponseClient<D>>;
49
+ }
50
+ export { HttpClient };
@@ -0,0 +1,26 @@
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
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./models/Claims";
2
+ export * from "./exceptions";
3
+ export * from "./globals/clients/AuthlyClient";
@@ -0,0 +1,48 @@
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
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Represents the error structure from the backend.
3
+ */
4
+ interface APIError {
5
+ /**
6
+ * The error code.
7
+ */
8
+ readonly code: string;
9
+ /**
10
+ * The error message.
11
+ */
12
+ readonly message: string;
13
+ /**
14
+ * Optional error details.
15
+ */
16
+ readonly details?: unknown;
17
+ }
18
+ /**
19
+ * Represents the response from a request.
20
+ * @template D - The type of the data returned from the request.
21
+ */
22
+ type IRequestResponseClient<D> = {
23
+ /**
24
+ * Whether the request was successful.
25
+ */
26
+ readonly success: true;
27
+ /**
28
+ * The data returned from the request.
29
+ */
30
+ readonly data: D;
31
+ /**
32
+ * The message returned from the request.
33
+ */
34
+ readonly message: string;
35
+ } | {
36
+ /**
37
+ * Whether the request was successful.
38
+ */
39
+ readonly success: false;
40
+ /**
41
+ * The error returned from the request.
42
+ */
43
+ readonly error: APIError;
44
+ };
45
+ export type { IRequestResponseClient, APIError };
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.0.1",
4
+ "version": "1.1.0",
5
5
  "author": {
6
6
  "name": "Anvoria",
7
7
  "url": "https://github.com/Anvoria"