2018-03-26 00:46:45 +05:30
|
|
|
// @flow
|
2017-06-08 00:10:44 +05:30
|
|
|
/* eslint camelcase: off */
|
2018-05-03 10:45:09 +05:30
|
|
|
import type { Resp } from 'services/request';
|
2018-05-05 14:31:25 +05:30
|
|
|
import type { ApplicationType } from 'components/dev/apps';
|
2016-07-27 23:57:21 +05:30
|
|
|
import request from 'services/request';
|
|
|
|
|
2018-03-26 00:46:45 +05:30
|
|
|
export type OauthAppResponse = {
|
|
|
|
clientId: string,
|
|
|
|
clientSecret: string,
|
2018-05-05 14:31:25 +05:30
|
|
|
type: ApplicationType,
|
2018-03-26 00:46:45 +05:30
|
|
|
name: string,
|
|
|
|
websiteUrl: string,
|
|
|
|
createdAt: number,
|
|
|
|
// fields for 'application' type
|
|
|
|
countUsers?: number,
|
|
|
|
description?: string,
|
|
|
|
redirectUri?: string,
|
|
|
|
// fields for 'minecraft-server' type
|
|
|
|
minecraftServerIp?: string,
|
|
|
|
};
|
|
|
|
|
2018-05-03 10:45:09 +05:30
|
|
|
type OauthRequestData = {
|
|
|
|
client_id: string,
|
|
|
|
redirect_uri: string,
|
|
|
|
response_type: string,
|
|
|
|
description: string,
|
|
|
|
scope: string,
|
|
|
|
prompt: string,
|
|
|
|
login_hint?: string,
|
|
|
|
state?: string,
|
|
|
|
};
|
|
|
|
|
|
|
|
export type OauthData = {
|
|
|
|
clientId: string,
|
|
|
|
redirectUrl: string,
|
|
|
|
responseType: string,
|
|
|
|
description: string,
|
|
|
|
scope: string,
|
|
|
|
prompt: 'none' | 'consent' | 'select_account',
|
|
|
|
loginHint?: string,
|
|
|
|
state?: string
|
|
|
|
};
|
|
|
|
|
2018-03-26 00:46:45 +05:30
|
|
|
type FormPayloads = {
|
|
|
|
name?: string,
|
|
|
|
description?: string,
|
|
|
|
websiteUrl?: string,
|
|
|
|
redirectUri?: string,
|
|
|
|
minecraftServerIp?: string,
|
|
|
|
};
|
|
|
|
|
2016-07-27 23:57:21 +05:30
|
|
|
export default {
|
2018-05-03 10:45:09 +05:30
|
|
|
validate(oauthData: OauthData) {
|
2016-07-27 23:57:21 +05:30
|
|
|
return request.get(
|
2016-07-28 01:15:50 +05:30
|
|
|
'/api/oauth2/v1/validate',
|
2016-07-27 23:57:21 +05:30
|
|
|
getOAuthRequest(oauthData)
|
|
|
|
).catch(handleOauthParamsValidation);
|
|
|
|
},
|
|
|
|
|
2018-05-03 10:45:09 +05:30
|
|
|
complete(oauthData: OauthData, params: {accept?: bool} = {}): Promise<Resp<{
|
|
|
|
success: bool,
|
|
|
|
redirectUri: string,
|
|
|
|
}>> {
|
2016-07-27 23:57:21 +05:30
|
|
|
const query = request.buildQuery(getOAuthRequest(oauthData));
|
|
|
|
|
|
|
|
return request.post(
|
2016-07-28 01:15:50 +05:30
|
|
|
`/api/oauth2/v1/complete?${query}`,
|
2016-07-27 23:57:21 +05:30
|
|
|
typeof params.accept === 'undefined' ? {} : {accept: params.accept}
|
|
|
|
).catch((resp = {}) => {
|
|
|
|
if (resp.statusCode === 401 && resp.error === 'access_denied') {
|
|
|
|
// user declined permissions
|
|
|
|
return {
|
|
|
|
success: false,
|
2018-05-03 10:45:09 +05:30
|
|
|
redirectUri: resp.redirectUri,
|
|
|
|
originalResponse: resp.originalResponse,
|
2016-07-27 23:57:21 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resp.status === 401 && resp.name === 'Unauthorized') {
|
2018-03-26 00:46:45 +05:30
|
|
|
const error: Object = new Error('Unauthorized');
|
2016-07-27 23:57:21 +05:30
|
|
|
error.unauthorized = true;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resp.statusCode === 401 && resp.error === 'accept_required') {
|
2018-03-26 00:46:45 +05:30
|
|
|
const error: Object = new Error('Permissions accept required');
|
2016-07-27 23:57:21 +05:30
|
|
|
error.acceptRequired = true;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return handleOauthParamsValidation(resp);
|
|
|
|
});
|
2018-03-26 00:46:45 +05:30
|
|
|
},
|
|
|
|
|
2018-05-03 10:45:09 +05:30
|
|
|
create(type: string, formParams: FormPayloads): Promise<Resp<{success: bool, data: OauthAppResponse}>> {
|
2018-03-26 00:46:45 +05:30
|
|
|
return request.post(`/api/v1/oauth2/${type}`, formParams);
|
|
|
|
},
|
|
|
|
|
2018-05-03 10:45:09 +05:30
|
|
|
update(clientId: string, formParams: FormPayloads): Promise<Resp<{success: bool, data: OauthAppResponse}>> {
|
2018-03-26 00:46:45 +05:30
|
|
|
return request.put(`/api/v1/oauth2/${clientId}`, formParams);
|
|
|
|
},
|
|
|
|
|
2018-05-03 10:45:09 +05:30
|
|
|
getApp(clientId: string): Promise<Resp<OauthAppResponse>> {
|
2018-03-26 00:46:45 +05:30
|
|
|
return request.get(`/api/v1/oauth2/${clientId}`);
|
|
|
|
},
|
|
|
|
|
2018-05-03 10:45:09 +05:30
|
|
|
getAppsByUser(userId: number): Promise<Resp<Array<OauthAppResponse>>> {
|
2018-03-26 00:46:45 +05:30
|
|
|
return request.get(`/api/v1/accounts/${userId}/oauth2/clients`);
|
|
|
|
},
|
|
|
|
|
2018-05-03 10:45:09 +05:30
|
|
|
reset(clientId: string, regenerateSecret: bool = false): Promise<Resp<{success: bool, data: OauthAppResponse}>> {
|
2018-03-26 00:46:45 +05:30
|
|
|
return request.post(`/api/v1/oauth2/${clientId}/reset${regenerateSecret ? '?regenerateSecret' : ''}`);
|
|
|
|
},
|
|
|
|
|
2018-05-03 10:45:09 +05:30
|
|
|
delete(clientId: string): Promise<Resp<{success: bool}>> {
|
2018-03-26 00:46:45 +05:30
|
|
|
return request.delete(`/api/v1/oauth2/${clientId}`);
|
|
|
|
},
|
2016-07-27 23:57:21 +05:30
|
|
|
};
|
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
|
|
|
|
*
|
|
|
|
* @return {object}
|
|
|
|
*/
|
2018-05-03 10:45:09 +05:30
|
|
|
function getOAuthRequest(oauthData: OauthData): OauthRequestData {
|
2016-07-27 23:57:21 +05:30
|
|
|
return {
|
|
|
|
client_id: oauthData.clientId,
|
|
|
|
redirect_uri: oauthData.redirectUrl,
|
|
|
|
response_type: oauthData.responseType,
|
2016-08-23 10:00:06 +05:30
|
|
|
description: oauthData.description,
|
2016-07-27 23:57:21 +05:30
|
|
|
scope: oauthData.scope,
|
2016-11-19 20:11:15 +05:30
|
|
|
prompt: oauthData.prompt,
|
|
|
|
login_hint: oauthData.loginHint,
|
2016-07-27 23:57:21 +05:30
|
|
|
state: oauthData.state
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleOauthParamsValidation(resp = {}) {
|
|
|
|
if (resp.statusCode === 400 && resp.error === 'invalid_request') {
|
2016-08-07 20:20:00 +05:30
|
|
|
resp.userMessage = `Invalid request (${resp.parameter} required).`;
|
2016-07-27 23:57:21 +05:30
|
|
|
} else if (resp.statusCode === 400 && resp.error === 'unsupported_response_type') {
|
2016-08-07 20:20:00 +05:30
|
|
|
resp.userMessage = `Invalid response type '${resp.parameter}'.`;
|
2016-07-27 23:57:21 +05:30
|
|
|
} else if (resp.statusCode === 400 && resp.error === 'invalid_scope') {
|
2016-08-07 20:20:00 +05:30
|
|
|
resp.userMessage = `Invalid scope '${resp.parameter}'.`;
|
2016-07-27 23:57:21 +05:30
|
|
|
} else if (resp.statusCode === 401 && resp.error === 'invalid_client') {
|
2016-08-07 20:20:00 +05:30
|
|
|
resp.userMessage = 'Can not find application you are trying to authorize.';
|
2016-07-27 23:57:21 +05:30
|
|
|
}
|
|
|
|
|
2016-08-07 20:20:00 +05:30
|
|
|
return Promise.reject(resp);
|
2016-07-27 23:57:21 +05:30
|
|
|
}
|