@cloudbase/oauth 2.19.1 → 2.19.3
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 +10 -0
- package/dist/cjs/auth/apis.js +6 -1
- package/dist/cjs/auth/consts.d.ts +50 -49
- package/dist/cjs/auth/consts.js +52 -51
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +10 -3
- package/dist/cjs/oauth2client/models.d.ts +5 -0
- package/dist/cjs/oauth2client/models.js +1 -1
- package/dist/cjs/oauth2client/oauth2client.d.ts +3 -0
- package/dist/cjs/oauth2client/oauth2client.js +94 -59
- package/dist/esm/auth/apis.d.ts +10 -0
- package/dist/esm/auth/apis.js +5 -0
- package/dist/esm/auth/consts.d.ts +50 -49
- package/dist/esm/auth/consts.js +50 -49
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +7 -1
- package/dist/esm/oauth2client/models.d.ts +5 -0
- package/dist/esm/oauth2client/oauth2client.d.ts +3 -0
- package/dist/esm/oauth2client/oauth2client.js +33 -10
- package/dist/miniprogram/index.js +1 -1
- package/package.json +4 -3
- package/src/auth/apis.ts +10 -0
- package/src/auth/consts.ts +51 -49
- package/src/index.ts +7 -1
- package/src/oauth2client/models.ts +5 -0
- package/src/oauth2client/oauth2client.ts +39 -10
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ErrorType } from './consts'
|
|
2
|
-
import { ApiUrls, ApiUrlsV2 } from '../auth/consts'
|
|
2
|
+
import { ApiUrls, ApiUrlsV2, AUTH_API_PREFIX } from '../auth/consts'
|
|
3
3
|
|
|
4
4
|
import { AuthClient, SimpleStorage } from './interface'
|
|
5
5
|
|
|
@@ -18,6 +18,9 @@ import { getPathName } from '../utils/index'
|
|
|
18
18
|
import { SinglePromise } from '../utils/function/single-promise'
|
|
19
19
|
import { weBtoa } from '../utils/base64'
|
|
20
20
|
import { isMatch } from '../utils/cloudbase-adapter-wx_mp'
|
|
21
|
+
import { AuthOptions } from '../auth/apis'
|
|
22
|
+
import { ICloudbaseConfig } from '@cloudbase/types'
|
|
23
|
+
import { langEvent } from '@cloudbase/utilities'
|
|
21
24
|
|
|
22
25
|
const RequestIdHeaderName = 'x-request-id'
|
|
23
26
|
const DeviceIdHeaderName = 'x-device-id'
|
|
@@ -292,7 +295,9 @@ export class OAuth2Client implements AuthClient {
|
|
|
292
295
|
private static retryInterval = 1000
|
|
293
296
|
|
|
294
297
|
private apiOrigin: string
|
|
298
|
+
private apiPath: string
|
|
295
299
|
private clientId: string
|
|
300
|
+
private i18n: ICloudbaseConfig['i18n']
|
|
296
301
|
private retry: number
|
|
297
302
|
private clientSecret?: string
|
|
298
303
|
private baseRequest: <T>(url: string, options?: RequestOptions) => Promise<T>
|
|
@@ -306,6 +311,7 @@ export class OAuth2Client implements AuthClient {
|
|
|
306
311
|
private anonymousSignInFunc: (Credentials) => Promise<Credentials | void>
|
|
307
312
|
private wxCloud: any
|
|
308
313
|
private basicAuth: string
|
|
314
|
+
private onCredentialsError: AuthOptions['onCredentialsError'] | undefined
|
|
309
315
|
|
|
310
316
|
/**
|
|
311
317
|
* constructor
|
|
@@ -321,7 +327,9 @@ export class OAuth2Client implements AuthClient {
|
|
|
321
327
|
}
|
|
322
328
|
|
|
323
329
|
this.apiOrigin = options.apiOrigin
|
|
330
|
+
this.apiPath = options.apiPath || AUTH_API_PREFIX
|
|
324
331
|
this.clientId = options.clientId
|
|
332
|
+
this.i18n = options.i18n
|
|
325
333
|
this.singlePromise = new SinglePromise({ clientId: this.clientId })
|
|
326
334
|
this.retry = this.formatRetry(options.retry, OAuth2Client.defaultRetry)
|
|
327
335
|
if (options.baseRequest) {
|
|
@@ -354,6 +362,11 @@ export class OAuth2Client implements AuthClient {
|
|
|
354
362
|
}
|
|
355
363
|
this.refreshTokenFunc = options.refreshTokenFunc || this.defaultRefreshTokenFunc
|
|
356
364
|
this.anonymousSignInFunc = options.anonymousSignInFunc
|
|
365
|
+
this.onCredentialsError = options.onCredentialsError
|
|
366
|
+
|
|
367
|
+
langEvent.bus.on(langEvent.LANG_CHANGE_EVENT, (params) => {
|
|
368
|
+
this.i18n = params.data?.i18n || this.i18n
|
|
369
|
+
})
|
|
357
370
|
}
|
|
358
371
|
|
|
359
372
|
/**
|
|
@@ -387,7 +400,7 @@ export class OAuth2Client implements AuthClient {
|
|
|
387
400
|
options = {}
|
|
388
401
|
}
|
|
389
402
|
const retry: number = this.formatRetry(options.retry, this.retry)
|
|
390
|
-
options.headers = options.headers
|
|
403
|
+
options.headers = { ...options.headers, [this.i18n?.LANG_HEADER_KEY]: this.i18n?.lang }
|
|
391
404
|
if (this.headers) {
|
|
392
405
|
options.headers = {
|
|
393
406
|
...this.headers,
|
|
@@ -423,7 +436,7 @@ export class OAuth2Client implements AuthClient {
|
|
|
423
436
|
}
|
|
424
437
|
}
|
|
425
438
|
if (url.startsWith('/')) {
|
|
426
|
-
url = this.apiOrigin
|
|
439
|
+
url = `${this.apiOrigin}${this.apiPath}${url}`
|
|
427
440
|
}
|
|
428
441
|
let response: T | null = null
|
|
429
442
|
const maxRequestTimes: number = retry + 1
|
|
@@ -499,7 +512,9 @@ export class OAuth2Client implements AuthClient {
|
|
|
499
512
|
public async getCredentials(): Promise<Credentials | null> {
|
|
500
513
|
let credentials: Credentials = await this.localCredentials.getCredentials()
|
|
501
514
|
if (!credentials) {
|
|
502
|
-
|
|
515
|
+
const msg = 'credentials not found'
|
|
516
|
+
this.onCredentialsError?.({ msg })
|
|
517
|
+
return this.unAuthenticatedError(msg)
|
|
503
518
|
}
|
|
504
519
|
if (isCredentialsExpired(credentials)) {
|
|
505
520
|
if (credentials.refresh_token) {
|
|
@@ -509,13 +524,16 @@ export class OAuth2Client implements AuthClient {
|
|
|
509
524
|
if (credentials.scope === 'anonymous') {
|
|
510
525
|
credentials = await this.anonymousLogin(credentials)
|
|
511
526
|
} else {
|
|
527
|
+
this.onCredentialsError?.({ msg: error.error_description })
|
|
512
528
|
return Promise.reject(error)
|
|
513
529
|
}
|
|
514
530
|
}
|
|
515
531
|
} else if (credentials.scope === 'anonymous') {
|
|
516
532
|
credentials = await this.anonymousLogin(credentials)
|
|
517
533
|
} else {
|
|
518
|
-
|
|
534
|
+
const msg = 'no refresh token found in credentials'
|
|
535
|
+
this.onCredentialsError?.({ msg })
|
|
536
|
+
return this.unAuthenticatedError(msg)
|
|
519
537
|
}
|
|
520
538
|
}
|
|
521
539
|
return credentials
|
|
@@ -536,7 +554,9 @@ export class OAuth2Client implements AuthClient {
|
|
|
536
554
|
public async getScope(): Promise<string> {
|
|
537
555
|
const credentials: Credentials = await this.localCredentials.getCredentials()
|
|
538
556
|
if (!credentials) {
|
|
539
|
-
|
|
557
|
+
const msg = 'credentials not found'
|
|
558
|
+
this.onCredentialsError?.({ msg })
|
|
559
|
+
return this.unAuthenticatedError(msg)
|
|
540
560
|
}
|
|
541
561
|
return credentials.scope
|
|
542
562
|
}
|
|
@@ -544,7 +564,9 @@ export class OAuth2Client implements AuthClient {
|
|
|
544
564
|
public async getGroups(): Promise<string[]> {
|
|
545
565
|
const credentials: Credentials = await this.localCredentials.getCredentials()
|
|
546
566
|
if (!credentials) {
|
|
547
|
-
|
|
567
|
+
const msg = 'credentials not found'
|
|
568
|
+
this.onCredentialsError?.({ msg })
|
|
569
|
+
return this.unAuthenticatedError(msg)
|
|
548
570
|
}
|
|
549
571
|
return credentials.groups
|
|
550
572
|
}
|
|
@@ -557,7 +579,9 @@ export class OAuth2Client implements AuthClient {
|
|
|
557
579
|
public async refreshToken(credentials: Credentials): Promise<Credentials> {
|
|
558
580
|
return this.singlePromise.run('_refreshToken', async () => {
|
|
559
581
|
if (!credentials || !credentials.refresh_token) {
|
|
560
|
-
|
|
582
|
+
const msg = 'no refresh token found in credentials'
|
|
583
|
+
this.onCredentialsError?.({ msg })
|
|
584
|
+
return this.unAuthenticatedError(msg)
|
|
561
585
|
}
|
|
562
586
|
try {
|
|
563
587
|
const newCredentials: Credentials = await this.refreshTokenFunc(credentials.refresh_token, credentials)
|
|
@@ -566,8 +590,11 @@ export class OAuth2Client implements AuthClient {
|
|
|
566
590
|
} catch (error) {
|
|
567
591
|
if (error.error === ErrorType.INVALID_GRANT) {
|
|
568
592
|
await this.localCredentials.setCredentials(null)
|
|
569
|
-
|
|
593
|
+
const msg = error.error_description
|
|
594
|
+
this.onCredentialsError?.({ msg })
|
|
595
|
+
return this.unAuthenticatedError(msg)
|
|
570
596
|
}
|
|
597
|
+
this.onCredentialsError?.({ msg: error.error_description })
|
|
571
598
|
return Promise.reject(error)
|
|
572
599
|
}
|
|
573
600
|
})
|
|
@@ -666,7 +693,9 @@ export class OAuth2Client implements AuthClient {
|
|
|
666
693
|
*/
|
|
667
694
|
private async defaultRefreshTokenFunc(refreshToken?: string, credentials?: Credentials): Promise<Credentials> {
|
|
668
695
|
if (refreshToken === undefined || refreshToken === '') {
|
|
669
|
-
|
|
696
|
+
const msg = 'refresh token not found'
|
|
697
|
+
this.onCredentialsError?.({ msg })
|
|
698
|
+
return this.unAuthenticatedError(msg)
|
|
670
699
|
}
|
|
671
700
|
|
|
672
701
|
let url: string = ApiUrls.AUTH_TOKEN_URL
|