@cloudbase/oauth 2.17.13 → 2.17.15
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 +2 -0
- package/dist/cjs/auth/apis.js +17 -12
- package/dist/cjs/captcha/captcha.d.ts +5 -0
- package/dist/cjs/captcha/captcha.js +7 -3
- package/dist/cjs/index.js +3 -3
- package/dist/cjs/utils/function/single-promise.js +6 -5
- package/dist/cjs/utils/mp.js +4 -1
- package/dist/esm/auth/apis.d.ts +2 -0
- package/dist/esm/auth/apis.js +15 -10
- package/dist/esm/captcha/captcha.d.ts +5 -0
- package/dist/esm/captcha/captcha.js +5 -2
- package/dist/esm/index.js +2 -1
- package/dist/esm/utils/function/single-promise.js +5 -4
- package/dist/esm/utils/mp.js +3 -0
- package/dist/miniprogram/index.js +1 -1
- package/package.json +3 -3
- package/src/auth/apis.ts +31 -18
- package/src/captcha/captcha.ts +8 -2
- package/src/index.ts +2 -1
- package/src/utils/function/single-promise.ts +6 -4
- package/src/utils/mp.ts +4 -0
- package/webpack/webpack.miniprogram.js +1 -0
package/src/captcha/captcha.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { AuthClientRequestOptions } from '../oauth2client/models'
|
|
|
4
4
|
import { defaultStorage } from '../oauth2client/oauth2client'
|
|
5
5
|
import { isInMpWebView, isMp } from '../utils/mp'
|
|
6
6
|
import MyURLSearchParams from '../utils/urlSearchParams'
|
|
7
|
+
import { SDKAdapterInterface } from '@cloudbase/adapter-interface'
|
|
7
8
|
|
|
8
9
|
export interface CaptchaOptions {
|
|
9
10
|
clientId: string
|
|
@@ -11,6 +12,7 @@ export interface CaptchaOptions {
|
|
|
11
12
|
storage: SimpleStorage
|
|
12
13
|
// 打开网页并通过URL回调获取 CaptchaToken,针对不通的平台,该函数可以自定义实现, 默认集成浏览器端认证
|
|
13
14
|
openURIWithCallback?: OpenURIWithCallbackFuction
|
|
15
|
+
adapter?: SDKAdapterInterface & { isMatch?: () => boolean }
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
type OpenURIWithCallbackFuction = (url: string) => Promise<CaptchaToken>
|
|
@@ -51,6 +53,10 @@ export class Captcha {
|
|
|
51
53
|
this.tokenSectionName = `captcha_${opts.clientId}`
|
|
52
54
|
}
|
|
53
55
|
|
|
56
|
+
public isMatch() {
|
|
57
|
+
return this.config.adapter?.isMatch?.() || isMp()
|
|
58
|
+
}
|
|
59
|
+
|
|
54
60
|
/**
|
|
55
61
|
* request http like simple fetch api, exp:request('/v1/user/me', {withCredentials:true})
|
|
56
62
|
* @param {string} url
|
|
@@ -83,7 +89,7 @@ export class Captcha {
|
|
|
83
89
|
}
|
|
84
90
|
|
|
85
91
|
private getDefaultOpenURIWithCallback(): OpenURIWithCallbackFuction {
|
|
86
|
-
if (!
|
|
92
|
+
if (!this.isMatch() && !isInMpWebView()) {
|
|
87
93
|
if (window.location.search.indexOf('__captcha') > 0) {
|
|
88
94
|
document.body.style.display = 'none'
|
|
89
95
|
}
|
|
@@ -168,7 +174,7 @@ export class Captcha {
|
|
|
168
174
|
captcha_token?: string
|
|
169
175
|
expires_in?: number
|
|
170
176
|
}
|
|
171
|
-
if (!
|
|
177
|
+
if (!this.isMatch() && !isInMpWebView()) {
|
|
172
178
|
const redirect_uri = `${window.location.origin + window.location.pathname}?__captcha=on`
|
|
173
179
|
captchaTokenResp = await this.config.request<GetCaptchaResponse>(ApiUrls.GET_CAPTCHA_URL, {
|
|
174
180
|
method: 'POST',
|
package/src/index.ts
CHANGED
|
@@ -11,7 +11,7 @@ export class CloudbaseOAuth {
|
|
|
11
11
|
public authApi: Auth
|
|
12
12
|
|
|
13
13
|
constructor(authOptions: AuthOptions) {
|
|
14
|
-
const { apiOrigin, clientId, env, storage, request, baseRequest, anonymousSignInFunc, wxCloud } = authOptions
|
|
14
|
+
const { apiOrigin, clientId, env, storage, request, baseRequest, anonymousSignInFunc, wxCloud, adapter } = authOptions
|
|
15
15
|
this.oauth2client = new OAuth2Client({
|
|
16
16
|
apiOrigin,
|
|
17
17
|
clientId,
|
|
@@ -27,6 +27,7 @@ export class CloudbaseOAuth {
|
|
|
27
27
|
...authOptions,
|
|
28
28
|
// 兼容老逻辑,有值传入则不走Auth内的验证码请求逻辑
|
|
29
29
|
request: request ? this.oauth2client.request.bind(this.oauth2client) : undefined,
|
|
30
|
+
adapter,
|
|
30
31
|
})
|
|
31
32
|
}
|
|
32
33
|
}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Single Promise
|
|
3
3
|
*/
|
|
4
|
+
|
|
5
|
+
let jsSdkFnPromiseMap = new Map()
|
|
4
6
|
export class SinglePromise {
|
|
5
7
|
private clientId: string
|
|
6
8
|
|
|
7
9
|
constructor(options) {
|
|
8
10
|
this.clientId = options?.clientId || ''
|
|
9
|
-
|
|
11
|
+
jsSdkFnPromiseMap = (jsSdkFnPromiseMap || new Map()) as Map<string, Promise<any>>
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
/**
|
|
@@ -17,7 +19,7 @@ export class SinglePromise {
|
|
|
17
19
|
*/
|
|
18
20
|
async run<T>(key: string, fn: () => Promise<T>): Promise<T> {
|
|
19
21
|
key = `${this.clientId}_${key}`
|
|
20
|
-
let result: Promise<any> =
|
|
22
|
+
let result: Promise<any> = jsSdkFnPromiseMap.get(key)
|
|
21
23
|
|
|
22
24
|
if (!(result as any)) {
|
|
23
25
|
result = new Promise<any>((resolve, reject) => {
|
|
@@ -31,11 +33,11 @@ export class SinglePromise {
|
|
|
31
33
|
} catch (error) {
|
|
32
34
|
reject(error)
|
|
33
35
|
} finally {
|
|
34
|
-
|
|
36
|
+
jsSdkFnPromiseMap.delete(key)
|
|
35
37
|
}
|
|
36
38
|
})()
|
|
37
39
|
})
|
|
38
|
-
|
|
40
|
+
jsSdkFnPromiseMap.set(key, result)
|
|
39
41
|
}
|
|
40
42
|
return result
|
|
41
43
|
}
|
package/src/utils/mp.ts
CHANGED