@authly/sdk 1.0.0 → 1.0.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.
- package/dist/config.d.ts +2 -0
- package/dist/config.js +5 -0
- package/dist/exceptions/index.d.ts +27 -0
- package/dist/exceptions/index.js +46 -0
- package/dist/globals/clients/AuthlyClient.d.ts +50 -0
- package/dist/globals/clients/AuthlyClient.js +42 -0
- package/dist/globals/clients/HttpClient.d.ts +50 -0
- package/dist/globals/clients/internal/JWTVerifier.d.ts +26 -0
- package/dist/globals/clients/internal/JWTVerifier.js +55 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +17 -0
- package/dist/models/Claims.d.ts +48 -0
- package/dist/models/Claims.js +2 -0
- package/dist/models/globals/clients/interfaces/IRequestResponseClient.d.ts +45 -0
- package/package.json +5 -2
package/dist/config.d.ts
ADDED
package/dist/config.js
ADDED
|
@@ -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,46 @@
|
|
|
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;
|
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
* A list of allowed signing algorithms. If omitted, defaults to ["RS256"].
|
|
24
|
+
*/
|
|
25
|
+
algorithms?: string[];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A client for verifying Authly JWT tokens.
|
|
29
|
+
*
|
|
30
|
+
* This client handles the validation of tokens against a specific issuer and audience,
|
|
31
|
+
* fetching the public keys (JWKS) automatically.
|
|
32
|
+
*/
|
|
33
|
+
export declare class AuthlyClient {
|
|
34
|
+
private readonly verifier;
|
|
35
|
+
private readonly serviceId;
|
|
36
|
+
private readonly issuer;
|
|
37
|
+
constructor(options: AuthlyClientOptions);
|
|
38
|
+
/**
|
|
39
|
+
* Verify a JWT token and return its decoded claims.
|
|
40
|
+
*
|
|
41
|
+
* This method verifies the token's signature using the provider's JWKS,
|
|
42
|
+
* and validates standard claims like expiration, issuer, and audience.
|
|
43
|
+
*
|
|
44
|
+
* @param token - The encoded JWT token string.
|
|
45
|
+
* @returns A promise that resolves to the token claims (e.g., sub, iss, aud).
|
|
46
|
+
* @throws {TokenExpiredError} If the token has expired.
|
|
47
|
+
* @throws {TokenInvalidError} If the token is invalid (e.g., bad signature, invalid audience).
|
|
48
|
+
*/
|
|
49
|
+
verify(token: string): Promise<Claims>;
|
|
50
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthlyClient = void 0;
|
|
4
|
+
const config_1 = require("../../config");
|
|
5
|
+
const JWTVerifier_1 = require("./internal/JWTVerifier");
|
|
6
|
+
/**
|
|
7
|
+
* A client for verifying Authly JWT tokens.
|
|
8
|
+
*
|
|
9
|
+
* This client handles the validation of tokens against a specific issuer and audience,
|
|
10
|
+
* fetching the public keys (JWKS) automatically.
|
|
11
|
+
*/
|
|
12
|
+
class AuthlyClient {
|
|
13
|
+
verifier;
|
|
14
|
+
serviceId;
|
|
15
|
+
issuer;
|
|
16
|
+
constructor(options) {
|
|
17
|
+
this.issuer = options.issuer.replace(/\/$/, "");
|
|
18
|
+
this.serviceId = options.serviceId;
|
|
19
|
+
const jwksPath = options.jwksPath || config_1.DEFAULT_JWKS_PATH;
|
|
20
|
+
this.verifier = new JWTVerifier_1.JWTVerifier({
|
|
21
|
+
issuer: this.issuer,
|
|
22
|
+
audience: options.audience,
|
|
23
|
+
jwksUrl: `${this.issuer}${jwksPath}`,
|
|
24
|
+
algorithms: options.algorithms,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Verify a JWT token and return its decoded claims.
|
|
29
|
+
*
|
|
30
|
+
* This method verifies the token's signature using the provider's JWKS,
|
|
31
|
+
* and validates standard claims like expiration, issuer, and audience.
|
|
32
|
+
*
|
|
33
|
+
* @param token - The encoded JWT token string.
|
|
34
|
+
* @returns A promise that resolves to the token claims (e.g., sub, iss, aud).
|
|
35
|
+
* @throws {TokenExpiredError} If the token has expired.
|
|
36
|
+
* @throws {TokenInvalidError} If the token is invalid (e.g., bad signature, invalid audience).
|
|
37
|
+
*/
|
|
38
|
+
async verify(token) {
|
|
39
|
+
return this.verifier.verify(token);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.AuthlyClient = AuthlyClient;
|
|
@@ -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,55 @@
|
|
|
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;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
CHANGED
|
@@ -1,2 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./models/Claims"), exports);
|
|
18
|
+
__exportStar(require("./exceptions"), exports);
|
|
19
|
+
__exportStar(require("./globals/clients/AuthlyClient"), exports);
|
|
@@ -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.
|
|
4
|
+
"version": "1.0.2",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Anvoria",
|
|
7
7
|
"url": "https://github.com/Anvoria"
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
|
-
"url": "https://github.com/Anvoria/authly-sdk-ts.git"
|
|
12
|
+
"url": "git+https://github.com/Anvoria/authly-sdk-ts.git"
|
|
13
13
|
},
|
|
14
14
|
"homepage": "https://github.com/Anvoria/authly-sdk-ts",
|
|
15
15
|
"bugs": {
|
|
@@ -67,5 +67,8 @@
|
|
|
67
67
|
"tsx": "^4.21.0",
|
|
68
68
|
"typescript": "^5.9.3",
|
|
69
69
|
"typescript-eslint": "^8.50.0"
|
|
70
|
+
},
|
|
71
|
+
"dependencies": {
|
|
72
|
+
"jose": "^6.1.3"
|
|
70
73
|
}
|
|
71
74
|
}
|