@cloudbase/oauth 1.2.0-beta → 2.2.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/.eslintignore +2 -0
- package/.eslintrc +26 -0
- package/CHANGELOG.md +30 -0
- package/Dockerfile +15 -0
- package/README.md +183 -0
- package/_exmaple/assets/scripts/function/function.ts +99 -0
- package/_exmaple/assets/scripts/index.ts +101 -0
- package/_exmaple/assets/scripts/request.ts +11 -0
- package/_exmaple/index.html +15 -0
- package/_exmaple/package.json +33 -0
- package/_exmaple/tsconfig.json +71 -0
- package/_exmaple/typings.d.ts +0 -0
- package/_exmaple/webpack.config.js +42 -0
- package/dist/cjs/auth/apis.d.ts +55 -0
- package/dist/cjs/auth/apis.js +568 -0
- package/{auth → dist/cjs/auth}/consts.d.ts +9 -21
- package/dist/cjs/auth/consts.js +61 -0
- package/dist/cjs/auth/models.d.ts +354 -0
- package/dist/cjs/auth/models.js +3 -0
- package/dist/cjs/captcha/captcha.d.ts +35 -0
- package/dist/cjs/captcha/captcha.js +267 -0
- package/dist/cjs/index.d.ts +14 -0
- package/dist/cjs/index.js +63 -0
- package/{oauthclient → dist/cjs/oauth2client}/consts.d.ts +22 -1
- package/dist/cjs/oauth2client/consts.js +73 -0
- package/dist/cjs/oauth2client/interface.d.ts +15 -0
- package/dist/cjs/oauth2client/interface.js +10 -0
- package/dist/cjs/oauth2client/models.d.ts +47 -0
- package/dist/cjs/oauth2client/models.js +3 -0
- package/dist/cjs/oauth2client/oauth2client.d.ts +71 -0
- package/dist/cjs/oauth2client/oauth2client.js +640 -0
- package/dist/cjs/utils/function/single-promise.d.ts +5 -0
- package/dist/cjs/utils/function/single-promise.js +89 -0
- package/dist/cjs/utils/uuid.d.ts +1 -0
- package/dist/cjs/utils/uuid.js +12 -0
- package/dist/esm/auth/apis.d.ts +55 -0
- package/dist/esm/auth/apis.js +566 -0
- package/dist/esm/auth/consts.d.ts +54 -0
- package/dist/esm/auth/consts.js +58 -0
- package/dist/esm/auth/models.d.ts +354 -0
- package/dist/esm/auth/models.js +1 -0
- package/dist/esm/captcha/captcha.d.ts +35 -0
- package/dist/esm/captcha/captcha.js +264 -0
- package/dist/esm/index.d.ts +14 -0
- package/dist/esm/index.js +32 -0
- package/dist/esm/oauth2client/consts.d.ts +67 -0
- package/dist/esm/oauth2client/consts.js +70 -0
- package/dist/esm/oauth2client/interface.d.ts +15 -0
- package/dist/esm/oauth2client/interface.js +7 -0
- package/dist/esm/oauth2client/models.d.ts +47 -0
- package/dist/esm/oauth2client/models.js +1 -0
- package/dist/esm/oauth2client/oauth2client.d.ts +71 -0
- package/dist/esm/oauth2client/oauth2client.js +636 -0
- package/dist/esm/utils/function/single-promise.d.ts +5 -0
- package/dist/esm/utils/function/single-promise.js +86 -0
- package/dist/esm/utils/uuid.d.ts +1 -0
- package/dist/esm/utils/uuid.js +8 -0
- package/package.json +27 -14
- package/publish.sh +2 -0
- package/src/auth/apis.ts +642 -0
- package/src/auth/consts.ts +57 -0
- package/src/auth/models.ts +444 -0
- package/src/captcha/captcha.ts +222 -0
- package/src/index.ts +49 -0
- package/src/oauth2client/consts.ts +69 -0
- package/src/oauth2client/interface.ts +57 -0
- package/src/oauth2client/models.ts +61 -0
- package/src/oauth2client/oauth2client.ts +620 -0
- package/src/utils/function/single-promise.ts +40 -0
- package/src/utils/uuid.ts +11 -0
- package/tsconfig.esm.json +44 -0
- package/tsconfig.json +44 -0
- package/wiki/README.md +75 -0
- package/app/index.d.ts +0 -29
- package/app/index.js +0 -50
- package/app/index.js.map +0 -1
- package/app/internal.d.ts +0 -12
- package/app/internal.js +0 -25
- package/app/internal.js.map +0 -1
- package/app/openuri.d.ts +0 -20
- package/app/openuri.js +0 -106
- package/app/openuri.js.map +0 -1
- package/app/request.d.ts +0 -19
- package/app/request.js +0 -52
- package/app/request.js.map +0 -1
- package/app/storage.d.ts +0 -41
- package/app/storage.js +0 -35
- package/app/storage.js.map +0 -1
- package/auth/consts.js +0 -79
- package/auth/consts.js.map +0 -1
- package/auth/index.d.ts +0 -242
- package/auth/index.js +0 -550
- package/auth/index.js.map +0 -1
- package/auth/models.d.ts +0 -284
- package/auth/models.js +0 -4
- package/auth/models.js.map +0 -1
- package/captcha/index.d.ts +0 -45
- package/captcha/index.js +0 -135
- package/captcha/index.js.map +0 -1
- package/function/index.d.ts +0 -38
- package/function/index.js +0 -80
- package/function/index.js.map +0 -1
- package/index.d.ts +0 -8
- package/index.js +0 -17
- package/index.js.map +0 -1
- package/oauthclient/consts.js +0 -53
- package/oauthclient/consts.js.map +0 -1
- package/oauthclient/index.d.ts +0 -18
- package/oauthclient/index.js +0 -21
- package/oauthclient/index.js.map +0 -1
- package/oauthclient/interface.d.ts +0 -29
- package/oauthclient/interface.js +0 -10
- package/oauthclient/interface.js.map +0 -1
- package/oauthclient/models.d.ts +0 -40
- package/oauthclient/models.js +0 -3
- package/oauthclient/models.js.map +0 -1
- package/oauthclient/oauthclient.d.ts +0 -146
- package/oauthclient/oauthclient.js +0 -414
- package/oauthclient/oauthclient.js.map +0 -1
- package/utils/promise.d.ts +0 -18
- package/utils/promise.js +0 -48
- package/utils/promise.js.map +0 -1
- package/utils/uuid.d.ts +0 -5
- package/utils/uuid.js +0 -16
- package/utils/uuid.js.map +0 -1
package/src/auth/apis.ts
ADDED
|
@@ -0,0 +1,642 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import { ApiUrls } from './consts';
|
|
4
|
+
import {
|
|
5
|
+
GetVerificationRequest,
|
|
6
|
+
GetVerificationResponse,
|
|
7
|
+
UserProfile,
|
|
8
|
+
UserInfo,
|
|
9
|
+
SignInRequest,
|
|
10
|
+
SignUpRequest,
|
|
11
|
+
VerifyRequest,
|
|
12
|
+
VerifyResponse,
|
|
13
|
+
GenProviderRedirectUriRequest,
|
|
14
|
+
GenProviderRedirectUriResponse,
|
|
15
|
+
GrantProviderTokenRequest,
|
|
16
|
+
GrantProviderTokenResponse,
|
|
17
|
+
PatchProviderTokenRequest,
|
|
18
|
+
PatchProviderTokenResponse,
|
|
19
|
+
SignInWithProviderRequest,
|
|
20
|
+
BindWithProviderRequest,
|
|
21
|
+
TransByProviderRequest,
|
|
22
|
+
GrantTokenRequest,
|
|
23
|
+
UserProfileProvider,
|
|
24
|
+
UnbindProviderRequest,
|
|
25
|
+
CheckPasswordrRequest,
|
|
26
|
+
BindPhoneRequest,
|
|
27
|
+
BindEmailRequest,
|
|
28
|
+
SetPasswordRequest,
|
|
29
|
+
ChangeBindedProviderRequest,
|
|
30
|
+
ChangeBindedProviderResponse,
|
|
31
|
+
UpdatePasswordRequest,
|
|
32
|
+
SudoResponse,
|
|
33
|
+
SudoRequest,
|
|
34
|
+
GetCustomSignTicketFn,
|
|
35
|
+
QueryUserProfileRequest,
|
|
36
|
+
QueryUserProfileResponse,
|
|
37
|
+
ResetPasswordRequest,
|
|
38
|
+
DeviceAuthorizeRequest,
|
|
39
|
+
DeviceAuthorizeResponse,
|
|
40
|
+
CheckUsernameRequest,
|
|
41
|
+
CheckIfUserExistRequest,
|
|
42
|
+
CheckIfUserExistResponse,
|
|
43
|
+
WithSudoRequest
|
|
44
|
+
} from './models';
|
|
45
|
+
import { SimpleStorage, RequestFunction } from '../oauth2client/interface';
|
|
46
|
+
import { OAuth2Client, defaultStorage } from '../oauth2client/oauth2client';
|
|
47
|
+
import { Credentials } from '../oauth2client/models';
|
|
48
|
+
import { Captcha } from '../captcha/captcha';
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
export interface AuthOptions {
|
|
52
|
+
apiOrigin: string;
|
|
53
|
+
clientId: string;
|
|
54
|
+
credentialsClient?: OAuth2Client;
|
|
55
|
+
request?: RequestFunction;
|
|
56
|
+
storage?: SimpleStorage;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Auth
|
|
61
|
+
*/
|
|
62
|
+
export class Auth {
|
|
63
|
+
private _config: AuthOptions;
|
|
64
|
+
private _getCustomSignTicketFn?: GetCustomSignTicketFn;
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* constructor
|
|
69
|
+
* @param {AuthOptions} opts
|
|
70
|
+
*/
|
|
71
|
+
constructor(opts: AuthOptions) {
|
|
72
|
+
let request = opts.request;
|
|
73
|
+
let oAuth2Client = opts.credentialsClient;
|
|
74
|
+
if (!oAuth2Client) {
|
|
75
|
+
const initOptions = {
|
|
76
|
+
apiOrigin: opts.apiOrigin,
|
|
77
|
+
clientId: opts.clientId,
|
|
78
|
+
storage: opts.storage,
|
|
79
|
+
};
|
|
80
|
+
oAuth2Client = new OAuth2Client(initOptions);
|
|
81
|
+
}
|
|
82
|
+
if (!request) {
|
|
83
|
+
const baseRequest = oAuth2Client.request.bind(oAuth2Client);
|
|
84
|
+
const captcha = new Captcha({
|
|
85
|
+
clientId: opts.clientId,
|
|
86
|
+
request: baseRequest,
|
|
87
|
+
storage: opts.storage,
|
|
88
|
+
})
|
|
89
|
+
request = captcha.request.bind(captcha)
|
|
90
|
+
}
|
|
91
|
+
this._config = {
|
|
92
|
+
apiOrigin: opts.apiOrigin,
|
|
93
|
+
clientId: opts.clientId,
|
|
94
|
+
request: request,
|
|
95
|
+
credentialsClient: oAuth2Client,
|
|
96
|
+
storage: opts.storage || defaultStorage,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Sign in.
|
|
102
|
+
* @param {SignInRequest} params A SignInRequest Object.
|
|
103
|
+
* @return {Promise<Credentials>} A Promise<Credentials> object.
|
|
104
|
+
*/
|
|
105
|
+
public async signIn(params: SignInRequest): Promise<Credentials> {
|
|
106
|
+
const credentials: Credentials = await this._config.request<Credentials>(
|
|
107
|
+
ApiUrls.AUTH_SIGN_IN_URL,
|
|
108
|
+
{
|
|
109
|
+
method: 'POST',
|
|
110
|
+
body: params
|
|
111
|
+
},
|
|
112
|
+
);
|
|
113
|
+
await this._config.credentialsClient.setCredentials(credentials);
|
|
114
|
+
return Promise.resolve(credentials);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Sign in Anonymously
|
|
119
|
+
* @return {Promise<Credentials>} A Promise<Credentials> object.
|
|
120
|
+
*/
|
|
121
|
+
public async signInAnonymously(): Promise<Credentials> {
|
|
122
|
+
const credentials: Credentials = await this._config.request<Credentials>(
|
|
123
|
+
ApiUrls.AUTH_SIGN_IN_ANONYMOUSLY_URL,
|
|
124
|
+
{
|
|
125
|
+
method: 'POST',
|
|
126
|
+
body: {}
|
|
127
|
+
},
|
|
128
|
+
);
|
|
129
|
+
await this._config.credentialsClient.setCredentials(credentials);
|
|
130
|
+
return Promise.resolve(credentials);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Sign up.
|
|
135
|
+
* @param {SignUpRequest} params A SignUpRequest Object.
|
|
136
|
+
* @return {Promise<Credentials>} A Promise<Credentials> object.
|
|
137
|
+
*/
|
|
138
|
+
public async signUp(params: SignUpRequest): Promise<Credentials> {
|
|
139
|
+
const data: Credentials = await this._config.request<Credentials>(
|
|
140
|
+
ApiUrls.AUTH_SIGN_UP_URL,
|
|
141
|
+
{
|
|
142
|
+
method: 'POST',
|
|
143
|
+
body: params,
|
|
144
|
+
},
|
|
145
|
+
);
|
|
146
|
+
await this._config.credentialsClient.setCredentials(data);
|
|
147
|
+
return Promise.resolve(data);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Sign out.
|
|
152
|
+
* @return {Object} A Promise<void> object.
|
|
153
|
+
*/
|
|
154
|
+
public async signOut(): Promise<any> {
|
|
155
|
+
const accessToken: string = await this._config.credentialsClient.getAccessToken();
|
|
156
|
+
const data = await this._config.request(ApiUrls.AUTH_REVOKE_URL, {
|
|
157
|
+
method: 'POST',
|
|
158
|
+
body: {
|
|
159
|
+
token: accessToken,
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
await this._config.credentialsClient.setCredentials();
|
|
163
|
+
return Promise.resolve(data);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Get the verification.
|
|
168
|
+
* @param {GetVerificationRequest} params A GetVerificationRequest Object.
|
|
169
|
+
* @return {Promise<GetVerificationResponse>} A Promise<GetVerificationResponse> object.
|
|
170
|
+
*/
|
|
171
|
+
public async getVerification(
|
|
172
|
+
params: GetVerificationRequest,
|
|
173
|
+
): Promise<GetVerificationResponse> {
|
|
174
|
+
let withCredentials = false;
|
|
175
|
+
// 发送短信时,如果时给当前用户发,则需要带上鉴权信息
|
|
176
|
+
if (params.target == 'CUR_USER') {
|
|
177
|
+
withCredentials = true
|
|
178
|
+
} else {
|
|
179
|
+
const hasLogin = await this.hasLoginState()
|
|
180
|
+
if (hasLogin) {
|
|
181
|
+
withCredentials = true
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return this._config.request<GetVerificationResponse>(
|
|
185
|
+
ApiUrls.VERIFICATION_URL,
|
|
186
|
+
{
|
|
187
|
+
method: 'POST',
|
|
188
|
+
body: params,
|
|
189
|
+
withCaptcha: true,
|
|
190
|
+
withCredentials: withCredentials,
|
|
191
|
+
},
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Verify the code
|
|
197
|
+
* @param {VerifyRequest} params A VerifyRequest Object.
|
|
198
|
+
* @return {Promise<VerifyResponse>} A Promise<VerifyResponse> object.
|
|
199
|
+
*/
|
|
200
|
+
public async verify(params: VerifyRequest): Promise<VerifyResponse> {
|
|
201
|
+
return this._config.request<VerifyResponse>(ApiUrls.VERIFY_URL, {
|
|
202
|
+
method: 'POST',
|
|
203
|
+
body: params,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Gen provider redirect uri.
|
|
209
|
+
* @param {GenProviderRedirectUriRequest} params A GenProviderRedirectUriRequest object.
|
|
210
|
+
* @return {Promise<GenProviderRedirectUriResponse>} A Promise<GenProviderRedirectUriResponse> object.
|
|
211
|
+
*/
|
|
212
|
+
public async genProviderRedirectUri(
|
|
213
|
+
params: GenProviderRedirectUriRequest,
|
|
214
|
+
): Promise<GenProviderRedirectUriResponse> {
|
|
215
|
+
let url = `${ApiUrls.PROVIDER_URI_URL}?client_id=${this._config.clientId
|
|
216
|
+
}&provider_id=${params.provider_id}&redirect_uri=${encodeURIComponent(
|
|
217
|
+
params.provider_redirect_uri,
|
|
218
|
+
)}&state=${params.state}`;
|
|
219
|
+
const other_params = params.other_params;
|
|
220
|
+
if (other_params) {
|
|
221
|
+
if (
|
|
222
|
+
typeof other_params.sign_out_uri === 'string' &&
|
|
223
|
+
other_params.sign_out_uri.length > 0
|
|
224
|
+
) {
|
|
225
|
+
url += `&other_params[sign_out_uri]=${other_params.sign_out_uri}`;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return this._config.request<GenProviderRedirectUriResponse>(url, {
|
|
229
|
+
method: 'GET',
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Grant provider token.
|
|
235
|
+
* @param {GrantProviderTokenRequest} params A GrantProviderTokenRequest object.
|
|
236
|
+
* @return {Promise<GrantProviderTokenResponse>} A Promise<GrantProviderTokenResponse> object.
|
|
237
|
+
*/
|
|
238
|
+
public async grantProviderToken(
|
|
239
|
+
params: GrantProviderTokenRequest,
|
|
240
|
+
): Promise<GrantProviderTokenResponse> {
|
|
241
|
+
return this._config.request<GrantProviderTokenResponse>(
|
|
242
|
+
ApiUrls.PROVIDER_TOKEN_URL,
|
|
243
|
+
{
|
|
244
|
+
method: 'POST',
|
|
245
|
+
body: params,
|
|
246
|
+
},
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Grant provider token.
|
|
252
|
+
* @param {PatchProviderTokenRequest} params A PatchProviderTokenRequest object.
|
|
253
|
+
* @return {Promise<PatchProviderTokenResponse>} A Promise<PatchProviderTokenResponse> object.
|
|
254
|
+
*/
|
|
255
|
+
public async patchProviderToken(
|
|
256
|
+
params: PatchProviderTokenRequest,
|
|
257
|
+
): Promise<PatchProviderTokenResponse> {
|
|
258
|
+
return this._config.request<PatchProviderTokenResponse>(
|
|
259
|
+
ApiUrls.PROVIDER_TOKEN_URL,
|
|
260
|
+
{
|
|
261
|
+
method: 'PATCH',
|
|
262
|
+
body: params,
|
|
263
|
+
},
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Signin with provider request.
|
|
269
|
+
* @param {SignInWithProviderRequest} params A SignInWithProviderRequest object.
|
|
270
|
+
* @return {Promise<Credentials>} A Promise<Credentials> object.
|
|
271
|
+
*/
|
|
272
|
+
public async signInWithProvider(
|
|
273
|
+
params: SignInWithProviderRequest,
|
|
274
|
+
): Promise<Credentials> {
|
|
275
|
+
const credentials: Credentials = await this._config.request<Credentials>(
|
|
276
|
+
ApiUrls.AUTH_SIGN_IN_WITH_PROVIDER_URL,
|
|
277
|
+
{
|
|
278
|
+
method: 'POST',
|
|
279
|
+
body: params,
|
|
280
|
+
},
|
|
281
|
+
);
|
|
282
|
+
await this._config.credentialsClient.setCredentials(credentials);
|
|
283
|
+
return Promise.resolve(credentials);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Bind with provider
|
|
288
|
+
* @param {BindWithProviderRequest} params A BindWithProviderRequest object.
|
|
289
|
+
* @return {Promise<void>} A Promise<any> object.
|
|
290
|
+
*/
|
|
291
|
+
public async bindWithProvider(
|
|
292
|
+
params: BindWithProviderRequest,
|
|
293
|
+
): Promise<void> {
|
|
294
|
+
return this._config.request<any>(ApiUrls.PROVIDER_BIND_URL, {
|
|
295
|
+
method: 'POST',
|
|
296
|
+
body: params,
|
|
297
|
+
withCredentials: true,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Get the user profile.
|
|
303
|
+
* @return {Promise<UserProfile>} A Promise<UserProfile> object.
|
|
304
|
+
*/
|
|
305
|
+
public async getUserProfile(): Promise<UserProfile> {
|
|
306
|
+
return this.getUserInfo();
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Get the user info.
|
|
311
|
+
* @return {Promise<UserInfo>} A Promise<UserProfile> object.
|
|
312
|
+
*/
|
|
313
|
+
public async getUserInfo(): Promise<UserInfo> {
|
|
314
|
+
const userInfo = await this._config.request<UserInfo>(ApiUrls.USER_ME_URL, {
|
|
315
|
+
method: 'GET',
|
|
316
|
+
withCredentials: true,
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
if (userInfo.sub) {
|
|
320
|
+
userInfo.uid = userInfo.sub
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
return userInfo;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Delete me
|
|
328
|
+
* @param params
|
|
329
|
+
*/
|
|
330
|
+
public async deleteMe(params: WithSudoRequest): Promise<UserProfile> {
|
|
331
|
+
const url = `${ApiUrls.USER_ME_URL}?${Auth.parseParamsToSearch(params)}`;
|
|
332
|
+
return this._config.request<UserProfile>(url, {
|
|
333
|
+
method: 'DELETE',
|
|
334
|
+
withCredentials: true,
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* hasLoginState check if has login state
|
|
340
|
+
* @return {Promise<boolean>} A Promise<boolean> object.
|
|
341
|
+
*/
|
|
342
|
+
public async hasLoginState(): Promise<boolean> {
|
|
343
|
+
try {
|
|
344
|
+
await this._config.credentialsClient.getAccessToken()
|
|
345
|
+
return true
|
|
346
|
+
} catch (error) {
|
|
347
|
+
return false
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
public hasLoginStateSync(): Credentials | null {
|
|
352
|
+
const credentials = this._config.credentialsClient.getCredentialsSync()
|
|
353
|
+
return credentials
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
public async getLoginState(): Promise<Credentials | null> {
|
|
357
|
+
return this._config.credentialsClient.getCredentialsAsync()
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Trans by provider.
|
|
362
|
+
* @param {TransByProviderRequest} params A TransByProviderRequest object.
|
|
363
|
+
* @return {Promise<Credentials>} A Promise<Credentials> object.
|
|
364
|
+
*/
|
|
365
|
+
public async transByProvider(
|
|
366
|
+
params: TransByProviderRequest,
|
|
367
|
+
): Promise<Credentials> {
|
|
368
|
+
return this._config.request<Credentials>(
|
|
369
|
+
ApiUrls.USER_TRANS_BY_PROVIDER_URL,
|
|
370
|
+
{
|
|
371
|
+
method: 'PATCH',
|
|
372
|
+
body: params,
|
|
373
|
+
withCredentials: true,
|
|
374
|
+
},
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Grant token.
|
|
380
|
+
* @param {GrantTokenRequest} params A GrantTokenRequest object.
|
|
381
|
+
* @return {Promise<Credentials>} A Promise<Credentials> object.
|
|
382
|
+
*/
|
|
383
|
+
public async grantToken(params: GrantTokenRequest): Promise<Credentials> {
|
|
384
|
+
return this._config.request<Credentials>(ApiUrls.AUTH_TOKEN_URL, {
|
|
385
|
+
method: 'POST',
|
|
386
|
+
body: params,
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Get the provide list.
|
|
392
|
+
* @return {Promise<UserProfileProvider>} A Promise<UserProfileProvider> object.
|
|
393
|
+
*/
|
|
394
|
+
public async getProviders(): Promise<UserProfileProvider> {
|
|
395
|
+
return this._config.request<UserProfileProvider>(ApiUrls.PROVIDER_LIST, {
|
|
396
|
+
method: 'GET',
|
|
397
|
+
withCredentials: true,
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* unbind provider.
|
|
403
|
+
* @param {UnbindProviderRequest} params
|
|
404
|
+
* @return {Promise<any>}
|
|
405
|
+
*/
|
|
406
|
+
public async unbindProvider(params: UnbindProviderRequest): Promise<void> {
|
|
407
|
+
return this._config.request<any>(
|
|
408
|
+
`${ApiUrls.PROVIDER_UNBIND_URL}/${params.provider_id}`,
|
|
409
|
+
{
|
|
410
|
+
method: 'DELETE',
|
|
411
|
+
withCredentials: true,
|
|
412
|
+
},
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* check Password.
|
|
418
|
+
* @param {CheckPasswordrRequest} params
|
|
419
|
+
* @return {Promise<any>}
|
|
420
|
+
*/
|
|
421
|
+
public async checkPassword(params: CheckPasswordrRequest): Promise<void> {
|
|
422
|
+
return this._config.request<any>(`${ApiUrls.CHECK_PWD_URL}`, {
|
|
423
|
+
method: 'POST',
|
|
424
|
+
withCredentials: true,
|
|
425
|
+
body: params,
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* check Password.
|
|
431
|
+
* @param {CheckPasswordrRequest} params
|
|
432
|
+
* @return {Promise<any>}
|
|
433
|
+
*/
|
|
434
|
+
public async bindPhone(params: BindPhoneRequest): Promise<void> {
|
|
435
|
+
return this._config.request<any>(`${ApiUrls.BIND_CONTACT_URL}`, {
|
|
436
|
+
method: 'PATCH',
|
|
437
|
+
withCredentials: true,
|
|
438
|
+
body: params,
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* check Password.
|
|
444
|
+
* @param {CheckPasswordrRequest} params
|
|
445
|
+
* @return {Promise<any>}
|
|
446
|
+
*/
|
|
447
|
+
public async bindEmail(params: BindEmailRequest): Promise<void> {
|
|
448
|
+
return this._config.request<any>(`${ApiUrls.BIND_CONTACT_URL}`, {
|
|
449
|
+
method: 'PATCH',
|
|
450
|
+
withCredentials: true,
|
|
451
|
+
body: params,
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Set Password.
|
|
457
|
+
* @param {SetPasswordrRequest} params
|
|
458
|
+
* @return {Promise<any>}
|
|
459
|
+
*/
|
|
460
|
+
public async setPassword(params: SetPasswordRequest): Promise<void> {
|
|
461
|
+
return this._config.request<any>(`${ApiUrls.AUTH_SET_PASSWORD}`, {
|
|
462
|
+
method: 'PATCH',
|
|
463
|
+
withCredentials: true,
|
|
464
|
+
body: params,
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* updatePasswordByOld 使用旧密码修改密码,如果已经绑定手机号,请先:sudo,再修改密码
|
|
470
|
+
* @param {SetPasswordrRequest} params
|
|
471
|
+
* @return {Promise<any>}
|
|
472
|
+
*/
|
|
473
|
+
public async updatePasswordByOld(params: UpdatePasswordRequest): Promise<void> {
|
|
474
|
+
const sudoToken = await this.sudo({ password: params.old_password })
|
|
475
|
+
return this.setPassword({
|
|
476
|
+
sudo_token: sudoToken.sudo_token,
|
|
477
|
+
new_password: params.new_password,
|
|
478
|
+
})
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* sudo
|
|
484
|
+
* @param {sudo} params
|
|
485
|
+
* @return {Promise<any>}
|
|
486
|
+
*/
|
|
487
|
+
public async sudo(params: SudoRequest): Promise<SudoResponse> {
|
|
488
|
+
return this._config.request<SudoResponse>(`${ApiUrls.SUDO_URL}`, {
|
|
489
|
+
method: 'POST',
|
|
490
|
+
withCredentials: true,
|
|
491
|
+
body: params,
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Get the current user verification.
|
|
497
|
+
* @param {GetVerificationRequest} params A GetVerificationRequest Object.
|
|
498
|
+
* @return {Promise<GetVerificationResponse>} A Promise<GetVerificationResponse> object.
|
|
499
|
+
*/
|
|
500
|
+
public async getCurUserVerification(
|
|
501
|
+
params: GetVerificationRequest,
|
|
502
|
+
): Promise<GetVerificationResponse> {
|
|
503
|
+
params.target = 'CUR_USER';
|
|
504
|
+
return this._config.request<GetVerificationResponse>(
|
|
505
|
+
ApiUrls.VERIFICATION_URL,
|
|
506
|
+
{
|
|
507
|
+
method: 'POST',
|
|
508
|
+
body: params,
|
|
509
|
+
withCredentials: true,
|
|
510
|
+
withCaptcha: true
|
|
511
|
+
},
|
|
512
|
+
);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* change binded provider.
|
|
517
|
+
* @param {GetVerificationRequest} params A GetVerificationRequest Object.
|
|
518
|
+
* @return {Promise<GetVerificationResponse>} A Promise<GetVerificationResponse> object.
|
|
519
|
+
*/
|
|
520
|
+
public async changeBindedProvider(
|
|
521
|
+
params: ChangeBindedProviderRequest,
|
|
522
|
+
): Promise<ChangeBindedProviderResponse> {
|
|
523
|
+
return this._config.request<ChangeBindedProviderResponse>(
|
|
524
|
+
`${ApiUrls.PROVIDER_LIST}/${params.provider_id}/trans`,
|
|
525
|
+
{
|
|
526
|
+
method: 'POST',
|
|
527
|
+
body: {
|
|
528
|
+
provider_trans_token: params.trans_token,
|
|
529
|
+
},
|
|
530
|
+
withCredentials: true,
|
|
531
|
+
},
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Patch the user profile.
|
|
537
|
+
* @param {UserProfile} params A UserProfile Object.
|
|
538
|
+
* @return {Promise<UserProfile>} A Promise<UserProfile> object.
|
|
539
|
+
*/
|
|
540
|
+
public async setUserProfile(params: UserProfile): Promise<UserProfile> {
|
|
541
|
+
return this._config.request<UserProfile>(ApiUrls.USER_PRIFILE_URL, {
|
|
542
|
+
method: 'PATCH',
|
|
543
|
+
body: params,
|
|
544
|
+
withCredentials: true,
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* Patch the user profile.
|
|
550
|
+
* @param {QueryUserProfileReq} appended_params A QueryUserProfileReq Object.
|
|
551
|
+
* @return {Promise<UserProfile>} A Promise<UserProfile> object.
|
|
552
|
+
*/
|
|
553
|
+
public async queryUserProfile(
|
|
554
|
+
params: QueryUserProfileRequest,
|
|
555
|
+
): Promise<QueryUserProfileResponse> {
|
|
556
|
+
// let url = new URL(ApiUrls.USER_QUERY_URL);
|
|
557
|
+
const searchParams = new URLSearchParams(params as any);
|
|
558
|
+
// url.search = searchParams.toString();
|
|
559
|
+
return this._config.request<QueryUserProfileResponse>(`${ApiUrls.USER_QUERY_URL}?${searchParams.toString()}`, {
|
|
560
|
+
method: 'GET',
|
|
561
|
+
withCredentials: true,
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* setCustomSignFunc set the get ticket function
|
|
567
|
+
* @param getTickFn
|
|
568
|
+
*/
|
|
569
|
+
public setCustomSignFunc(getTickFn: GetCustomSignTicketFn) {
|
|
570
|
+
this._getCustomSignTicketFn = getTickFn
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* SignInWithCustomTicket custom signIn
|
|
575
|
+
* @constructor
|
|
576
|
+
*/
|
|
577
|
+
public async signInWithCustomTicket(): Promise<Credentials> {
|
|
578
|
+
const customTicket = await this._getCustomSignTicketFn()
|
|
579
|
+
return this.signInWithProvider({
|
|
580
|
+
provider_id: 'custom',
|
|
581
|
+
provider_token: customTicket
|
|
582
|
+
})
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* Reset password
|
|
587
|
+
* @param {ResetPasswordRequest} params
|
|
588
|
+
* @returns {Promise<void>}
|
|
589
|
+
* @memberof Auth
|
|
590
|
+
*/
|
|
591
|
+
public async resetPassword(params: ResetPasswordRequest): Promise<void> {
|
|
592
|
+
return this._config.request(ApiUrls.AUTH_RESET_PASSWORD, {
|
|
593
|
+
method: 'POST',
|
|
594
|
+
body: params,
|
|
595
|
+
// withCredentials: true
|
|
596
|
+
})
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* device authorization
|
|
601
|
+
* @param {DeviceAuthorizeRequest} params
|
|
602
|
+
* @returns {Promise<DeviceAuthorizeResponse>}
|
|
603
|
+
* @memberof Auth
|
|
604
|
+
*/
|
|
605
|
+
public async deviceAuthorize(params: DeviceAuthorizeRequest): Promise<DeviceAuthorizeResponse> {
|
|
606
|
+
return this._config.request(ApiUrls.AUTH_GET_DEVICE_CODE, {
|
|
607
|
+
method: 'POST',
|
|
608
|
+
body: params,
|
|
609
|
+
withCredentials: true
|
|
610
|
+
})
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
public async checkUsername(params: CheckUsernameRequest): Promise<void> {
|
|
614
|
+
return this._config.request(ApiUrls.CHECK_USERNAME, {
|
|
615
|
+
method: 'GET',
|
|
616
|
+
body: params,
|
|
617
|
+
withCredentials: true
|
|
618
|
+
})
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
public async checkIfUserExist(params: CheckIfUserExistRequest): Promise<CheckIfUserExistResponse> {
|
|
622
|
+
const searchParams = new URLSearchParams(params as any);
|
|
623
|
+
|
|
624
|
+
return this._config.request<CheckIfUserExistResponse>(`${ApiUrls.CHECK_IF_USER_EXIST}?${searchParams.toString()}`, {
|
|
625
|
+
method: 'GET'
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
public async loginScope(): Promise<string> {
|
|
630
|
+
return this._config.credentialsClient.getScope()
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
private static parseParamsToSearch(params: any): string {
|
|
634
|
+
for (let key in params) {
|
|
635
|
+
if (!params[key]) {
|
|
636
|
+
delete params[key]
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
const searchParams = new URLSearchParams(params as any);
|
|
640
|
+
return searchParams.toString();
|
|
641
|
+
}
|
|
642
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export enum ApiUrls {
|
|
2
|
+
AUTH_SIGN_IN_URL = '/auth/v1/signin',
|
|
3
|
+
AUTH_SIGN_IN_ANONYMOUSLY_URL = '/auth/v1/signin/anonymously',
|
|
4
|
+
AUTH_SIGN_IN_WITH_PROVIDER_URL = '/auth/v1/signin/with/provider',
|
|
5
|
+
AUTH_SIGN_UP_URL = '/auth/v1/signup',
|
|
6
|
+
AUTH_TOKEN_URL = '/auth/v1/token',
|
|
7
|
+
AUTH_REVOKE_URL = '/auth/v1/revoke',
|
|
8
|
+
PROVIDER_BIND_URL = '/auth/v1/user/provider/bind',
|
|
9
|
+
PROVIDER_TOKEN_URL = '/auth/v1/provider/token',
|
|
10
|
+
PROVIDER_URI_URL = '/auth/v1/provider/uri',
|
|
11
|
+
USER_ME_URL = '/auth/v1/user/me',
|
|
12
|
+
USER_QUERY_URL = '/auth/v1/user/query',
|
|
13
|
+
USER_PRIFILE_URL = '/auth/v1/user/profile',
|
|
14
|
+
USER_TRANS_BY_PROVIDER_URL = '/auth/v1/user/trans/by/provider',
|
|
15
|
+
VERIFICATION_URL = '/auth/v1/verification',
|
|
16
|
+
VERIFY_URL = '/auth/v1/verification/verify',
|
|
17
|
+
PROVIDER_LIST = '/auth/v1/user/provider',
|
|
18
|
+
PROVIDER_UNBIND_URL = '/auth/v1/user/provider',
|
|
19
|
+
CHECK_PWD_URL = '/auth/v1/user/sudo',
|
|
20
|
+
SUDO_URL = '/auth/v1/user/sudo',
|
|
21
|
+
BIND_CONTACT_URL = '/auth/v1/user/contact',
|
|
22
|
+
AUTH_SET_PASSWORD = '/auth/v1/user/password',
|
|
23
|
+
AUTH_RESET_PASSWORD = '/auth/v1/reset',
|
|
24
|
+
AUTH_GET_DEVICE_CODE = '/auth/v1/device/code',
|
|
25
|
+
CHECK_USERNAME = '/auth/v1/checkUsername',
|
|
26
|
+
CHECK_IF_USER_EXIST = '/auth/v1/checkIfUserExist'
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export enum VerificationUsages {
|
|
30
|
+
REGISTER = 'REGISTER',
|
|
31
|
+
SIGN_IN = 'SIGN_IN',
|
|
32
|
+
PASSWORD_RESET = 'PASSWORD_RESET',
|
|
33
|
+
EMAIL_ADDRESS_CHANGE = 'EMAIL_ADDRESS_CHANGE',
|
|
34
|
+
PHONE_NUMBER_CHANGE = 'PHONE_NUMBER_CHANGE',
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export enum ErrorType {
|
|
38
|
+
INVALID_ARGUMENT = 'invalid_argument',
|
|
39
|
+
DEADLINE_EXCEEDED = 'deadline_exceeded',
|
|
40
|
+
NOT_FOUND = 'not_found',
|
|
41
|
+
ALREADY_EXISTS = 'already_exists',
|
|
42
|
+
PERMISSION_DENIED = 'permission_denied',
|
|
43
|
+
ABORTED = 'aborted',
|
|
44
|
+
OUT_OF_RANGE = 'out_of_range',
|
|
45
|
+
UNIMPLEMENTED = 'unimplemented',
|
|
46
|
+
INTERNAL = 'internal',
|
|
47
|
+
UNAVAILABLE = 'unavailable',
|
|
48
|
+
DATA_LOSS = 'data_loss',
|
|
49
|
+
// CommonError
|
|
50
|
+
CAPTCHA_REQUIRED = 'captcha_required',
|
|
51
|
+
CAPTCHA_INVALID = 'captcha_invalid',
|
|
52
|
+
INVALID_PASSWORD = 'invalid_password',
|
|
53
|
+
PASSWORD_NOT_SET = 'password_not_set',
|
|
54
|
+
INVALID_STATUS = 'invalid_status',
|
|
55
|
+
USER_PENDING = 'user_pending',
|
|
56
|
+
USER_BLOCKED = 'user_blocked',
|
|
57
|
+
}
|