@cloudbase/oauth 0.0.5-alpha.0 → 0.1.1-alpha
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/CHANGELOG.md +30 -0
- package/README.md +137 -5
- package/app/index.d.ts +28 -0
- package/app/index.js +54 -0
- package/app/index.js.map +1 -0
- package/app/internal.d.ts +12 -0
- package/app/internal.js +25 -0
- package/app/internal.js.map +1 -0
- package/app/openuri.d.ts +20 -0
- package/app/openuri.js +104 -0
- package/app/openuri.js.map +1 -0
- package/app/request.d.ts +18 -0
- package/app/request.js +44 -0
- package/app/request.js.map +1 -0
- package/app/storage.d.ts +41 -0
- package/app/storage.js +35 -0
- package/app/storage.js.map +1 -0
- package/{dist/auth → auth}/consts.d.ts +21 -1
- package/auth/consts.js +57 -0
- package/auth/consts.js.map +1 -0
- package/auth/index.d.ts +193 -0
- package/auth/index.js +403 -0
- package/auth/index.js.map +1 -0
- package/{dist/auth → auth}/models.d.ts +35 -25
- package/auth/models.js +3 -0
- package/auth/models.js.map +1 -0
- package/captcha/index.d.ts +45 -0
- package/captcha/index.js +133 -0
- package/captcha/index.js.map +1 -0
- package/index.d.ts +8 -0
- package/index.js +17 -0
- package/index.js.map +1 -0
- package/{dist/oauth2client → oauthclient}/consts.d.ts +1 -22
- package/oauthclient/consts.js +53 -0
- package/oauthclient/consts.js.map +1 -0
- package/oauthclient/index.d.ts +18 -0
- package/oauthclient/index.js +21 -0
- package/oauthclient/index.js.map +1 -0
- package/oauthclient/interface.d.ts +25 -0
- package/oauthclient/interface.js +10 -0
- package/oauthclient/interface.js.map +1 -0
- package/{dist/oauth2client → oauthclient}/models.d.ts +5 -13
- package/oauthclient/models.js +3 -0
- package/oauthclient/models.js.map +1 -0
- package/oauthclient/oauthclient.d.ts +142 -0
- package/oauthclient/oauthclient.js +399 -0
- package/oauthclient/oauthclient.js.map +1 -0
- package/package.json +29 -33
- package/utils/single-promise.d.ts +18 -0
- package/utils/single-promise.js +48 -0
- package/utils/single-promise.js.map +1 -0
- package/utils/uuid.d.ts +5 -0
- package/utils/uuid.js +16 -0
- package/utils/uuid.js.map +1 -0
- package/.eslintignore +0 -2
- package/.eslintrc +0 -26
- package/dist/auth/apis.d.ts +0 -44
- package/dist/auth/apis.js +0 -448
- package/dist/auth/consts.js +0 -35
- package/dist/auth/models.js +0 -3
- package/dist/captcha/captcha.d.ts +0 -35
- package/dist/captcha/captcha.js +0 -262
- package/dist/index.d.ts +0 -9
- package/dist/index.js +0 -234
- package/dist/oauth2client/consts.js +0 -73
- package/dist/oauth2client/interface.d.ts +0 -15
- package/dist/oauth2client/interface.js +0 -10
- package/dist/oauth2client/models.js +0 -3
- package/dist/oauth2client/oauth2client.d.ts +0 -70
- package/dist/oauth2client/oauth2client.js +0 -618
- package/dist/utils/function/single-promise.d.ts +0 -5
- package/dist/utils/function/single-promise.js +0 -89
- package/dist/utils/uuid.d.ts +0 -1
- package/dist/utils/uuid.js +0 -12
- package/src/auth/apis.ts +0 -507
- package/src/auth/consts.ts +0 -30
- package/src/auth/models.ts +0 -178
- package/src/captcha/captcha.ts +0 -217
- package/src/index.ts +0 -162
- package/src/oauth2client/consts.ts +0 -69
- package/src/oauth2client/interface.ts +0 -57
- package/src/oauth2client/models.ts +0 -61
- package/src/oauth2client/oauth2client.ts +0 -606
- package/src/utils/function/single-promise.ts +0 -40
- package/src/utils/uuid.ts +0 -11
- package/tsconfig.json +0 -44
package/src/auth/models.ts
DELETED
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
interface BaseRequest {
|
|
2
|
-
client_id?: string;
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
export interface SignInRequest extends BaseRequest {
|
|
6
|
-
username?: string;
|
|
7
|
-
password?: string;
|
|
8
|
-
verification_code?: string;
|
|
9
|
-
verification_token?: string;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface SignInWithProviderRequest extends BaseRequest {
|
|
13
|
-
provider_token: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface SignUpRequest extends BaseRequest {
|
|
17
|
-
phone_number?: string;
|
|
18
|
-
email?: string;
|
|
19
|
-
|
|
20
|
-
verification_code?: string;
|
|
21
|
-
verification_token?: string;
|
|
22
|
-
provider_token?: string;
|
|
23
|
-
|
|
24
|
-
password?: string;
|
|
25
|
-
name?: string;
|
|
26
|
-
gender?: string;
|
|
27
|
-
picture?: string;
|
|
28
|
-
locale?: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface GetVerificationRequest extends BaseRequest {
|
|
32
|
-
phone_number?: string;
|
|
33
|
-
email?: string;
|
|
34
|
-
target?: string | 'ANY';
|
|
35
|
-
usage?: string;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface GetVerificationResponse {
|
|
39
|
-
verification_id?: string;
|
|
40
|
-
is_user?: boolean | false;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export interface VerifyResponse {
|
|
44
|
-
verification_token?: string;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export interface VerifyRequest extends BaseRequest {
|
|
48
|
-
verification_code: string;
|
|
49
|
-
verification_id?: string;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface ProviderBindRequest {
|
|
53
|
-
provider_token: string;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export interface GrantProviderTokenRequest extends BaseRequest {
|
|
57
|
-
provider_id: string;
|
|
58
|
-
provider_redirect_uri?: string;
|
|
59
|
-
provider_code?: string;
|
|
60
|
-
provider_access_token?: string;
|
|
61
|
-
provider_id_token?: string;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export interface GrantProviderTokenResponse {
|
|
65
|
-
provider_token: string;
|
|
66
|
-
expires_in: number;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export interface PatchProviderTokenRequest extends BaseRequest {
|
|
70
|
-
provider_token: string;
|
|
71
|
-
provider_params: {
|
|
72
|
-
encryptedData: string;
|
|
73
|
-
iv: string;
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export interface PatchProviderTokenResponse {
|
|
78
|
-
provider_token: string;
|
|
79
|
-
expires_in: number;
|
|
80
|
-
provider_profile: ProviderProfile;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export interface GenProviderRedirectUriRequest {
|
|
84
|
-
provider_id: string;
|
|
85
|
-
provider_redirect_uri: string;
|
|
86
|
-
state: string;
|
|
87
|
-
other_params?: {
|
|
88
|
-
sign_out_uri?: string;
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export interface GenProviderRedirectUriResponse {
|
|
93
|
-
uri: string;
|
|
94
|
-
signout_uri?: string;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export interface BindWithProviderRequest extends BaseRequest {
|
|
98
|
-
provider_token: string;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export interface BindWithProviderRequest {
|
|
102
|
-
provider_token: string;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export interface UserProfileProvider {
|
|
106
|
-
id?: string;
|
|
107
|
-
provider_user_id?: string;
|
|
108
|
-
name?: string;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export interface UserProfile {
|
|
112
|
-
name?: string;
|
|
113
|
-
picture?: string;
|
|
114
|
-
avatarUrl?: string;
|
|
115
|
-
username?: string;
|
|
116
|
-
email?: string;
|
|
117
|
-
email_verified?: boolean;
|
|
118
|
-
phone_number?: string;
|
|
119
|
-
providers?: [UserProfileProvider];
|
|
120
|
-
gender?: string;
|
|
121
|
-
birthdate?: string;
|
|
122
|
-
zoneinfo?: string;
|
|
123
|
-
locale?: string;
|
|
124
|
-
created_from?: string;
|
|
125
|
-
sub?: string
|
|
126
|
-
uid?: string
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
export type UserInfo = UserProfile;
|
|
130
|
-
|
|
131
|
-
export interface ProviderProfile {
|
|
132
|
-
provider_id: string;
|
|
133
|
-
phone_number?: string;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
export interface TransByProviderRequest {
|
|
137
|
-
provider_token: string;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export interface GrantTokenRequest extends BaseRequest {
|
|
141
|
-
client_secret?: string;
|
|
142
|
-
code?: string;
|
|
143
|
-
grant_type?: string;
|
|
144
|
-
redirect_uri?: string;
|
|
145
|
-
nonce?: string;
|
|
146
|
-
refresh_token?: string;
|
|
147
|
-
scope?: string;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export interface UnbindProviderRequest extends BaseRequest {
|
|
151
|
-
provider_id: string;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
export interface CheckPasswordrRequest extends BaseRequest {
|
|
155
|
-
password: string;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export interface BindPhoneRequest extends BaseRequest {
|
|
159
|
-
phone_number: string;
|
|
160
|
-
sudo_token: string;
|
|
161
|
-
verification_token: string;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
export interface SetPasswordRequest extends BaseRequest {
|
|
165
|
-
new_password: string;
|
|
166
|
-
sudo_token: string;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
export interface ChangeBindedProviderRequest extends BaseRequest {
|
|
170
|
-
trans_token: string;
|
|
171
|
-
provider_id: string;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export type ChangeBindedProviderResponse = BaseRequest
|
|
175
|
-
|
|
176
|
-
export interface QueryUserProfileReq extends BaseRequest {
|
|
177
|
-
appended_params: string;
|
|
178
|
-
}
|
package/src/captcha/captcha.ts
DELETED
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
import {SimpleStorage, RequestFunction} from '../oauth2client/interface';
|
|
2
|
-
import {AuthClientRequestOptions} from "../oauth2client/models";
|
|
3
|
-
import {defaultStorage} from "../oauth2client/oauth2client";
|
|
4
|
-
|
|
5
|
-
export interface CaptchaOptions {
|
|
6
|
-
clientId: string
|
|
7
|
-
request: RequestFunction;
|
|
8
|
-
storage: SimpleStorage;
|
|
9
|
-
// 打开网页并通过URL回调获取 CaptchaToken,针对不通的平台,该函数可以自定义实现, 默认集成浏览器端认证
|
|
10
|
-
openURIWithCallback?: OpenURIWithCallbackFuction;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
type OpenURIWithCallbackFuction = (url: string) => Promise<CaptchaToken>;
|
|
14
|
-
|
|
15
|
-
export interface CaptchaToken {
|
|
16
|
-
captcha_token: string
|
|
17
|
-
expires_in: number
|
|
18
|
-
expires_at?: Date | null;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface CaptchaRequestOptions extends AuthClientRequestOptions {
|
|
22
|
-
withCaptcha?: boolean;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface GetCaptchaResponse {
|
|
26
|
-
captcha_token?: string
|
|
27
|
-
expires_in?: number
|
|
28
|
-
url?: string
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const GET_CAPTCHA_URL = '/auth/v1/captcha/init'
|
|
32
|
-
|
|
33
|
-
export class Captcha {
|
|
34
|
-
private _config: CaptchaOptions;
|
|
35
|
-
private _tokenSectionName: string;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* constructor
|
|
39
|
-
* @param {CaptchaOptions} opts
|
|
40
|
-
*/
|
|
41
|
-
constructor(opts: CaptchaOptions) {
|
|
42
|
-
if (!opts.openURIWithCallback) {
|
|
43
|
-
opts.openURIWithCallback = this._getDefaultOpenURIWithCallback()
|
|
44
|
-
}
|
|
45
|
-
if (!opts.storage) {
|
|
46
|
-
opts.storage = defaultStorage
|
|
47
|
-
}
|
|
48
|
-
this._config = opts
|
|
49
|
-
this._tokenSectionName = 'captcha_' + opts.clientId
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* request http like simple fetch api, exp:request('/v1/user/me', {withCredentials:true})
|
|
54
|
-
* @param {string} url
|
|
55
|
-
* @param {AuthClientRequestOptions} options
|
|
56
|
-
*/
|
|
57
|
-
public async request<T>(
|
|
58
|
-
url: string,
|
|
59
|
-
options?: CaptchaRequestOptions,
|
|
60
|
-
): Promise<T> {
|
|
61
|
-
if (!options) {
|
|
62
|
-
options = {};
|
|
63
|
-
}
|
|
64
|
-
if (!options.method) {
|
|
65
|
-
options.method = 'GET'
|
|
66
|
-
}
|
|
67
|
-
const state = options.method + ":" + url
|
|
68
|
-
let reqURL = url;
|
|
69
|
-
if (options.withCaptcha) {
|
|
70
|
-
reqURL = await this._appendCaptchaTokenToURL(url, state, false);
|
|
71
|
-
}
|
|
72
|
-
try {
|
|
73
|
-
return this._config.request<T>(reqURL, options)
|
|
74
|
-
} catch (err) {
|
|
75
|
-
if (err.error === 'captcha_required' || err.error === 'captcha_invalid') {
|
|
76
|
-
url = await this._appendCaptchaTokenToURL(url, state, err.error === 'captcha_invalid')
|
|
77
|
-
return this._config.request<T>(url, options)
|
|
78
|
-
} else {
|
|
79
|
-
return Promise.reject(err)
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
private _getDefaultOpenURIWithCallback(): OpenURIWithCallbackFuction {
|
|
85
|
-
if (window.location.search.indexOf('__captcha') > 0) {
|
|
86
|
-
document.body.style.display = 'none';
|
|
87
|
-
}
|
|
88
|
-
if (document.getElementById('captcha_panel_wrap') === null) {
|
|
89
|
-
var elementDiv = document.createElement('div');
|
|
90
|
-
elementDiv.style.cssText =
|
|
91
|
-
'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;';
|
|
92
|
-
elementDiv.setAttribute('id', 'captcha_panel_wrap');
|
|
93
|
-
document.body.appendChild(elementDiv);
|
|
94
|
-
}
|
|
95
|
-
return this._defaultOpenURIWithCallback
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* 默认通过浏览器打开网页并获取回调
|
|
100
|
-
*/
|
|
101
|
-
private async _defaultOpenURIWithCallback(url: string): Promise<CaptchaToken> {
|
|
102
|
-
const target = document.getElementById('captcha_panel_wrap'),
|
|
103
|
-
iframe = document.createElement('iframe')
|
|
104
|
-
target.innerHTML = '';
|
|
105
|
-
iframe.setAttribute('src', url)
|
|
106
|
-
iframe.setAttribute('id', 'review-panel-iframe')
|
|
107
|
-
iframe.style.cssText = 'min-width:355px;display:block;height:355px;margin:0 auto;background-color: rgb(255, 255, 255);border: none;';
|
|
108
|
-
target.appendChild(iframe);
|
|
109
|
-
target.style.display = 'block';
|
|
110
|
-
return new Promise<CaptchaToken>((resolve, reject) => {
|
|
111
|
-
iframe.onload = function () {
|
|
112
|
-
try {
|
|
113
|
-
var windowLocation = window.location;
|
|
114
|
-
var iframeLocation = iframe.contentWindow.location;
|
|
115
|
-
if (
|
|
116
|
-
iframeLocation.host +
|
|
117
|
-
iframeLocation.pathname ===
|
|
118
|
-
windowLocation.host +
|
|
119
|
-
windowLocation.pathname
|
|
120
|
-
) {
|
|
121
|
-
target.style.display = 'none';
|
|
122
|
-
const iframeUrlParams = new URLSearchParams(iframeLocation.search);
|
|
123
|
-
const captchToken = iframeUrlParams.get('captcha_token');
|
|
124
|
-
if (captchToken) {
|
|
125
|
-
return resolve({
|
|
126
|
-
captcha_token: captchToken,
|
|
127
|
-
expires_in: Number(iframeUrlParams.get('expires_in'))
|
|
128
|
-
})
|
|
129
|
-
}
|
|
130
|
-
return reject({
|
|
131
|
-
error: iframeUrlParams.get('error'),
|
|
132
|
-
error_description: iframeUrlParams.get('error_description')
|
|
133
|
-
})
|
|
134
|
-
} else {
|
|
135
|
-
target.style.display = 'block';
|
|
136
|
-
}
|
|
137
|
-
} catch (error) {
|
|
138
|
-
target.style.display = 'block';
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
})
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* _getCaptchaToken 获取captchaToken
|
|
145
|
-
*/
|
|
146
|
-
private async _getCaptchaToken(forceNewToken: boolean, state: string): Promise<string> {
|
|
147
|
-
if (!forceNewToken) {
|
|
148
|
-
// 如果本地存在,则直接返回
|
|
149
|
-
const captchaToken = await this._findCaptchaToken()
|
|
150
|
-
if (captchaToken) {
|
|
151
|
-
return captchaToken
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
const redirectURL = window.location.origin + window.location.pathname + "?__captcha=on"
|
|
155
|
-
const captchaTokenResp = await this._config.request<GetCaptchaResponse>(GET_CAPTCHA_URL, {
|
|
156
|
-
method: 'POST',
|
|
157
|
-
body: {
|
|
158
|
-
client_id: this._config.clientId,
|
|
159
|
-
redirect_uri: redirectURL,
|
|
160
|
-
state: state
|
|
161
|
-
},
|
|
162
|
-
withCredentials: false,
|
|
163
|
-
})
|
|
164
|
-
if (captchaTokenResp.captcha_token) {
|
|
165
|
-
const captchaToken = {
|
|
166
|
-
captcha_token: captchaTokenResp.captcha_token,
|
|
167
|
-
expires_in: captchaTokenResp.expires_in,
|
|
168
|
-
}
|
|
169
|
-
this._saveCaptchaToken(captchaToken)
|
|
170
|
-
return captchaTokenResp.captcha_token
|
|
171
|
-
}
|
|
172
|
-
const captchaToken = await this._config.openURIWithCallback(captchaTokenResp.url)
|
|
173
|
-
this._saveCaptchaToken(captchaToken)
|
|
174
|
-
return captchaToken.captcha_token
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
private async _appendCaptchaTokenToURL(url: string, state: string, forceNewToken: boolean): Promise<string> {
|
|
178
|
-
const captchaToken = await this._getCaptchaToken(forceNewToken, state);
|
|
179
|
-
if (url.indexOf("?") > 0) {
|
|
180
|
-
url += "&captcha_token=" + captchaToken
|
|
181
|
-
} else {
|
|
182
|
-
url += "?captcha_token=" + captchaToken
|
|
183
|
-
}
|
|
184
|
-
return url
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
private async _saveCaptchaToken(token: CaptchaToken) {
|
|
188
|
-
token.expires_at = new Date(
|
|
189
|
-
Date.now() + (token.expires_in - 10) * 1000,
|
|
190
|
-
);
|
|
191
|
-
const tokenStr: string = JSON.stringify(token);
|
|
192
|
-
await this._config.storage.setItem(this._tokenSectionName, tokenStr);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
private async _findCaptchaToken(): Promise<string> {
|
|
196
|
-
const tokenStr: string = await this._config.storage.getItem(
|
|
197
|
-
this._tokenSectionName,
|
|
198
|
-
);
|
|
199
|
-
if (tokenStr !== undefined && tokenStr !== null) {
|
|
200
|
-
try {
|
|
201
|
-
const captchaToken = JSON.parse(tokenStr);
|
|
202
|
-
if (captchaToken && captchaToken.expires_at) {
|
|
203
|
-
captchaToken.expires_at = new Date(captchaToken.expires_at);
|
|
204
|
-
}
|
|
205
|
-
const isExpired = captchaToken.expires_at < new Date();
|
|
206
|
-
if (isExpired) {
|
|
207
|
-
return null
|
|
208
|
-
}
|
|
209
|
-
return captchaToken.captcha_token
|
|
210
|
-
} catch (error) {
|
|
211
|
-
await this._config.storage.removeItem(this._tokenSectionName);
|
|
212
|
-
return null
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
return null
|
|
216
|
-
}
|
|
217
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
import { ICloudbaseComponent } from '@cloudbase/types/component';
|
|
2
|
-
import { ICloudbase } from '@cloudbase/types';
|
|
3
|
-
import { getBaseEndPoint } from '@cloudbase/app'
|
|
4
|
-
import { helpers } from '@cloudbase/utilities'
|
|
5
|
-
const { stopOAuthLoginWithAuth } = helpers
|
|
6
|
-
|
|
7
|
-
export { Syntax, ErrorType } from './oauth2client/consts';
|
|
8
|
-
|
|
9
|
-
import { OAuth2Client } from './oauth2client/oauth2client'
|
|
10
|
-
|
|
11
|
-
export {
|
|
12
|
-
defaultStorage,
|
|
13
|
-
defaultRequest,
|
|
14
|
-
ToResponseErrorOptions,
|
|
15
|
-
toResponseError,
|
|
16
|
-
generateRequestId,
|
|
17
|
-
OAuth2Client,
|
|
18
|
-
} from './oauth2client/oauth2client';
|
|
19
|
-
|
|
20
|
-
export { AuthClient, SimpleStorage } from './oauth2client/interface';
|
|
21
|
-
|
|
22
|
-
import { Credentials } from './oauth2client/models'
|
|
23
|
-
export {
|
|
24
|
-
Credentials,
|
|
25
|
-
ResponseError,
|
|
26
|
-
OAuth2ClientOptions,
|
|
27
|
-
AuthClientRequestOptions,
|
|
28
|
-
} from './oauth2client/models';
|
|
29
|
-
|
|
30
|
-
import { AuthOptions, Auth } from './auth/apis'
|
|
31
|
-
|
|
32
|
-
export { AuthOptions, Auth } from './auth/apis';
|
|
33
|
-
|
|
34
|
-
import * as authModels from './auth/models';
|
|
35
|
-
export { authModels };
|
|
36
|
-
|
|
37
|
-
declare const cloudbase: ICloudbase;
|
|
38
|
-
|
|
39
|
-
const COMPONENT_NAME = 'oauth';
|
|
40
|
-
|
|
41
|
-
class CloudbaseOAuth {
|
|
42
|
-
public oauth2client: OAuth2Client
|
|
43
|
-
public authApi: Auth
|
|
44
|
-
private _fromApp: ICloudbase
|
|
45
|
-
|
|
46
|
-
constructor(authOptions: AuthOptions) {
|
|
47
|
-
const { apiOrigin, clientId, _fromApp } = authOptions
|
|
48
|
-
this._fromApp = _fromApp
|
|
49
|
-
this.oauth2client = new OAuth2Client({
|
|
50
|
-
apiOrigin,
|
|
51
|
-
clientId
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
this.authApi = new Auth({
|
|
55
|
-
credentialsClient: this.oauth2client,
|
|
56
|
-
...authOptions
|
|
57
|
-
})
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
public async checkFromAuthV1OrV2() {
|
|
61
|
-
const { _fromApp } = this
|
|
62
|
-
const authInstance = _fromApp.authInstance
|
|
63
|
-
const oauthInstance = _fromApp.oauthInstance || (_fromApp as any).oauth()
|
|
64
|
-
const authLogin = authInstance && await authInstance.getLoginState()
|
|
65
|
-
if (authLogin) {
|
|
66
|
-
return 'auth'
|
|
67
|
-
}
|
|
68
|
-
const oauthLogin = oauthInstance && await oauthInstance.hasLoginState()
|
|
69
|
-
if (oauthLogin) {
|
|
70
|
-
return 'oauth'
|
|
71
|
-
}
|
|
72
|
-
return ''
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* 登录
|
|
78
|
-
* @param {authModels.SignInRequest} params
|
|
79
|
-
* @returns {Promise<Credentials>}
|
|
80
|
-
* @memberof CloudbaseOAuth
|
|
81
|
-
*/
|
|
82
|
-
@stopOAuthLoginWithAuth()
|
|
83
|
-
public async signIn(params: authModels.SignInRequest): Promise<Credentials> {
|
|
84
|
-
return this.authApi.signIn(params)
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
public async signOut(): Promise<any> {
|
|
88
|
-
return this.authApi.signOut()
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* check 当前是否已 oauth 登录
|
|
93
|
-
* @returns {Promise<boolean>}
|
|
94
|
-
* @memberof CloudbaseOAuth
|
|
95
|
-
*/
|
|
96
|
-
public async hasLoginState(): Promise<boolean> {
|
|
97
|
-
return this.authApi.hasLoginState()
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
public async getUserInfo(): Promise<authModels.UserProfile> {
|
|
101
|
-
return this.authApi.getUserInfo()
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* 获取 oauth 登录态
|
|
106
|
-
* @returns {Promise<boolean>}
|
|
107
|
-
* @memberof CloudbaseOAuth
|
|
108
|
-
*/
|
|
109
|
-
public async getLoginState(): Promise<Credentials> {
|
|
110
|
-
return this.authApi.getLoginState()
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
public hasLoginStateSync(): Credentials {
|
|
114
|
-
return this.authApi.hasLoginStateSync()
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* 匿名登录
|
|
119
|
-
* @returns {Promise<Credentials>}
|
|
120
|
-
* @memberof CloudbaseOAuth
|
|
121
|
-
*/
|
|
122
|
-
@stopOAuthLoginWithAuth()
|
|
123
|
-
public async signInAnonymously(): Promise<Credentials> {
|
|
124
|
-
return this.authApi.signInAnonymously()
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const component: ICloudbaseComponent = {
|
|
129
|
-
name: COMPONENT_NAME,
|
|
130
|
-
namespace: 'oauth',
|
|
131
|
-
entity: function () {
|
|
132
|
-
|
|
133
|
-
if (this.oauthInstance) {
|
|
134
|
-
return this.oauthInstance;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const { env } = this.config;
|
|
138
|
-
this.oauthInstance = new CloudbaseOAuth({
|
|
139
|
-
clientId: env,
|
|
140
|
-
apiOrigin: getBaseEndPoint(),
|
|
141
|
-
_fromApp: this
|
|
142
|
-
});
|
|
143
|
-
return this.oauthInstance;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
try {
|
|
148
|
-
// 尝试自动注册至全局变量cloudbase
|
|
149
|
-
// 此行为只在浏览器环境下有效
|
|
150
|
-
cloudbase.registerComponent(component);
|
|
151
|
-
} catch (e) { }
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* @api 手动注册至cloudbase app
|
|
155
|
-
*/
|
|
156
|
-
export function registerOAuth(app: Pick<ICloudbase, 'registerComponent'>) {
|
|
157
|
-
try {
|
|
158
|
-
app.registerComponent(component);
|
|
159
|
-
} catch (e) {
|
|
160
|
-
console.warn(e);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
export enum Syntax {
|
|
2
|
-
CLIENT_ID = 'client_id',
|
|
3
|
-
CLIENT_SECRET = 'client_secret',
|
|
4
|
-
RESPONSE_TYPE = 'response_type',
|
|
5
|
-
SCOPE = 'scope',
|
|
6
|
-
STATE = 'state',
|
|
7
|
-
REDIRECT_URI = 'redirect_uri',
|
|
8
|
-
ERROR = 'error',
|
|
9
|
-
ERROR_DESCRIPTION = 'error_description',
|
|
10
|
-
ERROR_URI = 'error_uri',
|
|
11
|
-
GRANT_TYPE = 'grant_type',
|
|
12
|
-
CODE = 'code',
|
|
13
|
-
ACCESS_TOKEN = 'access_token',
|
|
14
|
-
TOKEN_TYPE = 'token_type',
|
|
15
|
-
EXPIRES_IN = 'expires_in',
|
|
16
|
-
USERNAME = 'username',
|
|
17
|
-
PASSWORD = 'password',
|
|
18
|
-
REFRESH_TOKEN = 'refresh_token',
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export enum ErrorType {
|
|
22
|
-
UNREACHABLE = 'unreachable',
|
|
23
|
-
LOCAL = 'local',
|
|
24
|
-
CANCELLED = 'cancelled',
|
|
25
|
-
UNKNOWN = 'unknown',
|
|
26
|
-
INVALID_ARGUMENT = 'invalid_argument',
|
|
27
|
-
DEADLINE_EXCEEDED = 'deadline_exceeded',
|
|
28
|
-
NOT_FOUND = 'not_found',
|
|
29
|
-
ALREADY_EXISTS = 'already_exists',
|
|
30
|
-
PERMISSION_DENIED = 'permission_denied',
|
|
31
|
-
UNAUTHENTICATED = 'unauthenticated',
|
|
32
|
-
RESOURCE_EXHAUSTED = 'resource_exhausted',
|
|
33
|
-
FAILED_PRECONDITION = 'failed_precondition',
|
|
34
|
-
ABORTED = 'aborted',
|
|
35
|
-
OUT_OF_RANGE = 'out_of_range',
|
|
36
|
-
UNIMPLEMENTED = 'unimplemented',
|
|
37
|
-
INTERNAL = 'internal',
|
|
38
|
-
UNAVAILABLE = 'unavailable',
|
|
39
|
-
DATA_LOSS = 'data_loss',
|
|
40
|
-
// CommonError
|
|
41
|
-
CAPTCHA_REQUIRED = 'captcha_required',
|
|
42
|
-
CAPTCHA_INVALID = 'captcha_invalid',
|
|
43
|
-
INVALID_PASSWORD = 'invalid_password',
|
|
44
|
-
INVALID_STATUS = 'invalid_status',
|
|
45
|
-
USER_PENDING = 'user_pending',
|
|
46
|
-
USER_BLOCKED = 'user_blocked',
|
|
47
|
-
INVALID_VERIFICATION_CODE = 'invalid_verification_code',
|
|
48
|
-
TWO_FACTOR_REQUIRED = 'two_factor_required',
|
|
49
|
-
INVALID_TWO_FACTOR = 'invalid_two_factor',
|
|
50
|
-
INVALID_TWO_FACTOR_RECOVERY = 'invalid_two_factor_recovery',
|
|
51
|
-
UNDER_REVIEW = 'under_review',
|
|
52
|
-
INVALID_REQUEST = 'invalid_request',
|
|
53
|
-
UNAUTHORIZED_CLIENT = 'unauthorized_client',
|
|
54
|
-
ACCESS_DENIED = 'access_denied',
|
|
55
|
-
UNSUPPORTED_RESPONSE_TYPE = 'unsupported_response_type',
|
|
56
|
-
INVALID_SCOPE = 'invalid_scope',
|
|
57
|
-
INVALID_GRANT = 'invalid_grant',
|
|
58
|
-
SERVER_ERROR = 'server_error',
|
|
59
|
-
TEMPORARILY_UNAVAILABLE = 'temporarily_unavailable',
|
|
60
|
-
INTERACTION_REQUIRED = 'interaction_required',
|
|
61
|
-
LOGIN_REQUIRED = 'login_required',
|
|
62
|
-
ACCOUNT_SELECTION_REQUIRED = 'account_selection_required',
|
|
63
|
-
CONSENT_REQUIRED = 'consent_required',
|
|
64
|
-
INVALID_REQUEST_URI = 'invalid_request_uri',
|
|
65
|
-
INVALID_REQUEST_OBJECT = 'invalid_request_object',
|
|
66
|
-
REQUEST_NOT_SUPPORTED = 'request_not_supported',
|
|
67
|
-
REQUEST_URI_NOT_SUPPORTED = 'request_uri_not_supported',
|
|
68
|
-
REGISTRATION_NOT_SUPPORTED = 'registration_not_supported',
|
|
69
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { Credentials, AuthClientRequestOptions } from './models';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* the interface for the Oauth2Client
|
|
5
|
-
*/
|
|
6
|
-
export abstract class AuthClient {
|
|
7
|
-
/**
|
|
8
|
-
* Sets the auth credentials.
|
|
9
|
-
*/
|
|
10
|
-
abstract setCredentials(credentials?: Credentials): void;
|
|
11
|
-
/**
|
|
12
|
-
* Provides an alternative fetch api request implementation with auth credentials
|
|
13
|
-
* if options.withCredentials:true, the request will auto add Authorization: Bearer <AccessToken> in the request
|
|
14
|
-
* error:
|
|
15
|
-
* - unreachable, the network error or response is not json
|
|
16
|
-
* - unauthenticated: has no validate access token
|
|
17
|
-
*/
|
|
18
|
-
abstract request: RequestFunction;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* get the current accessToken from AuthClient, you can use this to detect login status
|
|
22
|
-
* error:
|
|
23
|
-
* - unauthenticated: has no validate access token
|
|
24
|
-
*/
|
|
25
|
-
abstract getAccessToken(): Promise<string>;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export type RequestFunction = <T>(url: string, options?: AuthClientRequestOptions) => Promise<T>;
|
|
29
|
-
|
|
30
|
-
/** An interface of the Simple Web Storage API */
|
|
31
|
-
export interface SimpleStorage {
|
|
32
|
-
/**
|
|
33
|
-
* value = storage[key]
|
|
34
|
-
*/
|
|
35
|
-
getItem(key: string): Promise<string | null>;
|
|
36
|
-
/**
|
|
37
|
-
* delete storage[key]
|
|
38
|
-
*/
|
|
39
|
-
removeItem(key: string): Promise<void>;
|
|
40
|
-
/**
|
|
41
|
-
* storage[key] = value
|
|
42
|
-
*/
|
|
43
|
-
setItem(key: string, value: string): Promise<void>;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* value = storage[key]
|
|
47
|
-
*/
|
|
48
|
-
getItemSync(key: string): string | null;
|
|
49
|
-
/**
|
|
50
|
-
* delete storage[key]
|
|
51
|
-
*/
|
|
52
|
-
removeItemSync(key: string): void;
|
|
53
|
-
/**
|
|
54
|
-
* storage[key] = value
|
|
55
|
-
*/
|
|
56
|
-
setItemSync(key: string, value: string): void;
|
|
57
|
-
}
|