# @tntd/sso-login

> koa 统一登录中间件

Latest version **1.0.1** (published 2021-03-02) · 0 weekly downloads

## Install

```sh
npm install @tntd/sso-login
pnpm add @tntd/sso-login
yarn add @tntd/sso-login
bun add @tntd/sso-login
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2021-03-02 |
| First published | 2021-03-02 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=v8.9.0 |
| Dependencies | 3 |
| Unpacked size | 7.6 KB |
| Known vulnerabilities | 0 (+25 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Anthony Li |
| Maintainers | mingquanliu, td-tnt, bruceliu68, bernardzhang, mizy, zhouzefei, leocmial, xiaofei.wang, anthonyli, xiaocuicui, popoqaq |
| Keywords | koa, cas, sso, middleware |

## Links

- npm: https://www.npmjs.com/package/@tntd/sso-login
- Repository: https://github.com/tntd/sso-login
- Homepage: https://github.com/tntd/sso-login#readme
- Issues: https://github.com/tntd/sso-login/issues
- npm.io page: https://npm.io/package/@tntd/sso-login

## Dependencies (3)

- [axios](https://npm.io/package/axios.md) ^0.19.0
- [node-rsa](https://npm.io/package/node-rsa.md) ^1.0.6
- [query-string](https://npm.io/package/query-string.md) ^6.8.1

## Alternatives

- [@mce/gif](https://npm.io/package/@mce/gif.md) — 2.6K weekly downloads
- [cleanse](https://npm.io/package/cleanse.md) — 173 weekly downloads
- [str](https://npm.io/package/str.md) — 127 weekly downloads
- [naming](https://npm.io/package/naming.md) — 95 weekly downloads
- [tap-telco-api](https://npm.io/package/tap-telco-api.md) — 19 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2021-03-02
- 1.0.0 — 2021-03-02

## README

# sso-longin koa 统一登录中间件
----

## 安装

```bash
npm i -g @tntd/sso-login 
```

## 使用

```bash
const Koa = require('koa');
const ssoLogin = require('@tntd/sso-login ');
const app = new Koa();

app.use(ssoLogin(options)); // options 为 JSON object
```

## options
	
| key	      | 是否必填     | 描述						|
| ---		  | ---       | ---								|
| rsaKey	  | 否		    | E-hr node 解密key				|
| logoutUrl   | 否		    | 退出接口，默认 '/api/logout'			|
| loginUrl	  | 是			| 登录页面地址，参考下面统一登录		|
| ehrapi      | 是         	| 参考 ehrapi 描述					|

### ehrapi 描述
```
ehrapi = { 
	host: "",  
	url: '/ehrapi/empinfo',  // 建议不填，除非接口地址变更了
	token: ""
}
```


> * [E-hr文档](http://wiki.tongdun.me/pages/viewpage.action?pageId=24612750)
> * [统一登录](http://wiki.tongdun.me/pages/viewpage.action?pageId=20160266#id-%E5%86%85%E9%83%A8%E7%BB%9F%E4%B8%80%E7%99%BB%E5%BD%95web%E6%96%B9%E5%BC%8F%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97-4.node%E7%89%88%E6%9C%AC)
> * [host列表文档](http://wiki.tongdun.me/pages/viewpage.action?pageId=31629956)

## 上下文挂载用户信息

```
ctx.session.user = {
    sso: true,
    ehrId: '',
    badge: '',
    nickname: '',
    empStatus: '',
    email: '',
    account: '',
    depId: '',
    leaderId: '',
    roleId: ''
};
```
### 字段描述 具体类型，参考返回值

| 字段	      | 类型     | 是否为空						| 说明 |
| ---		  | ---       | ---						 | --- |
| sso	  | Boolean		    | 否				| 上下文二次挂载用户新标识，默认 true	|
| ehrId	  | int		    | 否				| 员工ID	|
| badge   | String		  | 否			| 员工工号	|
| nickname	  | String	 |否		| 员工姓名	|
| empStatus      | int   | 否					| 在职状态：1-在职；2-离职	|
| email      | String         	| 否					| email	|
| account      | String         	| 否	 | AD账号	|
| depId      | String         	| 否					| 所在部门id	|
| leaderId      | int        	| 否					| 直接汇报对象人员ID	|
| roleId      | int         	| 否				| 在职状态：1-在职；2-离职 |

## 二次上下文挂载当前系统用户信息


### format-session.js 中间件
```
const UserDao = require('daos/user');
const userDao = new UserDao();

module.exports = (options = {}) => {
	return async (ctx, next) => {
		if (ctx.session && ctx.session.user && ctx.session.user.sso) { // sso 标识
			let userOne = await userDao.findOne({
				where: {
					account: ctx.session.user.account
				}
			});// 根据域账号 获取用户信息
			if (!userOne) {// 当前系统没用找到用户，立即新增用户
				userOne = await userDao.create({ ...ctx.session.user });
			}
			ctx.session.user = {
				...userOne.dataValues,
				password: undefined
			};
		}
		await next();
	};
};
```

```
const Koa = require('koa');
const ssoLogin = require('@tntd/sso-login ');
const app = new Koa();

app.use(ssoLogin(options)); // options 为 JSON object
app.use(formatSession());// 上面的中间件 format-session.js
```

## 前端 

### 401 跳转登录
```
const goToLogin = (url, params = {}) => {
    params.callbackUrl = params.callbackUrl || location.href;
    window.location.href = `${url || config.ssoLoginUrl}?tokenEncoding=true&callbackUrl=${params.callbackUrl}`;
};
```

### 退出登录

```
import { stringify } from 'query-string';

const logout = (params = {}) => {
	params.backUrl = params.backUrl || location.href;
	window.location.href = `/api/logout?${stringify(params)}`;
};
```

### 前后端工程分开项目

```
import { searchToObject } from '@/utils'; // URL format对象
import { stringify } from 'query-string';

// 统一登录 token 判断 本地开发使用，线上不会进 if
const { token, ...rest } = searchToObject(location.search);
const backUrl = `${location.protocol}//${location.host}${location.pathname}?${stringify(rest)}`;
if (token) {
    location.href = `/api/login?token=${encodeURIComponent(token)}&backUrl=${encodeURIComponent(backUrl)}`;
}
```

---
_Source: https://npm.io/package/@tntd/sso-login · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
