Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling refresh of auth token #9

Closed
damsleth opened this issue Dec 11, 2019 · 4 comments · Fixed by #84
Closed

Handling refresh of auth token #9

damsleth opened this issue Dec 11, 2019 · 4 comments · Fixed by #84
Assignees
Labels
backend Related to backend/apis Complexity: large Issue/PR with large complexity enhancement New feature or request help wanted Extra attention is needed Priority: high Issue with high priority

Comments

@damsleth
Copy link
Member

damsleth commented Dec 11, 2019

bitbucket.org/puzzlepart/pzl-did365/branch/feature/oauth2-refresh

@damsleth damsleth added this to the Internal testing milestone Dec 11, 2019
@damsleth damsleth added the bug Something isn't working label Dec 11, 2019
@olemp olemp added the backend Related to backend/apis label Dec 12, 2019
@damsleth
Copy link
Member Author

@olemp olemp mentioned this issue Jan 28, 2020
@olemp olemp added enhancement New feature or request help wanted Extra attention is needed Priority: high Issue with high priority Complexity: large Issue/PR with large complexity and removed bug Something isn't working labels Feb 6, 2020
@olemp
Copy link
Contributor

olemp commented Feb 10, 2020

So the GraphService is sent to the graphql service like this:

const path = require('path');
const graphql = require('express-graphql');
const { importSchema } = require('graphql-import');
const { makeExecutableSchema } = require('graphql-tools');
const StorageService = require('../../services/storage');
const GraphService = require('../../services/graph');

const schema = makeExecutableSchema({
  typeDefs: importSchema(path.join(__dirname, './schema.graphql')),
  resolvers: require('./resolvers'),
  resolverValidationOptions: {
    requireResolversForResolveType: false
  },
});

module.exports = graphql((req) => ({
  schema: schema,
  rootValue: global,
  graphiql: process.env.GRAPHIQL_ENABLED == '1',
  pretty: req.app.get('env') === 'development',
  context: {
    services: {
      graph: new GraphService(req.user.oauthToken),
      storage: new StorageService(req.user.profile._json.tid),
    },
    user: req.user,
    tid: req.user.profile._json.tid,
  }
}));

In the GraphService we could catch the error that occurs when the access token is expired, but I'm not sure how to update the user with the new authToken so it's available after a refresh.

GraphService.prototype.getEvents = async function (startDateTime, endDateTime) {
  log('Querying Graph /me/calendar/calendarView: %s', JSON.stringify({ startDateTime, endDateTime }));
  const { value } = await this.getClient()
    .api('/me/calendar/calendarView')
    .query({ startDateTime, endDateTime })
    .select('id,subject,body,start,end,lastModifiedDateTime,categories,webLink,isOrganizer')
    .filter(`sensitivity ne 'private' and isallday eq false and iscancelled eq false`)
    .orderby('start/dateTime asc')
    .top(50)
    .get();
  log('Retrieved %s events from /me/calendar/calendarView', value.length);
  let events = value
    .filter(evt => evt.subject)
    .map(evt => ({
      id: evt.id,
      title: evt.subject,
      body: stripHtml(evt.body.content),
      isOrganizer: evt.isOrganizer,
      categories: evt.categories,
      webLink: evt.webLink,
      lastModifiedDateTime: new Date(evt.lastModifiedDateTime).toISOString(),
      startTime: new Date(evt.start.dateTime).toISOString(),
      endTime: new Date(evt.end.dateTime).toISOString(),
      durationHours: utils.getDurationHours(evt.start.dateTime, evt.end.dateTime),
      durationMinutes: utils.getDurationMinutes(evt.start.dateTime, evt.end.dateTime),
    }));
  events = this.removeIgnoredEvents(events);
  return events;
};

cc @damsleth @okms

@olemp
Copy link
Contributor

olemp commented Feb 10, 2020

In fiznool/passport-oauth2-refresh#1 save() is available on request.user, so they can just do:

user.save({ accessToken: accessToken }, function() {
   makeRequest();
});

@olemp olemp linked a pull request Feb 10, 2020 that will close this issue
@olemp
Copy link
Contributor

olemp commented Feb 10, 2020

Seems like it works now locally using branch #84. When you have time @okms @damsleth, check it running locally, and review the PR.

[1] 2020-02-10T13:00:20.357Z services/tokens Successfully refreshed auth token

@olemp olemp closed this as completed in #84 Feb 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Related to backend/apis Complexity: large Issue/PR with large complexity enhancement New feature or request help wanted Extra attention is needed Priority: high Issue with high priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants