@cloudbase/oauth 2.4.5-beta.0 → 2.5.1-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.
Files changed (39) hide show
  1. package/.eslintrc.js +15 -0
  2. package/dist/cjs/auth/apis.d.ts +3 -3
  3. package/dist/cjs/auth/apis.js +69 -69
  4. package/dist/cjs/captcha/captcha.d.ts +9 -9
  5. package/dist/cjs/captcha/captcha.js +43 -46
  6. package/dist/cjs/index.d.ts +3 -3
  7. package/dist/cjs/index.js +6 -6
  8. package/dist/cjs/oauth2client/interface.d.ts +1 -1
  9. package/dist/cjs/oauth2client/interface.js +1 -1
  10. package/dist/cjs/oauth2client/models.js +1 -1
  11. package/dist/cjs/oauth2client/oauth2client.d.ts +31 -31
  12. package/dist/cjs/oauth2client/oauth2client.js +229 -229
  13. package/dist/cjs/utils/function/single-promise.d.ts +2 -2
  14. package/dist/cjs/utils/function/single-promise.js +40 -33
  15. package/dist/cjs/utils/uuid.js +2 -2
  16. package/dist/esm/auth/apis.d.ts +3 -3
  17. package/dist/esm/auth/apis.js +69 -69
  18. package/dist/esm/captcha/captcha.d.ts +9 -9
  19. package/dist/esm/captcha/captcha.js +44 -47
  20. package/dist/esm/index.d.ts +3 -3
  21. package/dist/esm/index.js +5 -5
  22. package/dist/esm/oauth2client/interface.d.ts +1 -1
  23. package/dist/esm/oauth2client/interface.js +1 -1
  24. package/dist/esm/oauth2client/models.js +1 -1
  25. package/dist/esm/oauth2client/oauth2client.d.ts +31 -31
  26. package/dist/esm/oauth2client/oauth2client.js +229 -229
  27. package/dist/esm/utils/function/single-promise.d.ts +2 -2
  28. package/dist/esm/utils/function/single-promise.js +40 -33
  29. package/dist/esm/utils/uuid.js +2 -2
  30. package/package.json +13 -6
  31. package/src/auth/apis.ts +135 -158
  32. package/src/captcha/captcha.ts +71 -78
  33. package/src/index.ts +13 -14
  34. package/src/oauth2client/interface.ts +5 -5
  35. package/src/oauth2client/models.ts +38 -38
  36. package/src/oauth2client/oauth2client.ts +255 -268
  37. package/src/utils/function/single-promise.ts +22 -21
  38. package/src/utils/uuid.ts +4 -4
  39. package/.eslintrc +0 -26
@@ -1,6 +1,6 @@
1
- import { SimpleStorage, RequestFunction } from '../oauth2client/interface';
2
- import { AuthClientRequestOptions } from "../oauth2client/models";
3
- import { defaultStorage } from "../oauth2client/oauth2client";
1
+ import { SimpleStorage, RequestFunction } from '../oauth2client/interface'
2
+ import { AuthClientRequestOptions } from '../oauth2client/models'
3
+ import { defaultStorage } from '../oauth2client/oauth2client'
4
4
 
5
5
  export interface CaptchaOptions {
6
6
  clientId: string
@@ -31,8 +31,8 @@ export interface GetCaptchaResponse {
31
31
  const GET_CAPTCHA_URL = '/auth/v1/captcha/init'
32
32
 
33
33
  export class Captcha {
34
- private _config: CaptchaOptions;
35
- private _tokenSectionName: string;
34
+ private config: CaptchaOptions
35
+ private tokenSectionName: string
36
36
 
37
37
  /**
38
38
  * constructor
@@ -40,13 +40,13 @@ export class Captcha {
40
40
  */
41
41
  constructor(opts: CaptchaOptions) {
42
42
  if (!opts.openURIWithCallback) {
43
- opts.openURIWithCallback = this._getDefaultOpenURIWithCallback()
43
+ opts.openURIWithCallback = this.getDefaultOpenURIWithCallback()
44
44
  }
45
45
  if (!opts.storage) {
46
46
  opts.storage = defaultStorage
47
47
  }
48
- this._config = opts
49
- this._tokenSectionName = 'captcha_' + opts.clientId
48
+ this.config = opts
49
+ this.tokenSectionName = `captcha_${opts.clientId}`
50
50
  }
51
51
 
52
52
  /**
@@ -59,110 +59,107 @@ export class Captcha {
59
59
  options?: CaptchaRequestOptions,
60
60
  ): Promise<T> {
61
61
  if (!options) {
62
- options = {};
62
+ options = {}
63
63
  }
64
64
  if (!options.method) {
65
65
  options.method = 'GET'
66
66
  }
67
- const state = options.method + ":" + url
68
- let reqURL = url;
67
+ const state = `${options.method}:${url}`
68
+ let reqURL = url
69
69
  if (options.withCaptcha) {
70
- reqURL = await this._appendCaptchaTokenToURL(url, state, false);
70
+ reqURL = await this.appendCaptchaTokenToURL(url, state, false)
71
71
  }
72
72
 
73
- let resp: T;
73
+ let resp: T
74
74
  try {
75
- resp = await this._config.request<T>(reqURL, options)
75
+ resp = await this.config.request<T>(reqURL, options)
76
76
  } catch (err) {
77
77
  if (err.error === 'captcha_required' || err.error === 'captcha_invalid') {
78
- url = await this._appendCaptchaTokenToURL(url, state, err.error === 'captcha_invalid')
79
- return this._config.request<T>(url, options)
80
- } else {
81
- return Promise.reject(err)
78
+ url = await this.appendCaptchaTokenToURL(url, state, err.error === 'captcha_invalid')
79
+ return this.config.request<T>(url, options)
82
80
  }
81
+ return Promise.reject(err)
83
82
  }
84
83
  return resp
85
84
  }
86
85
 
87
- private _getDefaultOpenURIWithCallback(): OpenURIWithCallbackFuction {
86
+ private getDefaultOpenURIWithCallback(): OpenURIWithCallbackFuction {
88
87
  if (window.location.search.indexOf('__captcha') > 0) {
89
- document.body.style.display = 'none';
88
+ document.body.style.display = 'none'
90
89
  }
91
90
  if (document.getElementById('captcha_panel_wrap') === null) {
92
- var elementDiv = document.createElement('div');
93
- elementDiv.style.cssText =
94
- 'background-color: rgba(0, 0, 0, 0.7);position: fixed;left: 0px;right: 0px;top: 0px;bottom: 0px;padding: 9vw 0 0 0;display: none;z-index:100;';
95
- elementDiv.setAttribute('id', 'captcha_panel_wrap');
91
+ const elementDiv = document.createElement('div')
92
+ elementDiv.style.cssText = 'background-color: rgba(0, 0, 0, 0.7);position: fixed;left: 0px;right: 0px;top: 0px;bottom: 0px;padding: 9vw 0 0 0;display: none;z-index:100;'
93
+ elementDiv.setAttribute('id', 'captcha_panel_wrap')
96
94
  setTimeout(() => {
97
- document.body.appendChild(elementDiv);
95
+ document.body.appendChild(elementDiv)
98
96
  }, 0)
99
97
  }
100
- return this._defaultOpenURIWithCallback
98
+ return this.defaultOpenURIWithCallback
101
99
  }
102
100
 
103
101
  /**
104
102
  * 默认通过浏览器打开网页并获取回调
105
103
  */
106
- private async _defaultOpenURIWithCallback(url: string): Promise<CaptchaToken> {
107
- const target = document.getElementById('captcha_panel_wrap'),
108
- iframe = document.createElement('iframe')
109
- target.innerHTML = '';
104
+ private async defaultOpenURIWithCallback(url: string): Promise<CaptchaToken> {
105
+ const target = document.getElementById('captcha_panel_wrap')
106
+ const iframe = document.createElement('iframe')
107
+ target.innerHTML = ''
110
108
  iframe.setAttribute('src', url)
111
109
  iframe.setAttribute('id', 'review-panel-iframe')
112
- iframe.style.cssText = 'min-width:355px;display:block;height:355px;margin:0 auto;background-color: rgb(255, 255, 255);border: none;';
113
- target.appendChild(iframe);
114
- target.style.display = 'block';
110
+ iframe.style.cssText = 'min-width:355px;display:block;height:355px;margin:0 auto;background-color: rgb(255, 255, 255);border: none;'
111
+ target.appendChild(iframe)
112
+ target.style.display = 'block'
115
113
  return new Promise<CaptchaToken>((resolve, reject) => {
116
114
  iframe.onload = function () {
117
115
  try {
118
- var windowLocation = window.location;
119
- var iframeLocation = iframe.contentWindow.location;
116
+ const windowLocation = window.location
117
+ const iframeLocation = iframe.contentWindow.location
120
118
  if (
121
- iframeLocation.host +
122
- iframeLocation.pathname ===
123
- windowLocation.host +
124
- windowLocation.pathname
119
+ iframeLocation.host
120
+ + iframeLocation.pathname
121
+ === windowLocation.host
122
+ + windowLocation.pathname
125
123
  ) {
126
- target.style.display = 'none';
127
- const iframeUrlParams = new URLSearchParams(iframeLocation.search);
128
- const captchToken = iframeUrlParams.get('captcha_token');
124
+ target.style.display = 'none'
125
+ const iframeUrlParams = new URLSearchParams(iframeLocation.search)
126
+ const captchToken = iframeUrlParams.get('captcha_token')
129
127
  if (captchToken) {
130
128
  return resolve({
131
129
  captcha_token: captchToken,
132
- expires_in: Number(iframeUrlParams.get('expires_in'))
130
+ expires_in: Number(iframeUrlParams.get('expires_in')),
133
131
  })
134
132
  }
135
133
  return reject({
136
134
  error: iframeUrlParams.get('error'),
137
- error_description: iframeUrlParams.get('error_description')
135
+ error_description: iframeUrlParams.get('error_description'),
138
136
  })
139
- } else {
140
- target.style.display = 'block';
141
137
  }
138
+ target.style.display = 'block'
142
139
  } catch (error) {
143
- target.style.display = 'block';
140
+ target.style.display = 'block'
144
141
  }
145
- };
142
+ }
146
143
  })
147
144
  }
148
145
  /**
149
- * _getCaptchaToken 获取captchaToken
146
+ * getCaptchaToken 获取captchaToken
150
147
  */
151
- private async _getCaptchaToken(forceNewToken: boolean, state: string): Promise<string> {
148
+ private async getCaptchaToken(forceNewToken: boolean, state: string): Promise<string> {
152
149
  if (!forceNewToken) {
153
150
  // 如果本地存在,则直接返回
154
- const captchaToken = await this._findCaptchaToken()
151
+ const captchaToken = await this.findCaptchaToken()
155
152
  if (captchaToken) {
156
153
  return captchaToken
157
154
  }
158
155
  }
159
- const redirectURL = window.location.origin + window.location.pathname + "?__captcha=on"
160
- const captchaTokenResp = await this._config.request<GetCaptchaResponse>(GET_CAPTCHA_URL, {
156
+ const redirectURL = `${window.location.origin + window.location.pathname}?__captcha=on`
157
+ const captchaTokenResp = await this.config.request<GetCaptchaResponse>(GET_CAPTCHA_URL, {
161
158
  method: 'POST',
162
159
  body: {
163
- client_id: this._config.clientId,
160
+ client_id: this.config.clientId,
164
161
  redirect_uri: redirectURL,
165
- state: state
162
+ state,
166
163
  },
167
164
  withCredentials: false,
168
165
  })
@@ -171,52 +168,48 @@ export class Captcha {
171
168
  captcha_token: captchaTokenResp.captcha_token,
172
169
  expires_in: captchaTokenResp.expires_in,
173
170
  }
174
- this._saveCaptchaToken(captchaToken)
171
+ this.saveCaptchaToken(captchaToken)
175
172
  return captchaTokenResp.captcha_token
176
173
  }
177
- const captchaToken = await this._config.openURIWithCallback(captchaTokenResp.url)
178
- this._saveCaptchaToken(captchaToken)
174
+ const captchaToken = await this.config.openURIWithCallback(captchaTokenResp.url)
175
+ this.saveCaptchaToken(captchaToken)
179
176
  return captchaToken.captcha_token
180
177
  }
181
178
 
182
- private async _appendCaptchaTokenToURL(url: string, state: string, forceNewToken: boolean): Promise<string> {
183
- const captchaToken = await this._getCaptchaToken(forceNewToken, state);
184
- if (url.indexOf("?") > 0) {
185
- url += "&captcha_token=" + captchaToken
179
+ private async appendCaptchaTokenToURL(url: string, state: string, forceNewToken: boolean): Promise<string> {
180
+ const captchaToken = await this.getCaptchaToken(forceNewToken, state)
181
+ if (url.indexOf('?') > 0) {
182
+ url += `&captcha_token=${captchaToken}`
186
183
  } else {
187
- url += "?captcha_token=" + captchaToken
184
+ url += `?captcha_token=${captchaToken}`
188
185
  }
189
186
  return url
190
187
  }
191
188
 
192
- private async _saveCaptchaToken(token: CaptchaToken) {
193
- token.expires_at = new Date(
194
- Date.now() + (token.expires_in - 10) * 1000,
195
- );
196
- const tokenStr: string = JSON.stringify(token);
197
- await this._config.storage.setItem(this._tokenSectionName, tokenStr);
189
+ private async saveCaptchaToken(token: CaptchaToken) {
190
+ token.expires_at = new Date(Date.now() + (token.expires_in - 10) * 1000,)
191
+ const tokenStr: string = JSON.stringify(token)
192
+ await this.config.storage.setItem(this.tokenSectionName, tokenStr)
198
193
  }
199
194
 
200
- private async _findCaptchaToken(): Promise<string> {
201
- const tokenStr: string = await this._config.storage.getItem(
202
- this._tokenSectionName,
203
- );
195
+ private async findCaptchaToken(): Promise<string> {
196
+ const tokenStr: string = await this.config.storage.getItem(this.tokenSectionName,)
204
197
  if (tokenStr !== undefined && tokenStr !== null) {
205
198
  try {
206
- const captchaToken = JSON.parse(tokenStr);
199
+ const captchaToken = JSON.parse(tokenStr)
207
200
  if (captchaToken?.expires_at) {
208
- captchaToken.expires_at = new Date(captchaToken.expires_at);
201
+ captchaToken.expires_at = new Date(captchaToken.expires_at)
209
202
  }
210
- const isExpired = captchaToken.expires_at < new Date();
203
+ const isExpired = captchaToken.expires_at < new Date()
211
204
  if (isExpired) {
212
205
  return null
213
206
  }
214
207
  return captchaToken.captcha_token
215
208
  } catch (error) {
216
- await this._config.storage.removeItem(this._tokenSectionName);
209
+ await this.config.storage.removeItem(this.tokenSectionName)
217
210
  return null
218
211
  }
219
212
  }
220
213
  return null
221
214
  }
222
- }
215
+ }
package/src/index.ts CHANGED
@@ -1,7 +1,11 @@
1
- export { Syntax, ErrorType } from './oauth2client/consts';
2
-
3
1
  import { OAuth2Client } from './oauth2client/oauth2client'
4
2
 
3
+ import { AuthOptions, Auth } from './auth/apis'
4
+
5
+ import * as authModels from './auth/models'
6
+
7
+ export { Syntax, ErrorType } from './oauth2client/consts'
8
+
5
9
  export {
6
10
  defaultStorage,
7
11
  defaultRequest,
@@ -9,24 +13,19 @@ export {
9
13
  toResponseError,
10
14
  generateRequestId,
11
15
  OAuth2Client,
12
- } from './oauth2client/oauth2client';
16
+ } from './oauth2client/oauth2client'
13
17
 
14
- export { AuthClient, SimpleStorage } from './oauth2client/interface';
18
+ export { AuthClient, SimpleStorage } from './oauth2client/interface'
15
19
 
16
- // import { Credentials } from './oauth2client/models'
17
20
  export {
18
21
  Credentials,
19
22
  ResponseError,
20
23
  OAuth2ClientOptions,
21
24
  AuthClientRequestOptions,
22
- } from './oauth2client/models';
23
-
24
- import { AuthOptions, Auth } from './auth/apis'
25
-
26
- export { AuthOptions, Auth } from './auth/apis';
25
+ } from './oauth2client/models'
27
26
 
28
- import * as authModels from './auth/models';
29
- export { authModels };
27
+ export { AuthOptions, Auth } from './auth/apis'
28
+ export { authModels }
30
29
 
31
30
 
32
31
  export class CloudbaseOAuth {
@@ -37,12 +36,12 @@ export class CloudbaseOAuth {
37
36
  const { apiOrigin, clientId } = authOptions
38
37
  this.oauth2client = new OAuth2Client({
39
38
  apiOrigin,
40
- clientId
39
+ clientId,
41
40
  })
42
41
 
43
42
  this.authApi = new Auth({
44
43
  credentialsClient: this.oauth2client,
45
- ...authOptions
44
+ ...authOptions,
46
45
  })
47
46
  }
48
47
  }
@@ -1,13 +1,9 @@
1
- import { Credentials, AuthClientRequestOptions } from './models';
1
+ import { Credentials, AuthClientRequestOptions } from './models'
2
2
 
3
3
  /**
4
4
  * the interface for the Oauth2Client
5
5
  */
6
6
  export abstract class AuthClient {
7
- /**
8
- * Sets the auth credentials.
9
- */
10
- abstract setCredentials(credentials?: Credentials): void;
11
7
  /**
12
8
  * Provides an alternative fetch api request implementation with auth credentials
13
9
  * if options.withCredentials:true, the request will auto add Authorization: Bearer <AccessToken> in the request
@@ -16,6 +12,10 @@ export abstract class AuthClient {
16
12
  * - unauthenticated: has no validate access token
17
13
  */
18
14
  abstract request: RequestFunction;
15
+ /**
16
+ * Sets the auth credentials.
17
+ */
18
+ abstract setCredentials(credentials?: Credentials): void;
19
19
 
20
20
  /**
21
21
  * get the current accessToken from AuthClient, you can use this to detect login status
@@ -1,32 +1,32 @@
1
- import {SimpleStorage} from './interface';
2
- import {ErrorType} from './consts';
1
+ import { SimpleStorage } from './interface'
2
+ import { ErrorType } from './consts'
3
3
 
4
4
  /** Credentials **/
5
5
  export interface Credentials {
6
- token_type?: string | null;
7
- access_token?: string | null;
8
- refresh_token?: string | null;
9
- scope?: string | null;
10
- expires_in?: number | null;
11
- expires_at?: Date | null;
12
- sub?: string | null;
13
- groups?: string[] | null;
6
+ token_type?: string | null;
7
+ access_token?: string | null;
8
+ refresh_token?: string | null;
9
+ scope?: string | null;
10
+ expires_in?: number | null;
11
+ expires_at?: Date | null;
12
+ sub?: string | null;
13
+ groups?: string[] | null;
14
14
  }
15
15
 
16
16
  /** An Error For all concern **/
17
17
  export interface ResponseError {
18
- error: ErrorType;
19
- error_description?: string | null;
20
- error_uri?: string | null;
21
- details?: any | null;
18
+ error: ErrorType;
19
+ error_description?: string | null;
20
+ error_uri?: string | null;
21
+ details?: any | null;
22
22
  }
23
23
 
24
24
  export interface RequestOptions {
25
- body?: any | null;
26
- headers?: any | null;
27
- method?: string;
25
+ body?: any | null;
26
+ headers?: any | null;
27
+ method?: string;
28
28
 
29
- [key: string]: any;
29
+ [key: string]: any;
30
30
  }
31
31
 
32
32
  export type RequestFunction = <T>(
@@ -35,28 +35,28 @@ export type RequestFunction = <T>(
35
35
  ) => Promise<T>;
36
36
 
37
37
  export interface AuthClientRequestOptions extends RequestOptions {
38
- headers?: {
39
- 'x-request-id'?: string;
40
- [key: string]: any;
41
- } | null;
42
- withCredentials?: boolean;
43
- retry?: number;
44
-
38
+ headers?: {
39
+ 'x-request-id'?: string;
45
40
  [key: string]: any;
41
+ } | null;
42
+ withCredentials?: boolean;
43
+ retry?: number;
44
+
45
+ [key: string]: any;
46
46
  }
47
47
 
48
48
  export interface OAuth2ClientOptions {
49
- devMode?: boolean;
50
- apiOrigin: string;
51
- clientId: string;
52
- // default value is 1,min value is 0, max value is 5
53
- retry?: number;
54
- baseRequest?: <T>(url: string, options?: RequestOptions) => Promise<T>;
55
- // Storage, default is localStorage, setItem(k, v), getItem(k),removeItem(k)
56
- storage?: SimpleStorage;
57
- clientSecret?: string;
58
- refreshTokenFunc?: (refreshToken?: string) => Promise<Credentials>;
59
- // set the token in url query instead of header
60
- tokenInURL?: boolean;
61
- headers?: { [key: string]: string };
49
+ devMode?: boolean;
50
+ apiOrigin: string;
51
+ clientId: string;
52
+ // default value is 1,min value is 0, max value is 5
53
+ retry?: number;
54
+ baseRequest?: <T>(url: string, options?: RequestOptions) => Promise<T>;
55
+ // Storage, default is localStorage, setItem(k, v), getItem(k),removeItem(k)
56
+ storage?: SimpleStorage;
57
+ clientSecret?: string;
58
+ refreshTokenFunc?: (refreshToken?: string) => Promise<Credentials>;
59
+ // set the token in url query instead of header
60
+ tokenInURL?: boolean;
61
+ headers?: { [key: string]: string };
62
62
  }