Browse Source

fix: respect disabled uni login types

Pchen. 2 weeks ago
parent
commit
9fe9d5deb6
3 changed files with 79 additions and 8 deletions
  1. 6 5
      apis/User/uniLogin/GetLoginUrl.js
  2. 45 0
      apis/User/uniLogin/Options.js
  3. 28 3
      lib/UniLoginClient.js

+ 6 - 5
apis/User/uniLogin/GetLoginUrl.js

@@ -2,8 +2,7 @@ const API = require("../../../lib/API")
 const axios = require('axios')
 const axios = require('axios')
 const { BaseStdResponse } = require("../../../BaseStdResponse");
 const { BaseStdResponse } = require("../../../BaseStdResponse");
 const https = require("https")
 const https = require("https")
-const { normalizeSocialType } = require('../../../lib/UniLoginClient')
-const { getRuntimeConfig } = require('../../../lib/RuntimeConfig')
+const { getUniLoginRuntimeConfig, normalizeSocialType } = require('../../../lib/UniLoginClient')
 
 
 class GetLoginUrl extends API {
 class GetLoginUrl extends API {
     constructor() {
     constructor() {
@@ -23,11 +22,13 @@ class GetLoginUrl extends API {
                 msg: '不支持的第三方登录类型'
                 msg: '不支持的第三方登录类型'
             })
             })
 
 
-        const uniConfig = await getRuntimeConfig('unilogin', { required: false, defaultValue: null })
-        if (!uniConfig || !uniConfig.url || !uniConfig.appid || !uniConfig.appkey) {
+        let uniConfig
+        try {
+            uniConfig = await getUniLoginRuntimeConfig({ requireEnabledType: type })
+        } catch (error) {
             return res.json({
             return res.json({
                 ...BaseStdResponse.ERR,
                 ...BaseStdResponse.ERR,
-                msg: '聚合登录暂未配置,请使用账号密码登录'
+                msg: error.message || '聚合登录暂未配置,请使用账号密码登录'
             })
             })
         }
         }
 
 

+ 45 - 0
apis/User/uniLogin/Options.js

@@ -0,0 +1,45 @@
+const API = require("../../../lib/API")
+const { BaseStdResponse } = require("../../../BaseStdResponse")
+const { getEnabledUniLoginTypes, getUniLoginRuntimeConfig, VALID_SOCIAL_TYPES } = require('../../../lib/UniLoginClient')
+
+class Options extends API {
+    constructor() {
+        super()
+
+        this.setPath('/UniLogin/Options')
+        this.setMethod('GET')
+    }
+
+    async onRequest(req, res) {
+        try {
+            const uniConfig = await getUniLoginRuntimeConfig()
+            const enabledTypes = getEnabledUniLoginTypes(uniConfig)
+
+            return res.json({
+                ...BaseStdResponse.OK,
+                data: {
+                    configured: true,
+                    enabledTypes,
+                    methods: VALID_SOCIAL_TYPES.map(type => ({
+                        type,
+                        enabled: enabledTypes.includes(type)
+                    }))
+                }
+            })
+        } catch {
+            return res.json({
+                ...BaseStdResponse.OK,
+                data: {
+                    configured: false,
+                    enabledTypes: [],
+                    methods: VALID_SOCIAL_TYPES.map(type => ({
+                        type,
+                        enabled: false
+                    }))
+                }
+            })
+        }
+    }
+}
+
+module.exports.Options = Options

+ 28 - 3
lib/UniLoginClient.js

@@ -9,15 +9,37 @@ function normalizeSocialType(type) {
     return VALID_SOCIAL_TYPES.includes(socialType) ? socialType : null
     return VALID_SOCIAL_TYPES.includes(socialType) ? socialType : null
 }
 }
 
 
-async function fetchUniLoginProfile(type, code) {
+function getEnabledUniLoginTypes(uniConfig = {}) {
+    if (!Array.isArray(uniConfig.enabledTypes)) return [...VALID_SOCIAL_TYPES]
+    return uniConfig.enabledTypes
+        .map(type => normalizeSocialType(type))
+        .filter(Boolean)
+        .filter((type, index, list) => list.indexOf(type) === index)
+}
+
+function isUniLoginTypeEnabled(uniConfig, type) {
     const socialType = normalizeSocialType(type)
     const socialType = normalizeSocialType(type)
-    if (!socialType)
-        throw new Error('不支持的第三方登录类型')
+    if (!socialType) return false
+    return getEnabledUniLoginTypes(uniConfig).includes(socialType)
+}
 
 
+async function getUniLoginRuntimeConfig({ requireEnabledType } = {}) {
     const uniConfig = await getRuntimeConfig('unilogin', { required: false, defaultValue: null })
     const uniConfig = await getRuntimeConfig('unilogin', { required: false, defaultValue: null })
     if (!uniConfig || !uniConfig.url || !uniConfig.appid || !uniConfig.appkey) {
     if (!uniConfig || !uniConfig.url || !uniConfig.appid || !uniConfig.appkey) {
         throw new Error('聚合登录暂未配置')
         throw new Error('聚合登录暂未配置')
     }
     }
+    if (requireEnabledType && !isUniLoginTypeEnabled(uniConfig, requireEnabledType)) {
+        throw new Error('该登录方式已关闭')
+    }
+    return uniConfig
+}
+
+async function fetchUniLoginProfile(type, code) {
+    const socialType = normalizeSocialType(type)
+    if (!socialType)
+        throw new Error('不支持的第三方登录类型')
+
+    const uniConfig = await getUniLoginRuntimeConfig({ requireEnabledType: socialType })
     const url = `${uniConfig.url}/connect.php?act=callback&appid=${uniConfig.appid}&appkey=${uniConfig.appkey}&type=${socialType}&code=${code}`
     const url = `${uniConfig.url}/connect.php?act=callback&appid=${uniConfig.appid}&appkey=${uniConfig.appkey}&type=${socialType}&code=${code}`
 
 
     const r = await axios.get(url, {
     const r = await axios.get(url, {
@@ -38,6 +60,9 @@ async function fetchUniLoginProfile(type, code) {
 
 
 module.exports = {
 module.exports = {
     VALID_SOCIAL_TYPES,
     VALID_SOCIAL_TYPES,
+    getEnabledUniLoginTypes,
+    getUniLoginRuntimeConfig,
+    isUniLoginTypeEnabled,
     normalizeSocialType,
     normalizeSocialType,
     fetchUniLoginProfile
     fetchUniLoginProfile
 }
 }