@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.
- package/LICENSE +21 -0
- package/README.md +49 -0
- package/buf.gen.yaml +14 -0
- package/lib/connect.d.ts +9 -0
- package/lib/connect.js +31 -0
- package/lib/connect.js.map +1 -0
- package/lib/connection.d.ts +28 -0
- package/lib/connection.js +68 -0
- package/lib/connection.js.map +1 -0
- package/lib/constants/user.d.ts +4 -0
- package/lib/constants/user.js +24 -0
- package/lib/constants/user.js.map +1 -0
- package/lib/core.d.ts +37 -0
- package/lib/core.js +160 -0
- package/lib/core.js.map +1 -0
- package/lib/domain.d.ts +33 -0
- package/lib/domain.js +74 -0
- package/lib/domain.js.map +1 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.js +26 -0
- package/lib/index.js.map +1 -0
- package/lib/organization.d.ts +64 -0
- package/lib/organization.js +116 -0
- package/lib/organization.js.map +1 -0
- package/lib/pkg/grpc/scalekit/v1/commons/commons_pb.d.ts +20 -0
- package/lib/pkg/grpc/scalekit/v1/commons/commons_pb.js +36 -0
- package/lib/pkg/grpc/scalekit/v1/commons/commons_pb.js.map +1 -0
- package/lib/pkg/grpc/scalekit/v1/connections/connections_connect.d.ts +73 -0
- package/lib/pkg/grpc/scalekit/v1/connections/connections_connect.js +81 -0
- package/lib/pkg/grpc/scalekit/v1/connections/connections_connect.js.map +1 -0
- package/lib/pkg/grpc/scalekit/v1/connections/connections_pb.d.ts +1252 -0
- package/lib/pkg/grpc/scalekit/v1/connections/connections_pb.js +1536 -0
- package/lib/pkg/grpc/scalekit/v1/connections/connections_pb.js.map +1 -0
- package/lib/pkg/grpc/scalekit/v1/domains/domains_connect.d.ts +55 -0
- package/lib/pkg/grpc/scalekit/v1/domains/domains_connect.js +63 -0
- package/lib/pkg/grpc/scalekit/v1/domains/domains_connect.js.map +1 -0
- package/lib/pkg/grpc/scalekit/v1/domains/domains_pb.d.ts +394 -0
- package/lib/pkg/grpc/scalekit/v1/domains/domains_pb.js +469 -0
- package/lib/pkg/grpc/scalekit/v1/domains/domains_pb.js.map +1 -0
- package/lib/pkg/grpc/scalekit/v1/errdetails/errdetails_pb.d.ts +245 -0
- package/lib/pkg/grpc/scalekit/v1/errdetails/errdetails_pb.js +369 -0
- package/lib/pkg/grpc/scalekit/v1/errdetails/errdetails_pb.js.map +1 -0
- package/lib/pkg/grpc/scalekit/v1/options/options_pb.d.ts +98 -0
- package/lib/pkg/grpc/scalekit/v1/options/options_pb.js +135 -0
- package/lib/pkg/grpc/scalekit/v1/options/options_pb.js.map +1 -0
- package/lib/pkg/grpc/scalekit/v1/organizations/organizations_connect.d.ts +92 -0
- package/lib/pkg/grpc/scalekit/v1/organizations/organizations_connect.js +100 -0
- package/lib/pkg/grpc/scalekit/v1/organizations/organizations_connect.js.map +1 -0
- package/lib/pkg/grpc/scalekit/v1/organizations/organizations_pb.d.ts +424 -0
- package/lib/pkg/grpc/scalekit/v1/organizations/organizations_pb.js +559 -0
- package/lib/pkg/grpc/scalekit/v1/organizations/organizations_pb.js.map +1 -0
- package/lib/scalekit.d.ts +60 -0
- package/lib/scalekit.js +140 -0
- package/lib/scalekit.js.map +1 -0
- package/lib/types/scalekit.d.ts +26 -0
- package/lib/types/scalekit.js +10 -0
- package/lib/types/scalekit.js.map +1 -0
- package/lib/types/user.d.ts +56 -0
- package/lib/types/user.js +3 -0
- package/lib/types/user.js.map +1 -0
- package/package.json +45 -0
- package/src/connect.ts +33 -0
- package/src/connection.ts +69 -0
- package/src/constants/user.ts +22 -0
- package/src/core.ts +127 -0
- package/src/domain.ts +75 -0
- package/src/index.ts +7 -0
- package/src/organization.ts +129 -0
- package/src/pkg/grpc/scalekit/v1/commons/commons_pb.ts +36 -0
- package/src/pkg/grpc/scalekit/v1/connections/connections_connect.ts +80 -0
- package/src/pkg/grpc/scalekit/v1/connections/connections_pb.ts +2138 -0
- package/src/pkg/grpc/scalekit/v1/domains/domains_connect.ts +62 -0
- package/src/pkg/grpc/scalekit/v1/domains/domains_pb.ts +687 -0
- package/src/pkg/grpc/scalekit/v1/errdetails/errdetails_pb.ts +461 -0
- package/src/pkg/grpc/scalekit/v1/options/options_pb.ts +156 -0
- package/src/pkg/grpc/scalekit/v1/organizations/organizations_connect.ts +99 -0
- package/src/pkg/grpc/scalekit/v1/organizations/organizations_pb.ts +774 -0
- package/src/scalekit.ts +155 -0
- package/src/types/scalekit.ts +30 -0
- package/src/types/user.ts +59 -0
- package/tsconfig.json +18 -0
package/src/scalekit.ts
ADDED
|
@@ -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
|
+
}
|