etherpad-lite/src/node/security/OAuth2Provider.ts

52 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-03-23 07:38:23 +01:00
import {ArgsExpressType} from "../types/ArgsExpressType";
2024-03-24 14:18:58 +01:00
import Provider, {Account, Configuration} from 'oidc-provider';
import {generateKeyPair, exportJWK} from 'jose'
2024-03-23 07:38:23 +01:00
const configuration: Configuration = {
// refer to the documentation for other available configuration
2024-03-24 14:18:58 +01:00
clients: [ {
client_id: 'oidc_client',
client_secret: 'a_different_secret',
grant_types: ['authorization_code'],
response_types: ['code'],
redirect_uris: ['http://localhost:3001/cb']
2024-03-23 07:38:23 +01:00
},
2024-03-24 14:18:58 +01:00
{
client_id: 'app',
client_secret: 'a_secret',
grant_types: ['client_credentials'],
redirect_uris: [],
response_types: []
}
],
scopes: ['openid', 'profile', 'email'],
//adapter: MemoryAdapter,
/*findAccount: async (ctx, id) => {
console.log(ctx, id)
return {
accountId: id,
claims: () => ({
sub: id,
})
} satisfies Account
},*/
2024-03-23 07:38:23 +01:00
};
2024-03-24 14:18:58 +01:00
export const expressCreateServer = async (hookName: string, args: ArgsExpressType, cb: Function) => {
const {privateKey} = await generateKeyPair('RS256');
const privateKeyJWK = await exportJWK(privateKey);
const oidc = new Provider('http://localhost:9001', {
...configuration, jwks: {
keys: [
privateKeyJWK
],
},
});
oidc.on('authorization.error', (ctx, error) => {
console.log('authorization.error', error);
})
args.app.use("/oidc", oidc.callback());
2024-03-23 07:38:23 +01:00
cb();
}