NodeJS 做 OIDC 服务

2022-07-21 13:18:05 浏览数 (2)

工具

  • NodeJS
  • KOA
  • node-oidc-provider

步骤

创建 KOA 服务

修改 app.js

代码语言:javascript复制
// app.js
const Koa = require('koa');

const { PORT = 3000 } = process.env;
const app = new Koa();

let server;
(async () => {
    let adapter;
    server = app.listen(PORT, () => {
        console.log(`application is listening on port ${PORT}`);
    });
})().catch((err) => {
    if (server && server.listening) server.close();
    console.error(err);
    process.exitCode = 1;
});

测试执行

代码语言:shell复制
npm run dev

成功显示

代码语言:shell复制
PS D:projectsjoy-sso> npm run dev

> joy-sso@1.0.0 dev
> node app.js

application is listening on port 3000.

配置 oidc provider

修改 app.js,引入OIDC,并创建 Provider。

代码语言:javascript复制
// app.js
...
const { Provider } = require('oidc-provider');
...
const ISSUER = `http://localhost:${PORT}`;
const provider = new Provider(ISSUER, {});

配置 Provider 到 KOA

代码语言:javascript复制
// app.js
...
const mount = require('koa-mount');
...
app.use(mount(provider.app));
server = app.listen(PORT, () => {
...

测试执行

代码语言:shell复制
npm run dev

成功显示

代码语言:shell复制
PS D:projectsjoy-sso> npm run dev

> joy-sso@1.0.0 dev
> node app.js

oidc-provider WARNING: a quick start development-only in-memory adapter is used, you MUST change it in order to not lose all stateful provider data upon restart and to be able to share these between processes
application is listening on port 3000.

到这一步,OIDC 服务就已经创建好了,我们拥有了一个极简的 OIDC 服务。

中间的警告可以忽略,意思是说数据存在内存中有风险,只适用于开发的场景。

接下来,我们通过一步步的配置,完善 OIDC 服务。

0 人点赞