Skip to content

Commit

Permalink
- adapter test updates, sequalize adapter example
Browse files Browse the repository at this point in the history
- closes #126
- closes #130
  • Loading branch information
panva committed Jul 12, 2017
1 parent 9fb8f2a commit 62161d8
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 20 deletions.
26 changes: 11 additions & 15 deletions example/adapters/mongodb_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@ const MongoAdapter = require('./mongodb');

const { AdapterTest } = Provider;

const provider = new Provider('http://localhost', {
adapter: MongoAdapter,
});
const provider = new Provider('http://localhost');
const test = new AdapterTest(provider);

MongoAdapter.once('ready', () => {
provider.initialize()
.then(() => test.execute())
.then(() => {
console.log('tests passed');
process.exit();
})
.catch((err) => {
console.dir(err);
process.exit(1);
});
});
provider.initialize({ adapter: MongoAdapter })
.then(() => test.execute())
.then(() => {
console.log('tests passed');
process.exit();
})
.catch((err) => {
console.dir(err);
process.exit(1);
});
6 changes: 2 additions & 4 deletions example/adapters/redis_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ const RedisAdapter = require('./redis');

const { AdapterTest } = Provider;

const provider = new Provider('http://localhost', {
adapter: RedisAdapter,
});
const provider = new Provider('http://localhost');
const test = new AdapterTest(provider);

provider.initialize()
provider.initialize({ adapter: RedisAdapter })
.then(() => test.execute())
.then(() => {
console.log('tests passed');
Expand Down
89 changes: 89 additions & 0 deletions example/adapters/sequelize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* eslint-disable */

/*
* This is a very rough-edged example, the idea is to still work with the fact that oidc-provider
* has a rather "dynamic" schema. This example uses sequelize with sqlite, and all dynamic data
* uses JSON fields. uuid is set to be the primary key and grantId should be additionaly indexed
* for token models.
*/

'use strict';

const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
dialect: 'sqlite',
storage: 'db.sqlite',
});

const grantable = [
'AccessToken',
'AuthorizationCode',
'RefreshToken',
];

const models = [
'Session',
'AccessToken',
'AuthorizationCode',
'RefreshToken',
'ClientCredentials',
'Client',
'InitialAccessToken',
'RegistrationAccessToken',
].reduce((map, name) => {
map.set(name, sequelize.define(name, {
uuid: { type: Sequelize.STRING, primaryKey: true },
grantId: { type: Sequelize.UUIDV4 },
data: { type: Sequelize.JSON },
expiresAt: { type: Sequelize.DATE },
consumedAt: { type: Sequelize.DATE },
}));

return map;
}, new Map());


class SequelizeAdapter {
constructor(name) {
this.model = models.get(name);
this.name = name;
}

async upsert(id, payload, expiresIn) {
return this.model.upsert(Object.assign(
{},
{ data: payload },
{ uuid: id },
payload.grantId ? { grantId: payload.grantId } : undefined,
expiresIn ? { expiresAt: new Date(Date.now() + (expiresIn * 1000)) } : undefined ));
}

async find(id) {
return this.model.findByPrimary(id).then((found) => {
if (found) {
return found.consumedAt ? Object.assign({}, found.data, { consumed: true }) : found.data;
}
return undefined;
});
}

async consume(id) {
return this.model.update({ consumedAt: new Date() }, { where: { uuid: id }});
}

async destroy(id) {
if (grantable.includes(this.name)) {
return this.model.findByPrimary(id).then((({ grantId }) => {
return Promise.all(grantable.map(name => models.get(name).destroy({ where: { grantId }})));
}));
}

return this.model.destroy({ where: { uuid: id }});
}

static async connect(provider) {
return sequelize.sync();
}
}

module.exports = SequelizeAdapter;
22 changes: 22 additions & 0 deletions example/adapters/sequelize_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// node example/adapters/redis_test.js

/* eslint-disable no-console */

const Provider = require('../../lib');
const SequelizeAdapter = require('./sequelize');

const { AdapterTest } = Provider;

const provider = new Provider('http://localhost');
const test = new AdapterTest(provider);

provider.initialize({ adapter: SequelizeAdapter })
.then(() => test.execute())
.then(() => {
console.log('tests passed');
process.exit();
})
.catch((err) => {
console.dir(err);
process.exit(1);
});
11 changes: 10 additions & 1 deletion example/my_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,20 @@ class MyAdapter {
*
* Client model will only use this when registered through Dynamic Registration features.
*
* Session model payload contains the following properties:
* OIDC Session model payload contains the following properties:
* - account {string} the session account identifier
* - authorizations {object} object with session authorized clients and their session identifiers
* - loginTs {number} timestamp of user's authentication
*
* Short-lived Interaction Session model payload contains the following properties:
* - returnTo {string} after resolving interactions send the user-agent to this url
* - interaction {object} details on the interaction details (error, reason code and descriptions)
* - uuid {string} - uuid of the grant
* - params {object} - parsed recognized parameters object
* - signed {array} - array of parameter names (keys) that were received from a signed and/or
* encrypted request/_uri object
* - result {object} - interaction results object is expected here
*
*/
}

Expand Down

0 comments on commit 62161d8

Please sign in to comment.