nw-auth
TypeScript icon, indicating that this package has built-in type declarations

1.0.3Β β€’Β PublicΒ β€’Β Published

Node Way Auth

dependency philosophy

l node test module MIT

JavaScript Style Guide

EN/δΈ­ζ–‡


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.

It supports OIDC protocol-compliant authentication systems and is very easy to use.


Content

Download and Run

git clone ... into <nw-auth-home>
cd <nw-auth-home>
β”œβ”€ <nw-auth-home>
β”‚   └── data
β”‚   └── dto
β”‚   └── error
β”‚   └── sample
β”‚   └── service
β”‚   └── test
β”‚   └── ...
# compile
npm run clean
npm run build
# test
npm run test
# start
npm run start
# run example
curl http(s)://<server_host>/github/login

Usage

example on github oidc

npm i nw-auth
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(callback, 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)

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

Platform

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

Package Sidebar

Install

npm i nw-auth

Weekly Downloads

2

Version

1.0.3

License

MIT

Unpacked Size

73.6 kB

Total Files

39

Last publish

Collaborators

  • chuck1in