forked from panva/node-oidc-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.js
125 lines (104 loc) · 2.96 KB
/
models.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* eslint-disable max-classes-per-file */
const { strict: assert } = require('assert');
const map = new Map();
map.del = function (...args) {
this.delete(...args);
};
const { expect } = require('chai');
const epochTime = require('../lib/helpers/epoch_time');
const MemoryAdapter = require('../lib/adapters/memory_adapter');
MemoryAdapter.setStorage(map);
const testStorage = new Map();
class TestAdapter extends MemoryAdapter {
constructor(name) {
if (testStorage.has(name)) return testStorage.get(name);
super(name);
this.store = map;
testStorage.set(name, this);
}
static for(name) {
if (testStorage.has(name)) return testStorage.get(name);
return new this(name);
}
get(key) {
return this.constructor.for(key);
}
static clear() {
map.clear();
}
clear() { // eslint-disable-line class-methods-use-this
map.clear();
}
syncFind(id) {
return map.get(this.key(id)) || undefined;
}
syncUpdate(id, update) {
const found = map.get(this.key(id));
if (!found) return;
Object.assign(found, update);
}
async upsert(id, payload, expiresIn) {
if (this.model !== 'RegistrationAccessToken' && this.model !== 'InitialAccessToken' && this.model !== 'Client' && this.model !== 'Grant') {
expect(payload).to.have.property('exp').that.is.a('number').and.is.finite;
expect(payload.exp).to.be.closeTo(expiresIn + epochTime(), 1);
}
return super.upsert(id, payload, expiresIn);
}
}
class Account {
constructor(id) {
this.accountId = id;
testStorage.set(`Account:${this.accountId}`, this);
}
static get storage() {
return testStorage;
}
claims(use, scope, claims, rejected) {
assert.equal(typeof use, 'string');
assert.equal(typeof scope, 'string');
assert.equal(typeof claims, 'object');
assert.ok(Array.isArray(rejected));
return {
address: {
country: '000',
formatted: '000',
locality: '000',
postal_code: '000',
region: '000',
street_address: '000',
},
birthdate: '1987-10-16',
email: '[email protected]',
email_verified: false,
family_name: 'Doe',
gender: 'male',
given_name: 'John',
locale: 'en-US',
middle_name: 'Middle',
name: 'John Doe',
nickname: 'Johny',
phone_number: '+420 721 773500',
phone_number_verified: false,
picture: 'http://lorempixel.com/400/200/',
preferred_username: 'johnny',
profile: 'https://johnswebsite.com',
sub: this.accountId,
updated_at: 1454704946,
website: 'http://example.com',
zoneinfo: 'Europe/Berlin',
};
}
// eslint-disable-next-line no-unused-vars
static async findAccount(ctx, sub, token) {
if (sub === 'notfound') {
return undefined;
}
assert.equal(typeof sub, 'string');
let acc = testStorage.get(`Account:${sub}`);
if (!acc) {
acc = new Account(sub);
}
return acc;
}
}
module.exports = { Account, TestAdapter };