Fix GraphQL + OAuth 2.0 (Fixes #699)

This commit is contained in:
Gregory Schier
2018-01-17 11:20:45 +07:00
parent 0739b6e6fc
commit f978015ca2
4 changed files with 25 additions and 19 deletions

View File

@@ -674,6 +674,7 @@ export async function sendWithSettings (
const settings = await models.settings.getOrCreate();
const ancestors = await db.withAncestors(request, [
models.request.type,
models.requestGroup.type,
models.workspace.type
]);

View File

@@ -65,9 +65,10 @@ async function _authorize (url, clientId, redirectUri = '', scope = '', state =
// Add query params to URL
const qs = buildQueryStringFromParams(params);
const finalUrl = joinUrlAndQueryString(url, qs);
const regex = new RegExp(`${escapeRegex(redirectUri)}.*(code=|error=)`, 'i');
const successRegex = new RegExp(`${escapeRegex(redirectUri)}.*(code=)`, 'i');
const failureRegex = new RegExp(`${escapeRegex(redirectUri)}.*(error=)`, 'i');
const redirectedTo = await authorizeUserInWindow(finalUrl, regex);
const redirectedTo = await authorizeUserInWindow(finalUrl, successRegex, failureRegex);
console.log('[oauth2] Detected redirect ' + redirectedTo);

View File

@@ -20,25 +20,28 @@ export function responseToObject (body, keys) {
let results = {};
for (const key of keys) {
const value = data[key] !== undefined ? data[key] : null;
results[key] = value;
results[key] = data[key] !== undefined ? data[key] : null;
}
return results;
}
export function authorizeUserInWindow (url, urlSuccessRegex = /.*/, urlFailureRegex = /.*/) {
export function authorizeUserInWindow (
url,
urlSuccessRegex = /(code=).*/,
urlFailureRegex = /(error=).*/
) {
return new Promise((resolve, reject) => {
let finalUrl = null;
let hasError = false;
function _parseUrl (currentUrl) {
if (currentUrl.match(urlSuccessRegex)) {
console.log(`[oauth2] Matched redirect to "${currentUrl}" with ${urlSuccessRegex.toString()}`);
console.log(`[oauth2] Matched success redirect to "${currentUrl}" with ${urlSuccessRegex.toString()}`);
finalUrl = currentUrl;
child.close();
} else if (currentUrl.match(urlFailureRegex)) {
console.log(`[oauth2] Matched redirect to "${currentUrl}" with ${urlFailureRegex.toString()}`);
console.log(`[oauth2] Matched error redirect to "${currentUrl}" with ${urlFailureRegex.toString()}`);
hasError = true;
child.close();
} else if (currentUrl === url) {

View File

@@ -19,6 +19,7 @@ import type {RenderedRequest} from '../../../../common/render';
import {getRenderedRequest} from '../../../../common/render';
import TimeFromNow from '../../time-from-now';
import * as models from '../../../../models/index';
import * as db from '../../../../common/database';
type GraphQLBody = {
query: string,
@@ -97,14 +98,15 @@ class GraphQLEditor extends React.PureComponent<Props, State> {
try {
const bodyJson = JSON.stringify({query: introspectionQuery});
const introspectionRequest = Object.assign({}, request, {
body: newBodyRaw(bodyJson, CONTENT_TYPE_JSON),
// NOTE: We're not actually saving this request or response but let's pretend
// like we are by setting these properties to prevent bugs in the future.
_id: request._id + '.graphql',
parentId: request._id
parentId: request._id,
body: newBodyRaw(bodyJson, CONTENT_TYPE_JSON)
});
// We need to save this request because other parts of the
// app may look it up
await db.upsert(introspectionRequest);
const response = await network._actuallySend(
introspectionRequest,
workspace,
@@ -113,17 +115,16 @@ class GraphQLEditor extends React.PureComponent<Props, State> {
const bodyBuffer = models.response.getBodyBuffer(response);
const status = response.statusCode || 0;
const status = typeof response.statusCode === 'number' ? response.statusCode : 0;
const error = typeof response.error === 'string' ? response.error : '';
if (response.error) {
newState.schemaFetchError = response.error;
if (error) {
newState.schemaFetchError = error;
} else if (status < 200 || status >= 300) {
const msg = `Got status ${status} fetching schema from "${request.url}"`;
newState.schemaFetchError = msg;
newState.schemaFetchError = `Got status ${status} fetching schema from "${request.url}"`;
} else if (bodyBuffer) {
const {data} = JSON.parse(bodyBuffer.toString());
const schema = buildClientSchema(data);
newState.schema = schema;
newState.schema = buildClientSchema(data);
newState.schemaLastFetchTime = Date.now();
} else {
newState.schemaFetchError = 'No response body received when fetching schema';