@authly/sdk 1.2.3 → 1.2.5
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.
|
@@ -20,25 +20,21 @@ declare class AuthlyClient {
|
|
|
20
20
|
*/
|
|
21
21
|
private readonly serviceId;
|
|
22
22
|
/**
|
|
23
|
-
* @summary The issuer of the client
|
|
23
|
+
* @summary The issuer of the client.
|
|
24
24
|
*/
|
|
25
25
|
private readonly issuer;
|
|
26
26
|
/**
|
|
27
|
-
* @summary The
|
|
27
|
+
* @summary The resolved authorize endpoint URL.
|
|
28
28
|
*/
|
|
29
|
-
private readonly
|
|
29
|
+
private readonly authorizeEndpoint;
|
|
30
30
|
/**
|
|
31
|
-
* @summary The
|
|
31
|
+
* @summary The resolved token endpoint URL.
|
|
32
32
|
*/
|
|
33
|
-
private readonly
|
|
33
|
+
private readonly tokenEndpoint;
|
|
34
34
|
/**
|
|
35
|
-
* @summary The
|
|
35
|
+
* @summary The resolved user info endpoint URL.
|
|
36
36
|
*/
|
|
37
|
-
private readonly
|
|
38
|
-
/**
|
|
39
|
-
* @summary The user info path.
|
|
40
|
-
*/
|
|
41
|
-
private readonly userInfoPath;
|
|
37
|
+
private readonly userInfoEndpoint;
|
|
42
38
|
/**
|
|
43
39
|
* @summary The redirect URI for the client.
|
|
44
40
|
*/
|
|
@@ -56,6 +52,12 @@ declare class AuthlyClient {
|
|
|
56
52
|
* @param options - The options for the client.
|
|
57
53
|
*/
|
|
58
54
|
constructor(options: IAuthlyClientOptions);
|
|
55
|
+
/**
|
|
56
|
+
* @summary Resolves a path or full URL.
|
|
57
|
+
* @param pathOrUrl - The relative path or absolute URL.
|
|
58
|
+
* @returns The full URL.
|
|
59
|
+
*/
|
|
60
|
+
private resolveUrl;
|
|
59
61
|
/**
|
|
60
62
|
* @summary Prepares the authorization request, stores PKCE state, and returns the URL.
|
|
61
63
|
* @param options - Optional overrides for the authorization request.
|
|
@@ -23,25 +23,21 @@ class AuthlyClient {
|
|
|
23
23
|
*/
|
|
24
24
|
serviceId;
|
|
25
25
|
/**
|
|
26
|
-
* @summary The issuer of the client
|
|
26
|
+
* @summary The issuer of the client.
|
|
27
27
|
*/
|
|
28
28
|
issuer;
|
|
29
29
|
/**
|
|
30
|
-
* @summary The
|
|
30
|
+
* @summary The resolved authorize endpoint URL.
|
|
31
31
|
*/
|
|
32
|
-
|
|
32
|
+
authorizeEndpoint;
|
|
33
33
|
/**
|
|
34
|
-
* @summary The
|
|
34
|
+
* @summary The resolved token endpoint URL.
|
|
35
35
|
*/
|
|
36
|
-
|
|
36
|
+
tokenEndpoint;
|
|
37
37
|
/**
|
|
38
|
-
* @summary The
|
|
38
|
+
* @summary The resolved user info endpoint URL.
|
|
39
39
|
*/
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @summary The user info path.
|
|
43
|
-
*/
|
|
44
|
-
userInfoPath;
|
|
40
|
+
userInfoEndpoint;
|
|
45
41
|
/**
|
|
46
42
|
* @summary The redirect URI for the client.
|
|
47
43
|
*/
|
|
@@ -60,24 +56,34 @@ class AuthlyClient {
|
|
|
60
56
|
*/
|
|
61
57
|
constructor(options) {
|
|
62
58
|
this.issuer = options.issuer.replace(/\/$/, "");
|
|
63
|
-
// Use baseUrl if provided, otherwise fallback to issuer
|
|
64
|
-
this.baseUrl = (options.baseUrl || this.issuer).replace(/\/$/, "");
|
|
65
59
|
this.serviceId = options.serviceId;
|
|
66
|
-
this.authorizePath = options.authorizePath || AuthlyConfiguration_1.AuthlyConfiguration.DEFAULT_AUTHORIZE_PATH;
|
|
67
|
-
this.tokenPath = options.tokenPath || AuthlyConfiguration_1.AuthlyConfiguration.DEFAULT_TOKEN_PATH;
|
|
68
|
-
this.userInfoPath = options.userInfoPath || AuthlyConfiguration_1.AuthlyConfiguration.DEFAULT_USER_INFO_PATH;
|
|
69
60
|
this.redirectUri = options.redirectUri;
|
|
70
61
|
this.storage = options.storage;
|
|
71
62
|
if (!this.storage && typeof window !== "undefined" && window.sessionStorage) {
|
|
72
63
|
this.storage = window.sessionStorage;
|
|
73
64
|
}
|
|
65
|
+
this.authorizeEndpoint = this.resolveUrl(options.authorizePath || AuthlyConfiguration_1.AuthlyConfiguration.DEFAULT_AUTHORIZE_PATH);
|
|
66
|
+
this.tokenEndpoint = this.resolveUrl(options.tokenPath || AuthlyConfiguration_1.AuthlyConfiguration.DEFAULT_TOKEN_PATH);
|
|
67
|
+
this.userInfoEndpoint = this.resolveUrl(options.userInfoPath || AuthlyConfiguration_1.AuthlyConfiguration.DEFAULT_USER_INFO_PATH);
|
|
68
|
+
const jwksUrl = this.resolveUrl(options.jwksPath || AuthlyConfiguration_1.AuthlyConfiguration.DEFAULT_JWKS_PATH);
|
|
74
69
|
this.verifier = new JWTVerifier_1.JWTVerifier({
|
|
75
70
|
issuer: this.issuer,
|
|
76
71
|
audience: options.audience,
|
|
77
|
-
jwksUrl:
|
|
72
|
+
jwksUrl: jwksUrl,
|
|
78
73
|
algorithms: options.algorithms,
|
|
79
74
|
});
|
|
80
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* @summary Resolves a path or full URL.
|
|
78
|
+
* @param pathOrUrl - The relative path or absolute URL.
|
|
79
|
+
* @returns The full URL.
|
|
80
|
+
*/
|
|
81
|
+
resolveUrl(pathOrUrl) {
|
|
82
|
+
if (pathOrUrl.startsWith("http://") || pathOrUrl.startsWith("https://")) {
|
|
83
|
+
return pathOrUrl;
|
|
84
|
+
}
|
|
85
|
+
return `${this.issuer}${pathOrUrl}`;
|
|
86
|
+
}
|
|
81
87
|
/**
|
|
82
88
|
* @summary Prepares the authorization request, stores PKCE state, and returns the URL.
|
|
83
89
|
* @param options - Optional overrides for the authorization request.
|
|
@@ -109,7 +115,7 @@ class AuthlyClient {
|
|
|
109
115
|
* @returns The full authorization URL.
|
|
110
116
|
*/
|
|
111
117
|
getAuthorizeUrl(options) {
|
|
112
|
-
const url = new URL(
|
|
118
|
+
const url = new URL(this.authorizeEndpoint);
|
|
113
119
|
url.searchParams.set("client_id", this.serviceId);
|
|
114
120
|
url.searchParams.set("redirect_uri", options.redirectUri);
|
|
115
121
|
url.searchParams.set("response_type", options.responseType || "code");
|
|
@@ -180,7 +186,7 @@ class AuthlyClient {
|
|
|
180
186
|
* @returns A promise that resolves to the new access token.
|
|
181
187
|
*/
|
|
182
188
|
async refreshToken(refreshToken) {
|
|
183
|
-
const url =
|
|
189
|
+
const url = this.tokenEndpoint;
|
|
184
190
|
const body = {
|
|
185
191
|
grant_type: "refresh_token",
|
|
186
192
|
client_id: this.serviceId,
|
|
@@ -193,6 +199,7 @@ class AuthlyClient {
|
|
|
193
199
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
194
200
|
},
|
|
195
201
|
body: new URLSearchParams(body).toString(),
|
|
202
|
+
credentials: "include",
|
|
196
203
|
});
|
|
197
204
|
if (!response.success) {
|
|
198
205
|
return null;
|
|
@@ -210,10 +217,11 @@ class AuthlyClient {
|
|
|
210
217
|
if (!token)
|
|
211
218
|
return null;
|
|
212
219
|
const fetchInfo = async (currentBuffer) => {
|
|
213
|
-
return HttpClient_1.HttpClient.get(
|
|
220
|
+
return HttpClient_1.HttpClient.get(this.userInfoEndpoint, {
|
|
214
221
|
headers: {
|
|
215
222
|
Authorization: `Bearer ${currentBuffer}`,
|
|
216
223
|
},
|
|
224
|
+
credentials: "include",
|
|
217
225
|
});
|
|
218
226
|
};
|
|
219
227
|
let response = await fetchInfo(token);
|
|
@@ -270,7 +278,7 @@ class AuthlyClient {
|
|
|
270
278
|
* @returns A promise that resolves to the token response.
|
|
271
279
|
*/
|
|
272
280
|
async exchangeCodeForToken(code, redirectUri, codeVerifier) {
|
|
273
|
-
const url =
|
|
281
|
+
const url = this.tokenEndpoint;
|
|
274
282
|
const body = {
|
|
275
283
|
grant_type: "authorization_code",
|
|
276
284
|
client_id: this.serviceId,
|
|
@@ -285,6 +293,7 @@ class AuthlyClient {
|
|
|
285
293
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
286
294
|
},
|
|
287
295
|
body: new URLSearchParams(body).toString(),
|
|
296
|
+
credentials: "include",
|
|
288
297
|
});
|
|
289
298
|
if (!response.success) {
|
|
290
299
|
throw new Error(response.error?.message || "Failed to exchange code for token");
|
|
@@ -5,13 +5,13 @@ import type { IAuthlyStorage } from "./IAuthlyStorage";
|
|
|
5
5
|
interface IAuthlyClientOptions {
|
|
6
6
|
/**
|
|
7
7
|
* @summary The base URL of the identity provider.
|
|
8
|
-
* @description Used for token validation (iss claim). If
|
|
8
|
+
* @description Used for token validation (iss claim). If paths are relative, they are appended to this URL (or baseUrl if provided).
|
|
9
9
|
* @example "https://auth.example.com"
|
|
10
10
|
*/
|
|
11
11
|
readonly issuer: string;
|
|
12
12
|
/**
|
|
13
13
|
* @summary The base URL for API requests (optional).
|
|
14
|
-
* @description Use this if your API is hosted on a different URL than the issuer
|
|
14
|
+
* @description Use this if your API is hosted on a different URL than the issuer.
|
|
15
15
|
* @example "http://localhost:8000"
|
|
16
16
|
*/
|
|
17
17
|
readonly baseUrl?: string;
|
|
@@ -36,26 +36,26 @@ interface IAuthlyClientOptions {
|
|
|
36
36
|
*/
|
|
37
37
|
readonly storage?: IAuthlyStorage;
|
|
38
38
|
/**
|
|
39
|
-
* @summary The path to the JWKS endpoint
|
|
40
|
-
* @example "/.well-known/jwks.json"
|
|
39
|
+
* @summary The path or full URL to the JWKS endpoint.
|
|
40
|
+
* @example "/.well-known/jwks.json" or "http://localhost:8000/.well-known/jwks.json"
|
|
41
41
|
* @default "/.well-known/jwks.json"
|
|
42
42
|
*/
|
|
43
43
|
readonly jwksPath?: string;
|
|
44
44
|
/**
|
|
45
|
-
* @summary The path to the authorize endpoint
|
|
46
|
-
* @example "/
|
|
47
|
-
* @default "/
|
|
45
|
+
* @summary The path or full URL to the authorize endpoint.
|
|
46
|
+
* @example "/authorize" or "http://localhost:3000/login"
|
|
47
|
+
* @default "/authorize"
|
|
48
48
|
*/
|
|
49
49
|
readonly authorizePath?: string;
|
|
50
50
|
/**
|
|
51
|
-
* @summary The path to the token endpoint
|
|
52
|
-
* @example "/
|
|
53
|
-
* @default "/
|
|
51
|
+
* @summary The path or full URL to the token endpoint.
|
|
52
|
+
* @example "/oauth/token" or "http://localhost:8000/oauth/token"
|
|
53
|
+
* @default "/oauth2/token"
|
|
54
54
|
*/
|
|
55
55
|
readonly tokenPath?: string;
|
|
56
56
|
/**
|
|
57
|
-
* @summary The path to the user info endpoint
|
|
58
|
-
* @example "/v1/oauth/userinfo"
|
|
57
|
+
* @summary The path or full URL to the user info endpoint.
|
|
58
|
+
* @example "/v1/oauth/userinfo" or "http://localhost:8000/v1/oauth/userinfo"
|
|
59
59
|
* @default "/v1/oauth/userinfo"
|
|
60
60
|
*/
|
|
61
61
|
readonly userInfoPath?: string;
|