@pubinfo-nightly/module-auth 2025.11.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/LICENSE +21 -0
- package/dist/components/LoginWithFourA/index.d.ts +15 -0
- package/dist/components/RedirectLogin/index.d.ts +11 -0
- package/dist/context.d.ts +2 -0
- package/dist/core.d.ts +25 -0
- package/dist/helper.d.ts +22 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +186 -0
- package/dist/interface.d.ts +51 -0
- package/dist/pages/auth.d.ts +11 -0
- package/dist/providers/4A.d.ts +15 -0
- package/dist/providers/4A.js +21 -0
- package/dist/providers/credentials.d.ts +7 -0
- package/dist/providers/credentials.js +11 -0
- package/dist/providers/ding-zj.d.ts +17 -0
- package/dist/providers/ding-zj.js +49 -0
- package/dist/providers/sso.d.ts +19 -0
- package/dist/providers/sso.js +23 -0
- package/package.json +36 -0
- package/src/assets/images/4A.png +0 -0
- package/src/components/LoginWithFourA/index.ts +16 -0
- package/src/components/RedirectLogin/index.ts +43 -0
- package/src/context.ts +4 -0
- package/src/core.ts +75 -0
- package/src/helper.ts +77 -0
- package/src/index.ts +78 -0
- package/src/interface.ts +65 -0
- package/src/pages/auth.ts +53 -0
- package/src/providers/4A.ts +36 -0
- package/src/providers/credentials.ts +15 -0
- package/src/providers/ding-zj.ts +82 -0
- package/src/providers/sso.ts +44 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { useUserStore } from 'pubinfo-nightly';
|
|
2
|
+
import { defineComponent, h, onMounted, ref } from 'vue';
|
|
3
|
+
|
|
4
|
+
export const PageAuth = defineComponent({
|
|
5
|
+
props: {
|
|
6
|
+
type: String,
|
|
7
|
+
redirectTo: Function,
|
|
8
|
+
authenticate: Function,
|
|
9
|
+
},
|
|
10
|
+
setup(props) {
|
|
11
|
+
const userStore = useUserStore();
|
|
12
|
+
const route = useRoute();
|
|
13
|
+
const router = useRouter();
|
|
14
|
+
|
|
15
|
+
const message = ref('授权登录中...');
|
|
16
|
+
|
|
17
|
+
onMounted(async () => {
|
|
18
|
+
const res = await props.authenticate?.(props.type);
|
|
19
|
+
if (res?.success) {
|
|
20
|
+
userStore.setToken(res?.data?.accessToken, res?.data?.refreshToken);
|
|
21
|
+
|
|
22
|
+
// 读取路由上的 redirect 参数
|
|
23
|
+
const redirect = route.query.redirect?.toString();
|
|
24
|
+
if (redirect) {
|
|
25
|
+
router.push(redirect);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// 默认的重定向地址
|
|
30
|
+
props?.redirectTo?.();
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
message.value = res?.msg ?? res?.message;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return () => {
|
|
38
|
+
return h(
|
|
39
|
+
'div',
|
|
40
|
+
{
|
|
41
|
+
style: {
|
|
42
|
+
display: 'flex',
|
|
43
|
+
justifyContent: 'center',
|
|
44
|
+
alignItems: 'center',
|
|
45
|
+
width: '100vw',
|
|
46
|
+
height: '100vh',
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
message.value,
|
|
50
|
+
);
|
|
51
|
+
};
|
|
52
|
+
},
|
|
53
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ProviderOptions, ProviderUserOptions } from '../interface';
|
|
2
|
+
|
|
3
|
+
export interface FourAOptions extends ProviderUserOptions {
|
|
4
|
+
/** `client_id` */
|
|
5
|
+
clientId?: string
|
|
6
|
+
|
|
7
|
+
/** `redirect_uri` */
|
|
8
|
+
redirectUri?: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* `4A 统一认证`
|
|
13
|
+
*
|
|
14
|
+
* - 携带 `client_id` 和 `redirect_uri`,前往 [4A认证中心](http://134.108.76.137:7001/index) 进行登录;
|
|
15
|
+
* - 成功登录后,4A认证中心会回调 `redirect_uri` 并携带参数;
|
|
16
|
+
* - 当前 `provider` 将获取参数值并调用 `/bs/loginBy4a` 完成登录认证。
|
|
17
|
+
*/
|
|
18
|
+
export default function FourA(options: FourAOptions): ProviderOptions {
|
|
19
|
+
const { clientId, redirectUri } = options;
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
id: '4A',
|
|
23
|
+
name: '4A登录',
|
|
24
|
+
type: 'oauth',
|
|
25
|
+
signIn: {
|
|
26
|
+
url: 'http://134.108.76.137:7001/index',
|
|
27
|
+
params: {
|
|
28
|
+
response_type: 'code',
|
|
29
|
+
client_id: clientId,
|
|
30
|
+
redirect_uri: redirectUri,
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
authenticate: '/bs/loginBy4a',
|
|
34
|
+
...options,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ProviderOptions, ProviderUserOptions } from '../interface';
|
|
2
|
+
|
|
3
|
+
export interface CredentialsOptions extends ProviderUserOptions {}
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 凭证登录
|
|
7
|
+
*/
|
|
8
|
+
export default function Credentials(options?: CredentialsOptions): ProviderOptions {
|
|
9
|
+
return {
|
|
10
|
+
id: 'credentials',
|
|
11
|
+
name: '凭证登录',
|
|
12
|
+
type: 'custom',
|
|
13
|
+
...options,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { Authorization, ProviderOptions, ProviderUserOptions } from '../interface';
|
|
2
|
+
|
|
3
|
+
export interface DingZJOptions extends ProviderUserOptions {
|
|
4
|
+
/**
|
|
5
|
+
* 登录方式
|
|
6
|
+
* - `redirect` 跳转浙政钉的扫码页面
|
|
7
|
+
* - `embed` 内嵌浙政钉的登录二维码
|
|
8
|
+
*/
|
|
9
|
+
mode: 'redirect' | 'embed'
|
|
10
|
+
|
|
11
|
+
/** `client_id` */
|
|
12
|
+
clientId?: string
|
|
13
|
+
|
|
14
|
+
/** `redirect_uri` */
|
|
15
|
+
redirectUri?: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 浙政钉扫码登录
|
|
20
|
+
*/
|
|
21
|
+
export default function DingZJ(options: DingZJOptions): ProviderOptions {
|
|
22
|
+
const { clientId, redirectUri } = options;
|
|
23
|
+
|
|
24
|
+
const signIn: Record<string, Authorization> = {
|
|
25
|
+
/** 跳转外部页面 */
|
|
26
|
+
redirect: {
|
|
27
|
+
url: 'https://openplatform-pro.ding.zj.gov.cn/oauth2/auth.htm',
|
|
28
|
+
params: {
|
|
29
|
+
response_type: 'code',
|
|
30
|
+
client_id: clientId,
|
|
31
|
+
redirect_uri: redirectUri,
|
|
32
|
+
scope: 'get_user_info',
|
|
33
|
+
authType: 'QRCODE',
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
/** 内嵌二维码 */
|
|
38
|
+
embed: {
|
|
39
|
+
url: 'https://login-pro.ding.zj.gov.cn/oauth2/auth.htm',
|
|
40
|
+
params: {
|
|
41
|
+
response_type: 'code',
|
|
42
|
+
client_id: clientId,
|
|
43
|
+
redirect_uri: redirectUri,
|
|
44
|
+
scope: 'get_user_info',
|
|
45
|
+
authType: 'QRCODE',
|
|
46
|
+
embedMode: 'true',
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const initQRCode: ProviderOptions['initQRCode'] = (url, options) => {
|
|
52
|
+
const target = document.getElementById(options.id);
|
|
53
|
+
|
|
54
|
+
if (!target) {
|
|
55
|
+
throw new Error(`未找到id为${options.id}的DOM元素`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const iframe = document.createElement('iframe');
|
|
59
|
+
iframe.src = url;
|
|
60
|
+
iframe.width = '200';
|
|
61
|
+
iframe.height = '200';
|
|
62
|
+
document.appendChild(iframe);
|
|
63
|
+
|
|
64
|
+
window.addEventListener('message', (e) => {
|
|
65
|
+
if (options.redirectUri) {
|
|
66
|
+
const url = new URL(options.redirectUri);
|
|
67
|
+
url.search = new URLSearchParams(e.data).toString();
|
|
68
|
+
window.location.href = url.toString();
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
id: 'ding-zj',
|
|
75
|
+
name: '浙政钉登录',
|
|
76
|
+
type: 'oauth',
|
|
77
|
+
signIn: signIn[options.mode],
|
|
78
|
+
authenticate: '/bs/loginByDingZJ',
|
|
79
|
+
initQRCode: options.mode === 'embed' ? initQRCode : undefined,
|
|
80
|
+
...options,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { ProviderOptions, ProviderUserOptions } from '@/interface';
|
|
2
|
+
|
|
3
|
+
export interface SsoOptions extends ProviderUserOptions {
|
|
4
|
+
/**
|
|
5
|
+
* 登录类型,根据调用 `/app/oauth/generateCode` 接口时传递的 `grantType` 参数值判断:
|
|
6
|
+
*
|
|
7
|
+
* - `OAUTH2` -> `defaultOauth2`
|
|
8
|
+
* - `SIMPLE_ENCRYPT` -> `encrypt`
|
|
9
|
+
*
|
|
10
|
+
* @default `defaultOauth2`
|
|
11
|
+
*/
|
|
12
|
+
loginType?: 'defaultOauth2' | 'encrypt'
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* `SSO 统一账号登录`
|
|
17
|
+
*
|
|
18
|
+
* - 主系统调用 `/app/oauth/generateCode?appKey=xxx` 获取 `code`,并作为参数跳转至子系统;
|
|
19
|
+
* - 当前 `provider` 将获取 `code` 并调用 `/auth/ssoLogin` 完成登录认证。
|
|
20
|
+
*/
|
|
21
|
+
export default function Sso(options: SsoOptions = {}): ProviderOptions {
|
|
22
|
+
const { loginType = 'defaultOauth2' } = options;
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
id: 'sso',
|
|
26
|
+
name: 'SSO统一账号登录',
|
|
27
|
+
type: 'custom',
|
|
28
|
+
authenticate: async (params, baseURL) => {
|
|
29
|
+
const res = await fetch(
|
|
30
|
+
`${baseURL}/auth/ssoLogin`,
|
|
31
|
+
{
|
|
32
|
+
headers: { 'Content-Type': 'application/json' },
|
|
33
|
+
method: 'POST',
|
|
34
|
+
body: JSON.stringify({
|
|
35
|
+
...params,
|
|
36
|
+
loginType,
|
|
37
|
+
}),
|
|
38
|
+
},
|
|
39
|
+
);
|
|
40
|
+
return await res.json();
|
|
41
|
+
},
|
|
42
|
+
...options,
|
|
43
|
+
};
|
|
44
|
+
}
|