@pubinfo/module-auth 2.0.0-beta.14

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/dist/index.cjs ADDED
@@ -0,0 +1,221 @@
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
+ });
@@ -0,0 +1,33 @@
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 };
@@ -0,0 +1,33 @@
1
+ import { A as AuthOptions, T as ThirdParty, R as Recordable } from './interface-C_qnbvmk.js';
2
+ export { b as AuthConfig, d as Authorization, F as Fn, a as FnApi, c as ProviderConfig, P as ProviderUserConfig } from './interface-C_qnbvmk.js';
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 };
package/dist/index.js ADDED
@@ -0,0 +1,196 @@
1
+ // src/index.ts
2
+ import { cleanup } from "pubinfo";
3
+
4
+ // src/core.ts
5
+ function createAuth(config = {}) {
6
+ const {
7
+ baseURL = "",
8
+ providers = []
9
+ } = config;
10
+ function getProvider(id) {
11
+ const provider = id ? providers.find((provider2) => provider2.id === id) : providers[0];
12
+ if (!provider) {
13
+ throw new Error(`Provider '${id}' is not found.`);
14
+ }
15
+ return provider;
16
+ }
17
+ return {
18
+ /**
19
+ * 前往登录
20
+ * @param id 唯一值
21
+ * @param options 登录时需要传递的参数
22
+ */
23
+ async signIn(id, options) {
24
+ const provider = getProvider(id);
25
+ const { type, authorization } = provider;
26
+ if (typeof authorization === "function") {
27
+ return await authorization(options);
28
+ }
29
+ if (type === "oauth" || type === "custom") {
30
+ const url = createUrl(provider);
31
+ window.location.replace(url);
32
+ }
33
+ },
34
+ /**
35
+ * 生成二维码
36
+ * @param id 第三方的唯一值
37
+ * @param options 生成二维码需要传递的参数
38
+ * @param options.id `getElementById`中指定的`id`
39
+ */
40
+ renderQRCode(id, options) {
41
+ const provider = getProvider(id);
42
+ const { type, initQRCode } = provider;
43
+ if (type === "oauth" || type === "custom") {
44
+ const url = createUrl(provider);
45
+ initQRCode?.(url, options);
46
+ }
47
+ },
48
+ /**
49
+ * 认证
50
+ * @param id 第三方的唯一值
51
+ */
52
+ async authentication(id) {
53
+ const provider = getProvider(id);
54
+ const { type, callbackUrl } = provider;
55
+ const qs = window.location.href.split("?")?.[1] ?? "";
56
+ const params = new URLSearchParams(qs);
57
+ if (typeof callbackUrl === "function") {
58
+ return await callbackUrl(Object.fromEntries(params.entries()), baseURL);
59
+ }
60
+ if (type === "oauth" || type === "cas" || type === "custom") {
61
+ const response = await fetch(`${baseURL}${callbackUrl}?${params.toString()}`);
62
+ return await response.json();
63
+ }
64
+ },
65
+ /**
66
+ * 重定向至统一登录页
67
+ */
68
+ redirect() {
69
+ const provider = providers.find((provider2) => provider2.type === "cas");
70
+ if (!provider) {
71
+ return;
72
+ }
73
+ const url = createUrl(provider);
74
+ window.location.replace(url);
75
+ }
76
+ };
77
+ }
78
+ function createUrl(provider) {
79
+ const { authorization } = provider;
80
+ if (typeof authorization === "function") {
81
+ return "";
82
+ }
83
+ const url = new URL(authorization?.url ?? "");
84
+ const params = new URLSearchParams(authorization?.params ?? {});
85
+ return `${url.toString()}?${params.toString()}`;
86
+ }
87
+
88
+ // src/pages/auth.ts
89
+ import { useUserStore } from "pubinfo";
90
+ import { defineComponent, h, onMounted, ref } from "vue";
91
+ var Auth = defineComponent({
92
+ props: {
93
+ type: String,
94
+ redirectTo: Function,
95
+ authentication: Function
96
+ },
97
+ setup(props) {
98
+ const userStore = useUserStore();
99
+ const message = ref("\u6388\u6743\u767B\u5F55\u4E2D...");
100
+ onMounted(async () => {
101
+ const res = await props.authentication?.(props.type);
102
+ if (res?.success) {
103
+ userStore.setToken(res?.data?.accessToken, res?.data?.refreshToken);
104
+ props?.redirectTo?.();
105
+ } else {
106
+ message.value = res?.msg ?? res?.message;
107
+ }
108
+ });
109
+ return () => {
110
+ return h(
111
+ "div",
112
+ {
113
+ style: {
114
+ display: "flex",
115
+ justifyContent: "center",
116
+ alignItems: "center",
117
+ width: "100vw",
118
+ height: "100vh"
119
+ }
120
+ },
121
+ message.value
122
+ );
123
+ };
124
+ }
125
+ });
126
+
127
+ // src/index.ts
128
+ function createAuth2(options = {}) {
129
+ const { redirectTo } = options;
130
+ const { signIn, renderQRCode, authentication, redirect } = createAuth(options);
131
+ function auth() {
132
+ return {
133
+ name: "pubinfo:auth",
134
+ enforce: "pre",
135
+ setup(ctx) {
136
+ const { router } = ctx;
137
+ const ROUTE_AUTH = {
138
+ path: "/auth/:type",
139
+ name: "Auth",
140
+ component: Auth,
141
+ meta: {
142
+ whiteList: true,
143
+ title: "\u6388\u6743\u767B\u5F55"
144
+ },
145
+ props: (route) => ({
146
+ type: route.params.type,
147
+ redirectTo() {
148
+ router.push(redirectTo ?? "/");
149
+ },
150
+ authentication
151
+ })
152
+ };
153
+ router.beforeEach((to) => {
154
+ if (!router.hasRoute(ROUTE_AUTH.name)) {
155
+ router.addRoute(ROUTE_AUTH);
156
+ return to.fullPath;
157
+ }
158
+ if (to.name === ROUTE_AUTH.name) {
159
+ cleanup();
160
+ }
161
+ if (to.name === "Login") {
162
+ redirect();
163
+ }
164
+ });
165
+ }
166
+ };
167
+ }
168
+ ;
169
+ return {
170
+ /**
171
+ * 前往登录
172
+ * @param id 唯一值
173
+ * @param options 登录时需要传递的参数
174
+ */
175
+ signIn,
176
+ /**
177
+ * 生成二维码
178
+ * @param id 第三方的唯一值
179
+ * @param options 生成二维码需要传递的参数
180
+ * @param options.id `getElementById`中指定的`id`
181
+ */
182
+ renderQRCode,
183
+ /**
184
+ * 认证
185
+ * @param id 第三方的唯一值
186
+ */
187
+ authentication,
188
+ /**
189
+ * 模块配置
190
+ */
191
+ auth
192
+ };
193
+ }
194
+ export {
195
+ createAuth2 as createAuth
196
+ };
@@ -0,0 +1,50 @@
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 };
@@ -0,0 +1,50 @@
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 };
@@ -0,0 +1,43 @@
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
+ }
@@ -0,0 +1,9 @@
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 };
@@ -0,0 +1,9 @@
1
+ import { P as ProviderUserConfig, c as ProviderConfig } from '../interface-C_qnbvmk.js';
2
+ import 'vue-router';
3
+
4
+ /**
5
+ * 4A登录
6
+ */
7
+ declare function FourA(options: ProviderUserConfig): ProviderConfig;
8
+
9
+ export { FourA as default };
@@ -0,0 +1,22 @@
1
+ // src/providers/4A.ts
2
+ function FourA(options) {
3
+ const { clientId, redirectUri } = options;
4
+ return {
5
+ id: "4A",
6
+ name: "4A\u767B\u5F55",
7
+ type: "oauth",
8
+ authorization: {
9
+ url: "http://134.108.76.137:7001/index",
10
+ params: {
11
+ response_type: "code",
12
+ client_id: clientId,
13
+ redirect_uri: redirectUri
14
+ }
15
+ },
16
+ callbackUrl: "/bs/loginBy4a",
17
+ ...options
18
+ };
19
+ }
20
+ export {
21
+ FourA as default
22
+ };