@logto/js 4.0.0-alpha.1 → 4.0.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.
@@ -39,6 +39,7 @@ exports.QueryKey = void 0;
39
39
  /** The prompt parameter to be used for the authorization request. */
40
40
  exports.Prompt = void 0;
41
41
  (function (Prompt) {
42
+ Prompt["None"] = "none";
42
43
  /**
43
44
  * The Authorization Server MUST prompt the End-User for consent
44
45
  * before returning information to the Client.
@@ -34,6 +34,7 @@ export declare enum QueryKey {
34
34
  }
35
35
  /** The prompt parameter to be used for the authorization request. */
36
36
  export declare enum Prompt {
37
+ None = "none",
37
38
  /**
38
39
  * The Authorization Server MUST prompt the End-User for consent
39
40
  * before returning information to the Client.
@@ -37,6 +37,7 @@ var QueryKey;
37
37
  /** The prompt parameter to be used for the authorization request. */
38
38
  var Prompt;
39
39
  (function (Prompt) {
40
+ Prompt["None"] = "none";
40
41
  /**
41
42
  * The Authorization Server MUST prompt the End-User for consent
42
43
  * before returning information to the Client.
@@ -5,6 +5,12 @@ var scopes = require('../utils/scopes.cjs');
5
5
 
6
6
  const codeChallengeMethod = 'S256';
7
7
  const responseType = 'code';
8
+ const buildPrompt = (prompt) => {
9
+ if (Array.isArray(prompt)) {
10
+ return prompt.join(' ');
11
+ }
12
+ return prompt ?? index.Prompt.Consent;
13
+ };
8
14
  const generateSignInUri = ({ authorizationEndpoint, clientId, redirectUri, codeChallenge, state, scopes: scopes$1, resources, prompt, interactionMode, }) => {
9
15
  const urlSearchParameters = new URLSearchParams({
10
16
  [index.QueryKey.ClientId]: clientId,
@@ -13,7 +19,7 @@ const generateSignInUri = ({ authorizationEndpoint, clientId, redirectUri, codeC
13
19
  [index.QueryKey.CodeChallengeMethod]: codeChallengeMethod,
14
20
  [index.QueryKey.State]: state,
15
21
  [index.QueryKey.ResponseType]: responseType,
16
- [index.QueryKey.Prompt]: prompt ?? index.Prompt.Consent,
22
+ [index.QueryKey.Prompt]: buildPrompt(prompt),
17
23
  [index.QueryKey.Scope]: scopes.withDefaultScopes(scopes$1),
18
24
  });
19
25
  for (const resource of resources ?? []) {
@@ -8,7 +8,7 @@ export type SignInUriParameters = {
8
8
  state: string;
9
9
  scopes?: string[];
10
10
  resources?: string[];
11
- prompt?: Prompt;
11
+ prompt?: Prompt | Prompt[];
12
12
  interactionMode?: InteractionMode;
13
13
  };
14
14
  export declare const generateSignInUri: ({ authorizationEndpoint, clientId, redirectUri, codeChallenge, state, scopes, resources, prompt, interactionMode, }: SignInUriParameters) => string;
@@ -3,6 +3,12 @@ import { withDefaultScopes } from '../utils/scopes.js';
3
3
 
4
4
  const codeChallengeMethod = 'S256';
5
5
  const responseType = 'code';
6
+ const buildPrompt = (prompt) => {
7
+ if (Array.isArray(prompt)) {
8
+ return prompt.join(' ');
9
+ }
10
+ return prompt ?? Prompt.Consent;
11
+ };
6
12
  const generateSignInUri = ({ authorizationEndpoint, clientId, redirectUri, codeChallenge, state, scopes, resources, prompt, interactionMode, }) => {
7
13
  const urlSearchParameters = new URLSearchParams({
8
14
  [QueryKey.ClientId]: clientId,
@@ -11,7 +17,7 @@ const generateSignInUri = ({ authorizationEndpoint, clientId, redirectUri, codeC
11
17
  [QueryKey.CodeChallengeMethod]: codeChallengeMethod,
12
18
  [QueryKey.State]: state,
13
19
  [QueryKey.ResponseType]: responseType,
14
- [QueryKey.Prompt]: prompt ?? Prompt.Consent,
20
+ [QueryKey.Prompt]: buildPrompt(prompt),
15
21
  [QueryKey.Scope]: withDefaultScopes(scopes),
16
22
  });
17
23
  for (const resource of resources ?? []) {
@@ -1,18 +1,10 @@
1
- import type { Nullable } from '@silverhand/essentials';
1
+ import { type IdTokenClaims } from '../index.js';
2
2
  import type { Requester } from '../types/index.js';
3
3
  type Identity = {
4
4
  userId: string;
5
5
  details?: Record<string, unknown>;
6
6
  };
7
- export type UserInfoResponse = {
8
- sub: string;
9
- name?: Nullable<string>;
10
- username?: Nullable<string>;
11
- picture?: Nullable<string>;
12
- email?: Nullable<string>;
13
- email_verified?: boolean;
14
- phone_number?: Nullable<string>;
15
- phone_number_verified?: boolean;
7
+ export type UserInfoResponse = IdTokenClaims & {
16
8
  custom_data?: unknown;
17
9
  identities?: Record<string, Identity>;
18
10
  };
package/lib/index.cjs CHANGED
@@ -12,6 +12,7 @@ var idToken = require('./utils/id-token.cjs');
12
12
  var accessToken = require('./utils/access-token.cjs');
13
13
  var scopes = require('./utils/scopes.cjs');
14
14
  var arbitraryObject = require('./utils/arbitrary-object.cjs');
15
+ var angular = require('./utils/angular.cjs');
15
16
  var index = require('./consts/index.cjs');
16
17
  var openid = require('./consts/openid.cjs');
17
18
 
@@ -35,6 +36,7 @@ exports.decodeIdToken = idToken.decodeIdToken;
35
36
  exports.decodeAccessToken = accessToken.decodeAccessToken;
36
37
  exports.withDefaultScopes = scopes.withDefaultScopes;
37
38
  exports.isArbitraryObject = arbitraryObject.isArbitraryObject;
39
+ exports.buildAngularAuthConfig = angular.buildAngularAuthConfig;
38
40
  exports.ContentType = index.ContentType;
39
41
  Object.defineProperty(exports, "Prompt", {
40
42
  enumerable: true,
package/lib/index.js CHANGED
@@ -10,5 +10,6 @@ export { decodeIdToken } from './utils/id-token.js';
10
10
  export { decodeAccessToken } from './utils/access-token.js';
11
11
  export { withDefaultScopes } from './utils/scopes.js';
12
12
  export { isArbitraryObject } from './utils/arbitrary-object.js';
13
+ export { buildAngularAuthConfig } from './utils/angular.js';
13
14
  export { ContentType, Prompt, QueryKey, TokenGrantType } from './consts/index.js';
14
15
  export { ReservedResource, ReservedScope, UserScope, buildOrganizationUrn, getOrganizationIdFromUrn, idTokenClaims, organizationUrnPrefix, userClaims, userinfoClaims } from './consts/openid.js';
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+
3
+ var index = require('../consts/index.cjs');
4
+ var scopes = require('./scopes.cjs');
5
+
6
+ /**
7
+ * A helper function to build the OpenID Connect configuration for `angular-auth-oidc-client`
8
+ * using a Logto-friendly way.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * // A minimal example
13
+ * import { buildAngularAuthConfig } from '@logto/js';
14
+ * import { provideAuth } from 'angular-auth-oidc-client';
15
+ *
16
+ * provideAuth({
17
+ * config: buildAngularAuthConfig({
18
+ * endpoint: '<your-logto-endpoint>',
19
+ * appId: '<your-app-id>',
20
+ * redirectUri: '<your-app-redirect-uri>',
21
+ * }),
22
+ * });
23
+ * ```
24
+ *
25
+ * @param logtoConfig The Logto configuration object for Angular apps.
26
+ * @returns The OpenID Connect configuration for `angular-auth-oidc-client`.
27
+ * @see {@link https://angular-auth-oidc-client.com/ | angular-auth-oidc-client} to learn more
28
+ * about how to use the library.
29
+ */
30
+ const buildAngularAuthConfig = (logtoConfig) => {
31
+ const { endpoint, appId: clientId, scopes: scopes$1, resource, redirectUri: redirectUrl, postLogoutRedirectUri, prompt = index.Prompt.Consent, } = logtoConfig;
32
+ const scope = scopes.withDefaultScopes(scopes$1);
33
+ const customParameters = resource ? { resource } : undefined;
34
+ return {
35
+ authority: new URL('/oidc', endpoint).href,
36
+ redirectUrl,
37
+ postLogoutRedirectUri,
38
+ clientId,
39
+ scope,
40
+ responseType: 'code',
41
+ autoUserInfo: !resource,
42
+ renewUserInfoAfterTokenRenew: !resource,
43
+ silentRenew: true,
44
+ useRefreshToken: true,
45
+ customParamsAuthRequest: {
46
+ prompt: Array.isArray(prompt) ? prompt.join(' ') : prompt,
47
+ ...customParameters,
48
+ },
49
+ customParamsCodeRequest: {
50
+ ...customParameters,
51
+ },
52
+ customParamsRefreshTokenRequest: {
53
+ ...customParameters,
54
+ },
55
+ };
56
+ };
57
+
58
+ exports.buildAngularAuthConfig = buildAngularAuthConfig;
@@ -0,0 +1,70 @@
1
+ import { type OpenIdConfiguration } from 'angular-auth-oidc-client';
2
+ import { Prompt } from '../consts/index.js';
3
+ /** The Logto configuration object for Angular apps. */
4
+ export type LogtoAngularConfig = {
5
+ /**
6
+ * The endpoint for the Logto server, you can get it from the integration guide
7
+ * or the team settings page of the Logto Console.
8
+ *
9
+ * @example https://foo.logto.app
10
+ */
11
+ endpoint: string;
12
+ /**
13
+ * The client ID of your application, you can get it from the integration guide
14
+ * or the application details page of the Logto Console.
15
+ */
16
+ appId: string;
17
+ /**
18
+ * The scopes (permissions) that your application needs to access.
19
+ * Scopes that will be added by default: `openid`, `offline_access` and `profile`.
20
+ */
21
+ scopes?: string[];
22
+ /**
23
+ * The API resource that your application needs to access.
24
+ *
25
+ * @see {@link https://docs.logto.io/docs/recipes/rbac/ | RBAC} to learn more about how to use
26
+ * role-based access control (RBAC) to protect API resources.
27
+ */
28
+ resource?: string;
29
+ /**
30
+ * @param redirectUri The redirect URI that the user will be redirected to after the sign-in flow
31
+ * is completed.
32
+ */
33
+ redirectUri: string;
34
+ /**
35
+ * @param postLogoutRedirectUri The URI that the user will be redirected to after the sign-out
36
+ * flow is completed.
37
+ */
38
+ postLogoutRedirectUri?: string;
39
+ /**
40
+ * The prompt parameter to be used for the authorization request.
41
+ *
42
+ * @default Prompt.Consent
43
+ */
44
+ prompt?: Prompt | Prompt[];
45
+ };
46
+ /**
47
+ * A helper function to build the OpenID Connect configuration for `angular-auth-oidc-client`
48
+ * using a Logto-friendly way.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * // A minimal example
53
+ * import { buildAngularAuthConfig } from '@logto/js';
54
+ * import { provideAuth } from 'angular-auth-oidc-client';
55
+ *
56
+ * provideAuth({
57
+ * config: buildAngularAuthConfig({
58
+ * endpoint: '<your-logto-endpoint>',
59
+ * appId: '<your-app-id>',
60
+ * redirectUri: '<your-app-redirect-uri>',
61
+ * }),
62
+ * });
63
+ * ```
64
+ *
65
+ * @param logtoConfig The Logto configuration object for Angular apps.
66
+ * @returns The OpenID Connect configuration for `angular-auth-oidc-client`.
67
+ * @see {@link https://angular-auth-oidc-client.com/ | angular-auth-oidc-client} to learn more
68
+ * about how to use the library.
69
+ */
70
+ export declare const buildAngularAuthConfig: (logtoConfig: LogtoAngularConfig) => OpenIdConfiguration;
@@ -0,0 +1,56 @@
1
+ import { Prompt } from '../consts/index.js';
2
+ import { withDefaultScopes } from './scopes.js';
3
+
4
+ /**
5
+ * A helper function to build the OpenID Connect configuration for `angular-auth-oidc-client`
6
+ * using a Logto-friendly way.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * // A minimal example
11
+ * import { buildAngularAuthConfig } from '@logto/js';
12
+ * import { provideAuth } from 'angular-auth-oidc-client';
13
+ *
14
+ * provideAuth({
15
+ * config: buildAngularAuthConfig({
16
+ * endpoint: '<your-logto-endpoint>',
17
+ * appId: '<your-app-id>',
18
+ * redirectUri: '<your-app-redirect-uri>',
19
+ * }),
20
+ * });
21
+ * ```
22
+ *
23
+ * @param logtoConfig The Logto configuration object for Angular apps.
24
+ * @returns The OpenID Connect configuration for `angular-auth-oidc-client`.
25
+ * @see {@link https://angular-auth-oidc-client.com/ | angular-auth-oidc-client} to learn more
26
+ * about how to use the library.
27
+ */
28
+ const buildAngularAuthConfig = (logtoConfig) => {
29
+ const { endpoint, appId: clientId, scopes, resource, redirectUri: redirectUrl, postLogoutRedirectUri, prompt = Prompt.Consent, } = logtoConfig;
30
+ const scope = withDefaultScopes(scopes);
31
+ const customParameters = resource ? { resource } : undefined;
32
+ return {
33
+ authority: new URL('/oidc', endpoint).href,
34
+ redirectUrl,
35
+ postLogoutRedirectUri,
36
+ clientId,
37
+ scope,
38
+ responseType: 'code',
39
+ autoUserInfo: !resource,
40
+ renewUserInfoAfterTokenRenew: !resource,
41
+ silentRenew: true,
42
+ useRefreshToken: true,
43
+ customParamsAuthRequest: {
44
+ prompt: Array.isArray(prompt) ? prompt.join(' ') : prompt,
45
+ ...customParameters,
46
+ },
47
+ customParamsCodeRequest: {
48
+ ...customParameters,
49
+ },
50
+ customParamsRefreshTokenRequest: {
51
+ ...customParameters,
52
+ },
53
+ };
54
+ };
55
+
56
+ export { buildAngularAuthConfig };
@@ -4,3 +4,4 @@ export * from './id-token.js';
4
4
  export * from './access-token.js';
5
5
  export * from './scopes.js';
6
6
  export * from './arbitrary-object.js';
7
+ export * from './angular.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logto/js",
3
- "version": "4.0.0-alpha.1",
3
+ "version": "4.0.0",
4
4
  "type": "module",
5
5
  "main": "./lib/index.cjs",
6
6
  "module": "./lib/index.js",
@@ -24,24 +24,26 @@
24
24
  "camelcase-keys": "^7.0.1"
25
25
  },
26
26
  "devDependencies": {
27
+ "@peculiar/webcrypto": "^1.4.5",
27
28
  "@silverhand/eslint-config": "^5.0.0",
28
29
  "@silverhand/ts-config": "^5.0.0",
29
30
  "@swc/core": "^1.3.50",
30
31
  "@swc/jest": "^0.2.24",
31
32
  "@types/jest": "^29.5.1",
32
- "@types/node": "^20.0.0",
33
+ "@types/node": "^20.11.19",
34
+ "angular-auth-oidc-client": "^17.0.0",
33
35
  "eslint": "^8.44.0",
34
36
  "jest": "^29.5.0",
35
37
  "jest-environment-jsdom": "^29.5.0",
36
38
  "jest-matcher-specific-error": "^1.0.0",
37
- "jose": "^5.0.0",
39
+ "jose": "^5.2.2",
38
40
  "lint-staged": "^15.0.0",
39
41
  "nock": "^13.3.0",
40
42
  "prettier": "^3.0.0",
41
43
  "rollup": "^4.0.0",
42
44
  "text-encoder": "^0.0.4",
43
45
  "type-fest": "^4.0.0",
44
- "typescript": "^5.0.0"
46
+ "typescript": "^5.3.3"
45
47
  },
46
48
  "eslintConfig": {
47
49
  "extends": "@silverhand"