SSOLogin.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <div v-show="ssoVisible" class="form-cont">
  3. <!-- 应用名 -->
  4. <LoginFormTitle style="width: 100%" />
  5. <el-tabs class="form" style="float: none" value="uname">
  6. <el-tab-pane :label="client.name" name="uname" />
  7. </el-tabs>
  8. <div>
  9. <el-form :model="formData" class="login-form">
  10. <!-- 授权范围的选择 -->
  11. 此第三方应用请求获得以下权限:
  12. <el-form-item prop="scopes">
  13. <el-checkbox-group v-model="formData.scopes">
  14. <el-checkbox
  15. v-for="scope in queryParams.scopes"
  16. :key="scope"
  17. :value="scope"
  18. style="display: block; margin-bottom: -10px"
  19. >
  20. {{ formatScope(scope) }}
  21. </el-checkbox>
  22. </el-checkbox-group>
  23. </el-form-item>
  24. <!-- 下方的登录按钮 -->
  25. <el-form-item class="w-1/1">
  26. <el-button
  27. :loading="formLoading"
  28. class="w-6/10"
  29. type="primary"
  30. @click.prevent="handleAuthorize(true)"
  31. >
  32. <span v-if="!formLoading">同意授权</span>
  33. <span v-else>授 权 中...</span>
  34. </el-button>
  35. <el-button class="w-3/10" @click.prevent="handleAuthorize(false)">拒绝</el-button>
  36. </el-form-item>
  37. </el-form>
  38. </div>
  39. </div>
  40. </template>
  41. <script lang="ts" setup>
  42. import LoginFormTitle from './LoginFormTitle.vue'
  43. import * as OAuth2Api from '@/api/login/oauth2'
  44. import { LoginStateEnum, useLoginState } from './useLogin'
  45. import type { RouteLocationNormalizedLoaded } from 'vue-router'
  46. defineOptions({ name: 'SSOLogin' })
  47. const route = useRoute() // 路由
  48. const { currentRoute } = useRouter() // 路由
  49. const { getLoginState, setLoginState } = useLoginState()
  50. const client = ref({
  51. // 客户端信息
  52. name: '',
  53. logo: ''
  54. })
  55. interface queryType {
  56. responseType: string
  57. clientId: string
  58. redirectUri: string
  59. state: string
  60. scopes: string[]
  61. }
  62. const queryParams = reactive<queryType>({
  63. // URL 上的 client_id、scope 等参数
  64. responseType: '',
  65. clientId: '',
  66. redirectUri: '',
  67. state: '',
  68. scopes: [] // 优先从 query 参数获取;如果未传递,从后端获取
  69. })
  70. const ssoVisible = computed(() => unref(getLoginState) === LoginStateEnum.SSO) // 是否展示 SSO 登录的表单
  71. interface formType {
  72. scopes: string[]
  73. }
  74. const formData = reactive<formType>({
  75. scopes: [] // 已选中的 scope 数组
  76. })
  77. const formLoading = ref(false) // 表单是否提交中
  78. /** 初始化授权信息 */
  79. const init = async () => {
  80. // 防止在没有登录的情况下循环弹窗
  81. if (typeof route.query.client_id === 'undefined') return
  82. // 解析参数
  83. // 例如说【自动授权不通过】:client_id=default&redirect_uri=https%3A%2F%2Fwww.iocoder.cn&response_type=code&scope=user.read%20user.write
  84. // 例如说【自动授权通过】:client_id=default&redirect_uri=https%3A%2F%2Fwww.iocoder.cn&response_type=code&scope=user.read
  85. queryParams.responseType = route.query.response_type as string
  86. queryParams.clientId = route.query.client_id as string
  87. queryParams.redirectUri = route.query.redirect_uri as string
  88. queryParams.state = route.query.state as string
  89. if (route.query.scope) {
  90. queryParams.scopes = (route.query.scope as string).split(' ')
  91. }
  92. // 如果有 scope 参数,先执行一次自动授权,看看是否之前都授权过了。
  93. if (queryParams.scopes.length > 0) {
  94. const data = await doAuthorize(true, queryParams.scopes, [])
  95. if (data) {
  96. location.href = data
  97. return
  98. }
  99. }
  100. // 获取授权页的基本信息
  101. const data = await OAuth2Api.getAuthorize(queryParams.clientId)
  102. client.value = data.client
  103. // 解析 scope
  104. let scopes
  105. // 1.1 如果 params.scope 非空,则过滤下返回的 scopes
  106. if (queryParams.scopes.length > 0) {
  107. scopes = []
  108. for (const scope of data.scopes) {
  109. if (queryParams.scopes.indexOf(scope.key) >= 0) {
  110. scopes.push(scope)
  111. }
  112. }
  113. // 1.2 如果 params.scope 为空,则使用返回的 scopes 设置它
  114. } else {
  115. scopes = data.scopes
  116. for (const scope of scopes) {
  117. queryParams.scopes.push(scope.key)
  118. }
  119. }
  120. // 生成已选中的 checkedScopes
  121. for (const scope of scopes) {
  122. if (scope.value) {
  123. formData.scopes.push(scope.key)
  124. }
  125. }
  126. }
  127. /** 处理授权的提交 */
  128. const handleAuthorize = async (approved) => {
  129. // 计算 checkedScopes + uncheckedScopes
  130. let checkedScopes
  131. let uncheckedScopes
  132. if (approved) {
  133. // 同意授权,按照用户的选择
  134. checkedScopes = formData.scopes
  135. uncheckedScopes = queryParams.scopes.filter((item) => checkedScopes.indexOf(item) === -1)
  136. } else {
  137. // 拒绝,则都是取消
  138. checkedScopes = []
  139. uncheckedScopes = queryParams.scopes
  140. }
  141. // 提交授权的请求
  142. formLoading.value = true
  143. try {
  144. const data = await doAuthorize(false, checkedScopes, uncheckedScopes)
  145. if (!data) {
  146. return
  147. }
  148. location.href = data
  149. } finally {
  150. formLoading.value = false
  151. }
  152. }
  153. /** 调用授权 API 接口 */
  154. const doAuthorize = (autoApprove, checkedScopes, uncheckedScopes) => {
  155. return OAuth2Api.authorize(
  156. queryParams.responseType,
  157. queryParams.clientId,
  158. queryParams.redirectUri,
  159. queryParams.state,
  160. autoApprove,
  161. checkedScopes,
  162. uncheckedScopes
  163. )
  164. }
  165. /** 格式化 scope 文本 */
  166. const formatScope = (scope) => {
  167. // 格式化 scope 授权范围,方便用户理解。
  168. // 这里仅仅是一个 demo,可以考虑录入到字典数据中,例如说字典类型 "system_oauth2_scope",它的每个 scope 都是一条字典数据。
  169. switch (scope) {
  170. case 'user.read':
  171. return '访问你的个人信息'
  172. case 'user.write':
  173. return '修改你的个人信息'
  174. default:
  175. return scope
  176. }
  177. }
  178. /** 监听当前路由为 SSOLogin 时,进行数据的初始化 */
  179. watch(
  180. () => currentRoute.value,
  181. (route: RouteLocationNormalizedLoaded) => {
  182. if (route.name === 'SSOLogin') {
  183. setLoginState(LoginStateEnum.SSO)
  184. init()
  185. }
  186. },
  187. { immediate: true }
  188. )
  189. </script>