-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient-store.js
More file actions
81 lines (74 loc) · 2.22 KB
/
client-store.js
File metadata and controls
81 lines (74 loc) · 2.22 KB
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
const OIDCRelyingParty = require('@solid/oidc-rp')
const KVPFileStore = require('kvplus-files')
const COLLECTION_NAME = 'clients'
module.exports = class OIDCClientStore {
/**
* @constructor
*
* @param [options={}] {Object}
*
* @param [options.collectionName='clients'] {string}
*
* @param [options.backend] {KVPFileStore} Either pass in a backend store
* @param [options.path] {string} Or initialize the store from path.
*/
constructor (options = {}) {
this.collectionName = options.collectionName || COLLECTION_NAME
this.backend = options.backend ||
new KVPFileStore({
path: options.path,
collections: [ this.collectionName ]
})
this.backend.serialize = (client) => { return client.serialize() }
this.backend.deserialize = (data) => {
let result
try {
result = JSON.parse(data)
} catch (error) {
console.log(`Error parsing JSON at '${this.backend.path}', ` +
`collection '${this.collectionName}': `, error)
}
return result
}
}
del (client) {
if (!this.backend) {
return Promise.reject(new Error('Client store not configured'))
}
if (!client) {
return Promise.reject(new Error('Cannot delete null client'))
}
let issuer = encodeURIComponent(client.provider.url)
return this.backend.del(this.collectionName, issuer)
}
put (client) {
if (!this.backend) {
return Promise.reject(new Error('Client store not configured'))
}
if (!client) {
return Promise.reject(new Error('Cannot store null client'))
}
let issuer = encodeURIComponent(client.provider.url)
return this.backend.put(this.collectionName, issuer, client)
.then(() => {
return client
})
}
get (issuer) {
if (!this.backend) {
return Promise.reject(new Error('Client store not configured'))
}
issuer = encodeURIComponent(issuer)
return this.backend.get(this.collectionName, issuer)
.then(result => {
if (result) {
return OIDCRelyingParty.from(result)
}
return result
})
.catch(error => {
console.error('Error in clientStore.get() while loading a RelyingParty:',
error)
})
}
}