@scalekit-sdk/node 1.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.
Files changed (81) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +49 -0
  3. package/buf.gen.yaml +14 -0
  4. package/lib/connect.d.ts +9 -0
  5. package/lib/connect.js +31 -0
  6. package/lib/connect.js.map +1 -0
  7. package/lib/connection.d.ts +28 -0
  8. package/lib/connection.js +68 -0
  9. package/lib/connection.js.map +1 -0
  10. package/lib/constants/user.d.ts +4 -0
  11. package/lib/constants/user.js +24 -0
  12. package/lib/constants/user.js.map +1 -0
  13. package/lib/core.d.ts +37 -0
  14. package/lib/core.js +160 -0
  15. package/lib/core.js.map +1 -0
  16. package/lib/domain.d.ts +33 -0
  17. package/lib/domain.js +74 -0
  18. package/lib/domain.js.map +1 -0
  19. package/lib/index.d.ts +5 -0
  20. package/lib/index.js +26 -0
  21. package/lib/index.js.map +1 -0
  22. package/lib/organization.d.ts +64 -0
  23. package/lib/organization.js +116 -0
  24. package/lib/organization.js.map +1 -0
  25. package/lib/pkg/grpc/scalekit/v1/commons/commons_pb.d.ts +20 -0
  26. package/lib/pkg/grpc/scalekit/v1/commons/commons_pb.js +36 -0
  27. package/lib/pkg/grpc/scalekit/v1/commons/commons_pb.js.map +1 -0
  28. package/lib/pkg/grpc/scalekit/v1/connections/connections_connect.d.ts +73 -0
  29. package/lib/pkg/grpc/scalekit/v1/connections/connections_connect.js +81 -0
  30. package/lib/pkg/grpc/scalekit/v1/connections/connections_connect.js.map +1 -0
  31. package/lib/pkg/grpc/scalekit/v1/connections/connections_pb.d.ts +1252 -0
  32. package/lib/pkg/grpc/scalekit/v1/connections/connections_pb.js +1536 -0
  33. package/lib/pkg/grpc/scalekit/v1/connections/connections_pb.js.map +1 -0
  34. package/lib/pkg/grpc/scalekit/v1/domains/domains_connect.d.ts +55 -0
  35. package/lib/pkg/grpc/scalekit/v1/domains/domains_connect.js +63 -0
  36. package/lib/pkg/grpc/scalekit/v1/domains/domains_connect.js.map +1 -0
  37. package/lib/pkg/grpc/scalekit/v1/domains/domains_pb.d.ts +394 -0
  38. package/lib/pkg/grpc/scalekit/v1/domains/domains_pb.js +469 -0
  39. package/lib/pkg/grpc/scalekit/v1/domains/domains_pb.js.map +1 -0
  40. package/lib/pkg/grpc/scalekit/v1/errdetails/errdetails_pb.d.ts +245 -0
  41. package/lib/pkg/grpc/scalekit/v1/errdetails/errdetails_pb.js +369 -0
  42. package/lib/pkg/grpc/scalekit/v1/errdetails/errdetails_pb.js.map +1 -0
  43. package/lib/pkg/grpc/scalekit/v1/options/options_pb.d.ts +98 -0
  44. package/lib/pkg/grpc/scalekit/v1/options/options_pb.js +135 -0
  45. package/lib/pkg/grpc/scalekit/v1/options/options_pb.js.map +1 -0
  46. package/lib/pkg/grpc/scalekit/v1/organizations/organizations_connect.d.ts +92 -0
  47. package/lib/pkg/grpc/scalekit/v1/organizations/organizations_connect.js +100 -0
  48. package/lib/pkg/grpc/scalekit/v1/organizations/organizations_connect.js.map +1 -0
  49. package/lib/pkg/grpc/scalekit/v1/organizations/organizations_pb.d.ts +424 -0
  50. package/lib/pkg/grpc/scalekit/v1/organizations/organizations_pb.js +559 -0
  51. package/lib/pkg/grpc/scalekit/v1/organizations/organizations_pb.js.map +1 -0
  52. package/lib/scalekit.d.ts +60 -0
  53. package/lib/scalekit.js +140 -0
  54. package/lib/scalekit.js.map +1 -0
  55. package/lib/types/scalekit.d.ts +26 -0
  56. package/lib/types/scalekit.js +10 -0
  57. package/lib/types/scalekit.js.map +1 -0
  58. package/lib/types/user.d.ts +56 -0
  59. package/lib/types/user.js +3 -0
  60. package/lib/types/user.js.map +1 -0
  61. package/package.json +45 -0
  62. package/src/connect.ts +33 -0
  63. package/src/connection.ts +69 -0
  64. package/src/constants/user.ts +22 -0
  65. package/src/core.ts +127 -0
  66. package/src/domain.ts +75 -0
  67. package/src/index.ts +7 -0
  68. package/src/organization.ts +129 -0
  69. package/src/pkg/grpc/scalekit/v1/commons/commons_pb.ts +36 -0
  70. package/src/pkg/grpc/scalekit/v1/connections/connections_connect.ts +80 -0
  71. package/src/pkg/grpc/scalekit/v1/connections/connections_pb.ts +2138 -0
  72. package/src/pkg/grpc/scalekit/v1/domains/domains_connect.ts +62 -0
  73. package/src/pkg/grpc/scalekit/v1/domains/domains_pb.ts +687 -0
  74. package/src/pkg/grpc/scalekit/v1/errdetails/errdetails_pb.ts +461 -0
  75. package/src/pkg/grpc/scalekit/v1/options/options_pb.ts +156 -0
  76. package/src/pkg/grpc/scalekit/v1/organizations/organizations_connect.ts +99 -0
  77. package/src/pkg/grpc/scalekit/v1/organizations/organizations_pb.ts +774 -0
  78. package/src/scalekit.ts +155 -0
  79. package/src/types/scalekit.ts +30 -0
  80. package/src/types/user.ts +59 -0
  81. package/tsconfig.json +18 -0
@@ -0,0 +1,155 @@
1
+ import * as jose from 'jose';
2
+ import QueryString from 'qs';
3
+ import GrpcConnect from './connect';
4
+ import ConnectionClient from './connection';
5
+ import { IdTokenClaimToUserMap } from './constants/user';
6
+ import CoreClient from './core';
7
+ import DomainClient from './domain';
8
+ import OrganizationClient from './organization';
9
+ import { AuthorizationUrlOptions, CodeAuthenticationOptions, GrantType } from './types/scalekit';
10
+ import { IdTokenClaim, User } from './types/user';
11
+
12
+ const authorizeEndpoint = "oauth/authorize";
13
+
14
+ /**
15
+ * To initiate scalekit
16
+ * @param {string} envUrl The environment url
17
+ * @param {string} clientId The client id
18
+ * @param {string} clientSecret The client secret
19
+ * @returns {Scalekit} Returns the scalekit instance
20
+ * @example
21
+ * const scalekit = new Scalekit(envUrl, clientId, clientSecret);
22
+ */
23
+ export default class Scalekit {
24
+ private readonly coreClient: CoreClient;
25
+ private readonly grpcConnect: GrpcConnect;
26
+ readonly organization: OrganizationClient;
27
+ readonly connection: ConnectionClient;
28
+ readonly domain: DomainClient;
29
+ constructor(
30
+ envUrl: string,
31
+ clientId: string,
32
+ clientSecret: string
33
+ ) {
34
+ this.coreClient = new CoreClient(
35
+ envUrl,
36
+ clientId,
37
+ clientSecret
38
+ );
39
+ this.grpcConnect = new GrpcConnect(
40
+ this.coreClient
41
+ );
42
+
43
+ this.organization = new OrganizationClient(
44
+ this.grpcConnect,
45
+ this.coreClient
46
+ );
47
+ this.connection = new ConnectionClient(
48
+ this.grpcConnect,
49
+ this.coreClient
50
+ );
51
+ this.domain = new DomainClient(
52
+ this.grpcConnect,
53
+ this.coreClient
54
+ )
55
+ }
56
+
57
+ /**
58
+ * Returns the authorization url to initiate the authentication request.
59
+ * @param {string} redirectUri Redirect uri
60
+ * @param {AuthorizationUrlOptions} options Authorization url options
61
+ * @param {string[]} options.scopes Scopes to request from the user
62
+ * @param {string} options.state State parameter
63
+ * @param {string} options.nonce Nonce parameter
64
+ * @param {string} options.loginHint Login hint parameter
65
+ * @param {string} options.domainHint Domain hint parameter
66
+ * @param {string} options.connectionId Connection id parameter
67
+ * @param {string} options.organizationId Organization id parameter
68
+ *
69
+ * @example
70
+ * const scalekit = new Scalekit(envUrl, clientId, clientSecret);
71
+ * const authorizationUrl = scalekit.getAuthorizationUrl(redirectUri, { scopes: ['openid', 'profile'] });
72
+ * @returns {string} authorization url
73
+ */
74
+ getAuthorizationUrl(
75
+ redirectUri: string,
76
+ options?: AuthorizationUrlOptions
77
+ ): string {
78
+ const defaultOptions: AuthorizationUrlOptions = {
79
+ scopes: ['openid', 'profile']
80
+ }
81
+ options = {
82
+ ...defaultOptions,
83
+ ...options
84
+ }
85
+ const qs = QueryString.stringify({
86
+ response_type: 'code',
87
+ client_id: this.coreClient.clientId,
88
+ redirect_uri: redirectUri,
89
+ scope: options.scopes?.join(" "),
90
+ ...(options.state && { state: options.state }),
91
+ ...(options.nonce && { nonce: options.nonce }),
92
+ ...(options.loginHint && { login_hint: options.loginHint }),
93
+ ...(options.domainHint && { domain_hint: options.domainHint }),
94
+ ...(options.domainHint && { domain: options.domainHint }),
95
+ ...(options.connectionId && { connection_id: options.connectionId }),
96
+ ...(options.organizationId && { organization_id: options.organizationId }),
97
+ })
98
+
99
+ return `${this.coreClient.envUrl}/${authorizeEndpoint}?${qs}`
100
+ }
101
+
102
+ /**
103
+ * 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
109
+ */
110
+ async authenticateWithCode(options: CodeAuthenticationOptions): Promise<{ user: Partial<User>; idToken: string; accessToken: string; }> {
111
+ const res = await this.coreClient.authenticate(QueryString.stringify({
112
+ code: options.code,
113
+ redirect_uri: options.redirectUri,
114
+ grant_type: GrantType.AuthorizationCode,
115
+ client_id: this.coreClient.clientId,
116
+ client_secret: this.coreClient.clientSecret,
117
+ ...(options.codeVerifier && { code_verifier: options.codeVerifier })
118
+ }))
119
+ const { id_token, access_token } = res.data;
120
+ const claims = jose.decodeJwt<IdTokenClaim>(id_token);
121
+ const user: Partial<User> = {};
122
+ for (const [k, v] of Object.entries(claims)) {
123
+ if (IdTokenClaimToUserMap[k]) {
124
+ user[IdTokenClaimToUserMap[k]] = v;
125
+ }
126
+ }
127
+
128
+ return {
129
+ user,
130
+ idToken: id_token,
131
+ accessToken: access_token
132
+ }
133
+ }
134
+
135
+ /**
136
+ * Validates the access token.
137
+ *
138
+ * @param {string} token The token to be validated.
139
+ * @return {Promise<boolean>} Returns true if the token is valid, false otherwise.
140
+ */
141
+ async validateAccessToken(token: string): Promise<boolean> {
142
+ await this.coreClient.getJwks();
143
+ const JWKS = jose.createLocalJWKSet({
144
+ keys: this.coreClient.keys
145
+ })
146
+ try {
147
+ await jose.jwtVerify(token, JWKS);
148
+ return true;
149
+ } catch (error) {
150
+ return false;
151
+ }
152
+ }
153
+ }
154
+
155
+
@@ -0,0 +1,30 @@
1
+ export enum GrantType {
2
+ AuthorizationCode = 'authorization_code',
3
+ RefreshToken = 'refresh_token',
4
+ ClientCredentials = 'client_credentials',
5
+ }
6
+
7
+ export type AuthorizationUrlOptions = {
8
+ connectionId?: string;
9
+ organizationId?: string;
10
+ scopes?: string[];
11
+ state?: string;
12
+ nonce?: string;
13
+ domainHint?: string;
14
+ loginHint?: string;
15
+ }
16
+
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;
26
+ }
27
+
28
+ export type AuthenticationOptions = {
29
+ refreshToken: string;
30
+ }
@@ -0,0 +1,59 @@
1
+ export type User = {
2
+ id: string;
3
+ username: string | undefined;
4
+ name: string;
5
+ givenName: string;
6
+ familyName: string | undefined;
7
+ email: string;
8
+ emailVerified: boolean;
9
+ phoneNumber: string | undefined;
10
+ phoneNumberVerified: boolean | undefined;
11
+ profile: string | undefined;
12
+ picture: string | undefined;
13
+ gender: string | undefined;
14
+ birthDate: string | undefined;
15
+ zoneInfo: string | undefined;
16
+ locale: string | undefined;
17
+ updatedAt: string | undefined;
18
+ identities: Identity[];
19
+ metadata: string | undefined;
20
+ }
21
+
22
+ export type Identity = {
23
+ connectionId: string;
24
+ organizationId: string;
25
+ connectionType: string;
26
+ providerName: string;
27
+ social: boolean;
28
+ providerRawAttributes: string;
29
+ }
30
+
31
+ export type IdTokenClaimIdentity = {
32
+ connection_id: string;
33
+ organization_id: string;
34
+ connection_type: string;
35
+ provider_name: string;
36
+ social: boolean;
37
+ provider_raw_attributes: string;
38
+ }
39
+
40
+ export type IdTokenClaim = {
41
+ sub: string;
42
+ name: string;
43
+ preferred_username: string | undefined;
44
+ given_name: string;
45
+ family_name: string | undefined;
46
+ email: string;
47
+ email_verified: boolean;
48
+ phone_number: string | undefined;
49
+ phone_number_verified: boolean | undefined;
50
+ profile: string | undefined;
51
+ picture: string | undefined;
52
+ gender: string | undefined;
53
+ birthdate: string | undefined;
54
+ zoneinfo: string | undefined;
55
+ locale: string | undefined;
56
+ updated_at: string | undefined;
57
+ identities: IdTokenClaimIdentity[];
58
+ metadata: string | undefined;
59
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "compilerOptions": {
3
+ "noImplicitAny": false,
4
+ "target": "ES6",
5
+ "module": "commonjs",
6
+ "declaration": true,
7
+ "strictNullChecks": true,
8
+ "strictPropertyInitialization": true,
9
+ "outDir": "lib",
10
+ "strict": true,
11
+ "sourceMap": true,
12
+ "skipLibCheck": true,
13
+ "esModuleInterop": true,
14
+ "typeRoots": ["src/types"],
15
+ "moduleResolution": "Node"
16
+ },
17
+ "include": ["src"],
18
+ }