Skip to content

Commit 33b6edc

Browse files
feat(X-Pack): Add OAuth2 authentication mechanism
1 parent 6b5bdc1 commit 33b6edc

File tree

9 files changed

+286
-75
lines changed

9 files changed

+286
-75
lines changed

frontend/auto-imports.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
export {}
88
declare global {
99
const ElButton: typeof import('element-plus-secondary/es')['ElButton']
10+
const ElCheckbox: typeof import('element-plus-secondary/es')['ElCheckbox']
11+
const ElDatePicker: typeof import('element-plus-secondary/es')['ElDatePicker']
12+
const ElInput: typeof import('element-plus-secondary/es')['ElInput']
1013
const ElMessage: typeof import('element-plus-secondary/es')['ElMessage']
1114
const ElMessageBox: typeof import('element-plus-secondary/es')['ElMessageBox']
15+
const ElRadio: typeof import('element-plus-secondary/es')['ElRadio']
16+
const ElRadioGroup: typeof import('element-plus-secondary/es')['ElRadioGroup']
17+
const ElSelect: typeof import('element-plus-secondary/es')['ElSelect']
1218
const LicenseGenerator: any
1319
}

frontend/src/i18n/en.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,17 @@
704704
"field_mapping": "User Attribute Mapping",
705705
"field_mapping_placeholder": "Example: {'{'}\"account\": \"saml2Account\", \"name\": \"saml2Name\", \"email\": \"email\"{'}'}",
706706
"incorrect_please_re_enter": "Incorrect format, please re-enter",
707-
"in_json_format": "Please enter JSON format"
707+
"in_json_format": "Please enter JSON format",
708+
"authorize_url": "Authorization URL",
709+
"client_id": "Client ID",
710+
"client_secret": "Client Secret",
711+
"redirect_url": "Redirect URL",
712+
"oauth2_settings": "OAuth2 Settings",
713+
"scope": "Scope",
714+
"userinfo_url": "User Info URL",
715+
"token_url": "Token URL",
716+
"revoke_url": "Revocation URL",
717+
"oauth2_field_mapping_placeholder": "Example: {'{'}\"account\": \"OAuth2Account\", \"name\": \"OAuth2Name\", \"email\": \"email\"{'}'}"
708718
},
709719
"login": {
710720
"default_login": "Default",

frontend/src/i18n/ko-KR.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,17 @@
704704
"field_mapping": "사용자 속성 매핑",
705705
"field_mapping_placeholder": "예: {'{'}\"account\": \"saml2Account\", \"name\": \"saml2Name\", \"email\": \"email\"{'}'}",
706706
"incorrect_please_re_enter": "형식이 잘못되었습니다. 다시 입력해 주세요",
707-
"in_json_format": "JSON 형식으로 입력해 주세요"
707+
"in_json_format": "JSON 형식으로 입력해 주세요",
708+
"authorize_url": "인증 URL",
709+
"client_id": "클라이언트 ID",
710+
"client_secret": "클라이언트 시크릿",
711+
"redirect_url": "리디렉션 URL",
712+
"oauth2_settings": "OAuth2 설정",
713+
"scope": "권한 범위",
714+
"userinfo_url": "사용자 정보 URL",
715+
"token_url": "토큰 URL",
716+
"revoke_url": "취소 URL",
717+
"oauth2_field_mapping_placeholder": "예: {'{'}\"account\": \"OAuth2Account\", \"name\": \"OAuth2Name\", \"email\": \"email\"{'}'}"
708718
},
709719
"login": {
710720
"default_login": "기본값",

frontend/src/i18n/zh-CN.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,17 @@
704704
"field_mapping": "用户属性映射",
705705
"field_mapping_placeholder": "例如:{'{'}\"account\": \"saml2Account\", \"name\": \"saml2Name\", \"email\": \"email\"{'}'}",
706706
"incorrect_please_re_enter": "格式错误,请重新填写",
707-
"in_json_format": "请输入json格式"
707+
"in_json_format": "请输入json格式",
708+
"authorize_url": "授权地址",
709+
"client_id": "客户端 ID",
710+
"client_secret": "客户端密钥",
711+
"redirect_url": "回调地址",
712+
"oauth2_settings": "OAuth2 设置",
713+
"scope": "授权范围",
714+
"userinfo_url": "用户信息地址",
715+
"token_url": "令牌地址",
716+
"revoke_url": "撤销地址",
717+
"oauth2_field_mapping_placeholder": "例如:{'{'}\"account\": \"oauth2Account\", \"name\": \"oauth2Name\", \"email\": \"email\"{'}'}"
708718
},
709719
"login": {
710720
"default_login": "默认",

frontend/src/utils/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ export const getQueryString = (name: string) => {
213213
return null
214214
}
215215

216+
export const getUrlParams = () => {
217+
const urlParams = new URLSearchParams(window.location.search) as any
218+
return Object.fromEntries(urlParams)
219+
}
220+
216221
export const isLarkPlatform = () => {
217222
return !!getQueryString('state') && !!getQueryString('code')
218223
}

frontend/src/views/login/xpack/Handler.vue

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
:ldap="loginCategory.ldap"
2121
@status-change="qrStatusChange"
2222
/>
23-
<Oidc v-if="loginCategory.oidc" @switch-category="switcherCategory" />
24-
<Oauth2 v-if="loginCategory.oauth2" ref="oauth2Handler" @switch-category="switcherCategory" /> -->
23+
<Oidc v-if="loginCategory.oidc" @switch-category="switcherCategory" /> -->
24+
<Oauth2 v-if="loginCategory.oauth2" ref="oauth2Handler" @switch-category="switcherCategory" />
2525
<Cas v-if="loginCategory.cas" @switch-category="switcherCategory" />
2626
<!-- <Saml2 v-if="loginCategory.saml2" ref="saml2Handler" @switch-category="switcherCategory" /> -->
2727
</div>
@@ -38,13 +38,14 @@ import Oidc from './Oidc.vue'
3838
import Oauth2 from './Oauth2.vue'
3939
import Saml2 from './Saml2.vue' */
4040
import Cas from './Cas.vue'
41+
import Oauth2 from './Oauth2.vue'
4142
// import QrTab from './QrTab.vue'
4243
import { request } from '@/utils/request'
4344
import { useCache } from '@/utils/useCache'
4445
4546
import router from '@/router'
4647
import { useUserStore } from '@/stores/user.ts'
47-
import { getQueryString, isPlatformClient } from '@/utils/utils'
48+
import { getQueryString, getUrlParams, isPlatformClient } from '@/utils/utils'
4849
import { loadClient, type LoginCategory } from './PlatformClient'
4950
// import MfaStep from './MfaStep.vue'
5051
// import { logoutHandler } from '@/utils/logout'
@@ -150,7 +151,10 @@ const switcherCategory = (param: Categoryparam) => {
150151
}
151152
const nextPage = curOrigin + pathname + proxy + curLocation
152153
if (category === 'oauth2') {
153-
oauth2Handler?.value?.toLoginPage()
154+
request.get('/system/authentication/login/4').then((res: any) => {
155+
window.location.href = res
156+
window.open(res, '_self')
157+
})
154158
return
155159
}
156160
if (category === 'saml2') {
@@ -176,9 +180,12 @@ const getCurLocation = () => {
176180
}
177181
178182
const casLogin = () => {
183+
const urlParams = getUrlParams()
179184
const ticket = getQueryString('ticket')
185+
/* request
186+
.get('/system/authentication/sso/cas?ticket=' + ticket) */
180187
request
181-
.get('/system/authentication/sso/cas?ticket=' + ticket)
188+
.post('/system/authentication/sso/1', urlParams)
182189
.then((res: any) => {
183190
const token = res.access_token
184191
if (token && isPlatformClient()) {
@@ -207,6 +214,39 @@ const casLogin = () => {
207214
}, 1500)
208215
})
209216
}
217+
const oauth2Login = () => {
218+
const urlParams = getUrlParams()
219+
request
220+
.post('/system/authentication/sso/4', urlParams)
221+
.then((res: any) => {
222+
const token = res.access_token
223+
const id_token = res.id_token
224+
if (token && isPlatformClient()) {
225+
wsCache.set('de-platform-client', true)
226+
}
227+
userStore.setToken(token)
228+
userStore.setExp(res.exp)
229+
userStore.setTime(Date.now())
230+
userStore.setPlatformInfo({
231+
flag: 'oauth2',
232+
data: id_token,
233+
origin: 4,
234+
})
235+
const queryRedirectPath = getCurLocation()
236+
router.push({ path: queryRedirectPath })
237+
})
238+
.catch((e: any) => {
239+
userStore.setToken('')
240+
setTimeout(() => {
241+
// logoutHandler(true, true)
242+
platformLoginMsg.value = e?.message || e
243+
setTimeout(() => {
244+
window.location.href =
245+
window.location.origin + window.location.pathname + window.location.hash
246+
}, 2000)
247+
}, 1500)
248+
})
249+
}
210250
/* const platformLogin = (origin: number) => {
211251
const url = '/system/authentication/sso/cas'
212252
request
@@ -384,6 +424,8 @@ onMounted(() => {
384424
if (state?.includes('cas') && getQueryString('ticket')) {
385425
// platformLogin(1)
386426
casLogin()
427+
} else if (state?.includes('oauth2')) {
428+
oauth2Login()
387429
} else {
388430
updateLoading(false)
389431
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<template>
2+
<div class="item CAS" @click="execute">
3+
<el-icon>
4+
<Icon name="logo_oauth"><logo_oauth class="svg-icon" /></Icon>
5+
</el-icon>
6+
<span class="name"> OAuth2 </span>
7+
</div>
8+
</template>
9+
10+
<script lang="ts" setup>
11+
import logo_oauth from '@/assets/svg/logo_oauth.svg'
12+
import { Icon } from '@/components/icon-custom'
13+
const emits = defineEmits(['switch-category'])
14+
const execute = () => {
15+
emits('switch-category', { category: 'oauth2', proxy: '/#' })
16+
}
17+
</script>
18+
<style lang="less" scoped>
19+
.item {
20+
width: 32px;
21+
cursor: pointer;
22+
23+
&.qrcode,
24+
&.account {
25+
.ed-icon {
26+
padding: 5px;
27+
}
28+
}
29+
30+
.ed-icon {
31+
font-size: 32px;
32+
border: 1px solid #dee0e3;
33+
border-radius: 50%;
34+
}
35+
display: flex;
36+
align-items: center;
37+
flex-direction: column;
38+
justify-content: space-between;
39+
40+
.name {
41+
margin-top: 8px;
42+
color: #000;
43+
text-align: center;
44+
font-family: var(--de-custom_font, 'PingFang');
45+
font-size: 12px;
46+
font-style: normal;
47+
font-weight: 400;
48+
line-height: 20px; /* 166.667% */
49+
display: none;
50+
}
51+
}
52+
</style>

0 commit comments

Comments
 (0)