Skip to content

Commit

Permalink
refactor interaction helpers, assert id in Session.find
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Sep 19, 2017
1 parent 1b8e87f commit 8e1c5d5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
7 changes: 5 additions & 2 deletions lib/models/session.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const uuid = require('uuid');
const epochTime = require('../helpers/epoch_time');
const assert = require('assert');
const instance = require('../helpers/weak_cache');

module.exports = function getSession(provider) {
Expand Down Expand Up @@ -57,8 +58,10 @@ module.exports = function getSession(provider) {
return this.authorizations[key] && this.authorizations[key].sid;
}

static find(id) {
return getAdapter().find(id).then(data => new Session(id, data));
static async find(id) {
assert(id, 'id must be provided to Session#find');
const data = await getAdapter().find(id);
return new Session(id, data);
}

static async get(ctx) {
Expand Down
42 changes: 21 additions & 21 deletions lib/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ function grantCookie(cookies) {
}
}

async function getInteraction(req, res) {
assert(
req instanceof http.IncomingMessage,
'first argument must be the request (http.IncomingMessage), for express req, for koa ctx.req',
);
if (arguments.length === 2) {
assert(
res instanceof http.ServerResponse,
'second argument must be the response (http.ServerResponse), for express res, for koa ctx.res',
);
}
const { cookies } = this.app.createContext(req, res);
const id = grantCookie.call(this, cookies);
const interaction = await this.Session.find(id);
assert(interaction, 'interaction session not found');
return interaction;
}

class Provider extends events.EventEmitter {
constructor(issuer, setup = {}) {
assert(issuer, 'first argument must be the Issuer Identifier, i.e. https://op.example.com');
Expand Down Expand Up @@ -129,19 +147,7 @@ class Provider extends events.EventEmitter {
}

async interactionFinished(req, res, result) {
assert(
req instanceof http.IncomingMessage,
'first argument must be the request (http.IncomingMessage), for express req, for koa ctx.req',
);
assert(
res instanceof http.ServerResponse,
'second argument must be the response (http.ServerResponse), for express res, for koa ctx.res',
);

const { cookies } = this.app.createContext(req, res);
const interaction = await this.Session.find(grantCookie.call(this, cookies));
assert(interaction, 'interaction session not found');

const interaction = await getInteraction.call(this, req, res);
interaction.result = result;
await interaction.save(60); // TODO: ttl read from the session

Expand All @@ -152,14 +158,8 @@ class Provider extends events.EventEmitter {
}

async interactionDetails(req) {
assert(
req instanceof http.IncomingMessage,
'first argument must be the request, for express req, for koa ctx.req',
);

const id = grantCookie.call(this, this.app.createContext(req).cookies);

return this.Session.find(id);
const interaction = await getInteraction.call(this, req);
return interaction;
}

httpOptions(values) {
Expand Down

0 comments on commit 8e1c5d5

Please sign in to comment.