-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathadapter.js
More file actions
204 lines (184 loc) · 4.91 KB
/
adapter.js
File metadata and controls
204 lines (184 loc) · 4.91 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* Filesystem adapter for oidc-provider
* Stores OIDC data (tokens, sessions, clients, etc.) as JSON files
*/
import fs from 'fs-extra';
import path from 'path';
/**
* Get IDP root directory (dynamic to support changing DATA_ROOT)
*/
function getIdpRoot() {
const dataRoot = process.env.DATA_ROOT || './data';
return path.join(dataRoot, '.idp');
}
/**
* Convert model name to directory name
* e.g., 'AccessToken' -> 'access_token'
*/
function modelToDir(model) {
return model.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
}
/**
* Filesystem adapter for oidc-provider
* Implements the adapter interface required by oidc-provider
*/
class FilesystemAdapter {
constructor(model) {
this.model = model;
}
/**
* Get directory for this model (computed dynamically)
*/
get dir() {
return path.join(getIdpRoot(), modelToDir(this.model));
}
/**
* Get file path for an ID
*/
_path(id) {
// Sanitize ID to prevent path traversal
const safeId = id.replace(/[^a-zA-Z0-9_-]/g, '_');
return path.join(this.dir, `${safeId}.json`);
}
/**
* Create or update a stored item
* @param {string} id - Unique identifier
* @param {object} payload - Data to store
* @param {number} expiresIn - TTL in seconds
*/
async upsert(id, payload, expiresIn) {
await fs.ensureDir(this.dir);
const data = {
...payload,
_id: id,
};
// Set expiration if provided
if (expiresIn) {
data._expiresAt = Date.now() + (expiresIn * 1000);
}
await fs.writeJson(this._path(id), data, { spaces: 2 });
}
/**
* Find an item by ID
* @param {string} id - Unique identifier
* @returns {object|undefined} - The payload or undefined if not found/expired
*/
async find(id) {
try {
const data = await fs.readJson(this._path(id));
// Check if expired
if (data._expiresAt && data._expiresAt < Date.now()) {
await this.destroy(id);
return undefined;
}
return data;
} catch (err) {
if (err.code === 'ENOENT') {
return undefined;
}
throw err;
}
}
/**
* Find by user code (for device flow)
* @param {string} userCode - Device flow user code
*/
async findByUserCode(userCode) {
try {
const files = await fs.readdir(this.dir);
for (const file of files) {
if (file.startsWith('_')) continue; // Skip index files
const data = await fs.readJson(path.join(this.dir, file));
if (data.userCode === userCode) {
// Check expiry
if (data._expiresAt && data._expiresAt < Date.now()) {
await this.destroy(data._id);
continue;
}
return data;
}
}
} catch (err) {
if (err.code !== 'ENOENT') throw err;
}
return undefined;
}
/**
* Find by UID (for sessions/interactions)
* @param {string} uid - Session/interaction UID
*/
async findByUid(uid) {
try {
const files = await fs.readdir(this.dir);
for (const file of files) {
if (file.startsWith('_')) continue; // Skip index files
const data = await fs.readJson(path.join(this.dir, file));
if (data.uid === uid) {
// Check expiry
if (data._expiresAt && data._expiresAt < Date.now()) {
await this.destroy(data._id);
continue;
}
return data;
}
}
} catch (err) {
if (err.code !== 'ENOENT') throw err;
}
return undefined;
}
/**
* Delete an item
* @param {string} id - Unique identifier
*/
async destroy(id) {
try {
await fs.remove(this._path(id));
} catch (err) {
if (err.code !== 'ENOENT') throw err;
}
}
/**
* Mark a token as consumed (one-time use)
* @param {string} id - Token identifier
*/
async consume(id) {
const data = await this.find(id);
if (data) {
data.consumed = Date.now() / 1000; // oidc-provider expects seconds
await this.upsert(id, data);
}
}
/**
* Revoke all tokens for a grant
* Used when user revokes consent or logs out
* @param {string} grantId - Grant identifier
*/
async revokeByGrantId(grantId) {
try {
const files = await fs.readdir(this.dir);
for (const file of files) {
if (file.startsWith('_')) continue; // Skip index files
try {
const data = await fs.readJson(path.join(this.dir, file));
if (data.grantId === grantId) {
await fs.remove(path.join(this.dir, file));
}
} catch (err) {
// Skip files that can't be read
}
}
} catch (err) {
if (err.code !== 'ENOENT') throw err;
}
}
}
/**
* Adapter factory for oidc-provider
* @param {string} model - Model name (e.g., 'AccessToken', 'Client')
* @returns {FilesystemAdapter} - Adapter instance
*/
export function createAdapter(model) {
return new FilesystemAdapter(model);
}
export default FilesystemAdapter;