@scalekit-sdk/node 1.0.3 → 1.0.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.
package/src/scalekit.ts CHANGED
@@ -6,8 +6,8 @@ import { IdTokenClaimToUserMap } from './constants/user';
6
6
  import CoreClient from './core';
7
7
  import DomainClient from './domain';
8
8
  import OrganizationClient from './organization';
9
- import { AuthorizationUrlOptions, CodeAuthenticationOptions, GrantType } from './types/scalekit';
10
- import { IdTokenClaim, User } from './types/user';
9
+ import { AuthorizationUrlOptions, AuthenticationOptions, GrantType, AuthenticationResponse } from './types/scalekit';
10
+ import { IdTokenClaim, User } from './types/auth';
11
11
 
12
12
  const authorizeEndpoint = "oauth/authorize";
13
13
 
@@ -16,11 +16,11 @@ const authorizeEndpoint = "oauth/authorize";
16
16
  * @param {string} envUrl The environment url
17
17
  * @param {string} clientId The client id
18
18
  * @param {string} clientSecret The client secret
19
- * @returns {Scalekit} Returns the scalekit instance
19
+ * @returns {ScalekitClient} Returns the scalekit instance
20
20
  * @example
21
21
  * const scalekit = new Scalekit(envUrl, clientId, clientSecret);
22
22
  */
23
- export default class Scalekit {
23
+ export default class ScalekitClient {
24
24
  private readonly coreClient: CoreClient;
25
25
  private readonly grpcConnect: GrpcConnect;
26
26
  readonly organization: OrganizationClient;
@@ -65,6 +65,8 @@ export default class Scalekit {
65
65
  * @param {string} options.domainHint Domain hint parameter
66
66
  * @param {string} options.connectionId Connection id parameter
67
67
  * @param {string} options.organizationId Organization id parameter
68
+ * @param {string} options.codeChallenge Code challenge parameter in case of PKCE
69
+ * @param {string} options.codeChallengeMethod Code challenge method parameter in case of PKCE
68
70
  *
69
71
  * @example
70
72
  * const scalekit = new Scalekit(envUrl, clientId, clientSecret);
@@ -76,7 +78,7 @@ export default class Scalekit {
76
78
  options?: AuthorizationUrlOptions
77
79
  ): string {
78
80
  const defaultOptions: AuthorizationUrlOptions = {
79
- scopes: ['openid', 'profile']
81
+ scopes: ['openid', 'profile', 'email']
80
82
  }
81
83
  options = {
82
84
  ...defaultOptions,
@@ -94,6 +96,8 @@ export default class Scalekit {
94
96
  ...(options.domainHint && { domain: options.domainHint }),
95
97
  ...(options.connectionId && { connection_id: options.connectionId }),
96
98
  ...(options.organizationId && { organization_id: options.organizationId }),
99
+ ...(options.codeChallenge && { code_challenge: options.codeChallenge }),
100
+ ...(options.codeChallengeMethod && { code_challenge_method: options.codeChallengeMethod })
97
101
  })
98
102
 
99
103
  return `${this.coreClient.envUrl}/${authorizeEndpoint}?${qs}`
@@ -101,24 +105,28 @@ export default class Scalekit {
101
105
 
102
106
  /**
103
107
  * Authenticate with the code
104
- * @param {CodeAuthenticationOptions} options Code authentication options
105
- * @param {string} options.code Code
106
- * @param {string} options.redirectUri Redirect uri
107
- * @param {string} options.codeVerifier Code verifier
108
- * @returns {Promise<{ user: Partial<User>, idToken: string, accessToken: string }>} Returns user, id token and access token
108
+ * @param {string} code Code
109
+ * @param {string} redirectUri Redirect uri
110
+ * @param {AuthenticationOptions} options Code authentication options
111
+ * @param {string} options.codeVerifier Code verifier in case of PKCE
112
+ * @returns {Promise<AuthenticationResponse>} Returns user, id token and access token
109
113
  */
110
- async authenticateWithCode(options: CodeAuthenticationOptions): Promise<{ user: Partial<User>; idToken: string; accessToken: string; }> {
114
+ async authenticateWithCode(
115
+ code: string,
116
+ redirectUri: string,
117
+ options?: AuthenticationOptions,
118
+ ): Promise<AuthenticationResponse> {
111
119
  const res = await this.coreClient.authenticate(QueryString.stringify({
112
- code: options.code,
113
- redirect_uri: options.redirectUri,
120
+ code: code,
121
+ redirect_uri: redirectUri,
114
122
  grant_type: GrantType.AuthorizationCode,
115
123
  client_id: this.coreClient.clientId,
116
124
  client_secret: this.coreClient.clientSecret,
117
- ...(options.codeVerifier && { code_verifier: options.codeVerifier })
125
+ ...(options?.codeVerifier && { code_verifier: options.codeVerifier })
118
126
  }))
119
- const { id_token, access_token } = res.data;
127
+ const { id_token, access_token, expires_in } = res.data;
120
128
  const claims = jose.decodeJwt<IdTokenClaim>(id_token);
121
- const user: Partial<User> = {};
129
+ const user = <User>{};
122
130
  for (const [k, v] of Object.entries(claims)) {
123
131
  if (IdTokenClaimToUserMap[k]) {
124
132
  user[IdTokenClaimToUserMap[k]] = v;
@@ -128,7 +136,8 @@ export default class Scalekit {
128
136
  return {
129
137
  user,
130
138
  idToken: id_token,
131
- accessToken: access_token
139
+ accessToken: access_token,
140
+ expiresIn: expires_in
132
141
  }
133
142
  }
134
143
 
@@ -56,4 +56,10 @@ export type IdTokenClaim = {
56
56
  updated_at: string | undefined;
57
57
  identities: IdTokenClaimIdentity[];
58
58
  metadata: string | undefined;
59
+ }
60
+
61
+ export type TokenResponse = {
62
+ access_token: string;
63
+ id_token: string;
64
+ expires_in: number;
59
65
  }
@@ -1,3 +1,5 @@
1
+ import { User } from './auth';
2
+
1
3
  export enum GrantType {
2
4
  AuthorizationCode = 'authorization_code',
3
5
  RefreshToken = 'refresh_token',
@@ -12,19 +14,17 @@ export type AuthorizationUrlOptions = {
12
14
  nonce?: string;
13
15
  domainHint?: string;
14
16
  loginHint?: string;
17
+ codeChallenge?: string;
18
+ codeChallengeMethod?: string;
15
19
  }
16
20
 
17
- export type CodeAuthenticationOptions = {
18
- code: string;
19
- redirectUri: string;
20
- codeVerifier?: string;
21
- }
22
-
23
- export type RefreshTokenAuthenticationOptions = {
24
- code: string;
25
- redirectUri: string;
21
+ export type AuthenticationOptions = {
22
+ codeVerifier?: string;
26
23
  }
27
24
 
28
- export type AuthenticationOptions = {
29
- refreshToken: string;
25
+ export type AuthenticationResponse = {
26
+ user: User;
27
+ idToken: string;
28
+ accessToken: string;
29
+ expiresIn: number;
30
30
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"user.js","sourceRoot":"","sources":["../../src/types/user.ts"],"names":[],"mappings":""}