nw-auth
v1.0.3
Published
π‘A node-way -> [small,simple,fast] third-part-login library written in Typescript with no runtime dependency
Downloads
4
Maintainers
Readme
Node Way 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.
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 |
| ------------------------------------------------------------------------------ | ----------------------------------------------------------- | --------------------- | ----------------------- |
| | WechatOidc<appid,appsecret,redirectUrl>
| dto/wechat.d.ts
| |
| | SinaOidc<clientId,clientSecret,redirectUrl>
| dto/sina.d.ts
| example/sina.ts
|
| | FeishuOidc<appId,appSecret,appTicket,redirectUrl>
| dto/feishu.d.ts
| |
| | GithubOidc<clientId,clientSecret,redirectUrl,appName>
| dto/github.d.ts
| example/github.ts
|
| | GoogleOidc<clientId,clientSecret,redirectUrl>
| dto/google.d.ts
| example/google.ts
|