From 431cf76b054fda3e4fd43020acb1be579803577d Mon Sep 17 00:00:00 2001 From: Tim Leslie Date: Wed, 17 Jun 2020 11:29:36 +1000 Subject: [PATCH] Use keystone.executeGraphQL in Passport package (#3163) --- .changeset/hip-pears-visit.md | 5 ++ packages/auth-passport/lib/Passport.js | 93 +++++++++++--------------- 2 files changed, 45 insertions(+), 53 deletions(-) create mode 100644 .changeset/hip-pears-visit.md diff --git a/.changeset/hip-pears-visit.md b/.changeset/hip-pears-visit.md new file mode 100644 index 00000000000..80cf6dd1b77 --- /dev/null +++ b/.changeset/hip-pears-visit.md @@ -0,0 +1,5 @@ +--- +'@keystonejs/auth-passport': patch +--- + +Updated to use `keystone.executeGraphQL` for all server-side GraphQL operations. diff --git a/packages/auth-passport/lib/Passport.js b/packages/auth-passport/lib/Passport.js index 69036ccf5d5..32216b58715 100644 --- a/packages/auth-passport/lib/Passport.js +++ b/packages/auth-passport/lib/Passport.js @@ -29,7 +29,7 @@ const sessionFragment = ` * - Item: An item from a Keystone 5 list */ class PassportAuthStrategy { - constructor(authType, keystone, listKey, config, ServiceStrategy = null) { + constructor(authType, keystone, listKey, config, ServiceStrategy = null, schemaName = 'public') { assert(!!config.callbackPath, 'Must provide `config.callbackPath` option.'); assert(!!config.appId, 'Must provide `config.appId` option.'); assert(!!config.appSecret, 'Must provide `config.appSecret` option.'); @@ -81,6 +81,7 @@ class PassportAuthStrategy { this._listKey = listKey; this._ServiceStrategy = ServiceStrategy; this._sessionManager = keystone._sessionManager; + this._schemaName = schemaName; // Pull all the required data off the `config` object this._serviceAppId = config.appId; @@ -121,6 +122,21 @@ class PassportAuthStrategy { `; } + async _executeQuery({ query, variables }) { + const { errors, data } = await this._keystone.executeGraphQL({ + context: this._keystone.createContext({ + schemaName: this._schemaName, + skipAccessControl: true, + }), + query, + variables, + }); + if (errors) { + throw errors; + } + return data; + } + // Called this from within Keystone's .prepare() method prepareMiddleware() { const app = express(); @@ -161,8 +177,8 @@ class PassportAuthStrategy { let item; try { const queryName = this._getList().gqlNames.listQueryName; - const { errors, data } = await this._keystone.executeQuery( - ` + const data = await this._executeQuery({ + query: ` query($serviceId: String) { ${queryName}( first: 1 @@ -175,12 +191,8 @@ class PassportAuthStrategy { } } `, - { variables: { serviceId: serviceProfile.id } } - ); - - if (errors) { - throw errors; - } + variables: { serviceId: serviceProfile.id }, + }); item = data[queryName].length ? data[queryName][0] : null; } catch (error) { @@ -269,23 +281,16 @@ class PassportAuthStrategy { // Extract the accessToken from the Passport Session List Item const queryName = this._getSessionList().gqlNames.itemQueryName; - const { - errors, - data: { getSession: passportSessionInfo }, - } = await this._keystone.executeQuery( - ` + const { getSession: passportSessionInfo } = await this._executeQuery({ + query: ` query($id: ID!) { getSession: ${queryName}(where: { id: $id }) { ${sessionFragment} } } `, - { variables: { id: passportSessionId } } - ); - - if (errors) { - throw errors; - } + variables: { id: passportSessionId }, + }); // Ask the service for all the profile info for the given accessToken const serviceProfile = await this._validateWithService( @@ -408,32 +413,23 @@ class PassportAuthStrategy { const mutationName = this._getSessionList().gqlNames.createMutationName; const mutationInputName = this._getSessionList().gqlNames.createInputName; - const { - errors, - data: { createSession: passportSessionInfo }, - } = await this._keystone.executeQuery( - ` + const { createSession: passportSessionInfo } = await this._executeQuery({ + query: ` mutation($newSessionDataObject: ${mutationInputName}) { createSession: ${mutationName}(data: $newSessionDataObject) { ${sessionFragment} } } `, - { - variables: { - newSessionDataObject: { - [FIELD_TOKEN_SECRET]: accessToken, - [FIELD_REFRESH_TOKEN]: refreshToken, - [FIELD_SERVICE_NAME]: this.authType, - [FIELD_USER_ID]: serviceProfile.id, - }, + variables: { + newSessionDataObject: { + [FIELD_TOKEN_SECRET]: accessToken, + [FIELD_REFRESH_TOKEN]: refreshToken, + [FIELD_SERVICE_NAME]: this.authType, + [FIELD_USER_ID]: serviceProfile.id, }, - } - ); - - if (errors) { - throw errors; - } + }, + }); done(null, { serviceProfile, accessToken, passportSessionInfo }); } @@ -481,8 +477,8 @@ class PassportAuthStrategy { // Here we create both the Passport Session Item and the User Item // in KS5 as a single, nested mutation. - const { errors, data: sessionItem } = await this._keystone.executeQuery( - ` + const { sessionItem } = await this._executeQuery({ + query: ` mutation($id: ID!, $data: ${passportSessionMutationInputName}) { session: ${passportSessionMutationName}(id: $id , data: $data) { item { @@ -491,18 +487,9 @@ class PassportAuthStrategy { } } `, - { - variables: { - id: passportSessionInfo.id, - // Create the Keystone item as a Nested Mutation - data: { item: { [operation]: itemData } }, - }, - } - ); - - if (errors) { - throw errors; - } + // Create the Keystone item as a Nested Mutation + variables: { id: passportSessionInfo.id, data: { item: { [operation]: itemData } } }, + }); return sessionItem.session.item; }