2019-12-08 00:32:00 +05:30
|
|
|
import { ApplicationType } from 'app/components/dev/apps';
|
|
|
|
import request from 'app/services/request';
|
2016-07-27 23:57:21 +05:30
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
export type Scope =
|
2019-11-27 14:33:32 +05:30
|
|
|
| 'minecraft_server_session'
|
|
|
|
| 'offline_access'
|
|
|
|
| 'account_info'
|
|
|
|
| 'account_email';
|
2019-06-30 19:02:50 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
export interface Client {
|
2019-12-07 16:58:52 +05:30
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
description: string;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
2019-06-30 19:02:50 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
export interface OauthAppResponse {
|
2019-12-07 16:58:52 +05:30
|
|
|
clientId: string;
|
|
|
|
clientSecret: string;
|
|
|
|
type: ApplicationType;
|
|
|
|
name: string;
|
|
|
|
websiteUrl: string;
|
|
|
|
createdAt: number;
|
2019-11-27 14:33:32 +05:30
|
|
|
// fields for 'application' type
|
2019-12-07 16:58:52 +05:30
|
|
|
countUsers?: number;
|
|
|
|
description?: string;
|
|
|
|
redirectUri?: string;
|
2019-11-27 14:33:32 +05:30
|
|
|
// fields for 'minecraft-server' type
|
2019-12-07 16:58:52 +05:30
|
|
|
minecraftServerIp?: string;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
interface OauthRequestData {
|
2019-12-07 16:58:52 +05:30
|
|
|
client_id: string;
|
|
|
|
redirect_uri: string;
|
|
|
|
response_type: string;
|
2019-12-09 12:37:07 +05:30
|
|
|
description?: string;
|
2019-12-07 16:58:52 +05:30
|
|
|
scope: string;
|
|
|
|
prompt: string;
|
|
|
|
login_hint?: string;
|
|
|
|
state?: string;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
2018-05-03 10:45:09 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
export interface OauthData {
|
2019-12-07 16:58:52 +05:30
|
|
|
clientId: string;
|
|
|
|
redirectUrl: string;
|
|
|
|
responseType: string;
|
2019-12-09 12:37:07 +05:30
|
|
|
description?: string;
|
2019-12-07 16:58:52 +05:30
|
|
|
scope: string;
|
2020-01-18 02:07:52 +05:30
|
|
|
// TODO: why prompt is not nullable?
|
2019-12-07 16:58:52 +05:30
|
|
|
prompt: string; // comma separated list of 'none' | 'consent' | 'select_account';
|
|
|
|
loginHint?: string;
|
|
|
|
state?: string;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
2018-05-03 10:45:09 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
export interface OAuthValidateResponse {
|
|
|
|
session: {
|
|
|
|
scopes: Scope[];
|
|
|
|
};
|
|
|
|
client: Client;
|
|
|
|
oAuth: {}; // TODO: improve typing
|
|
|
|
}
|
|
|
|
|
|
|
|
interface FormPayloads {
|
2019-12-07 16:58:52 +05:30
|
|
|
name?: string;
|
|
|
|
description?: string;
|
|
|
|
websiteUrl?: string;
|
|
|
|
redirectUri?: string;
|
|
|
|
minecraftServerIp?: string;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2018-11-10 14:33:47 +05:30
|
|
|
const api = {
|
2019-11-27 14:33:32 +05:30
|
|
|
validate(oauthData: OauthData) {
|
|
|
|
return request
|
2020-01-18 02:07:52 +05:30
|
|
|
.get<OAuthValidateResponse>(
|
|
|
|
'/api/oauth2/v1/validate',
|
|
|
|
getOAuthRequest(oauthData),
|
|
|
|
)
|
2019-11-27 14:33:32 +05:30
|
|
|
.catch(handleOauthParamsValidation);
|
|
|
|
},
|
|
|
|
|
|
|
|
complete(
|
|
|
|
oauthData: OauthData,
|
|
|
|
params: { accept?: boolean } = {},
|
|
|
|
): Promise<{
|
2019-12-07 16:58:52 +05:30
|
|
|
success: boolean;
|
|
|
|
redirectUri: string;
|
2019-11-27 14:33:32 +05:30
|
|
|
}> {
|
|
|
|
const query = request.buildQuery(getOAuthRequest(oauthData));
|
|
|
|
|
|
|
|
return request
|
|
|
|
.post<{
|
2019-12-07 16:58:52 +05:30
|
|
|
success: boolean;
|
|
|
|
redirectUri: string;
|
2019-11-27 14:33:32 +05:30
|
|
|
}>(
|
|
|
|
`/api/oauth2/v1/complete?${query}`,
|
|
|
|
typeof params.accept === 'undefined' ? {} : { accept: params.accept },
|
|
|
|
)
|
|
|
|
.catch((resp = {}) => {
|
|
|
|
if (resp.statusCode === 401 && resp.error === 'access_denied') {
|
|
|
|
// user declined permissions
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
redirectUri: resp.redirectUri,
|
|
|
|
originalResponse: resp.originalResponse,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resp.status === 401 && resp.name === 'Unauthorized') {
|
2019-12-07 16:58:52 +05:30
|
|
|
const error: { [key: string]: any } = new Error('Unauthorized');
|
2019-11-27 14:33:32 +05:30
|
|
|
error.unauthorized = true;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resp.statusCode === 401 && resp.error === 'accept_required') {
|
2019-12-07 16:58:52 +05:30
|
|
|
const error: { [key: string]: any } = new Error(
|
|
|
|
'Permissions accept required',
|
|
|
|
);
|
2019-11-27 14:33:32 +05:30
|
|
|
error.acceptRequired = true;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return handleOauthParamsValidation(resp);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
create(type: string, formParams: FormPayloads) {
|
2019-12-07 16:58:52 +05:30
|
|
|
return request.post<{ success: boolean; data: OauthAppResponse }>(
|
2019-11-27 14:33:32 +05:30
|
|
|
`/api/v1/oauth2/${type}`,
|
|
|
|
formParams,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
update(clientId: string, formParams: FormPayloads) {
|
2019-12-07 16:58:52 +05:30
|
|
|
return request.put<{ success: boolean; data: OauthAppResponse }>(
|
2019-11-27 14:33:32 +05:30
|
|
|
`/api/v1/oauth2/${clientId}`,
|
|
|
|
formParams,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
getApp(clientId: string) {
|
|
|
|
return request.get<OauthAppResponse>(`/api/v1/oauth2/${clientId}`);
|
|
|
|
},
|
|
|
|
|
|
|
|
getAppsByUser(userId: number): Promise<OauthAppResponse[]> {
|
|
|
|
return request.get(`/api/v1/accounts/${userId}/oauth2/clients`);
|
|
|
|
},
|
|
|
|
|
|
|
|
reset(clientId: string, regenerateSecret: boolean = false) {
|
2019-12-07 16:58:52 +05:30
|
|
|
return request.post<{ success: boolean; data: OauthAppResponse }>(
|
2019-11-27 14:33:32 +05:30
|
|
|
`/api/v1/oauth2/${clientId}/reset${
|
|
|
|
regenerateSecret ? '?regenerateSecret' : ''
|
|
|
|
}`,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
delete(clientId: string) {
|
|
|
|
return request.delete<{ success: boolean }>(`/api/v1/oauth2/${clientId}`);
|
|
|
|
},
|
2016-07-27 23:57:21 +05:30
|
|
|
};
|
2018-11-10 14:33:47 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
if ('Cypress' in window) {
|
|
|
|
(window as any).oauthApi = api;
|
2018-11-10 14:33:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export default api;
|
|
|
|
|
2016-08-23 10:00:06 +05:30
|
|
|
/**
|
|
|
|
* @param {object} oauthData
|
|
|
|
* @param {string} oauthData.clientId
|
|
|
|
* @param {string} oauthData.redirectUrl
|
|
|
|
* @param {string} oauthData.responseType
|
|
|
|
* @param {string} oauthData.description
|
|
|
|
* @param {string} oauthData.scope
|
|
|
|
* @param {string} oauthData.state
|
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {object}
|
2016-08-23 10:00:06 +05:30
|
|
|
*/
|
2018-05-03 10:45:09 +05:30
|
|
|
function getOAuthRequest(oauthData: OauthData): OauthRequestData {
|
2019-11-27 14:33:32 +05:30
|
|
|
return {
|
|
|
|
client_id: oauthData.clientId,
|
|
|
|
redirect_uri: oauthData.redirectUrl,
|
|
|
|
response_type: oauthData.responseType,
|
|
|
|
description: oauthData.description,
|
|
|
|
scope: oauthData.scope,
|
|
|
|
prompt: oauthData.prompt,
|
|
|
|
login_hint: oauthData.loginHint,
|
|
|
|
state: oauthData.state,
|
|
|
|
};
|
2016-07-27 23:57:21 +05:30
|
|
|
}
|
|
|
|
|
2019-12-26 17:48:58 +05:30
|
|
|
function handleOauthParamsValidation(
|
|
|
|
resp:
|
|
|
|
| { [key: string]: any }
|
|
|
|
| {
|
|
|
|
statusCode: number;
|
|
|
|
success: false;
|
|
|
|
error:
|
|
|
|
| 'invalid_request'
|
|
|
|
| 'unsupported_response_type'
|
|
|
|
| 'invalid_scope'
|
|
|
|
| 'invalid_client';
|
|
|
|
parameter: string;
|
|
|
|
} = {},
|
|
|
|
) {
|
|
|
|
let userMessage: string | null = null;
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (resp.statusCode === 400 && resp.error === 'invalid_request') {
|
2019-12-26 17:48:58 +05:30
|
|
|
userMessage = `Invalid request (${resp.parameter} required).`;
|
2019-11-27 14:33:32 +05:30
|
|
|
} else if (
|
|
|
|
resp.statusCode === 400 &&
|
|
|
|
resp.error === 'unsupported_response_type'
|
|
|
|
) {
|
2019-12-26 17:48:58 +05:30
|
|
|
userMessage = `Invalid response type '${resp.parameter}'.`;
|
2019-11-27 14:33:32 +05:30
|
|
|
} else if (resp.statusCode === 400 && resp.error === 'invalid_scope') {
|
2019-12-26 17:48:58 +05:30
|
|
|
userMessage = `Invalid scope '${resp.parameter}'.`;
|
2019-11-27 14:33:32 +05:30
|
|
|
} else if (resp.statusCode === 401 && resp.error === 'invalid_client') {
|
2019-12-26 17:48:58 +05:30
|
|
|
userMessage = 'Can not find application you are trying to authorize.';
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
|
|
|
|
2019-12-26 17:48:58 +05:30
|
|
|
return userMessage
|
|
|
|
? Promise.reject({ ...resp, userMessage })
|
|
|
|
: Promise.reject(resp);
|
2016-07-27 23:57:21 +05:30
|
|
|
}
|