@cloudbase/oauth 2.5.25-beta.0 → 2.5.27-beta.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/dist/cjs/auth/apis.d.ts +15 -2
- package/dist/cjs/auth/apis.js +78 -22
- package/dist/cjs/auth/consts.d.ts +5 -0
- package/dist/cjs/auth/consts.js +8 -2
- package/dist/cjs/auth/models.d.ts +2 -0
- package/dist/cjs/auth/models.js +1 -1
- package/dist/cjs/oauth2client/models.d.ts +1 -0
- package/dist/cjs/oauth2client/models.js +1 -1
- package/dist/cjs/oauth2client/oauth2client.d.ts +1 -1
- package/dist/cjs/oauth2client/oauth2client.js +62 -46
- package/dist/cjs/utils/index.d.ts +1 -0
- package/dist/cjs/utils/index.js +49 -0
- package/dist/esm/auth/apis.d.ts +15 -2
- package/dist/esm/auth/apis.js +79 -23
- package/dist/esm/auth/consts.d.ts +5 -0
- package/dist/esm/auth/consts.js +7 -1
- package/dist/esm/auth/models.d.ts +2 -0
- package/dist/esm/auth/models.js +1 -1
- package/dist/esm/oauth2client/models.d.ts +1 -0
- package/dist/esm/oauth2client/models.js +1 -1
- package/dist/esm/oauth2client/oauth2client.d.ts +1 -1
- package/dist/esm/oauth2client/oauth2client.js +62 -46
- package/dist/esm/utils/index.d.ts +1 -0
- package/dist/esm/utils/index.js +45 -0
- package/package.json +2 -2
- package/src/auth/apis.ts +49 -11
- package/src/auth/consts.ts +6 -0
- package/src/auth/models.ts +3 -1
- package/src/oauth2client/models.ts +1 -0
- package/src/oauth2client/oauth2client.ts +37 -27
- package/src/utils/index.ts +61 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ErrorType } from './consts'
|
|
2
|
+
import { ApiUrls, ApiUrlsV2 } from '../auth/consts'
|
|
2
3
|
|
|
3
4
|
import { AuthClient, SimpleStorage } from './interface'
|
|
4
5
|
|
|
@@ -291,7 +292,7 @@ export class OAuth2Client implements AuthClient {
|
|
|
291
292
|
private storage: SimpleStorage
|
|
292
293
|
private deviceID?: string
|
|
293
294
|
private tokenInURL?: boolean
|
|
294
|
-
private refreshTokenFunc: (refreshToken?: string) => Promise<Credentials>
|
|
295
|
+
private refreshTokenFunc: (refreshToken?: string, credentials?: Credentials) => Promise<Credentials>
|
|
295
296
|
private headers?: { [key: string]: string }
|
|
296
297
|
private singlePromise: SinglePromise = new SinglePromise()
|
|
297
298
|
private anonymousSignInFunc: (Credentials) => Promise<Credentials | void>
|
|
@@ -469,6 +470,30 @@ export class OAuth2Client implements AuthClient {
|
|
|
469
470
|
return credentials.groups
|
|
470
471
|
}
|
|
471
472
|
|
|
473
|
+
/**
|
|
474
|
+
* Refresh expired token.
|
|
475
|
+
* @param {Credentials} credentials
|
|
476
|
+
* @return {Promise<Credentials>}
|
|
477
|
+
*/
|
|
478
|
+
public async refreshToken(credentials: Credentials): Promise<Credentials> {
|
|
479
|
+
return this.singlePromise.run('_refreshToken', async () => {
|
|
480
|
+
if (!credentials || !credentials.refresh_token) {
|
|
481
|
+
return this.unAuthenticatedError('no refresh token found in credentials')
|
|
482
|
+
}
|
|
483
|
+
try {
|
|
484
|
+
const newCredentials: Credentials = await this.refreshTokenFunc(credentials.refresh_token, credentials)
|
|
485
|
+
await this.localCredentials.setCredentials(newCredentials)
|
|
486
|
+
return newCredentials
|
|
487
|
+
} catch (error) {
|
|
488
|
+
if (error.error === ErrorType.INVALID_GRANT) {
|
|
489
|
+
await this.localCredentials.setCredentials(null)
|
|
490
|
+
return this.unAuthenticatedError(error.error_description)
|
|
491
|
+
}
|
|
492
|
+
return Promise.reject(error)
|
|
493
|
+
}
|
|
494
|
+
})
|
|
495
|
+
}
|
|
496
|
+
|
|
472
497
|
/**
|
|
473
498
|
* Check retry value.
|
|
474
499
|
* @param {number} retry
|
|
@@ -518,30 +543,6 @@ export class OAuth2Client implements AuthClient {
|
|
|
518
543
|
})
|
|
519
544
|
}
|
|
520
545
|
|
|
521
|
-
/**
|
|
522
|
-
* Refresh expired token.
|
|
523
|
-
* @param {Credentials} credentials
|
|
524
|
-
* @return {Promise<Credentials>}
|
|
525
|
-
*/
|
|
526
|
-
private async refreshToken(credentials: Credentials): Promise<Credentials> {
|
|
527
|
-
return this.singlePromise.run('_refreshToken', async () => {
|
|
528
|
-
if (!credentials || !credentials.refresh_token) {
|
|
529
|
-
return this.unAuthenticatedError('no refresh token found in credentials')
|
|
530
|
-
}
|
|
531
|
-
try {
|
|
532
|
-
const newCredentials: Credentials = await this.refreshTokenFunc(credentials.refresh_token,)
|
|
533
|
-
await this.localCredentials.setCredentials(newCredentials)
|
|
534
|
-
return newCredentials
|
|
535
|
-
} catch (error) {
|
|
536
|
-
if (error.error === ErrorType.INVALID_GRANT) {
|
|
537
|
-
await this.localCredentials.setCredentials(null)
|
|
538
|
-
return this.unAuthenticatedError(error.error_description)
|
|
539
|
-
}
|
|
540
|
-
return Promise.reject(error)
|
|
541
|
-
}
|
|
542
|
-
})
|
|
543
|
-
}
|
|
544
|
-
|
|
545
546
|
/**
|
|
546
547
|
* anonymous signIn
|
|
547
548
|
* @param {Credentials} credentials
|
|
@@ -577,11 +578,18 @@ export class OAuth2Client implements AuthClient {
|
|
|
577
578
|
* @param {string} refreshToken
|
|
578
579
|
* @return {Promise<Credentials>}
|
|
579
580
|
*/
|
|
580
|
-
private defaultRefreshTokenFunc(refreshToken?: string,): Promise<Credentials> {
|
|
581
|
+
private async defaultRefreshTokenFunc(refreshToken?: string, credentials?: Credentials): Promise<Credentials> {
|
|
581
582
|
if (refreshToken === undefined || refreshToken === '') {
|
|
582
583
|
return this.unAuthenticatedError('refresh token not found')
|
|
583
584
|
}
|
|
584
|
-
|
|
585
|
+
|
|
586
|
+
let url: string = ApiUrls.AUTH_TOKEN_URL
|
|
587
|
+
|
|
588
|
+
if (credentials?.version === 'v2') {
|
|
589
|
+
url = ApiUrlsV2.AUTH_TOKEN_URL
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
const newCredentials: Credentials = await this.request(url, {
|
|
585
593
|
method: 'POST',
|
|
586
594
|
body: {
|
|
587
595
|
client_id: this.clientId,
|
|
@@ -590,6 +598,8 @@ export class OAuth2Client implements AuthClient {
|
|
|
590
598
|
refresh_token: refreshToken,
|
|
591
599
|
},
|
|
592
600
|
})
|
|
601
|
+
|
|
602
|
+
return { ...newCredentials, version: credentials?.version || 'v1' }
|
|
593
603
|
}
|
|
594
604
|
|
|
595
605
|
/**
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 深拷贝
|
|
3
|
+
* @param {*} value 需要拷贝的值
|
|
4
|
+
* @returns {*} 深拷贝后的值
|
|
5
|
+
* @example
|
|
6
|
+
* const obj = { a: 1, b: { c: 2 } };
|
|
7
|
+
* const newObj = deepClone(obj);
|
|
8
|
+
*/
|
|
9
|
+
export const deepClone = (value: any) => {
|
|
10
|
+
const clone = (copiedValue: any) => {
|
|
11
|
+
for (const key in value) {
|
|
12
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
13
|
+
if (value.hasOwnProperty(key)) {
|
|
14
|
+
copiedValue[key] = deepClone(value[key])
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return copiedValue
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const type = value === null || value === undefined
|
|
21
|
+
? 'NullOrUndefined'
|
|
22
|
+
: Object.prototype.toString.call(value).slice(8, -1)
|
|
23
|
+
|
|
24
|
+
if (
|
|
25
|
+
[
|
|
26
|
+
'Int8Array',
|
|
27
|
+
'Uint8Array',
|
|
28
|
+
'Uint8ClampedArray',
|
|
29
|
+
'Int16Array',
|
|
30
|
+
'Uint16Array',
|
|
31
|
+
'Int32Array',
|
|
32
|
+
'Uint32Array',
|
|
33
|
+
'Float32Array',
|
|
34
|
+
'Float64Array',
|
|
35
|
+
'BigInt64Array',
|
|
36
|
+
'BigUint64Array',
|
|
37
|
+
].includes(type)
|
|
38
|
+
) {
|
|
39
|
+
return value.slice()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
switch (type) {
|
|
43
|
+
case 'Object':
|
|
44
|
+
return clone(Object.create(Object.getPrototypeOf(value)))
|
|
45
|
+
case 'Array':
|
|
46
|
+
return clone([])
|
|
47
|
+
case 'Date':
|
|
48
|
+
return new Date(value.valueOf())
|
|
49
|
+
case 'RegExp':
|
|
50
|
+
return new RegExp(
|
|
51
|
+
value.source,
|
|
52
|
+
(value.global ? 'g' : '')
|
|
53
|
+
+ (value.ignoreCase ? 'i' : '')
|
|
54
|
+
+ (value.multiline ? 'm' : '')
|
|
55
|
+
+ (value.sticky ? 'y' : '')
|
|
56
|
+
+ (value.unicode ? 'u' : ''),
|
|
57
|
+
)
|
|
58
|
+
default:
|
|
59
|
+
return value
|
|
60
|
+
}
|
|
61
|
+
}
|