@authly/sdk 1.2.1 → 1.2.3
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,19 +20,23 @@ 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 (for validation).
|
|
24
24
|
*/
|
|
25
25
|
private readonly issuer;
|
|
26
26
|
/**
|
|
27
|
-
* @summary The
|
|
27
|
+
* @summary The base URL for API requests.
|
|
28
|
+
*/
|
|
29
|
+
private readonly baseUrl;
|
|
30
|
+
/**
|
|
31
|
+
* @summary The authorize path.
|
|
28
32
|
*/
|
|
29
33
|
private readonly authorizePath;
|
|
30
34
|
/**
|
|
31
|
-
* @summary The token path
|
|
35
|
+
* @summary The token path.
|
|
32
36
|
*/
|
|
33
37
|
private readonly tokenPath;
|
|
34
38
|
/**
|
|
35
|
-
* @summary The user info path
|
|
39
|
+
* @summary The user info path.
|
|
36
40
|
*/
|
|
37
41
|
private readonly userInfoPath;
|
|
38
42
|
/**
|
|
@@ -23,19 +23,23 @@ class AuthlyClient {
|
|
|
23
23
|
*/
|
|
24
24
|
serviceId;
|
|
25
25
|
/**
|
|
26
|
-
* @summary The issuer of the client.
|
|
26
|
+
* @summary The issuer of the client (for validation).
|
|
27
27
|
*/
|
|
28
28
|
issuer;
|
|
29
29
|
/**
|
|
30
|
-
* @summary The
|
|
30
|
+
* @summary The base URL for API requests.
|
|
31
|
+
*/
|
|
32
|
+
baseUrl;
|
|
33
|
+
/**
|
|
34
|
+
* @summary The authorize path.
|
|
31
35
|
*/
|
|
32
36
|
authorizePath;
|
|
33
37
|
/**
|
|
34
|
-
* @summary The token path
|
|
38
|
+
* @summary The token path.
|
|
35
39
|
*/
|
|
36
40
|
tokenPath;
|
|
37
41
|
/**
|
|
38
|
-
* @summary The user info path
|
|
42
|
+
* @summary The user info path.
|
|
39
43
|
*/
|
|
40
44
|
userInfoPath;
|
|
41
45
|
/**
|
|
@@ -56,6 +60,8 @@ class AuthlyClient {
|
|
|
56
60
|
*/
|
|
57
61
|
constructor(options) {
|
|
58
62
|
this.issuer = options.issuer.replace(/\/$/, "");
|
|
63
|
+
// Use baseUrl if provided, otherwise fallback to issuer
|
|
64
|
+
this.baseUrl = (options.baseUrl || this.issuer).replace(/\/$/, "");
|
|
59
65
|
this.serviceId = options.serviceId;
|
|
60
66
|
this.authorizePath = options.authorizePath || AuthlyConfiguration_1.AuthlyConfiguration.DEFAULT_AUTHORIZE_PATH;
|
|
61
67
|
this.tokenPath = options.tokenPath || AuthlyConfiguration_1.AuthlyConfiguration.DEFAULT_TOKEN_PATH;
|
|
@@ -68,7 +74,7 @@ class AuthlyClient {
|
|
|
68
74
|
this.verifier = new JWTVerifier_1.JWTVerifier({
|
|
69
75
|
issuer: this.issuer,
|
|
70
76
|
audience: options.audience,
|
|
71
|
-
jwksUrl: `${this.
|
|
77
|
+
jwksUrl: `${this.baseUrl}${options.jwksPath || AuthlyConfiguration_1.AuthlyConfiguration.DEFAULT_JWKS_PATH}`,
|
|
72
78
|
algorithms: options.algorithms,
|
|
73
79
|
});
|
|
74
80
|
}
|
|
@@ -103,7 +109,7 @@ class AuthlyClient {
|
|
|
103
109
|
* @returns The full authorization URL.
|
|
104
110
|
*/
|
|
105
111
|
getAuthorizeUrl(options) {
|
|
106
|
-
const url = new URL(`${this.
|
|
112
|
+
const url = new URL(`${this.baseUrl}${this.authorizePath}`);
|
|
107
113
|
url.searchParams.set("client_id", this.serviceId);
|
|
108
114
|
url.searchParams.set("redirect_uri", options.redirectUri);
|
|
109
115
|
url.searchParams.set("response_type", options.responseType || "code");
|
|
@@ -174,7 +180,7 @@ class AuthlyClient {
|
|
|
174
180
|
* @returns A promise that resolves to the new access token.
|
|
175
181
|
*/
|
|
176
182
|
async refreshToken(refreshToken) {
|
|
177
|
-
const url = `${this.
|
|
183
|
+
const url = `${this.baseUrl}${this.tokenPath}`;
|
|
178
184
|
const body = {
|
|
179
185
|
grant_type: "refresh_token",
|
|
180
186
|
client_id: this.serviceId,
|
|
@@ -204,7 +210,7 @@ class AuthlyClient {
|
|
|
204
210
|
if (!token)
|
|
205
211
|
return null;
|
|
206
212
|
const fetchInfo = async (currentBuffer) => {
|
|
207
|
-
return HttpClient_1.HttpClient.get(`${this.
|
|
213
|
+
return HttpClient_1.HttpClient.get(`${this.baseUrl}${this.userInfoPath}`, {
|
|
208
214
|
headers: {
|
|
209
215
|
Authorization: `Bearer ${currentBuffer}`,
|
|
210
216
|
},
|
|
@@ -239,6 +245,7 @@ class AuthlyClient {
|
|
|
239
245
|
const decoded = (0, jose_1.decodeJwt)(token);
|
|
240
246
|
if (!decoded.exp)
|
|
241
247
|
return true;
|
|
248
|
+
// Check if token is expired with a 10s buffer
|
|
242
249
|
const now = Math.floor(Date.now() / 1000);
|
|
243
250
|
return decoded.exp > now + 10;
|
|
244
251
|
}
|
|
@@ -263,7 +270,7 @@ class AuthlyClient {
|
|
|
263
270
|
* @returns A promise that resolves to the token response.
|
|
264
271
|
*/
|
|
265
272
|
async exchangeCodeForToken(code, redirectUri, codeVerifier) {
|
|
266
|
-
const url = `${this.
|
|
273
|
+
const url = `${this.baseUrl}${this.tokenPath}`;
|
|
267
274
|
const body = {
|
|
268
275
|
grant_type: "authorization_code",
|
|
269
276
|
client_id: this.serviceId,
|
|
@@ -5,9 +5,16 @@ 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 baseUrl is not provided, it is also used for API requests.
|
|
8
9
|
* @example "https://auth.example.com"
|
|
9
10
|
*/
|
|
10
11
|
readonly issuer: string;
|
|
12
|
+
/**
|
|
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 (e.g. localhost vs production domain).
|
|
15
|
+
* @example "http://localhost:8000"
|
|
16
|
+
*/
|
|
17
|
+
readonly baseUrl?: string;
|
|
11
18
|
/**
|
|
12
19
|
* @summary The expected audience claim (aud) in the token.
|
|
13
20
|
* @example "https://app.example.com"
|
|
@@ -29,25 +36,25 @@ interface IAuthlyClientOptions {
|
|
|
29
36
|
*/
|
|
30
37
|
readonly storage?: IAuthlyStorage;
|
|
31
38
|
/**
|
|
32
|
-
* @summary The path to the JWKS endpoint relative to the
|
|
39
|
+
* @summary The path to the JWKS endpoint relative to the baseUrl.
|
|
33
40
|
* @example "/.well-known/jwks.json"
|
|
34
41
|
* @default "/.well-known/jwks.json"
|
|
35
42
|
*/
|
|
36
43
|
readonly jwksPath?: string;
|
|
37
44
|
/**
|
|
38
|
-
* @summary The path to the authorize endpoint relative to the
|
|
45
|
+
* @summary The path to the authorize endpoint relative to the baseUrl.
|
|
39
46
|
* @example "/v1/oauth/authorize"
|
|
40
|
-
* @default "/authorize"
|
|
47
|
+
* @default "/v1/oauth/authorize"
|
|
41
48
|
*/
|
|
42
49
|
readonly authorizePath?: string;
|
|
43
50
|
/**
|
|
44
|
-
* @summary The path to the token endpoint relative to the
|
|
51
|
+
* @summary The path to the token endpoint relative to the baseUrl.
|
|
45
52
|
* @example "/v1/oauth/token"
|
|
46
|
-
* @default "/oauth/token"
|
|
53
|
+
* @default "/v1/oauth/token"
|
|
47
54
|
*/
|
|
48
55
|
readonly tokenPath?: string;
|
|
49
56
|
/**
|
|
50
|
-
* @summary The path to the user info endpoint relative to the
|
|
57
|
+
* @summary The path to the user info endpoint relative to the baseUrl.
|
|
51
58
|
* @example "/v1/oauth/userinfo"
|
|
52
59
|
* @default "/v1/oauth/userinfo"
|
|
53
60
|
*/
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
"use client";
|
|
2
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
4
|
exports.AuthlyCallback = void 0;
|
|
4
5
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
@@ -15,11 +16,15 @@ const AuthlyCallback = ({ onSuccess = "/", onFailure = "/login", navigate }) =>
|
|
|
15
16
|
const params = new URLSearchParams(window.location.search);
|
|
16
17
|
const code = params.get("code");
|
|
17
18
|
const state = params.get("state");
|
|
19
|
+
console.log("[AuthlyCallback] Processing callback...", { code: !!code, state: !!state });
|
|
18
20
|
if (!code || !state) {
|
|
21
|
+
console.warn("[AuthlyCallback] Missing code or state parameters.");
|
|
19
22
|
return;
|
|
20
23
|
}
|
|
21
24
|
try {
|
|
25
|
+
console.log("[AuthlyCallback] Exchanging token...");
|
|
22
26
|
await client.exchangeToken(params);
|
|
27
|
+
console.log("[AuthlyCallback] Token exchanged successfully.");
|
|
23
28
|
await refresh();
|
|
24
29
|
if (navigate) {
|
|
25
30
|
navigate(onSuccess);
|