Skip to content

Latest commit

 

History

History
208 lines (173 loc) · 6.06 KB

README_CN.md

File metadata and controls

208 lines (173 loc) · 6.06 KB

Node Auth

dependency philosophy

l node test module MIT

JavaScript Style Guide

中文/EN


一款采用 node way 理念开发的三方登录组件。其特点为:

  • 🍒 代码体积小
  • 🍋 接口暴露少
  • 🥝 无运行时依赖

组件基于 OIDC 身份认证流程设计与开发,无论是常见的三方登录平台还是自部署的 OIDC 认证服务器都可以得到完善的支持。

Usage

npm i nw-auth

Github 三方登录示例

git clone ... into ${NW-AUTH-HOME}
vim/nano ${NW-AUTH-HOME}/packages/core/example/github.ts
import http from 'http'

import { GithubOidc } from '../service/github'

export const server = http
 .createServer((req, res) => {
  const reqUrl = req.url as string
  const url = new URL(reqUrl, `https://${req.headers.host as string}`)
  if (url.pathname === '/github/login') {
   const callback = `https://${req.headers.host as string}/github/login`
   const code = url.searchParams.get('code')
   const state = url.searchParams.get('state')
   const oidcService = new GithubOidc('<client_id>', '<client_secret>', callback, '<appName>')
   if (code === null || state === null) {
    oidcService
     .processOidc(callback)
     .then((oidcResp) => {
      if (oidcResp.type === 'redirect') {
       console.info('redirect user to -> ', oidcResp)
       res.writeHead(301, { Location: oidcResp.result as string })
       res.end()
      }
     })
     .catch((err) => {
      console.log(err)
      res.writeHead(500)
      res.end()
     })
   } else {
    console.log('handle user login callback ->', url)
    oidcService
     .processOidc(code, state)
     .then((oidcResp) => {
      if (oidcResp.type === 'userInfo') {
       console.info('request access token successful and get user info ->', oidcResp)
       res.write(JSON.stringify(oidcResp.result))
       res.writeHead(200)
       res.end()
      }
     })
     .catch((error) => {
      res.writeHead(500)
      res.end()
      console.error('backend channel error ->', error)
     })
   }
  }
 })
 .listen(80)

OIDC 流程节点类型声明

export interface RedirectReq {
    client_id: string;
    redirect_uri: string;
    login?: string;
    scope?: string;
    state?: string;
    allow_signup?: string;
}
export interface CallbackReq {
    code: string;
    state: string;
}
export interface AccessTokenReq {
    client_id: string;
    client_secret: string;
    code: string;
    redirect_uri?: string;
}
export interface AccessTokenReqHeader {
    Accept: 'application/json';
    'User-Agent': string;
    Authorization: 'string';
}
export interface AccessTokenResp {
    access_token: string;
    scope: string;
    token_type: string;
}
export interface UserInfoReqHeader {
    Authorization: string;
    Accept: 'application/json';
}
export interface UserInfoResp {
    login: string;
    id: string;
    node_id: string;
    avatar_url: string;
    gravatar_id: string;
    url: string;
    ...
}

测试

单元测试

git clone ... into ${NW-AUTH-HOME}
cd ${NW-AUTH-HOME}
npm i
npm run test -w packages/core

自部署测试

🎁 新版本的组件增加了一个自部署的 web 应用,以对接示例和可视化页面的方式提供三方登录平台的对接测试。

flow

使用示例测试

git clone ... into ${NW-AUTH-HOME}
cd ${NW-AUTH-HOME}
npm i
# Default app server -> http://localhost:80
npm run dev -w packages/core
# Run example
curl http(s)://<server_host>/github/login

可视化测试

git clone ... into ${NW-AUTH-HOME}
cd ${NW-AUTH-HOME}
.
├──LICENSE
├──package-lock.json
├──package.json
├──.gitignore
├──packages/
│   ├──core/
│   │   ├── ...
│   └──page/
│       ├── ...
└──README.md
# On shell session1 (default app server host port -> http://localhost:80)
npm run dev -w packages/core
# On shell session2 (default page server host port -> http://localhost:5173)
npm run dev -w packages/page

page

支持平台

Platform Constructor Type declaration Example
wechat WechatOidc<appid,appsecret,redirectUrl> dto/wechat.d.ts
sina SinaOidc<clientId,clientSecret,redirectUrl> dto/sina.d.ts example/sina.ts
feishu FeishuOidc<appId,appSecret,appTicket,redirectUrl> dto/feishu.d.ts
github GithubOidc<clientId,clientSecret,redirectUrl,appName> dto/github.d.ts example/github.ts
google GoogleOidc<clientId,clientSecret,redirectUrl> dto/google.d.ts example/google.ts
twitter TwitterOidc<clientId,redirectUrl> dto/twitter.d.ts example/twitter.ts