@authly/sdk 1.2.2 → 1.2.4

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.
@@ -24,17 +24,17 @@ declare class AuthlyClient {
24
24
  */
25
25
  private readonly issuer;
26
26
  /**
27
- * @summary The authorize path of the client.
27
+ * @summary The resolved authorize endpoint URL.
28
28
  */
29
- private readonly authorizePath;
29
+ private readonly authorizeEndpoint;
30
30
  /**
31
- * @summary The token path of the client.
31
+ * @summary The resolved token endpoint URL.
32
32
  */
33
- private readonly tokenPath;
33
+ private readonly tokenEndpoint;
34
34
  /**
35
- * @summary The user info path of the client.
35
+ * @summary The resolved user info endpoint URL.
36
36
  */
37
- private readonly userInfoPath;
37
+ private readonly userInfoEndpoint;
38
38
  /**
39
39
  * @summary The redirect URI for the client.
40
40
  */
@@ -52,6 +52,12 @@ declare class AuthlyClient {
52
52
  * @param options - The options for the client.
53
53
  */
54
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;
55
61
  /**
56
62
  * @summary Prepares the authorization request, stores PKCE state, and returns the URL.
57
63
  * @param options - Optional overrides for the authorization request.
@@ -27,17 +27,17 @@ class AuthlyClient {
27
27
  */
28
28
  issuer;
29
29
  /**
30
- * @summary The authorize path of the client.
30
+ * @summary The resolved authorize endpoint URL.
31
31
  */
32
- authorizePath;
32
+ authorizeEndpoint;
33
33
  /**
34
- * @summary The token path of the client.
34
+ * @summary The resolved token endpoint URL.
35
35
  */
36
- tokenPath;
36
+ tokenEndpoint;
37
37
  /**
38
- * @summary The user info path of the client.
38
+ * @summary The resolved user info endpoint URL.
39
39
  */
40
- userInfoPath;
40
+ userInfoEndpoint;
41
41
  /**
42
42
  * @summary The redirect URI for the client.
43
43
  */
@@ -57,21 +57,33 @@ class AuthlyClient {
57
57
  constructor(options) {
58
58
  this.issuer = options.issuer.replace(/\/$/, "");
59
59
  this.serviceId = options.serviceId;
60
- this.authorizePath = options.authorizePath || AuthlyConfiguration_1.AuthlyConfiguration.DEFAULT_AUTHORIZE_PATH;
61
- this.tokenPath = options.tokenPath || AuthlyConfiguration_1.AuthlyConfiguration.DEFAULT_TOKEN_PATH;
62
- this.userInfoPath = options.userInfoPath || AuthlyConfiguration_1.AuthlyConfiguration.DEFAULT_USER_INFO_PATH;
63
60
  this.redirectUri = options.redirectUri;
64
61
  this.storage = options.storage;
65
62
  if (!this.storage && typeof window !== "undefined" && window.sessionStorage) {
66
63
  this.storage = window.sessionStorage;
67
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);
68
69
  this.verifier = new JWTVerifier_1.JWTVerifier({
69
70
  issuer: this.issuer,
70
71
  audience: options.audience,
71
- jwksUrl: `${this.issuer}${options.jwksPath || AuthlyConfiguration_1.AuthlyConfiguration.DEFAULT_JWKS_PATH}`,
72
+ jwksUrl: jwksUrl,
72
73
  algorithms: options.algorithms,
73
74
  });
74
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
+ }
75
87
  /**
76
88
  * @summary Prepares the authorization request, stores PKCE state, and returns the URL.
77
89
  * @param options - Optional overrides for the authorization request.
@@ -103,7 +115,7 @@ class AuthlyClient {
103
115
  * @returns The full authorization URL.
104
116
  */
105
117
  getAuthorizeUrl(options) {
106
- const url = new URL(`${this.issuer}${this.authorizePath}`);
118
+ const url = new URL(this.authorizeEndpoint);
107
119
  url.searchParams.set("client_id", this.serviceId);
108
120
  url.searchParams.set("redirect_uri", options.redirectUri);
109
121
  url.searchParams.set("response_type", options.responseType || "code");
@@ -174,7 +186,7 @@ class AuthlyClient {
174
186
  * @returns A promise that resolves to the new access token.
175
187
  */
176
188
  async refreshToken(refreshToken) {
177
- const url = `${this.issuer}${this.tokenPath}`;
189
+ const url = this.tokenEndpoint;
178
190
  const body = {
179
191
  grant_type: "refresh_token",
180
192
  client_id: this.serviceId,
@@ -204,7 +216,7 @@ class AuthlyClient {
204
216
  if (!token)
205
217
  return null;
206
218
  const fetchInfo = async (currentBuffer) => {
207
- return HttpClient_1.HttpClient.get(`${this.issuer}${this.userInfoPath}`, {
219
+ return HttpClient_1.HttpClient.get(this.userInfoEndpoint, {
208
220
  headers: {
209
221
  Authorization: `Bearer ${currentBuffer}`,
210
222
  },
@@ -239,6 +251,7 @@ class AuthlyClient {
239
251
  const decoded = (0, jose_1.decodeJwt)(token);
240
252
  if (!decoded.exp)
241
253
  return true;
254
+ // Check if token is expired with a 10s buffer
242
255
  const now = Math.floor(Date.now() / 1000);
243
256
  return decoded.exp > now + 10;
244
257
  }
@@ -263,7 +276,7 @@ class AuthlyClient {
263
276
  * @returns A promise that resolves to the token response.
264
277
  */
265
278
  async exchangeCodeForToken(code, redirectUri, codeVerifier) {
266
- const url = `${this.issuer}${this.tokenPath}`;
279
+ const url = this.tokenEndpoint;
267
280
  const body = {
268
281
  grant_type: "authorization_code",
269
282
  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 paths are relative, they are appended to this URL (or baseUrl if provided).
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.
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,26 +36,26 @@ interface IAuthlyClientOptions {
29
36
  */
30
37
  readonly storage?: IAuthlyStorage;
31
38
  /**
32
- * @summary The path to the JWKS endpoint relative to the issuer.
33
- * @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"
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 issuer.
39
- * @example "/v1/oauth/authorize"
45
+ * @summary The path or full URL to the authorize endpoint.
46
+ * @example "/authorize" or "http://localhost:3000/login"
40
47
  * @default "/authorize"
41
48
  */
42
49
  readonly authorizePath?: string;
43
50
  /**
44
- * @summary The path to the token endpoint relative to the issuer.
45
- * @example "/v1/oauth/token"
46
- * @default "/oauth/token"
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"
47
54
  */
48
55
  readonly tokenPath?: string;
49
56
  /**
50
- * @summary The path to the user info endpoint relative to the issuer.
51
- * @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"
52
59
  * @default "/v1/oauth/userinfo"
53
60
  */
54
61
  readonly userInfoPath?: string;
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.2.2",
4
+ "version": "1.2.4",
5
5
  "author": {
6
6
  "name": "Anvoria",
7
7
  "url": "https://github.com/Anvoria"