/nw-auth

πŸ’« Node way auth is a third-party-login component developed by node-way that has πŸ’ small code size, πŸ‹ less interface exposure, and πŸ₯ no runtime library.

Primary LanguageTypeScriptMIT LicenseMIT

Node Auth

dependency philosophy

l node test module MIT

JavaScript Style Guide

δΈ­ζ–‡/EN


A third-party login component developed with the node way philosophy. Its features include:

  • πŸ’ Small code size
  • πŸ‹ Few exposed interfaces
  • πŸ₯ No runtime dependencies

The component is designed and developed based on the OIDC authentication process, providing comprehensive support for both common third-party login platforms and self-deployed OIDC authentication servers.

Usage

npm i nw-auth

Github Third-party Login Example

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 Process Node Type Declaration

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;
    ...
}

Testing

Unit Testing

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

Self-deployment Testing

🎁 The new version of the component adds a self-deployed web application, which provides docking tests for third-party login platforms in the form of docking examples and visualization pages.

flow

Use Example Testing

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

Visualization Testing

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

Supported Platforms

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