Skip to content

Commit

Permalink
Use keystone.executeGraphQL in Passport package (#3163)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Jun 17, 2020
1 parent 7071886 commit 431cf76
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 53 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-pears-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/auth-passport': patch
---

Updated to use `keystone.executeGraphQL` for all server-side GraphQL operations.
93 changes: 40 additions & 53 deletions packages/auth-passport/lib/Passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 });
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down

0 comments on commit 431cf76

Please sign in to comment.