@pubinfo/module-auth 2.0.0-beta.14 → 2.0.0-beta.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pubinfo/module-auth",
3
3
  "type": "module",
4
- "version": "2.0.0-beta.14",
4
+ "version": "2.0.0-beta.15",
5
5
  "exports": {
6
6
  ".": {
7
7
  "types": "./dist/index.d.ts",
@@ -20,11 +20,11 @@
20
20
  "src"
21
21
  ],
22
22
  "peerDependencies": {
23
- "pubinfo": "2.0.0-beta.14"
23
+ "pubinfo": "2.0.0-beta.15"
24
24
  },
25
25
  "devDependencies": {
26
26
  "tsup": "^8.4.0",
27
- "pubinfo": "2.0.0-beta.14"
27
+ "pubinfo": "2.0.0-beta.15"
28
28
  },
29
29
  "scripts": {
30
30
  "dev": "tsup --watch src",
package/dist/index.cjs DELETED
@@ -1,221 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- createAuth: () => createAuth2
24
- });
25
- module.exports = __toCommonJS(index_exports);
26
- var import_pubinfo2 = require("pubinfo");
27
-
28
- // src/core.ts
29
- function createAuth(config = {}) {
30
- const {
31
- baseURL = "",
32
- providers = []
33
- } = config;
34
- function getProvider(id) {
35
- const provider = id ? providers.find((provider2) => provider2.id === id) : providers[0];
36
- if (!provider) {
37
- throw new Error(`Provider '${id}' is not found.`);
38
- }
39
- return provider;
40
- }
41
- return {
42
- /**
43
- * 前往登录
44
- * @param id 唯一值
45
- * @param options 登录时需要传递的参数
46
- */
47
- async signIn(id, options) {
48
- const provider = getProvider(id);
49
- const { type, authorization } = provider;
50
- if (typeof authorization === "function") {
51
- return await authorization(options);
52
- }
53
- if (type === "oauth" || type === "custom") {
54
- const url = createUrl(provider);
55
- window.location.replace(url);
56
- }
57
- },
58
- /**
59
- * 生成二维码
60
- * @param id 第三方的唯一值
61
- * @param options 生成二维码需要传递的参数
62
- * @param options.id `getElementById`中指定的`id`
63
- */
64
- renderQRCode(id, options) {
65
- const provider = getProvider(id);
66
- const { type, initQRCode } = provider;
67
- if (type === "oauth" || type === "custom") {
68
- const url = createUrl(provider);
69
- initQRCode?.(url, options);
70
- }
71
- },
72
- /**
73
- * 认证
74
- * @param id 第三方的唯一值
75
- */
76
- async authentication(id) {
77
- const provider = getProvider(id);
78
- const { type, callbackUrl } = provider;
79
- const qs = window.location.href.split("?")?.[1] ?? "";
80
- const params = new URLSearchParams(qs);
81
- if (typeof callbackUrl === "function") {
82
- return await callbackUrl(Object.fromEntries(params.entries()), baseURL);
83
- }
84
- if (type === "oauth" || type === "cas" || type === "custom") {
85
- const response = await fetch(`${baseURL}${callbackUrl}?${params.toString()}`);
86
- return await response.json();
87
- }
88
- },
89
- /**
90
- * 重定向至统一登录页
91
- */
92
- redirect() {
93
- const provider = providers.find((provider2) => provider2.type === "cas");
94
- if (!provider) {
95
- return;
96
- }
97
- const url = createUrl(provider);
98
- window.location.replace(url);
99
- }
100
- };
101
- }
102
- function createUrl(provider) {
103
- const { authorization } = provider;
104
- if (typeof authorization === "function") {
105
- return "";
106
- }
107
- const url = new URL(authorization?.url ?? "");
108
- const params = new URLSearchParams(authorization?.params ?? {});
109
- return `${url.toString()}?${params.toString()}`;
110
- }
111
-
112
- // src/pages/auth.ts
113
- var import_pubinfo = require("pubinfo");
114
- var import_vue = require("vue");
115
- var Auth = (0, import_vue.defineComponent)({
116
- props: {
117
- type: String,
118
- redirectTo: Function,
119
- authentication: Function
120
- },
121
- setup(props) {
122
- const userStore = (0, import_pubinfo.useUserStore)();
123
- const message = (0, import_vue.ref)("\u6388\u6743\u767B\u5F55\u4E2D...");
124
- (0, import_vue.onMounted)(async () => {
125
- const res = await props.authentication?.(props.type);
126
- if (res?.success) {
127
- userStore.setToken(res?.data?.accessToken, res?.data?.refreshToken);
128
- props?.redirectTo?.();
129
- } else {
130
- message.value = res?.msg ?? res?.message;
131
- }
132
- });
133
- return () => {
134
- return (0, import_vue.h)(
135
- "div",
136
- {
137
- style: {
138
- display: "flex",
139
- justifyContent: "center",
140
- alignItems: "center",
141
- width: "100vw",
142
- height: "100vh"
143
- }
144
- },
145
- message.value
146
- );
147
- };
148
- }
149
- });
150
-
151
- // src/index.ts
152
- function createAuth2(options = {}) {
153
- const { redirectTo } = options;
154
- const { signIn, renderQRCode, authentication, redirect } = createAuth(options);
155
- function auth() {
156
- return {
157
- name: "pubinfo:auth",
158
- enforce: "pre",
159
- setup(ctx) {
160
- const { router } = ctx;
161
- const ROUTE_AUTH = {
162
- path: "/auth/:type",
163
- name: "Auth",
164
- component: Auth,
165
- meta: {
166
- whiteList: true,
167
- title: "\u6388\u6743\u767B\u5F55"
168
- },
169
- props: (route) => ({
170
- type: route.params.type,
171
- redirectTo() {
172
- router.push(redirectTo ?? "/");
173
- },
174
- authentication
175
- })
176
- };
177
- router.beforeEach((to) => {
178
- if (!router.hasRoute(ROUTE_AUTH.name)) {
179
- router.addRoute(ROUTE_AUTH);
180
- return to.fullPath;
181
- }
182
- if (to.name === ROUTE_AUTH.name) {
183
- (0, import_pubinfo2.cleanup)();
184
- }
185
- if (to.name === "Login") {
186
- redirect();
187
- }
188
- });
189
- }
190
- };
191
- }
192
- ;
193
- return {
194
- /**
195
- * 前往登录
196
- * @param id 唯一值
197
- * @param options 登录时需要传递的参数
198
- */
199
- signIn,
200
- /**
201
- * 生成二维码
202
- * @param id 第三方的唯一值
203
- * @param options 生成二维码需要传递的参数
204
- * @param options.id `getElementById`中指定的`id`
205
- */
206
- renderQRCode,
207
- /**
208
- * 认证
209
- * @param id 第三方的唯一值
210
- */
211
- authentication,
212
- /**
213
- * 模块配置
214
- */
215
- auth
216
- };
217
- }
218
- // Annotate the CommonJS export names for ESM import in node:
219
- 0 && (module.exports = {
220
- createAuth
221
- });
package/dist/index.d.cts DELETED
@@ -1,33 +0,0 @@
1
- import { A as AuthOptions, T as ThirdParty, R as Recordable } from './interface-C_qnbvmk.cjs';
2
- export { b as AuthConfig, d as Authorization, F as Fn, a as FnApi, c as ProviderConfig, P as ProviderUserConfig } from './interface-C_qnbvmk.cjs';
3
- import { ModuleOptions } from 'pubinfo';
4
- import 'vue-router';
5
-
6
- declare function createAuth(options?: AuthOptions): {
7
- /**
8
- * 前往登录
9
- * @param id 唯一值
10
- * @param options 登录时需要传递的参数
11
- */
12
- signIn: (id: ThirdParty, options?: Recordable) => Promise<any>;
13
- /**
14
- * 生成二维码
15
- * @param id 第三方的唯一值
16
- * @param options 生成二维码需要传递的参数
17
- * @param options.id `getElementById`中指定的`id`
18
- */
19
- renderQRCode: (id: ThirdParty, options: {
20
- id: string;
21
- } & Recordable) => void;
22
- /**
23
- * 认证
24
- * @param id 第三方的唯一值
25
- */
26
- authentication: (id: ThirdParty) => Promise<any>;
27
- /**
28
- * 模块配置
29
- */
30
- auth: () => ModuleOptions;
31
- };
32
-
33
- export { AuthOptions, Recordable, ThirdParty, createAuth };
@@ -1,50 +0,0 @@
1
- import { RouteLocationRaw } from 'vue-router';
2
-
3
- type Recordable = Record<string, any>;
4
- type Fn<T = any, K = any> = (params?: Recordable & T) => Promise<K> | K;
5
- type FnApi<T = any> = (params: Recordable & T, baseURL: string) => Promise<any>;
6
- interface AuthOptions extends AuthConfig {
7
- /** 登录后重定向至 */
8
- redirectTo?: RouteLocationRaw;
9
- }
10
- interface AuthConfig {
11
- /** 接口的baseURL */
12
- baseURL?: string;
13
- providers?: ProviderConfig[];
14
- }
15
- /** 第三方类型 */
16
- type ThirdParty = string;
17
- type ProviderUserConfig = Partial<ProviderConfig>;
18
- interface ProviderConfig {
19
- /** 唯一值 */
20
- id: string;
21
- /** 名称 */
22
- name: string;
23
- /**
24
- * 协议类型
25
- * - `oauth` 第三方授权登录
26
- * - `cas` 统一账号登录
27
- * - `custom` 自定义
28
- */
29
- type: 'oauth' | 'cas' | 'custom';
30
- /** 授权登录地址 */
31
- authorization?: Authorization | Fn;
32
- /** `client_id` */
33
- clientId?: string;
34
- /** `redirect_uri` */
35
- redirectUri?: string;
36
- /** 授权登录接口 */
37
- callbackUrl?: string | FnApi;
38
- /** 初始化二维码 */
39
- initQRCode?: (url: string, params: {
40
- id: string;
41
- } & Recordable) => Promise<void> | void;
42
- }
43
- interface Authorization {
44
- /** 授权登录地址 */
45
- url: string;
46
- /** 授权登录参数 */
47
- params?: Recordable;
48
- }
49
-
50
- export type { AuthOptions as A, Fn as F, ProviderUserConfig as P, Recordable as R, ThirdParty as T, FnApi as a, AuthConfig as b, ProviderConfig as c, Authorization as d };
@@ -1,43 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/providers/4A.ts
21
- var A_exports = {};
22
- __export(A_exports, {
23
- default: () => FourA
24
- });
25
- module.exports = __toCommonJS(A_exports);
26
- function FourA(options) {
27
- const { clientId, redirectUri } = options;
28
- return {
29
- id: "4A",
30
- name: "4A\u767B\u5F55",
31
- type: "oauth",
32
- authorization: {
33
- url: "http://134.108.76.137:7001/index",
34
- params: {
35
- response_type: "code",
36
- client_id: clientId,
37
- redirect_uri: redirectUri
38
- }
39
- },
40
- callbackUrl: "/bs/loginBy4a",
41
- ...options
42
- };
43
- }
@@ -1,9 +0,0 @@
1
- import { P as ProviderUserConfig, c as ProviderConfig } from '../interface-C_qnbvmk.cjs';
2
- import 'vue-router';
3
-
4
- /**
5
- * 4A登录
6
- */
7
- declare function FourA(options: ProviderUserConfig): ProviderConfig;
8
-
9
- export { FourA as default };
@@ -1,34 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/providers/credentials.ts
21
- var credentials_exports = {};
22
- __export(credentials_exports, {
23
- default: () => Credentials
24
- });
25
- module.exports = __toCommonJS(credentials_exports);
26
- function Credentials(options) {
27
- const { authorize } = options;
28
- return {
29
- id: "credentials",
30
- name: "\u51ED\u8BC1\u767B\u5F55",
31
- type: "custom",
32
- authorization: authorize
33
- };
34
- }
@@ -1,12 +0,0 @@
1
- import { F as Fn, c as ProviderConfig } from '../interface-C_qnbvmk.cjs';
2
- import 'vue-router';
3
-
4
- interface CredentialsConfig {
5
- authorize: Fn<any>;
6
- }
7
- /**
8
- * 凭证登录
9
- */
10
- declare function Credentials(options: CredentialsConfig): ProviderConfig;
11
-
12
- export { Credentials as default };
@@ -1,80 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/providers/ding-zj.ts
21
- var ding_zj_exports = {};
22
- __export(ding_zj_exports, {
23
- default: () => DingZJ
24
- });
25
- module.exports = __toCommonJS(ding_zj_exports);
26
- function DingZJ(options) {
27
- const { clientId, redirectUri } = options;
28
- const authorization = {
29
- /** 跳转外部页面 */
30
- redirect: {
31
- url: "https://openplatform-pro.ding.zj.gov.cn/oauth2/auth.htm",
32
- params: {
33
- response_type: "code",
34
- client_id: clientId,
35
- redirect_uri: redirectUri,
36
- scope: "get_user_info",
37
- authType: "QRCODE"
38
- }
39
- },
40
- /** 内嵌二维码 */
41
- embed: {
42
- url: "https://login-pro.ding.zj.gov.cn/oauth2/auth.htm",
43
- params: {
44
- response_type: "code",
45
- client_id: clientId,
46
- redirect_uri: redirectUri,
47
- scope: "get_user_info",
48
- authType: "QRCODE",
49
- embedMode: "true"
50
- }
51
- }
52
- };
53
- const initQRCode = (url, options2) => {
54
- const target = document.getElementById(options2.id);
55
- if (!target) {
56
- throw new Error(`\u672A\u627E\u5230id\u4E3A${options2.id}\u7684DOM\u5143\u7D20`);
57
- }
58
- const iframe = document.createElement("iframe");
59
- iframe.src = url;
60
- iframe.width = "200";
61
- iframe.height = "200";
62
- document.appendChild(iframe);
63
- window.addEventListener("message", (e) => {
64
- if (options2.redirectUri) {
65
- const url2 = new URL(options2.redirectUri);
66
- url2.search = new URLSearchParams(e.data).toString();
67
- window.location.href = url2.toString();
68
- }
69
- });
70
- };
71
- return {
72
- id: "ding-zj",
73
- name: "\u6D59\u653F\u9489\u767B\u5F55",
74
- type: "oauth",
75
- authorization: authorization[options.mode],
76
- callbackUrl: "/bs/loginByDingZJ",
77
- initQRCode: options.mode === "embed" ? initQRCode : void 0,
78
- ...options
79
- };
80
- }
@@ -1,17 +0,0 @@
1
- import { P as ProviderUserConfig, c as ProviderConfig } from '../interface-C_qnbvmk.cjs';
2
- import 'vue-router';
3
-
4
- interface DingZJConfig extends ProviderUserConfig {
5
- /**
6
- * 登录方式
7
- * - `redirect` 跳转浙政钉的扫码页面
8
- * - `embed` 内嵌浙政钉的登录二维码
9
- */
10
- mode: 'redirect' | 'embed';
11
- }
12
- /**
13
- * 浙政钉扫码登录
14
- */
15
- declare function DingZJ(options: DingZJConfig): ProviderConfig;
16
-
17
- export { DingZJ as default };