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
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
export type Scope = '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 {
|
2020-05-24 04:38:24 +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 {
|
2020-05-24 04:38:24 +05:30
|
|
|
clientId: string;
|
|
|
|
clientSecret: string;
|
|
|
|
type: ApplicationType;
|
|
|
|
name: string;
|
|
|
|
websiteUrl: string;
|
|
|
|
createdAt: number;
|
|
|
|
// fields for 'application' type
|
|
|
|
countUsers?: number;
|
|
|
|
description?: string;
|
|
|
|
redirectUri?: string;
|
|
|
|
// fields for 'minecraft-server' type
|
|
|
|
minecraftServerIp?: string;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2024-12-11 01:12:06 +05:30
|
|
|
interface AuthCodeFlowRequestData {
|
2020-05-24 04:38:24 +05:30
|
|
|
client_id: string;
|
|
|
|
redirect_uri: string;
|
|
|
|
response_type: string;
|
|
|
|
scope: string;
|
|
|
|
state?: string;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
2018-05-03 10:45:09 +05:30
|
|
|
|
2024-12-11 01:12:06 +05:30
|
|
|
interface DeviceCodeFlowRequestData {
|
|
|
|
user_code: string;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
2018-05-03 10:45:09 +05:30
|
|
|
|
2024-12-11 01:12:06 +05:30
|
|
|
export type OauthRequestData = (AuthCodeFlowRequestData | DeviceCodeFlowRequestData) & {
|
|
|
|
description?: string;
|
|
|
|
};
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
export interface OAuthValidateResponse {
|
2020-05-24 04:38:24 +05:30
|
|
|
session: {
|
|
|
|
scopes: Scope[];
|
|
|
|
};
|
|
|
|
client: Client;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
interface FormPayloads {
|
2020-05-24 04:38:24 +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 = {
|
2024-12-11 01:12:06 +05:30
|
|
|
validate(oauthData: OauthRequestData) {
|
2020-05-24 04:38:24 +05:30
|
|
|
return request
|
2024-12-11 01:12:06 +05:30
|
|
|
.get<OAuthValidateResponse>('/api/oauth2/v1/validate', oauthData)
|
2020-05-24 04:38:24 +05:30
|
|
|
.catch(handleOauthParamsValidation);
|
|
|
|
},
|
|
|
|
|
|
|
|
complete(
|
2024-12-11 01:12:06 +05:30
|
|
|
oauthData: OauthRequestData,
|
2020-05-24 04:38:24 +05:30
|
|
|
params: { accept?: boolean } = {},
|
|
|
|
): Promise<{
|
2019-12-07 16:58:52 +05:30
|
|
|
success: boolean;
|
2024-12-11 01:12:06 +05:30
|
|
|
redirectUri?: string;
|
2020-05-24 04:38:24 +05:30
|
|
|
}> {
|
2024-12-18 02:19:05 +05:30
|
|
|
const data: Record<string, any> = {};
|
|
|
|
|
|
|
|
if (typeof params.accept !== 'undefined') {
|
|
|
|
data.accept = params.accept;
|
|
|
|
}
|
2020-05-24 04:38:24 +05:30
|
|
|
|
|
|
|
return request
|
|
|
|
.post<{
|
|
|
|
success: boolean;
|
|
|
|
redirectUri: string;
|
2024-12-18 02:19:05 +05:30
|
|
|
}>(`/api/oauth2/v1/complete?${request.buildQuery(oauthData)}`, data)
|
2020-05-24 04:38:24 +05:30
|
|
|
.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') {
|
|
|
|
const error: { [key: string]: any } = new Error('Unauthorized');
|
|
|
|
error.unauthorized = true;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resp.statusCode === 401 && resp.error === 'accept_required') {
|
|
|
|
const error: { [key: string]: any } = new Error('Permissions accept required');
|
|
|
|
error.acceptRequired = true;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return handleOauthParamsValidation(resp);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
create(type: string, formParams: FormPayloads) {
|
|
|
|
return request.post<{ success: boolean; data: OauthAppResponse }>(`/api/v1/oauth2/${type}`, formParams);
|
|
|
|
},
|
|
|
|
|
|
|
|
update(clientId: string, formParams: FormPayloads) {
|
|
|
|
return request.put<{ success: boolean; data: OauthAppResponse }>(`/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) {
|
|
|
|
return request.post<{ success: boolean; data: OauthAppResponse }>(
|
|
|
|
`/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) {
|
2020-05-24 04:38:24 +05:30
|
|
|
(window as any).oauthApi = api;
|
2018-11-10 14:33:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export default api;
|
|
|
|
|
2019-12-26 17:48:58 +05:30
|
|
|
function handleOauthParamsValidation(
|
2020-05-24 04:38:24 +05:30
|
|
|
resp:
|
2024-12-11 01:12:06 +05:30
|
|
|
| Record<string, any>
|
2020-05-24 04:38:24 +05:30
|
|
|
| {
|
|
|
|
statusCode: number;
|
|
|
|
success: false;
|
2024-12-11 01:12:06 +05:30
|
|
|
error:
|
|
|
|
| 'invalid_request'
|
|
|
|
| 'unsupported_response_type'
|
|
|
|
| 'invalid_scope'
|
|
|
|
| 'invalid_client'
|
|
|
|
| 'invalid_user_code';
|
2020-05-24 04:38:24 +05:30
|
|
|
parameter: string;
|
|
|
|
} = {},
|
2019-12-26 17:48:58 +05:30
|
|
|
) {
|
2020-05-24 04:38:24 +05:30
|
|
|
let userMessage: string | null = null;
|
|
|
|
|
|
|
|
if (resp.statusCode === 400 && resp.error === 'invalid_request') {
|
|
|
|
userMessage = `Invalid request (${resp.parameter} required).`;
|
|
|
|
} else if (resp.statusCode === 400 && resp.error === 'unsupported_response_type') {
|
|
|
|
userMessage = `Invalid response type '${resp.parameter}'.`;
|
|
|
|
} else if (resp.statusCode === 400 && resp.error === 'invalid_scope') {
|
|
|
|
userMessage = `Invalid scope '${resp.parameter}'.`;
|
|
|
|
} else if (resp.statusCode === 401 && resp.error === 'invalid_client') {
|
|
|
|
userMessage = 'Can not find application you are trying to authorize.';
|
|
|
|
}
|
|
|
|
|
|
|
|
return userMessage ? Promise.reject({ ...resp, userMessage }) : Promise.reject(resp);
|
2016-07-27 23:57:21 +05:30
|
|
|
}
|