mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-24 09:26:14 -04:00
Feat/oauth2 (#6281): Added oauth to API paths
* Added oauth provider. * Fixed provider. * Added auth flow. * Fixed auth flow and added scaffolding vite config. * Added working oauth2. * Fixed dockerfile. * Adapted run.sh script * Moved api tests to oauth2. * Updated security schemes. * Removed api key from existance. * Fixed installation * Added missing issuer in config. * Fixed dev dependencies. * Updated lock file.
This commit is contained in:
parent
562177022f
commit
fb56809e55
44 changed files with 1782 additions and 237 deletions
115
src/node/security/OIDCAdapter.ts
Normal file
115
src/node/security/OIDCAdapter.ts
Normal file
|
@ -0,0 +1,115 @@
|
|||
import {LRUCache} from 'lru-cache';
|
||||
import type {Adapter, AdapterPayload} from "oidc-provider";
|
||||
|
||||
|
||||
const options = {
|
||||
max: 500,
|
||||
sizeCalculation: (item:any, key:any) => {
|
||||
return 1
|
||||
},
|
||||
// for use with tracking overall storage size
|
||||
maxSize: 5000,
|
||||
|
||||
// how long to live in ms
|
||||
ttl: 1000 * 60 * 5,
|
||||
|
||||
// return stale items before removing from cache?
|
||||
allowStale: false,
|
||||
|
||||
updateAgeOnGet: false,
|
||||
updateAgeOnHas: false,
|
||||
}
|
||||
|
||||
const epochTime = (date = Date.now()) => Math.floor(date / 1000);
|
||||
|
||||
const storage = new LRUCache<string,AdapterPayload|string[]|string>(options);
|
||||
|
||||
function grantKeyFor(id: string) {
|
||||
return `grant:${id}`;
|
||||
}
|
||||
|
||||
function userCodeKeyFor(userCode:string) {
|
||||
return `userCode:${userCode}`;
|
||||
}
|
||||
|
||||
class MemoryAdapter implements Adapter{
|
||||
private readonly name: string;
|
||||
constructor(name:string) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
key(id:string) {
|
||||
return `${this.name}:${id}`;
|
||||
}
|
||||
|
||||
destroy(id:string) {
|
||||
const key = this.key(id);
|
||||
|
||||
const found = storage.get(key) as AdapterPayload;
|
||||
const grantId = found && found.grantId;
|
||||
|
||||
storage.delete(key);
|
||||
|
||||
if (grantId) {
|
||||
const grantKey = grantKeyFor(grantId);
|
||||
(storage.get(grantKey) as string[])!.forEach(token => storage.delete(token));
|
||||
storage.delete(grantKey);
|
||||
}
|
||||
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
consume(id: string) {
|
||||
(storage.get(this.key(id)) as AdapterPayload)!.consumed = epochTime();
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
find(id: string): Promise<AdapterPayload | void | undefined> {
|
||||
if (storage.has(this.key(id))){
|
||||
return Promise.resolve<AdapterPayload>(storage.get(this.key(id)) as AdapterPayload);
|
||||
}
|
||||
return Promise.resolve<undefined>(undefined)
|
||||
}
|
||||
|
||||
findByUserCode(userCode: string) {
|
||||
const id = storage.get(userCodeKeyFor(userCode)) as string;
|
||||
return this.find(id);
|
||||
}
|
||||
|
||||
upsert(id: string, payload: {
|
||||
iat: number;
|
||||
exp: number;
|
||||
uid: string;
|
||||
kind: string;
|
||||
jti: string;
|
||||
accountId: string;
|
||||
loginTs: number;
|
||||
}, expiresIn: number) {
|
||||
const key = this.key(id);
|
||||
|
||||
storage.set(key, payload, {ttl: expiresIn * 1000});
|
||||
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
findByUid(uid: string): Promise<AdapterPayload | void | undefined> {
|
||||
for(const [_, value] of storage.entries()){
|
||||
if(typeof value ==="object" && "uid" in value && value.uid === uid){
|
||||
return Promise.resolve(value);
|
||||
}
|
||||
}
|
||||
return Promise.resolve(undefined);
|
||||
}
|
||||
|
||||
revokeByGrantId(grantId: string): Promise<void | undefined> {
|
||||
const grantKey = grantKeyFor(grantId);
|
||||
const grant = storage.get(grantKey) as string[];
|
||||
if (grant) {
|
||||
grant.forEach((token) => storage.delete(token));
|
||||
storage.delete(grantKey);
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
export default MemoryAdapter
|
Loading…
Add table
Add a link
Reference in a new issue