Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
陈曦
sub2api
Commits
6b97a8be
Commit
6b97a8be
authored
Jan 09, 2026
by
Edric Li
Browse files
Merge branch 'main' into feat/api-key-ip-restriction
parents
90798f14
62dc0b95
Changes
70
Hide whitespace changes
Inline
Side-by-side
Linux DO Connect.md
0 → 100644
View file @
6b97a8be
# Linux DO Connect
OAuth(Open Authorization)是一个开放的网络授权标准,目前最新版本为 OAuth 2.0。我们日常使用的第三方登录(如 Google 账号登录)就采用了该标准。OAuth 允许用户授权第三方应用访问存储在其他服务提供商(如 Google)上的信息,无需在不同平台上重复填写注册信息。用户授权后,平台可以直接访问用户的账户信息进行身份验证,而用户无需向第三方应用提供密码。
目前系统已实现完整的 OAuth2 授权码(code)方式鉴权,但界面等配套功能还在持续完善中。让我们一起打造一个更完善的共享方案。
## 基本介绍
这是一套标准的 OAuth2 鉴权系统,可以让开发者共享论坛的用户基本信息。
-
可获取字段:
| 参数 | 说明 |
| ----------------- | ------------------------------- |
|
`id`
| 用户唯一标识(不可变) |
|
`username`
| 论坛用户名 |
|
`name`
| 论坛用户昵称(可变) |
|
`avatar_template`
| 用户头像模板URL(支持多种尺寸) |
|
`active`
| 账号活跃状态 |
|
`trust_level`
| 信任等级(0-4) |
|
`silenced`
| 禁言状态 |
|
`external_ids`
| 外部ID关联信息 |
|
`api_key`
| API访问密钥 |
通过这些信息,公益网站/接口可以实现:
1.
基于
`id`
的服务频率限制
2.
基于
`trust_level`
的服务额度分配
3.
基于用户信息的滥用举报机制
## 相关端点
-
Authorize 端点:
`https://connect.linux.do/oauth2/authorize`
-
Token 端点:
`https://connect.linux.do/oauth2/token`
-
用户信息 端点:
`https://connect.linux.do/api/user`
## 申请使用
-
访问
[
Connect.Linux.Do
](
https://connect.linux.do/
)
申请接入你的应用。

-
点击
**`我的应用接入`**
-
**`申请新接入`**
,填写相关信息。其中
**`回调地址`**
是你的应用接收用户信息的地址。

-
申请成功后,你将获得
**`Client Id`**
和
**`Client Secret`**
,这是你应用的唯一身份凭证。

## 接入 Linux Do
JavaScript
```
JavaScript
// 安装第三方请求库(或使用原生的 Fetch API),本例中使用 axios
// npm install axios
// 通过 OAuth2 获取 Linux Do 用户信息的参考流程
const axios = require('axios');
const readline = require('readline');
// 配置信息(建议通过环境变量配置,避免使用硬编码)
const CLIENT_ID = '你的 Client ID';
const CLIENT_SECRET = '你的 Client Secret';
const REDIRECT_URI = '你的回调地址';
const AUTH_URL = 'https://connect.linux.do/oauth2/authorize';
const TOKEN_URL = 'https://connect.linux.do/oauth2/token';
const USER_INFO_URL = 'https://connect.linux.do/api/user';
// 第一步:生成授权 URL
function getAuthUrl() {
const params = new URLSearchParams({
client_id: CLIENT_ID,
redirect_uri: REDIRECT_URI,
response_type: 'code',
scope: 'user'
});
return `${AUTH_URL}?${params.toString()}`;
}
// 第二步:获取 code 参数
function getCode() {
return new Promise((resolve) => {
// 本例中使用终端输入来模拟流程,仅供本地测试
// 请在实际应用中替换为真实的处理逻辑
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
rl.question('从回调 URL 中提取出 code,粘贴到此处并按回车:', (answer) => {
rl.close();
resolve(answer.trim());
});
});
}
// 第三步:使用 code 参数获取访问令牌
async function getAccessToken(code) {
try {
const form = new URLSearchParams({
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
code: code,
redirect_uri: REDIRECT_URI,
grant_type: 'authorization_code'
}).toString();
const response = await axios.post(TOKEN_URL, form, {
// 提醒:需正确配置请求头,否则无法正常获取访问令牌
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
});
return response.data;
} catch (error) {
console.error(`获取访问令牌失败:${error.response ? JSON.stringify(error.response.data) : error.message}`);
throw error;
}
}
// 第四步:使用访问令牌获取用户信息
async function getUserInfo(accessToken) {
try {
const response = await axios.get(USER_INFO_URL, {
headers: {
Authorization: `Bearer ${accessToken}`
}
});
return response.data;
} catch (error) {
console.error(`获取用户信息失败:${error.response ? JSON.stringify(error.response.data) : error.message}`);
throw error;
}
}
// 主流程
async function main() {
// 1. 生成授权 URL,前端引导用户访问授权页
const authUrl = getAuthUrl();
console.log(`请访问此 URL 授权:${authUrl}
`);
// 2. 用户授权后,从回调 URL 获取 code 参数
const code = await getCode();
try {
// 3. 使用 code 参数获取访问令牌
const tokenData = await getAccessToken(code);
const accessToken = tokenData.access_token;
// 4. 使用访问令牌获取用户信息
if (accessToken) {
const userInfo = await getUserInfo(accessToken);
console.log(`
获取用户信息成功:${JSON.stringify(userInfo, null, 2)}`);
} else {
console.log(`
获取访问令牌失败:${JSON.stringify(tokenData)}`);
}
} catch (error) {
console.error('发生错误:', error);
}
}
```
Python
```
python
# 安装第三方请求库,本例中使用 requests
# pip install requests
# 通过 OAuth2 获取 Linux Do 用户信息的参考流程
import
requests
import
json
# 配置信息(建议通过环境变量配置,避免使用硬编码)
CLIENT_ID
=
'你的 Client ID'
CLIENT_SECRET
=
'你的 Client Secret'
REDIRECT_URI
=
'你的回调地址'
AUTH_URL
=
'https://connect.linux.do/oauth2/authorize'
TOKEN_URL
=
'https://connect.linux.do/oauth2/token'
USER_INFO_URL
=
'https://connect.linux.do/api/user'
# 第一步:生成授权 URL
def
get_auth_url
():
params
=
{
'client_id'
:
CLIENT_ID
,
'redirect_uri'
:
REDIRECT_URI
,
'response_type'
:
'code'
,
'scope'
:
'user'
}
auth_url
=
f
"
{
AUTH_URL
}
?
{
'&'
.
join
(
f
'
{
k
}
=
{
v
}
' for k, v in params.items())
}
"
return
auth_url
# 第二步:获取 code 参数
def
get_code
():
# 本例中使用终端输入来模拟流程,仅供本地测试
# 请在实际应用中替换为真实的处理逻辑
return
input
(
'从回调 URL 中提取出 code,粘贴到此处并按回车:'
).
strip
()
# 第三步:使用 code 参数获取访问令牌
def
get_access_token
(
code
):
try
:
data
=
{
'client_id'
:
CLIENT_ID
,
'client_secret'
:
CLIENT_SECRET
,
'code'
:
code
,
'redirect_uri'
:
REDIRECT_URI
,
'grant_type'
:
'authorization_code'
}
# 提醒:需正确配置请求头,否则无法正常获取访问令牌
headers
=
{
'Content-Type'
:
'application/x-www-form-urlencoded'
,
'Accept'
:
'application/json'
}
response
=
requests
.
post
(
TOKEN_URL
,
data
=
data
,
headers
=
headers
)
response
.
raise_for_status
()
return
response
.
json
()
except
requests
.
exceptions
.
RequestException
as
e
:
print
(
f
"获取访问令牌失败:
{
e
}
"
)
return
None
# 第四步:使用访问令牌获取用户信息
def
get_user_info
(
access_token
):
try
:
headers
=
{
'Authorization'
:
f
'Bearer
{
access_token
}
'
}
response
=
requests
.
get
(
USER_INFO_URL
,
headers
=
headers
)
response
.
raise_for_status
()
return
response
.
json
()
except
requests
.
exceptions
.
RequestException
as
e
:
print
(
f
"获取用户信息失败:
{
e
}
"
)
return
None
# 主流程
if
__name__
==
'__main__'
:
# 1. 生成授权 URL,前端引导用户访问授权页
auth_url
=
get_auth_url
()
print
(
f
'请访问此 URL 授权:
{
auth_url
}
'
)
# 2. 用户授权后,从回调 URL 获取 code 参数
code
=
get_code
()
# 3. 使用 code 参数获取访问令牌
token_data
=
get_access_token
(
code
)
if
token_data
:
access_token
=
token_data
.
get
(
'access_token'
)
# 4. 使用访问令牌获取用户信息
if
access_token
:
user_info
=
get_user_info
(
access_token
)
if
user_info
:
print
(
f
"
获取用户信息成功:
{
json
.
dumps
(
user_info
,
indent
=
2
)
}
"
)
else
:
print
(
"
获取用户信息失败"
)
else
:
print
(
f
"
获取访问令牌失败:
{
json
.
dumps
(
token_data
,
indent
=
2
)
}
"
)
else
:
print
(
"
获取访问令牌失败"
)
```
PHP
```
php
// 通过 OAuth2 获取 Linux Do 用户信息的参考流程
// 配置信息
$CLIENT_ID
=
'你的 Client ID'
;
$CLIENT_SECRET
=
'你的 Client Secret'
;
$REDIRECT_URI
=
'你的回调地址'
;
$AUTH_URL
=
'https://connect.linux.do/oauth2/authorize'
;
$TOKEN_URL
=
'https://connect.linux.do/oauth2/token'
;
$USER_INFO_URL
=
'https://connect.linux.do/api/user'
;
// 生成授权 URL
function
getAuthUrl
(
$clientId
,
$redirectUri
)
{
global
$AUTH_URL
;
return
$AUTH_URL
.
'?'
.
http_build_query
([
'client_id'
=>
$clientId
,
'redirect_uri'
=>
$redirectUri
,
'response_type'
=>
'code'
,
'scope'
=>
'user'
]);
}
// 使用 code 参数获取用户信息(合并获取令牌和获取用户信息的步骤)
function
getUserInfoWithCode
(
$code
,
$clientId
,
$clientSecret
,
$redirectUri
)
{
global
$TOKEN_URL
,
$USER_INFO_URL
;
// 1. 获取访问令牌
$ch
=
curl_init
(
$TOKEN_URL
);
curl_setopt
(
$ch
,
CURLOPT_RETURNTRANSFER
,
true
);
curl_setopt
(
$ch
,
CURLOPT_POST
,
true
);
curl_setopt
(
$ch
,
CURLOPT_POSTFIELDS
,
http_build_query
([
'client_id'
=>
$clientId
,
'client_secret'
=>
$clientSecret
,
'code'
=>
$code
,
'redirect_uri'
=>
$redirectUri
,
'grant_type'
=>
'authorization_code'
]));
curl_setopt
(
$ch
,
CURLOPT_HTTPHEADER
,
[
'Content-Type: application/x-www-form-urlencoded'
,
'Accept: application/json'
]);
$tokenResponse
=
curl_exec
(
$ch
);
curl_close
(
$ch
);
$tokenData
=
json_decode
(
$tokenResponse
,
true
);
if
(
!
isset
(
$tokenData
[
'access_token'
]))
{
return
[
'error'
=>
'获取访问令牌失败'
,
'details'
=>
$tokenData
];
}
// 2. 获取用户信息
$ch
=
curl_init
(
$USER_INFO_URL
);
curl_setopt
(
$ch
,
CURLOPT_RETURNTRANSFER
,
true
);
curl_setopt
(
$ch
,
CURLOPT_HTTPHEADER
,
[
'Authorization: Bearer '
.
$tokenData
[
'access_token'
]
]);
$userResponse
=
curl_exec
(
$ch
);
curl_close
(
$ch
);
return
json_decode
(
$userResponse
,
true
);
}
// 主流程
// 1. 生成授权 URL
$authUrl
=
getAuthUrl
(
$CLIENT_ID
,
$REDIRECT_URI
);
echo
"<a href='
$authUrl
'>使用 Linux Do 登录</a>"
;
// 2. 处理回调并获取用户信息
if
(
isset
(
$_GET
[
'code'
]))
{
$userInfo
=
getUserInfoWithCode
(
$_GET
[
'code'
],
$CLIENT_ID
,
$CLIENT_SECRET
,
$REDIRECT_URI
);
if
(
isset
(
$userInfo
[
'error'
]))
{
echo
'错误: '
.
$userInfo
[
'error'
];
}
else
{
echo
'欢迎, '
.
$userInfo
[
'name'
]
.
'!'
;
// 处理用户登录逻辑...
}
}
```
## 使用说明
### 授权流程
1.
用户点击应用中的’使用 Linux Do 登录’按钮
2.
系统将用户重定向至 Linux Do 的授权页面
3.
用户完成授权后,系统自动重定向回应用并携带授权码
4.
应用使用授权码获取访问令牌
5.
使用访问令牌获取用户信息
### 安全建议
-
切勿在前端代码中暴露 Client Secret
-
对所有用户输入数据进行严格验证
-
确保使用 HTTPS 协议传输数据
-
定期更新并妥善保管 Client Secret
\ No newline at end of file
backend/cmd/server/VERSION
View file @
6b97a8be
0.1.
1
0.1.
46
backend/cmd/server/wire_gen.go
View file @
6b97a8be
...
@@ -53,7 +53,7 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
...
@@ -53,7 +53,7 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
emailQueueService
:=
service
.
ProvideEmailQueueService
(
emailService
)
emailQueueService
:=
service
.
ProvideEmailQueueService
(
emailService
)
authService
:=
service
.
NewAuthService
(
userRepository
,
configConfig
,
settingService
,
emailService
,
turnstileService
,
emailQueueService
)
authService
:=
service
.
NewAuthService
(
userRepository
,
configConfig
,
settingService
,
emailService
,
turnstileService
,
emailQueueService
)
userService
:=
service
.
NewUserService
(
userRepository
)
userService
:=
service
.
NewUserService
(
userRepository
)
authHandler
:=
handler
.
NewAuthHandler
(
configConfig
,
authService
,
userService
)
authHandler
:=
handler
.
NewAuthHandler
(
configConfig
,
authService
,
userService
,
settingService
)
userHandler
:=
handler
.
NewUserHandler
(
userService
)
userHandler
:=
handler
.
NewUserHandler
(
userService
)
apiKeyRepository
:=
repository
.
NewAPIKeyRepository
(
client
)
apiKeyRepository
:=
repository
.
NewAPIKeyRepository
(
client
)
groupRepository
:=
repository
.
NewGroupRepository
(
client
,
db
)
groupRepository
:=
repository
.
NewGroupRepository
(
client
,
db
)
...
...
backend/internal/config/config.go
View file @
6b97a8be
...
@@ -6,6 +6,7 @@ import (
...
@@ -6,6 +6,7 @@ import (
"encoding/hex"
"encoding/hex"
"fmt"
"fmt"
"log"
"log"
"net/url"
"os"
"os"
"strings"
"strings"
"time"
"time"
...
@@ -35,24 +36,25 @@ const (
...
@@ -35,24 +36,25 @@ const (
)
)
type
Config
struct
{
type
Config
struct
{
Server
ServerConfig
`mapstructure:"server"`
Server
ServerConfig
`mapstructure:"server"`
CORS
CORSConfig
`mapstructure:"cors"`
CORS
CORSConfig
`mapstructure:"cors"`
Security
SecurityConfig
`mapstructure:"security"`
Security
SecurityConfig
`mapstructure:"security"`
Billing
BillingConfig
`mapstructure:"billing"`
Billing
BillingConfig
`mapstructure:"billing"`
Turnstile
TurnstileConfig
`mapstructure:"turnstile"`
Turnstile
TurnstileConfig
`mapstructure:"turnstile"`
Database
DatabaseConfig
`mapstructure:"database"`
Database
DatabaseConfig
`mapstructure:"database"`
Redis
RedisConfig
`mapstructure:"redis"`
Redis
RedisConfig
`mapstructure:"redis"`
JWT
JWTConfig
`mapstructure:"jwt"`
JWT
JWTConfig
`mapstructure:"jwt"`
Default
DefaultConfig
`mapstructure:"default"`
LinuxDo
LinuxDoConnectConfig
`mapstructure:"linuxdo_connect"`
RateLimit
RateLimitConfig
`mapstructure:"rate_limit"`
Default
DefaultConfig
`mapstructure:"default"`
Pricing
PricingConfig
`mapstructure:"pricing"`
RateLimit
RateLimitConfig
`mapstructure:"rate_limit"`
Gateway
GatewayConfig
`mapstructure:"gateway"`
Pricing
PricingConfig
`mapstructure:"pricing"`
Concurrency
ConcurrencyConfig
`mapstructure:"concurrency"`
Gateway
GatewayConfig
`mapstructure:"gateway"`
TokenRefresh
TokenRefreshConfig
`mapstructure:"token_refresh"`
Concurrency
ConcurrencyConfig
`mapstructure:"concurrency"`
RunMode
string
`mapstructure:"run_mode" yaml:"run_mode"`
TokenRefresh
TokenRefreshConfig
`mapstructure:"token_refresh"`
Timezone
string
`mapstructure:"timezone"`
// e.g. "Asia/Shanghai", "UTC"
RunMode
string
`mapstructure:"run_mode" yaml:"run_mode"`
Gemini
GeminiConfig
`mapstructure:"gemini"`
Timezone
string
`mapstructure:"timezone"`
// e.g. "Asia/Shanghai", "UTC"
Update
UpdateConfig
`mapstructure:"update"`
Gemini
GeminiConfig
`mapstructure:"gemini"`
Update
UpdateConfig
`mapstructure:"update"`
}
}
// UpdateConfig 在线更新相关配置
// UpdateConfig 在线更新相关配置
...
@@ -322,6 +324,30 @@ type TurnstileConfig struct {
...
@@ -322,6 +324,30 @@ type TurnstileConfig struct {
Required
bool
`mapstructure:"required"`
Required
bool
`mapstructure:"required"`
}
}
// LinuxDoConnectConfig 用于 LinuxDo Connect OAuth 登录(终端用户 SSO)。
//
// 注意:这与上游账号的 OAuth(例如 OpenAI/Gemini 账号接入)不是一回事。
// 这里是用于登录 Sub2API 本身的用户体系。
type
LinuxDoConnectConfig
struct
{
Enabled
bool
`mapstructure:"enabled"`
ClientID
string
`mapstructure:"client_id"`
ClientSecret
string
`mapstructure:"client_secret"`
AuthorizeURL
string
`mapstructure:"authorize_url"`
TokenURL
string
`mapstructure:"token_url"`
UserInfoURL
string
`mapstructure:"userinfo_url"`
Scopes
string
`mapstructure:"scopes"`
RedirectURL
string
`mapstructure:"redirect_url"`
// 后端回调地址(需在提供方后台登记)
FrontendRedirectURL
string
`mapstructure:"frontend_redirect_url"`
// 前端接收 token 的路由(默认:/auth/linuxdo/callback)
TokenAuthMethod
string
`mapstructure:"token_auth_method"`
// client_secret_post / client_secret_basic / none
UsePKCE
bool
`mapstructure:"use_pkce"`
// 可选:用于从 userinfo JSON 中提取字段的 gjson 路径。
// 为空时,服务端会尝试一组常见字段名。
UserInfoEmailPath
string
`mapstructure:"userinfo_email_path"`
UserInfoIDPath
string
`mapstructure:"userinfo_id_path"`
UserInfoUsernamePath
string
`mapstructure:"userinfo_username_path"`
}
type
DefaultConfig
struct
{
type
DefaultConfig
struct
{
AdminEmail
string
`mapstructure:"admin_email"`
AdminEmail
string
`mapstructure:"admin_email"`
AdminPassword
string
`mapstructure:"admin_password"`
AdminPassword
string
`mapstructure:"admin_password"`
...
@@ -388,6 +414,18 @@ func Load() (*Config, error) {
...
@@ -388,6 +414,18 @@ func Load() (*Config, error) {
cfg
.
Server
.
Mode
=
"debug"
cfg
.
Server
.
Mode
=
"debug"
}
}
cfg
.
JWT
.
Secret
=
strings
.
TrimSpace
(
cfg
.
JWT
.
Secret
)
cfg
.
JWT
.
Secret
=
strings
.
TrimSpace
(
cfg
.
JWT
.
Secret
)
cfg
.
LinuxDo
.
ClientID
=
strings
.
TrimSpace
(
cfg
.
LinuxDo
.
ClientID
)
cfg
.
LinuxDo
.
ClientSecret
=
strings
.
TrimSpace
(
cfg
.
LinuxDo
.
ClientSecret
)
cfg
.
LinuxDo
.
AuthorizeURL
=
strings
.
TrimSpace
(
cfg
.
LinuxDo
.
AuthorizeURL
)
cfg
.
LinuxDo
.
TokenURL
=
strings
.
TrimSpace
(
cfg
.
LinuxDo
.
TokenURL
)
cfg
.
LinuxDo
.
UserInfoURL
=
strings
.
TrimSpace
(
cfg
.
LinuxDo
.
UserInfoURL
)
cfg
.
LinuxDo
.
Scopes
=
strings
.
TrimSpace
(
cfg
.
LinuxDo
.
Scopes
)
cfg
.
LinuxDo
.
RedirectURL
=
strings
.
TrimSpace
(
cfg
.
LinuxDo
.
RedirectURL
)
cfg
.
LinuxDo
.
FrontendRedirectURL
=
strings
.
TrimSpace
(
cfg
.
LinuxDo
.
FrontendRedirectURL
)
cfg
.
LinuxDo
.
TokenAuthMethod
=
strings
.
ToLower
(
strings
.
TrimSpace
(
cfg
.
LinuxDo
.
TokenAuthMethod
))
cfg
.
LinuxDo
.
UserInfoEmailPath
=
strings
.
TrimSpace
(
cfg
.
LinuxDo
.
UserInfoEmailPath
)
cfg
.
LinuxDo
.
UserInfoIDPath
=
strings
.
TrimSpace
(
cfg
.
LinuxDo
.
UserInfoIDPath
)
cfg
.
LinuxDo
.
UserInfoUsernamePath
=
strings
.
TrimSpace
(
cfg
.
LinuxDo
.
UserInfoUsernamePath
)
cfg
.
CORS
.
AllowedOrigins
=
normalizeStringSlice
(
cfg
.
CORS
.
AllowedOrigins
)
cfg
.
CORS
.
AllowedOrigins
=
normalizeStringSlice
(
cfg
.
CORS
.
AllowedOrigins
)
cfg
.
Security
.
ResponseHeaders
.
AdditionalAllowed
=
normalizeStringSlice
(
cfg
.
Security
.
ResponseHeaders
.
AdditionalAllowed
)
cfg
.
Security
.
ResponseHeaders
.
AdditionalAllowed
=
normalizeStringSlice
(
cfg
.
Security
.
ResponseHeaders
.
AdditionalAllowed
)
cfg
.
Security
.
ResponseHeaders
.
ForceRemove
=
normalizeStringSlice
(
cfg
.
Security
.
ResponseHeaders
.
ForceRemove
)
cfg
.
Security
.
ResponseHeaders
.
ForceRemove
=
normalizeStringSlice
(
cfg
.
Security
.
ResponseHeaders
.
ForceRemove
)
...
@@ -426,6 +464,81 @@ func Load() (*Config, error) {
...
@@ -426,6 +464,81 @@ func Load() (*Config, error) {
return
&
cfg
,
nil
return
&
cfg
,
nil
}
}
// ValidateAbsoluteHTTPURL 校验一个绝对 http(s) URL(禁止 fragment)。
func
ValidateAbsoluteHTTPURL
(
raw
string
)
error
{
raw
=
strings
.
TrimSpace
(
raw
)
if
raw
==
""
{
return
fmt
.
Errorf
(
"empty url"
)
}
u
,
err
:=
url
.
Parse
(
raw
)
if
err
!=
nil
{
return
err
}
if
!
u
.
IsAbs
()
{
return
fmt
.
Errorf
(
"must be absolute"
)
}
if
!
isHTTPScheme
(
u
.
Scheme
)
{
return
fmt
.
Errorf
(
"unsupported scheme: %s"
,
u
.
Scheme
)
}
if
strings
.
TrimSpace
(
u
.
Host
)
==
""
{
return
fmt
.
Errorf
(
"missing host"
)
}
if
u
.
Fragment
!=
""
{
return
fmt
.
Errorf
(
"must not include fragment"
)
}
return
nil
}
// ValidateFrontendRedirectURL 校验前端回调地址:
// - 允许同源相对路径(以 / 开头)
// - 或绝对 http(s) URL(禁止 fragment)
func
ValidateFrontendRedirectURL
(
raw
string
)
error
{
raw
=
strings
.
TrimSpace
(
raw
)
if
raw
==
""
{
return
fmt
.
Errorf
(
"empty url"
)
}
if
strings
.
ContainsAny
(
raw
,
"
\r\n
"
)
{
return
fmt
.
Errorf
(
"contains invalid characters"
)
}
if
strings
.
HasPrefix
(
raw
,
"/"
)
{
if
strings
.
HasPrefix
(
raw
,
"//"
)
{
return
fmt
.
Errorf
(
"must not start with //"
)
}
return
nil
}
u
,
err
:=
url
.
Parse
(
raw
)
if
err
!=
nil
{
return
err
}
if
!
u
.
IsAbs
()
{
return
fmt
.
Errorf
(
"must be absolute http(s) url or relative path"
)
}
if
!
isHTTPScheme
(
u
.
Scheme
)
{
return
fmt
.
Errorf
(
"unsupported scheme: %s"
,
u
.
Scheme
)
}
if
strings
.
TrimSpace
(
u
.
Host
)
==
""
{
return
fmt
.
Errorf
(
"missing host"
)
}
if
u
.
Fragment
!=
""
{
return
fmt
.
Errorf
(
"must not include fragment"
)
}
return
nil
}
func
isHTTPScheme
(
scheme
string
)
bool
{
return
strings
.
EqualFold
(
scheme
,
"http"
)
||
strings
.
EqualFold
(
scheme
,
"https"
)
}
func
warnIfInsecureURL
(
field
,
raw
string
)
{
u
,
err
:=
url
.
Parse
(
strings
.
TrimSpace
(
raw
))
if
err
!=
nil
{
return
}
if
strings
.
EqualFold
(
u
.
Scheme
,
"http"
)
{
log
.
Printf
(
"Warning: %s uses http scheme; use https in production to avoid token leakage."
,
field
)
}
}
func
setDefaults
()
{
func
setDefaults
()
{
viper
.
SetDefault
(
"run_mode"
,
RunModeStandard
)
viper
.
SetDefault
(
"run_mode"
,
RunModeStandard
)
...
@@ -475,6 +588,22 @@ func setDefaults() {
...
@@ -475,6 +588,22 @@ func setDefaults() {
// Turnstile
// Turnstile
viper
.
SetDefault
(
"turnstile.required"
,
false
)
viper
.
SetDefault
(
"turnstile.required"
,
false
)
// LinuxDo Connect OAuth 登录(终端用户 SSO)
viper
.
SetDefault
(
"linuxdo_connect.enabled"
,
false
)
viper
.
SetDefault
(
"linuxdo_connect.client_id"
,
""
)
viper
.
SetDefault
(
"linuxdo_connect.client_secret"
,
""
)
viper
.
SetDefault
(
"linuxdo_connect.authorize_url"
,
"https://connect.linux.do/oauth2/authorize"
)
viper
.
SetDefault
(
"linuxdo_connect.token_url"
,
"https://connect.linux.do/oauth2/token"
)
viper
.
SetDefault
(
"linuxdo_connect.userinfo_url"
,
"https://connect.linux.do/api/user"
)
viper
.
SetDefault
(
"linuxdo_connect.scopes"
,
"user"
)
viper
.
SetDefault
(
"linuxdo_connect.redirect_url"
,
""
)
viper
.
SetDefault
(
"linuxdo_connect.frontend_redirect_url"
,
"/auth/linuxdo/callback"
)
viper
.
SetDefault
(
"linuxdo_connect.token_auth_method"
,
"client_secret_post"
)
viper
.
SetDefault
(
"linuxdo_connect.use_pkce"
,
false
)
viper
.
SetDefault
(
"linuxdo_connect.userinfo_email_path"
,
""
)
viper
.
SetDefault
(
"linuxdo_connect.userinfo_id_path"
,
""
)
viper
.
SetDefault
(
"linuxdo_connect.userinfo_username_path"
,
""
)
// Database
// Database
viper
.
SetDefault
(
"database.host"
,
"localhost"
)
viper
.
SetDefault
(
"database.host"
,
"localhost"
)
viper
.
SetDefault
(
"database.port"
,
5432
)
viper
.
SetDefault
(
"database.port"
,
5432
)
...
@@ -586,6 +715,60 @@ func (c *Config) Validate() error {
...
@@ -586,6 +715,60 @@ func (c *Config) Validate() error {
if
c
.
Security
.
CSP
.
Enabled
&&
strings
.
TrimSpace
(
c
.
Security
.
CSP
.
Policy
)
==
""
{
if
c
.
Security
.
CSP
.
Enabled
&&
strings
.
TrimSpace
(
c
.
Security
.
CSP
.
Policy
)
==
""
{
return
fmt
.
Errorf
(
"security.csp.policy is required when CSP is enabled"
)
return
fmt
.
Errorf
(
"security.csp.policy is required when CSP is enabled"
)
}
}
if
c
.
LinuxDo
.
Enabled
{
if
strings
.
TrimSpace
(
c
.
LinuxDo
.
ClientID
)
==
""
{
return
fmt
.
Errorf
(
"linuxdo_connect.client_id is required when linuxdo_connect.enabled=true"
)
}
if
strings
.
TrimSpace
(
c
.
LinuxDo
.
AuthorizeURL
)
==
""
{
return
fmt
.
Errorf
(
"linuxdo_connect.authorize_url is required when linuxdo_connect.enabled=true"
)
}
if
strings
.
TrimSpace
(
c
.
LinuxDo
.
TokenURL
)
==
""
{
return
fmt
.
Errorf
(
"linuxdo_connect.token_url is required when linuxdo_connect.enabled=true"
)
}
if
strings
.
TrimSpace
(
c
.
LinuxDo
.
UserInfoURL
)
==
""
{
return
fmt
.
Errorf
(
"linuxdo_connect.userinfo_url is required when linuxdo_connect.enabled=true"
)
}
if
strings
.
TrimSpace
(
c
.
LinuxDo
.
RedirectURL
)
==
""
{
return
fmt
.
Errorf
(
"linuxdo_connect.redirect_url is required when linuxdo_connect.enabled=true"
)
}
method
:=
strings
.
ToLower
(
strings
.
TrimSpace
(
c
.
LinuxDo
.
TokenAuthMethod
))
switch
method
{
case
""
,
"client_secret_post"
,
"client_secret_basic"
,
"none"
:
default
:
return
fmt
.
Errorf
(
"linuxdo_connect.token_auth_method must be one of: client_secret_post/client_secret_basic/none"
)
}
if
method
==
"none"
&&
!
c
.
LinuxDo
.
UsePKCE
{
return
fmt
.
Errorf
(
"linuxdo_connect.use_pkce must be true when linuxdo_connect.token_auth_method=none"
)
}
if
(
method
==
""
||
method
==
"client_secret_post"
||
method
==
"client_secret_basic"
)
&&
strings
.
TrimSpace
(
c
.
LinuxDo
.
ClientSecret
)
==
""
{
return
fmt
.
Errorf
(
"linuxdo_connect.client_secret is required when linuxdo_connect.enabled=true and token_auth_method is client_secret_post/client_secret_basic"
)
}
if
strings
.
TrimSpace
(
c
.
LinuxDo
.
FrontendRedirectURL
)
==
""
{
return
fmt
.
Errorf
(
"linuxdo_connect.frontend_redirect_url is required when linuxdo_connect.enabled=true"
)
}
if
err
:=
ValidateAbsoluteHTTPURL
(
c
.
LinuxDo
.
AuthorizeURL
);
err
!=
nil
{
return
fmt
.
Errorf
(
"linuxdo_connect.authorize_url invalid: %w"
,
err
)
}
if
err
:=
ValidateAbsoluteHTTPURL
(
c
.
LinuxDo
.
TokenURL
);
err
!=
nil
{
return
fmt
.
Errorf
(
"linuxdo_connect.token_url invalid: %w"
,
err
)
}
if
err
:=
ValidateAbsoluteHTTPURL
(
c
.
LinuxDo
.
UserInfoURL
);
err
!=
nil
{
return
fmt
.
Errorf
(
"linuxdo_connect.userinfo_url invalid: %w"
,
err
)
}
if
err
:=
ValidateAbsoluteHTTPURL
(
c
.
LinuxDo
.
RedirectURL
);
err
!=
nil
{
return
fmt
.
Errorf
(
"linuxdo_connect.redirect_url invalid: %w"
,
err
)
}
if
err
:=
ValidateFrontendRedirectURL
(
c
.
LinuxDo
.
FrontendRedirectURL
);
err
!=
nil
{
return
fmt
.
Errorf
(
"linuxdo_connect.frontend_redirect_url invalid: %w"
,
err
)
}
warnIfInsecureURL
(
"linuxdo_connect.authorize_url"
,
c
.
LinuxDo
.
AuthorizeURL
)
warnIfInsecureURL
(
"linuxdo_connect.token_url"
,
c
.
LinuxDo
.
TokenURL
)
warnIfInsecureURL
(
"linuxdo_connect.userinfo_url"
,
c
.
LinuxDo
.
UserInfoURL
)
warnIfInsecureURL
(
"linuxdo_connect.redirect_url"
,
c
.
LinuxDo
.
RedirectURL
)
warnIfInsecureURL
(
"linuxdo_connect.frontend_redirect_url"
,
c
.
LinuxDo
.
FrontendRedirectURL
)
}
if
c
.
Billing
.
CircuitBreaker
.
Enabled
{
if
c
.
Billing
.
CircuitBreaker
.
Enabled
{
if
c
.
Billing
.
CircuitBreaker
.
FailureThreshold
<=
0
{
if
c
.
Billing
.
CircuitBreaker
.
FailureThreshold
<=
0
{
return
fmt
.
Errorf
(
"billing.circuit_breaker.failure_threshold must be positive"
)
return
fmt
.
Errorf
(
"billing.circuit_breaker.failure_threshold must be positive"
)
...
...
backend/internal/config/config_test.go
View file @
6b97a8be
package
config
package
config
import
(
import
(
"strings"
"testing"
"testing"
"time"
"time"
...
@@ -90,3 +91,53 @@ func TestLoadDefaultSecurityToggles(t *testing.T) {
...
@@ -90,3 +91,53 @@ func TestLoadDefaultSecurityToggles(t *testing.T) {
t
.
Fatalf
(
"ResponseHeaders.Enabled = true, want false"
)
t
.
Fatalf
(
"ResponseHeaders.Enabled = true, want false"
)
}
}
}
}
func
TestValidateLinuxDoFrontendRedirectURL
(
t
*
testing
.
T
)
{
viper
.
Reset
()
cfg
,
err
:=
Load
()
if
err
!=
nil
{
t
.
Fatalf
(
"Load() error: %v"
,
err
)
}
cfg
.
LinuxDo
.
Enabled
=
true
cfg
.
LinuxDo
.
ClientID
=
"test-client"
cfg
.
LinuxDo
.
ClientSecret
=
"test-secret"
cfg
.
LinuxDo
.
RedirectURL
=
"https://example.com/api/v1/auth/oauth/linuxdo/callback"
cfg
.
LinuxDo
.
TokenAuthMethod
=
"client_secret_post"
cfg
.
LinuxDo
.
UsePKCE
=
false
cfg
.
LinuxDo
.
FrontendRedirectURL
=
"javascript:alert(1)"
err
=
cfg
.
Validate
()
if
err
==
nil
{
t
.
Fatalf
(
"Validate() expected error for javascript scheme, got nil"
)
}
if
!
strings
.
Contains
(
err
.
Error
(),
"linuxdo_connect.frontend_redirect_url"
)
{
t
.
Fatalf
(
"Validate() expected frontend_redirect_url error, got: %v"
,
err
)
}
}
func
TestValidateLinuxDoPKCERequiredForPublicClient
(
t
*
testing
.
T
)
{
viper
.
Reset
()
cfg
,
err
:=
Load
()
if
err
!=
nil
{
t
.
Fatalf
(
"Load() error: %v"
,
err
)
}
cfg
.
LinuxDo
.
Enabled
=
true
cfg
.
LinuxDo
.
ClientID
=
"test-client"
cfg
.
LinuxDo
.
ClientSecret
=
""
cfg
.
LinuxDo
.
RedirectURL
=
"https://example.com/api/v1/auth/oauth/linuxdo/callback"
cfg
.
LinuxDo
.
FrontendRedirectURL
=
"/auth/linuxdo/callback"
cfg
.
LinuxDo
.
TokenAuthMethod
=
"none"
cfg
.
LinuxDo
.
UsePKCE
=
false
err
=
cfg
.
Validate
()
if
err
==
nil
{
t
.
Fatalf
(
"Validate() expected error when token_auth_method=none and use_pkce=false, got nil"
)
}
if
!
strings
.
Contains
(
err
.
Error
(),
"linuxdo_connect.use_pkce"
)
{
t
.
Fatalf
(
"Validate() expected use_pkce error, got: %v"
,
err
)
}
}
backend/internal/handler/admin/account_handler.go
View file @
6b97a8be
...
@@ -116,6 +116,7 @@ type BulkUpdateAccountsRequest struct {
...
@@ -116,6 +116,7 @@ type BulkUpdateAccountsRequest struct {
Concurrency
*
int
`json:"concurrency"`
Concurrency
*
int
`json:"concurrency"`
Priority
*
int
`json:"priority"`
Priority
*
int
`json:"priority"`
Status
string
`json:"status" binding:"omitempty,oneof=active inactive error"`
Status
string
`json:"status" binding:"omitempty,oneof=active inactive error"`
Schedulable
*
bool
`json:"schedulable"`
GroupIDs
*
[]
int64
`json:"group_ids"`
GroupIDs
*
[]
int64
`json:"group_ids"`
Credentials
map
[
string
]
any
`json:"credentials"`
Credentials
map
[
string
]
any
`json:"credentials"`
Extra
map
[
string
]
any
`json:"extra"`
Extra
map
[
string
]
any
`json:"extra"`
...
@@ -136,6 +137,11 @@ func (h *AccountHandler) List(c *gin.Context) {
...
@@ -136,6 +137,11 @@ func (h *AccountHandler) List(c *gin.Context) {
accountType
:=
c
.
Query
(
"type"
)
accountType
:=
c
.
Query
(
"type"
)
status
:=
c
.
Query
(
"status"
)
status
:=
c
.
Query
(
"status"
)
search
:=
c
.
Query
(
"search"
)
search
:=
c
.
Query
(
"search"
)
// 标准化和验证 search 参数
search
=
strings
.
TrimSpace
(
search
)
if
len
(
search
)
>
100
{
search
=
search
[
:
100
]
}
accounts
,
total
,
err
:=
h
.
adminService
.
ListAccounts
(
c
.
Request
.
Context
(),
page
,
pageSize
,
platform
,
accountType
,
status
,
search
)
accounts
,
total
,
err
:=
h
.
adminService
.
ListAccounts
(
c
.
Request
.
Context
(),
page
,
pageSize
,
platform
,
accountType
,
status
,
search
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -655,6 +661,7 @@ func (h *AccountHandler) BulkUpdate(c *gin.Context) {
...
@@ -655,6 +661,7 @@ func (h *AccountHandler) BulkUpdate(c *gin.Context) {
req
.
Concurrency
!=
nil
||
req
.
Concurrency
!=
nil
||
req
.
Priority
!=
nil
||
req
.
Priority
!=
nil
||
req
.
Status
!=
""
||
req
.
Status
!=
""
||
req
.
Schedulable
!=
nil
||
req
.
GroupIDs
!=
nil
||
req
.
GroupIDs
!=
nil
||
len
(
req
.
Credentials
)
>
0
||
len
(
req
.
Credentials
)
>
0
||
len
(
req
.
Extra
)
>
0
len
(
req
.
Extra
)
>
0
...
@@ -671,6 +678,7 @@ func (h *AccountHandler) BulkUpdate(c *gin.Context) {
...
@@ -671,6 +678,7 @@ func (h *AccountHandler) BulkUpdate(c *gin.Context) {
Concurrency
:
req
.
Concurrency
,
Concurrency
:
req
.
Concurrency
,
Priority
:
req
.
Priority
,
Priority
:
req
.
Priority
,
Status
:
req
.
Status
,
Status
:
req
.
Status
,
Schedulable
:
req
.
Schedulable
,
GroupIDs
:
req
.
GroupIDs
,
GroupIDs
:
req
.
GroupIDs
,
Credentials
:
req
.
Credentials
,
Credentials
:
req
.
Credentials
,
Extra
:
req
.
Extra
,
Extra
:
req
.
Extra
,
...
...
backend/internal/handler/admin/group_handler.go
View file @
6b97a8be
...
@@ -2,6 +2,7 @@ package admin
...
@@ -2,6 +2,7 @@ package admin
import
(
import
(
"strconv"
"strconv"
"strings"
"github.com/Wei-Shaw/sub2api/internal/handler/dto"
"github.com/Wei-Shaw/sub2api/internal/handler/dto"
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
...
@@ -67,6 +68,12 @@ func (h *GroupHandler) List(c *gin.Context) {
...
@@ -67,6 +68,12 @@ func (h *GroupHandler) List(c *gin.Context) {
page
,
pageSize
:=
response
.
ParsePagination
(
c
)
page
,
pageSize
:=
response
.
ParsePagination
(
c
)
platform
:=
c
.
Query
(
"platform"
)
platform
:=
c
.
Query
(
"platform"
)
status
:=
c
.
Query
(
"status"
)
status
:=
c
.
Query
(
"status"
)
search
:=
c
.
Query
(
"search"
)
// 标准化和验证 search 参数
search
=
strings
.
TrimSpace
(
search
)
if
len
(
search
)
>
100
{
search
=
search
[
:
100
]
}
isExclusiveStr
:=
c
.
Query
(
"is_exclusive"
)
isExclusiveStr
:=
c
.
Query
(
"is_exclusive"
)
var
isExclusive
*
bool
var
isExclusive
*
bool
...
@@ -75,7 +82,7 @@ func (h *GroupHandler) List(c *gin.Context) {
...
@@ -75,7 +82,7 @@ func (h *GroupHandler) List(c *gin.Context) {
isExclusive
=
&
val
isExclusive
=
&
val
}
}
groups
,
total
,
err
:=
h
.
adminService
.
ListGroups
(
c
.
Request
.
Context
(),
page
,
pageSize
,
platform
,
status
,
isExclusive
)
groups
,
total
,
err
:=
h
.
adminService
.
ListGroups
(
c
.
Request
.
Context
(),
page
,
pageSize
,
platform
,
status
,
search
,
isExclusive
)
if
err
!=
nil
{
if
err
!=
nil
{
response
.
ErrorFrom
(
c
,
err
)
response
.
ErrorFrom
(
c
,
err
)
return
return
...
...
backend/internal/handler/admin/proxy_handler.go
View file @
6b97a8be
...
@@ -51,6 +51,11 @@ func (h *ProxyHandler) List(c *gin.Context) {
...
@@ -51,6 +51,11 @@ func (h *ProxyHandler) List(c *gin.Context) {
protocol
:=
c
.
Query
(
"protocol"
)
protocol
:=
c
.
Query
(
"protocol"
)
status
:=
c
.
Query
(
"status"
)
status
:=
c
.
Query
(
"status"
)
search
:=
c
.
Query
(
"search"
)
search
:=
c
.
Query
(
"search"
)
// 标准化和验证 search 参数
search
=
strings
.
TrimSpace
(
search
)
if
len
(
search
)
>
100
{
search
=
search
[
:
100
]
}
proxies
,
total
,
err
:=
h
.
adminService
.
ListProxiesWithAccountCount
(
c
.
Request
.
Context
(),
page
,
pageSize
,
protocol
,
status
,
search
)
proxies
,
total
,
err
:=
h
.
adminService
.
ListProxiesWithAccountCount
(
c
.
Request
.
Context
(),
page
,
pageSize
,
protocol
,
status
,
search
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
backend/internal/handler/admin/redeem_handler.go
View file @
6b97a8be
...
@@ -5,6 +5,7 @@ import (
...
@@ -5,6 +5,7 @@ import (
"encoding/csv"
"encoding/csv"
"fmt"
"fmt"
"strconv"
"strconv"
"strings"
"github.com/Wei-Shaw/sub2api/internal/handler/dto"
"github.com/Wei-Shaw/sub2api/internal/handler/dto"
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
...
@@ -41,6 +42,11 @@ func (h *RedeemHandler) List(c *gin.Context) {
...
@@ -41,6 +42,11 @@ func (h *RedeemHandler) List(c *gin.Context) {
codeType
:=
c
.
Query
(
"type"
)
codeType
:=
c
.
Query
(
"type"
)
status
:=
c
.
Query
(
"status"
)
status
:=
c
.
Query
(
"status"
)
search
:=
c
.
Query
(
"search"
)
search
:=
c
.
Query
(
"search"
)
// 标准化和验证 search 参数
search
=
strings
.
TrimSpace
(
search
)
if
len
(
search
)
>
100
{
search
=
search
[
:
100
]
}
codes
,
total
,
err
:=
h
.
adminService
.
ListRedeemCodes
(
c
.
Request
.
Context
(),
page
,
pageSize
,
codeType
,
status
,
search
)
codes
,
total
,
err
:=
h
.
adminService
.
ListRedeemCodes
(
c
.
Request
.
Context
(),
page
,
pageSize
,
codeType
,
status
,
search
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
backend/internal/handler/admin/setting_handler.go
View file @
6b97a8be
...
@@ -2,8 +2,10 @@ package admin
...
@@ -2,8 +2,10 @@ package admin
import
(
import
(
"log"
"log"
"strings"
"time"
"time"
"github.com/Wei-Shaw/sub2api/internal/config"
"github.com/Wei-Shaw/sub2api/internal/handler/dto"
"github.com/Wei-Shaw/sub2api/internal/handler/dto"
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
"github.com/Wei-Shaw/sub2api/internal/server/middleware"
"github.com/Wei-Shaw/sub2api/internal/server/middleware"
...
@@ -38,33 +40,37 @@ func (h *SettingHandler) GetSettings(c *gin.Context) {
...
@@ -38,33 +40,37 @@ func (h *SettingHandler) GetSettings(c *gin.Context) {
}
}
response
.
Success
(
c
,
dto
.
SystemSettings
{
response
.
Success
(
c
,
dto
.
SystemSettings
{
RegistrationEnabled
:
settings
.
RegistrationEnabled
,
RegistrationEnabled
:
settings
.
RegistrationEnabled
,
EmailVerifyEnabled
:
settings
.
EmailVerifyEnabled
,
EmailVerifyEnabled
:
settings
.
EmailVerifyEnabled
,
SMTPHost
:
settings
.
SMTPHost
,
SMTPHost
:
settings
.
SMTPHost
,
SMTPPort
:
settings
.
SMTPPort
,
SMTPPort
:
settings
.
SMTPPort
,
SMTPUsername
:
settings
.
SMTPUsername
,
SMTPUsername
:
settings
.
SMTPUsername
,
SMTPPasswordConfigured
:
settings
.
SMTPPasswordConfigured
,
SMTPPasswordConfigured
:
settings
.
SMTPPasswordConfigured
,
SMTPFrom
:
settings
.
SMTPFrom
,
SMTPFrom
:
settings
.
SMTPFrom
,
SMTPFromName
:
settings
.
SMTPFromName
,
SMTPFromName
:
settings
.
SMTPFromName
,
SMTPUseTLS
:
settings
.
SMTPUseTLS
,
SMTPUseTLS
:
settings
.
SMTPUseTLS
,
TurnstileEnabled
:
settings
.
TurnstileEnabled
,
TurnstileEnabled
:
settings
.
TurnstileEnabled
,
TurnstileSiteKey
:
settings
.
TurnstileSiteKey
,
TurnstileSiteKey
:
settings
.
TurnstileSiteKey
,
TurnstileSecretKeyConfigured
:
settings
.
TurnstileSecretKeyConfigured
,
TurnstileSecretKeyConfigured
:
settings
.
TurnstileSecretKeyConfigured
,
SiteName
:
settings
.
SiteName
,
LinuxDoConnectEnabled
:
settings
.
LinuxDoConnectEnabled
,
SiteLogo
:
settings
.
SiteLogo
,
LinuxDoConnectClientID
:
settings
.
LinuxDoConnectClientID
,
SiteSubtitle
:
settings
.
SiteSubtitle
,
LinuxDoConnectClientSecretConfigured
:
settings
.
LinuxDoConnectClientSecretConfigured
,
APIBaseURL
:
settings
.
APIBaseURL
,
LinuxDoConnectRedirectURL
:
settings
.
LinuxDoConnectRedirectURL
,
ContactInfo
:
settings
.
ContactInfo
,
SiteName
:
settings
.
SiteName
,
DocURL
:
settings
.
DocURL
,
SiteLogo
:
settings
.
SiteLogo
,
DefaultConcurrency
:
settings
.
DefaultConcurrency
,
SiteSubtitle
:
settings
.
SiteSubtitle
,
DefaultBalance
:
settings
.
DefaultBalance
,
APIBaseURL
:
settings
.
APIBaseURL
,
EnableModelFallback
:
settings
.
EnableModelFallback
,
ContactInfo
:
settings
.
ContactInfo
,
FallbackModelAnthropic
:
settings
.
FallbackModelAnthropic
,
DocURL
:
settings
.
DocURL
,
FallbackModelOpenAI
:
settings
.
FallbackModelOpenAI
,
DefaultConcurrency
:
settings
.
DefaultConcurrency
,
FallbackModelGemini
:
settings
.
FallbackModelGemini
,
DefaultBalance
:
settings
.
DefaultBalance
,
FallbackModelAntigravity
:
settings
.
FallbackModelAntigravity
,
EnableModelFallback
:
settings
.
EnableModelFallback
,
EnableIdentityPatch
:
settings
.
EnableIdentityPatch
,
FallbackModelAnthropic
:
settings
.
FallbackModelAnthropic
,
IdentityPatchPrompt
:
settings
.
IdentityPatchPrompt
,
FallbackModelOpenAI
:
settings
.
FallbackModelOpenAI
,
FallbackModelGemini
:
settings
.
FallbackModelGemini
,
FallbackModelAntigravity
:
settings
.
FallbackModelAntigravity
,
EnableIdentityPatch
:
settings
.
EnableIdentityPatch
,
IdentityPatchPrompt
:
settings
.
IdentityPatchPrompt
,
})
})
}
}
...
@@ -88,6 +94,12 @@ type UpdateSettingsRequest struct {
...
@@ -88,6 +94,12 @@ type UpdateSettingsRequest struct {
TurnstileSiteKey
string
`json:"turnstile_site_key"`
TurnstileSiteKey
string
`json:"turnstile_site_key"`
TurnstileSecretKey
string
`json:"turnstile_secret_key"`
TurnstileSecretKey
string
`json:"turnstile_secret_key"`
// LinuxDo Connect OAuth 登录(终端用户 SSO)
LinuxDoConnectEnabled
bool
`json:"linuxdo_connect_enabled"`
LinuxDoConnectClientID
string
`json:"linuxdo_connect_client_id"`
LinuxDoConnectClientSecret
string
`json:"linuxdo_connect_client_secret"`
LinuxDoConnectRedirectURL
string
`json:"linuxdo_connect_redirect_url"`
// OEM设置
// OEM设置
SiteName
string
`json:"site_name"`
SiteName
string
`json:"site_name"`
SiteLogo
string
`json:"site_logo"`
SiteLogo
string
`json:"site_logo"`
...
@@ -165,34 +177,67 @@ func (h *SettingHandler) UpdateSettings(c *gin.Context) {
...
@@ -165,34 +177,67 @@ func (h *SettingHandler) UpdateSettings(c *gin.Context) {
}
}
}
}
// LinuxDo Connect 参数验证
if
req
.
LinuxDoConnectEnabled
{
req
.
LinuxDoConnectClientID
=
strings
.
TrimSpace
(
req
.
LinuxDoConnectClientID
)
req
.
LinuxDoConnectClientSecret
=
strings
.
TrimSpace
(
req
.
LinuxDoConnectClientSecret
)
req
.
LinuxDoConnectRedirectURL
=
strings
.
TrimSpace
(
req
.
LinuxDoConnectRedirectURL
)
if
req
.
LinuxDoConnectClientID
==
""
{
response
.
BadRequest
(
c
,
"LinuxDo Client ID is required when enabled"
)
return
}
if
req
.
LinuxDoConnectRedirectURL
==
""
{
response
.
BadRequest
(
c
,
"LinuxDo Redirect URL is required when enabled"
)
return
}
if
err
:=
config
.
ValidateAbsoluteHTTPURL
(
req
.
LinuxDoConnectRedirectURL
);
err
!=
nil
{
response
.
BadRequest
(
c
,
"LinuxDo Redirect URL must be an absolute http(s) URL"
)
return
}
// 如果未提供 client_secret,则保留现有值(如有)。
if
req
.
LinuxDoConnectClientSecret
==
""
{
if
previousSettings
.
LinuxDoConnectClientSecret
==
""
{
response
.
BadRequest
(
c
,
"LinuxDo Client Secret is required when enabled"
)
return
}
req
.
LinuxDoConnectClientSecret
=
previousSettings
.
LinuxDoConnectClientSecret
}
}
settings
:=
&
service
.
SystemSettings
{
settings
:=
&
service
.
SystemSettings
{
RegistrationEnabled
:
req
.
RegistrationEnabled
,
RegistrationEnabled
:
req
.
RegistrationEnabled
,
EmailVerifyEnabled
:
req
.
EmailVerifyEnabled
,
EmailVerifyEnabled
:
req
.
EmailVerifyEnabled
,
SMTPHost
:
req
.
SMTPHost
,
SMTPHost
:
req
.
SMTPHost
,
SMTPPort
:
req
.
SMTPPort
,
SMTPPort
:
req
.
SMTPPort
,
SMTPUsername
:
req
.
SMTPUsername
,
SMTPUsername
:
req
.
SMTPUsername
,
SMTPPassword
:
req
.
SMTPPassword
,
SMTPPassword
:
req
.
SMTPPassword
,
SMTPFrom
:
req
.
SMTPFrom
,
SMTPFrom
:
req
.
SMTPFrom
,
SMTPFromName
:
req
.
SMTPFromName
,
SMTPFromName
:
req
.
SMTPFromName
,
SMTPUseTLS
:
req
.
SMTPUseTLS
,
SMTPUseTLS
:
req
.
SMTPUseTLS
,
TurnstileEnabled
:
req
.
TurnstileEnabled
,
TurnstileEnabled
:
req
.
TurnstileEnabled
,
TurnstileSiteKey
:
req
.
TurnstileSiteKey
,
TurnstileSiteKey
:
req
.
TurnstileSiteKey
,
TurnstileSecretKey
:
req
.
TurnstileSecretKey
,
TurnstileSecretKey
:
req
.
TurnstileSecretKey
,
SiteName
:
req
.
SiteName
,
LinuxDoConnectEnabled
:
req
.
LinuxDoConnectEnabled
,
SiteLogo
:
req
.
SiteLogo
,
LinuxDoConnectClientID
:
req
.
LinuxDoConnectClientID
,
SiteSubtitle
:
req
.
SiteSubtitle
,
LinuxDoConnectClientSecret
:
req
.
LinuxDoConnectClientSecret
,
APIBaseURL
:
req
.
APIBaseURL
,
LinuxDoConnectRedirectURL
:
req
.
LinuxDoConnectRedirectURL
,
ContactInfo
:
req
.
ContactInfo
,
SiteName
:
req
.
SiteName
,
DocURL
:
req
.
DocURL
,
SiteLogo
:
req
.
SiteLogo
,
DefaultConcurrency
:
req
.
DefaultConcurrency
,
SiteSubtitle
:
req
.
SiteSubtitle
,
DefaultBalance
:
req
.
DefaultBalance
,
APIBaseURL
:
req
.
APIBaseURL
,
EnableModelFallback
:
req
.
EnableModelFallback
,
ContactInfo
:
req
.
ContactInfo
,
FallbackModelAnthropic
:
req
.
FallbackModelAnthropic
,
DocURL
:
req
.
DocURL
,
FallbackModelOpenAI
:
req
.
FallbackModelOpenAI
,
DefaultConcurrency
:
req
.
DefaultConcurrency
,
FallbackModelGemini
:
req
.
FallbackModelGemini
,
DefaultBalance
:
req
.
DefaultBalance
,
FallbackModelAntigravity
:
req
.
FallbackModelAntigravity
,
EnableModelFallback
:
req
.
EnableModelFallback
,
EnableIdentityPatch
:
req
.
EnableIdentityPatch
,
FallbackModelAnthropic
:
req
.
FallbackModelAnthropic
,
IdentityPatchPrompt
:
req
.
IdentityPatchPrompt
,
FallbackModelOpenAI
:
req
.
FallbackModelOpenAI
,
FallbackModelGemini
:
req
.
FallbackModelGemini
,
FallbackModelAntigravity
:
req
.
FallbackModelAntigravity
,
EnableIdentityPatch
:
req
.
EnableIdentityPatch
,
IdentityPatchPrompt
:
req
.
IdentityPatchPrompt
,
}
}
if
err
:=
h
.
settingService
.
UpdateSettings
(
c
.
Request
.
Context
(),
settings
);
err
!=
nil
{
if
err
:=
h
.
settingService
.
UpdateSettings
(
c
.
Request
.
Context
(),
settings
);
err
!=
nil
{
...
@@ -210,33 +255,37 @@ func (h *SettingHandler) UpdateSettings(c *gin.Context) {
...
@@ -210,33 +255,37 @@ func (h *SettingHandler) UpdateSettings(c *gin.Context) {
}
}
response
.
Success
(
c
,
dto
.
SystemSettings
{
response
.
Success
(
c
,
dto
.
SystemSettings
{
RegistrationEnabled
:
updatedSettings
.
RegistrationEnabled
,
RegistrationEnabled
:
updatedSettings
.
RegistrationEnabled
,
EmailVerifyEnabled
:
updatedSettings
.
EmailVerifyEnabled
,
EmailVerifyEnabled
:
updatedSettings
.
EmailVerifyEnabled
,
SMTPHost
:
updatedSettings
.
SMTPHost
,
SMTPHost
:
updatedSettings
.
SMTPHost
,
SMTPPort
:
updatedSettings
.
SMTPPort
,
SMTPPort
:
updatedSettings
.
SMTPPort
,
SMTPUsername
:
updatedSettings
.
SMTPUsername
,
SMTPUsername
:
updatedSettings
.
SMTPUsername
,
SMTPPasswordConfigured
:
updatedSettings
.
SMTPPasswordConfigured
,
SMTPPasswordConfigured
:
updatedSettings
.
SMTPPasswordConfigured
,
SMTPFrom
:
updatedSettings
.
SMTPFrom
,
SMTPFrom
:
updatedSettings
.
SMTPFrom
,
SMTPFromName
:
updatedSettings
.
SMTPFromName
,
SMTPFromName
:
updatedSettings
.
SMTPFromName
,
SMTPUseTLS
:
updatedSettings
.
SMTPUseTLS
,
SMTPUseTLS
:
updatedSettings
.
SMTPUseTLS
,
TurnstileEnabled
:
updatedSettings
.
TurnstileEnabled
,
TurnstileEnabled
:
updatedSettings
.
TurnstileEnabled
,
TurnstileSiteKey
:
updatedSettings
.
TurnstileSiteKey
,
TurnstileSiteKey
:
updatedSettings
.
TurnstileSiteKey
,
TurnstileSecretKeyConfigured
:
updatedSettings
.
TurnstileSecretKeyConfigured
,
TurnstileSecretKeyConfigured
:
updatedSettings
.
TurnstileSecretKeyConfigured
,
SiteName
:
updatedSettings
.
SiteName
,
LinuxDoConnectEnabled
:
updatedSettings
.
LinuxDoConnectEnabled
,
SiteLogo
:
updatedSettings
.
SiteLogo
,
LinuxDoConnectClientID
:
updatedSettings
.
LinuxDoConnectClientID
,
SiteSubtitle
:
updatedSettings
.
SiteSubtitle
,
LinuxDoConnectClientSecretConfigured
:
updatedSettings
.
LinuxDoConnectClientSecretConfigured
,
APIBaseURL
:
updatedSettings
.
APIBaseURL
,
LinuxDoConnectRedirectURL
:
updatedSettings
.
LinuxDoConnectRedirectURL
,
ContactInfo
:
updatedSettings
.
ContactInfo
,
SiteName
:
updatedSettings
.
SiteName
,
DocURL
:
updatedSettings
.
DocURL
,
SiteLogo
:
updatedSettings
.
SiteLogo
,
DefaultConcurrency
:
updatedSettings
.
DefaultConcurrency
,
SiteSubtitle
:
updatedSettings
.
SiteSubtitle
,
DefaultBalance
:
updatedSettings
.
DefaultBalance
,
APIBaseURL
:
updatedSettings
.
APIBaseURL
,
EnableModelFallback
:
updatedSettings
.
EnableModelFallback
,
ContactInfo
:
updatedSettings
.
ContactInfo
,
FallbackModelAnthropic
:
updatedSettings
.
FallbackModelAnthropic
,
DocURL
:
updatedSettings
.
DocURL
,
FallbackModelOpenAI
:
updatedSettings
.
FallbackModelOpenAI
,
DefaultConcurrency
:
updatedSettings
.
DefaultConcurrency
,
FallbackModelGemini
:
updatedSettings
.
FallbackModelGemini
,
DefaultBalance
:
updatedSettings
.
DefaultBalance
,
FallbackModelAntigravity
:
updatedSettings
.
FallbackModelAntigravity
,
EnableModelFallback
:
updatedSettings
.
EnableModelFallback
,
EnableIdentityPatch
:
updatedSettings
.
EnableIdentityPatch
,
FallbackModelAnthropic
:
updatedSettings
.
FallbackModelAnthropic
,
IdentityPatchPrompt
:
updatedSettings
.
IdentityPatchPrompt
,
FallbackModelOpenAI
:
updatedSettings
.
FallbackModelOpenAI
,
FallbackModelGemini
:
updatedSettings
.
FallbackModelGemini
,
FallbackModelAntigravity
:
updatedSettings
.
FallbackModelAntigravity
,
EnableIdentityPatch
:
updatedSettings
.
EnableIdentityPatch
,
IdentityPatchPrompt
:
updatedSettings
.
IdentityPatchPrompt
,
})
})
}
}
...
@@ -298,6 +347,18 @@ func diffSettings(before *service.SystemSettings, after *service.SystemSettings,
...
@@ -298,6 +347,18 @@ func diffSettings(before *service.SystemSettings, after *service.SystemSettings,
if
req
.
TurnstileSecretKey
!=
""
{
if
req
.
TurnstileSecretKey
!=
""
{
changed
=
append
(
changed
,
"turnstile_secret_key"
)
changed
=
append
(
changed
,
"turnstile_secret_key"
)
}
}
if
before
.
LinuxDoConnectEnabled
!=
after
.
LinuxDoConnectEnabled
{
changed
=
append
(
changed
,
"linuxdo_connect_enabled"
)
}
if
before
.
LinuxDoConnectClientID
!=
after
.
LinuxDoConnectClientID
{
changed
=
append
(
changed
,
"linuxdo_connect_client_id"
)
}
if
req
.
LinuxDoConnectClientSecret
!=
""
{
changed
=
append
(
changed
,
"linuxdo_connect_client_secret"
)
}
if
before
.
LinuxDoConnectRedirectURL
!=
after
.
LinuxDoConnectRedirectURL
{
changed
=
append
(
changed
,
"linuxdo_connect_redirect_url"
)
}
if
before
.
SiteName
!=
after
.
SiteName
{
if
before
.
SiteName
!=
after
.
SiteName
{
changed
=
append
(
changed
,
"site_name"
)
changed
=
append
(
changed
,
"site_name"
)
}
}
...
@@ -337,6 +398,12 @@ func diffSettings(before *service.SystemSettings, after *service.SystemSettings,
...
@@ -337,6 +398,12 @@ func diffSettings(before *service.SystemSettings, after *service.SystemSettings,
if
before
.
FallbackModelAntigravity
!=
after
.
FallbackModelAntigravity
{
if
before
.
FallbackModelAntigravity
!=
after
.
FallbackModelAntigravity
{
changed
=
append
(
changed
,
"fallback_model_antigravity"
)
changed
=
append
(
changed
,
"fallback_model_antigravity"
)
}
}
if
before
.
EnableIdentityPatch
!=
after
.
EnableIdentityPatch
{
changed
=
append
(
changed
,
"enable_identity_patch"
)
}
if
before
.
IdentityPatchPrompt
!=
after
.
IdentityPatchPrompt
{
changed
=
append
(
changed
,
"identity_patch_prompt"
)
}
return
changed
return
changed
}
}
...
...
backend/internal/handler/admin/user_handler.go
View file @
6b97a8be
...
@@ -2,6 +2,7 @@ package admin
...
@@ -2,6 +2,7 @@ package admin
import
(
import
(
"strconv"
"strconv"
"strings"
"github.com/Wei-Shaw/sub2api/internal/handler/dto"
"github.com/Wei-Shaw/sub2api/internal/handler/dto"
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
...
@@ -63,10 +64,17 @@ type UpdateBalanceRequest struct {
...
@@ -63,10 +64,17 @@ type UpdateBalanceRequest struct {
func
(
h
*
UserHandler
)
List
(
c
*
gin
.
Context
)
{
func
(
h
*
UserHandler
)
List
(
c
*
gin
.
Context
)
{
page
,
pageSize
:=
response
.
ParsePagination
(
c
)
page
,
pageSize
:=
response
.
ParsePagination
(
c
)
search
:=
c
.
Query
(
"search"
)
// 标准化和验证 search 参数
search
=
strings
.
TrimSpace
(
search
)
if
len
(
search
)
>
100
{
search
=
search
[
:
100
]
}
filters
:=
service
.
UserListFilters
{
filters
:=
service
.
UserListFilters
{
Status
:
c
.
Query
(
"status"
),
Status
:
c
.
Query
(
"status"
),
Role
:
c
.
Query
(
"role"
),
Role
:
c
.
Query
(
"role"
),
Search
:
c
.
Query
(
"
search
"
)
,
Search
:
search
,
Attributes
:
parseAttributeFilters
(
c
),
Attributes
:
parseAttributeFilters
(
c
),
}
}
...
...
backend/internal/handler/auth_handler.go
View file @
6b97a8be
...
@@ -15,14 +15,16 @@ type AuthHandler struct {
...
@@ -15,14 +15,16 @@ type AuthHandler struct {
cfg
*
config
.
Config
cfg
*
config
.
Config
authService
*
service
.
AuthService
authService
*
service
.
AuthService
userService
*
service
.
UserService
userService
*
service
.
UserService
settingSvc
*
service
.
SettingService
}
}
// NewAuthHandler creates a new AuthHandler
// NewAuthHandler creates a new AuthHandler
func
NewAuthHandler
(
cfg
*
config
.
Config
,
authService
*
service
.
AuthService
,
userService
*
service
.
UserService
)
*
AuthHandler
{
func
NewAuthHandler
(
cfg
*
config
.
Config
,
authService
*
service
.
AuthService
,
userService
*
service
.
UserService
,
settingService
*
service
.
SettingService
)
*
AuthHandler
{
return
&
AuthHandler
{
return
&
AuthHandler
{
cfg
:
cfg
,
cfg
:
cfg
,
authService
:
authService
,
authService
:
authService
,
userService
:
userService
,
userService
:
userService
,
settingSvc
:
settingService
,
}
}
}
}
...
...
backend/internal/handler/auth_linuxdo_oauth.go
0 → 100644
View file @
6b97a8be
package
handler
import
(
"context"
"encoding/base64"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/Wei-Shaw/sub2api/internal/config"
infraerrors
"github.com/Wei-Shaw/sub2api/internal/pkg/errors"
"github.com/Wei-Shaw/sub2api/internal/pkg/oauth"
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
"github.com/Wei-Shaw/sub2api/internal/service"
"github.com/gin-gonic/gin"
"github.com/imroc/req/v3"
"github.com/tidwall/gjson"
)
const
(
linuxDoOAuthCookiePath
=
"/api/v1/auth/oauth/linuxdo"
linuxDoOAuthStateCookieName
=
"linuxdo_oauth_state"
linuxDoOAuthVerifierCookie
=
"linuxdo_oauth_verifier"
linuxDoOAuthRedirectCookie
=
"linuxdo_oauth_redirect"
linuxDoOAuthCookieMaxAgeSec
=
10
*
60
// 10 minutes
linuxDoOAuthDefaultRedirectTo
=
"/dashboard"
linuxDoOAuthDefaultFrontendCB
=
"/auth/linuxdo/callback"
linuxDoOAuthMaxRedirectLen
=
2048
linuxDoOAuthMaxFragmentValueLen
=
512
linuxDoOAuthMaxSubjectLen
=
64
-
len
(
"linuxdo-"
)
)
type
linuxDoTokenResponse
struct
{
AccessToken
string
`json:"access_token"`
TokenType
string
`json:"token_type"`
ExpiresIn
int64
`json:"expires_in"`
RefreshToken
string
`json:"refresh_token,omitempty"`
Scope
string
`json:"scope,omitempty"`
}
type
linuxDoTokenExchangeError
struct
{
StatusCode
int
ProviderError
string
ProviderDescription
string
Body
string
}
func
(
e
*
linuxDoTokenExchangeError
)
Error
()
string
{
if
e
==
nil
{
return
""
}
parts
:=
[]
string
{
fmt
.
Sprintf
(
"token exchange status=%d"
,
e
.
StatusCode
)}
if
strings
.
TrimSpace
(
e
.
ProviderError
)
!=
""
{
parts
=
append
(
parts
,
"error="
+
strings
.
TrimSpace
(
e
.
ProviderError
))
}
if
strings
.
TrimSpace
(
e
.
ProviderDescription
)
!=
""
{
parts
=
append
(
parts
,
"error_description="
+
strings
.
TrimSpace
(
e
.
ProviderDescription
))
}
return
strings
.
Join
(
parts
,
" "
)
}
// LinuxDoOAuthStart 启动 LinuxDo Connect OAuth 登录流程。
// GET /api/v1/auth/oauth/linuxdo/start?redirect=/dashboard
func
(
h
*
AuthHandler
)
LinuxDoOAuthStart
(
c
*
gin
.
Context
)
{
cfg
,
err
:=
h
.
getLinuxDoOAuthConfig
(
c
.
Request
.
Context
())
if
err
!=
nil
{
response
.
ErrorFrom
(
c
,
err
)
return
}
state
,
err
:=
oauth
.
GenerateState
()
if
err
!=
nil
{
response
.
ErrorFrom
(
c
,
infraerrors
.
InternalServer
(
"OAUTH_STATE_GEN_FAILED"
,
"failed to generate oauth state"
)
.
WithCause
(
err
))
return
}
redirectTo
:=
sanitizeFrontendRedirectPath
(
c
.
Query
(
"redirect"
))
if
redirectTo
==
""
{
redirectTo
=
linuxDoOAuthDefaultRedirectTo
}
secureCookie
:=
isRequestHTTPS
(
c
)
setCookie
(
c
,
linuxDoOAuthStateCookieName
,
encodeCookieValue
(
state
),
linuxDoOAuthCookieMaxAgeSec
,
secureCookie
)
setCookie
(
c
,
linuxDoOAuthRedirectCookie
,
encodeCookieValue
(
redirectTo
),
linuxDoOAuthCookieMaxAgeSec
,
secureCookie
)
codeChallenge
:=
""
if
cfg
.
UsePKCE
{
verifier
,
err
:=
oauth
.
GenerateCodeVerifier
()
if
err
!=
nil
{
response
.
ErrorFrom
(
c
,
infraerrors
.
InternalServer
(
"OAUTH_PKCE_GEN_FAILED"
,
"failed to generate pkce verifier"
)
.
WithCause
(
err
))
return
}
codeChallenge
=
oauth
.
GenerateCodeChallenge
(
verifier
)
setCookie
(
c
,
linuxDoOAuthVerifierCookie
,
encodeCookieValue
(
verifier
),
linuxDoOAuthCookieMaxAgeSec
,
secureCookie
)
}
redirectURI
:=
strings
.
TrimSpace
(
cfg
.
RedirectURL
)
if
redirectURI
==
""
{
response
.
ErrorFrom
(
c
,
infraerrors
.
InternalServer
(
"OAUTH_CONFIG_INVALID"
,
"oauth redirect url not configured"
))
return
}
authURL
,
err
:=
buildLinuxDoAuthorizeURL
(
cfg
,
state
,
codeChallenge
,
redirectURI
)
if
err
!=
nil
{
response
.
ErrorFrom
(
c
,
infraerrors
.
InternalServer
(
"OAUTH_BUILD_URL_FAILED"
,
"failed to build oauth authorization url"
)
.
WithCause
(
err
))
return
}
c
.
Redirect
(
http
.
StatusFound
,
authURL
)
}
// LinuxDoOAuthCallback 处理 OAuth 回调:创建/登录用户,然后重定向到前端。
// GET /api/v1/auth/oauth/linuxdo/callback?code=...&state=...
func
(
h
*
AuthHandler
)
LinuxDoOAuthCallback
(
c
*
gin
.
Context
)
{
cfg
,
cfgErr
:=
h
.
getLinuxDoOAuthConfig
(
c
.
Request
.
Context
())
if
cfgErr
!=
nil
{
response
.
ErrorFrom
(
c
,
cfgErr
)
return
}
frontendCallback
:=
strings
.
TrimSpace
(
cfg
.
FrontendRedirectURL
)
if
frontendCallback
==
""
{
frontendCallback
=
linuxDoOAuthDefaultFrontendCB
}
if
providerErr
:=
strings
.
TrimSpace
(
c
.
Query
(
"error"
));
providerErr
!=
""
{
redirectOAuthError
(
c
,
frontendCallback
,
"provider_error"
,
providerErr
,
c
.
Query
(
"error_description"
))
return
}
code
:=
strings
.
TrimSpace
(
c
.
Query
(
"code"
))
state
:=
strings
.
TrimSpace
(
c
.
Query
(
"state"
))
if
code
==
""
||
state
==
""
{
redirectOAuthError
(
c
,
frontendCallback
,
"missing_params"
,
"missing code/state"
,
""
)
return
}
secureCookie
:=
isRequestHTTPS
(
c
)
defer
func
()
{
clearCookie
(
c
,
linuxDoOAuthStateCookieName
,
secureCookie
)
clearCookie
(
c
,
linuxDoOAuthVerifierCookie
,
secureCookie
)
clearCookie
(
c
,
linuxDoOAuthRedirectCookie
,
secureCookie
)
}()
expectedState
,
err
:=
readCookieDecoded
(
c
,
linuxDoOAuthStateCookieName
)
if
err
!=
nil
||
expectedState
==
""
||
state
!=
expectedState
{
redirectOAuthError
(
c
,
frontendCallback
,
"invalid_state"
,
"invalid oauth state"
,
""
)
return
}
redirectTo
,
_
:=
readCookieDecoded
(
c
,
linuxDoOAuthRedirectCookie
)
redirectTo
=
sanitizeFrontendRedirectPath
(
redirectTo
)
if
redirectTo
==
""
{
redirectTo
=
linuxDoOAuthDefaultRedirectTo
}
codeVerifier
:=
""
if
cfg
.
UsePKCE
{
codeVerifier
,
_
=
readCookieDecoded
(
c
,
linuxDoOAuthVerifierCookie
)
if
codeVerifier
==
""
{
redirectOAuthError
(
c
,
frontendCallback
,
"missing_verifier"
,
"missing pkce verifier"
,
""
)
return
}
}
redirectURI
:=
strings
.
TrimSpace
(
cfg
.
RedirectURL
)
if
redirectURI
==
""
{
redirectOAuthError
(
c
,
frontendCallback
,
"config_error"
,
"oauth redirect url not configured"
,
""
)
return
}
tokenResp
,
err
:=
linuxDoExchangeCode
(
c
.
Request
.
Context
(),
cfg
,
code
,
redirectURI
,
codeVerifier
)
if
err
!=
nil
{
description
:=
""
var
exchangeErr
*
linuxDoTokenExchangeError
if
errors
.
As
(
err
,
&
exchangeErr
)
&&
exchangeErr
!=
nil
{
log
.
Printf
(
"[LinuxDo OAuth] token exchange failed: status=%d provider_error=%q provider_description=%q body=%s"
,
exchangeErr
.
StatusCode
,
exchangeErr
.
ProviderError
,
exchangeErr
.
ProviderDescription
,
truncateLogValue
(
exchangeErr
.
Body
,
2048
),
)
description
=
exchangeErr
.
Error
()
}
else
{
log
.
Printf
(
"[LinuxDo OAuth] token exchange failed: %v"
,
err
)
description
=
err
.
Error
()
}
redirectOAuthError
(
c
,
frontendCallback
,
"token_exchange_failed"
,
"failed to exchange oauth code"
,
singleLine
(
description
))
return
}
email
,
username
,
subject
,
err
:=
linuxDoFetchUserInfo
(
c
.
Request
.
Context
(),
cfg
,
tokenResp
)
if
err
!=
nil
{
log
.
Printf
(
"[LinuxDo OAuth] userinfo fetch failed: %v"
,
err
)
redirectOAuthError
(
c
,
frontendCallback
,
"userinfo_failed"
,
"failed to fetch user info"
,
""
)
return
}
// 安全考虑:不要把第三方返回的 email 直接映射到本地账号(可能与本地邮箱用户冲突导致账号被接管)。
// 统一使用基于 subject 的稳定合成邮箱来做账号绑定。
if
subject
!=
""
{
email
=
linuxDoSyntheticEmail
(
subject
)
}
jwtToken
,
_
,
err
:=
h
.
authService
.
LoginOrRegisterOAuth
(
c
.
Request
.
Context
(),
email
,
username
)
if
err
!=
nil
{
// 避免把内部细节泄露给客户端;给前端保留结构化原因与提示信息即可。
redirectOAuthError
(
c
,
frontendCallback
,
"login_failed"
,
infraerrors
.
Reason
(
err
),
infraerrors
.
Message
(
err
))
return
}
fragment
:=
url
.
Values
{}
fragment
.
Set
(
"access_token"
,
jwtToken
)
fragment
.
Set
(
"token_type"
,
"Bearer"
)
fragment
.
Set
(
"redirect"
,
redirectTo
)
redirectWithFragment
(
c
,
frontendCallback
,
fragment
)
}
func
(
h
*
AuthHandler
)
getLinuxDoOAuthConfig
(
ctx
context
.
Context
)
(
config
.
LinuxDoConnectConfig
,
error
)
{
if
h
!=
nil
&&
h
.
settingSvc
!=
nil
{
return
h
.
settingSvc
.
GetLinuxDoConnectOAuthConfig
(
ctx
)
}
if
h
==
nil
||
h
.
cfg
==
nil
{
return
config
.
LinuxDoConnectConfig
{},
infraerrors
.
ServiceUnavailable
(
"CONFIG_NOT_READY"
,
"config not loaded"
)
}
if
!
h
.
cfg
.
LinuxDo
.
Enabled
{
return
config
.
LinuxDoConnectConfig
{},
infraerrors
.
NotFound
(
"OAUTH_DISABLED"
,
"oauth login is disabled"
)
}
return
h
.
cfg
.
LinuxDo
,
nil
}
func
linuxDoExchangeCode
(
ctx
context
.
Context
,
cfg
config
.
LinuxDoConnectConfig
,
code
string
,
redirectURI
string
,
codeVerifier
string
,
)
(
*
linuxDoTokenResponse
,
error
)
{
client
:=
req
.
C
()
.
SetTimeout
(
30
*
time
.
Second
)
form
:=
url
.
Values
{}
form
.
Set
(
"grant_type"
,
"authorization_code"
)
form
.
Set
(
"client_id"
,
cfg
.
ClientID
)
form
.
Set
(
"code"
,
code
)
form
.
Set
(
"redirect_uri"
,
redirectURI
)
if
cfg
.
UsePKCE
{
form
.
Set
(
"code_verifier"
,
codeVerifier
)
}
r
:=
client
.
R
()
.
SetContext
(
ctx
)
.
SetHeader
(
"Accept"
,
"application/json"
)
switch
strings
.
ToLower
(
strings
.
TrimSpace
(
cfg
.
TokenAuthMethod
))
{
case
""
,
"client_secret_post"
:
form
.
Set
(
"client_secret"
,
cfg
.
ClientSecret
)
case
"client_secret_basic"
:
r
.
SetBasicAuth
(
cfg
.
ClientID
,
cfg
.
ClientSecret
)
case
"none"
:
default
:
return
nil
,
fmt
.
Errorf
(
"unsupported token_auth_method: %s"
,
cfg
.
TokenAuthMethod
)
}
resp
,
err
:=
r
.
SetFormDataFromValues
(
form
)
.
Post
(
cfg
.
TokenURL
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"request token: %w"
,
err
)
}
body
:=
strings
.
TrimSpace
(
resp
.
String
())
if
!
resp
.
IsSuccessState
()
{
providerErr
,
providerDesc
:=
parseOAuthProviderError
(
body
)
return
nil
,
&
linuxDoTokenExchangeError
{
StatusCode
:
resp
.
StatusCode
,
ProviderError
:
providerErr
,
ProviderDescription
:
providerDesc
,
Body
:
body
,
}
}
tokenResp
,
ok
:=
parseLinuxDoTokenResponse
(
body
)
if
!
ok
||
strings
.
TrimSpace
(
tokenResp
.
AccessToken
)
==
""
{
return
nil
,
&
linuxDoTokenExchangeError
{
StatusCode
:
resp
.
StatusCode
,
Body
:
body
,
}
}
if
strings
.
TrimSpace
(
tokenResp
.
TokenType
)
==
""
{
tokenResp
.
TokenType
=
"Bearer"
}
return
tokenResp
,
nil
}
func
linuxDoFetchUserInfo
(
ctx
context
.
Context
,
cfg
config
.
LinuxDoConnectConfig
,
token
*
linuxDoTokenResponse
,
)
(
email
string
,
username
string
,
subject
string
,
err
error
)
{
client
:=
req
.
C
()
.
SetTimeout
(
30
*
time
.
Second
)
authorization
,
err
:=
buildBearerAuthorization
(
token
.
TokenType
,
token
.
AccessToken
)
if
err
!=
nil
{
return
""
,
""
,
""
,
fmt
.
Errorf
(
"invalid token for userinfo request: %w"
,
err
)
}
resp
,
err
:=
client
.
R
()
.
SetContext
(
ctx
)
.
SetHeader
(
"Accept"
,
"application/json"
)
.
SetHeader
(
"Authorization"
,
authorization
)
.
Get
(
cfg
.
UserInfoURL
)
if
err
!=
nil
{
return
""
,
""
,
""
,
fmt
.
Errorf
(
"request userinfo: %w"
,
err
)
}
if
!
resp
.
IsSuccessState
()
{
return
""
,
""
,
""
,
fmt
.
Errorf
(
"userinfo status=%d"
,
resp
.
StatusCode
)
}
return
linuxDoParseUserInfo
(
resp
.
String
(),
cfg
)
}
func
linuxDoParseUserInfo
(
body
string
,
cfg
config
.
LinuxDoConnectConfig
)
(
email
string
,
username
string
,
subject
string
,
err
error
)
{
email
=
firstNonEmpty
(
getGJSON
(
body
,
cfg
.
UserInfoEmailPath
),
getGJSON
(
body
,
"email"
),
getGJSON
(
body
,
"user.email"
),
getGJSON
(
body
,
"data.email"
),
getGJSON
(
body
,
"attributes.email"
),
)
username
=
firstNonEmpty
(
getGJSON
(
body
,
cfg
.
UserInfoUsernamePath
),
getGJSON
(
body
,
"username"
),
getGJSON
(
body
,
"preferred_username"
),
getGJSON
(
body
,
"name"
),
getGJSON
(
body
,
"user.username"
),
getGJSON
(
body
,
"user.name"
),
)
subject
=
firstNonEmpty
(
getGJSON
(
body
,
cfg
.
UserInfoIDPath
),
getGJSON
(
body
,
"sub"
),
getGJSON
(
body
,
"id"
),
getGJSON
(
body
,
"user_id"
),
getGJSON
(
body
,
"uid"
),
getGJSON
(
body
,
"user.id"
),
)
subject
=
strings
.
TrimSpace
(
subject
)
if
subject
==
""
{
return
""
,
""
,
""
,
errors
.
New
(
"userinfo missing id field"
)
}
if
!
isSafeLinuxDoSubject
(
subject
)
{
return
""
,
""
,
""
,
errors
.
New
(
"userinfo returned invalid id field"
)
}
email
=
strings
.
TrimSpace
(
email
)
if
email
==
""
{
// LinuxDo Connect 的 userinfo 可能不提供 email。为兼容现有用户模型(email 必填且唯一),使用稳定的合成邮箱。
email
=
linuxDoSyntheticEmail
(
subject
)
}
username
=
strings
.
TrimSpace
(
username
)
if
username
==
""
{
username
=
"linuxdo_"
+
subject
}
return
email
,
username
,
subject
,
nil
}
func
buildLinuxDoAuthorizeURL
(
cfg
config
.
LinuxDoConnectConfig
,
state
string
,
codeChallenge
string
,
redirectURI
string
)
(
string
,
error
)
{
u
,
err
:=
url
.
Parse
(
cfg
.
AuthorizeURL
)
if
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"parse authorize_url: %w"
,
err
)
}
q
:=
u
.
Query
()
q
.
Set
(
"response_type"
,
"code"
)
q
.
Set
(
"client_id"
,
cfg
.
ClientID
)
q
.
Set
(
"redirect_uri"
,
redirectURI
)
if
strings
.
TrimSpace
(
cfg
.
Scopes
)
!=
""
{
q
.
Set
(
"scope"
,
cfg
.
Scopes
)
}
q
.
Set
(
"state"
,
state
)
if
cfg
.
UsePKCE
{
q
.
Set
(
"code_challenge"
,
codeChallenge
)
q
.
Set
(
"code_challenge_method"
,
"S256"
)
}
u
.
RawQuery
=
q
.
Encode
()
return
u
.
String
(),
nil
}
func
redirectOAuthError
(
c
*
gin
.
Context
,
frontendCallback
string
,
code
string
,
message
string
,
description
string
)
{
fragment
:=
url
.
Values
{}
fragment
.
Set
(
"error"
,
truncateFragmentValue
(
code
))
if
strings
.
TrimSpace
(
message
)
!=
""
{
fragment
.
Set
(
"error_message"
,
truncateFragmentValue
(
message
))
}
if
strings
.
TrimSpace
(
description
)
!=
""
{
fragment
.
Set
(
"error_description"
,
truncateFragmentValue
(
description
))
}
redirectWithFragment
(
c
,
frontendCallback
,
fragment
)
}
func
redirectWithFragment
(
c
*
gin
.
Context
,
frontendCallback
string
,
fragment
url
.
Values
)
{
u
,
err
:=
url
.
Parse
(
frontendCallback
)
if
err
!=
nil
{
// 兜底:尽力跳转到默认页面,避免卡死在回调页。
c
.
Redirect
(
http
.
StatusFound
,
linuxDoOAuthDefaultRedirectTo
)
return
}
if
u
.
Scheme
!=
""
&&
!
strings
.
EqualFold
(
u
.
Scheme
,
"http"
)
&&
!
strings
.
EqualFold
(
u
.
Scheme
,
"https"
)
{
c
.
Redirect
(
http
.
StatusFound
,
linuxDoOAuthDefaultRedirectTo
)
return
}
u
.
Fragment
=
fragment
.
Encode
()
c
.
Header
(
"Cache-Control"
,
"no-store"
)
c
.
Header
(
"Pragma"
,
"no-cache"
)
c
.
Redirect
(
http
.
StatusFound
,
u
.
String
())
}
func
firstNonEmpty
(
values
...
string
)
string
{
for
_
,
v
:=
range
values
{
v
=
strings
.
TrimSpace
(
v
)
if
v
!=
""
{
return
v
}
}
return
""
}
func
parseOAuthProviderError
(
body
string
)
(
providerErr
string
,
providerDesc
string
)
{
body
=
strings
.
TrimSpace
(
body
)
if
body
==
""
{
return
""
,
""
}
providerErr
=
firstNonEmpty
(
getGJSON
(
body
,
"error"
),
getGJSON
(
body
,
"code"
),
getGJSON
(
body
,
"error.code"
),
)
providerDesc
=
firstNonEmpty
(
getGJSON
(
body
,
"error_description"
),
getGJSON
(
body
,
"error.message"
),
getGJSON
(
body
,
"message"
),
getGJSON
(
body
,
"detail"
),
)
if
providerErr
!=
""
||
providerDesc
!=
""
{
return
providerErr
,
providerDesc
}
values
,
err
:=
url
.
ParseQuery
(
body
)
if
err
!=
nil
{
return
""
,
""
}
providerErr
=
firstNonEmpty
(
values
.
Get
(
"error"
),
values
.
Get
(
"code"
))
providerDesc
=
firstNonEmpty
(
values
.
Get
(
"error_description"
),
values
.
Get
(
"error_message"
),
values
.
Get
(
"message"
))
return
providerErr
,
providerDesc
}
func
parseLinuxDoTokenResponse
(
body
string
)
(
*
linuxDoTokenResponse
,
bool
)
{
body
=
strings
.
TrimSpace
(
body
)
if
body
==
""
{
return
nil
,
false
}
accessToken
:=
strings
.
TrimSpace
(
getGJSON
(
body
,
"access_token"
))
if
accessToken
!=
""
{
tokenType
:=
strings
.
TrimSpace
(
getGJSON
(
body
,
"token_type"
))
refreshToken
:=
strings
.
TrimSpace
(
getGJSON
(
body
,
"refresh_token"
))
scope
:=
strings
.
TrimSpace
(
getGJSON
(
body
,
"scope"
))
expiresIn
:=
gjson
.
Get
(
body
,
"expires_in"
)
.
Int
()
return
&
linuxDoTokenResponse
{
AccessToken
:
accessToken
,
TokenType
:
tokenType
,
ExpiresIn
:
expiresIn
,
RefreshToken
:
refreshToken
,
Scope
:
scope
,
},
true
}
values
,
err
:=
url
.
ParseQuery
(
body
)
if
err
!=
nil
{
return
nil
,
false
}
accessToken
=
strings
.
TrimSpace
(
values
.
Get
(
"access_token"
))
if
accessToken
==
""
{
return
nil
,
false
}
expiresIn
:=
int64
(
0
)
if
raw
:=
strings
.
TrimSpace
(
values
.
Get
(
"expires_in"
));
raw
!=
""
{
if
v
,
err
:=
strconv
.
ParseInt
(
raw
,
10
,
64
);
err
==
nil
{
expiresIn
=
v
}
}
return
&
linuxDoTokenResponse
{
AccessToken
:
accessToken
,
TokenType
:
strings
.
TrimSpace
(
values
.
Get
(
"token_type"
)),
ExpiresIn
:
expiresIn
,
RefreshToken
:
strings
.
TrimSpace
(
values
.
Get
(
"refresh_token"
)),
Scope
:
strings
.
TrimSpace
(
values
.
Get
(
"scope"
)),
},
true
}
func
getGJSON
(
body
string
,
path
string
)
string
{
path
=
strings
.
TrimSpace
(
path
)
if
path
==
""
{
return
""
}
res
:=
gjson
.
Get
(
body
,
path
)
if
!
res
.
Exists
()
{
return
""
}
return
res
.
String
()
}
func
truncateLogValue
(
value
string
,
maxLen
int
)
string
{
value
=
strings
.
TrimSpace
(
value
)
if
value
==
""
||
maxLen
<=
0
{
return
""
}
if
len
(
value
)
<=
maxLen
{
return
value
}
value
=
value
[
:
maxLen
]
for
!
utf8
.
ValidString
(
value
)
{
value
=
value
[
:
len
(
value
)
-
1
]
}
return
value
}
func
singleLine
(
value
string
)
string
{
value
=
strings
.
TrimSpace
(
value
)
if
value
==
""
{
return
""
}
return
strings
.
Join
(
strings
.
Fields
(
value
),
" "
)
}
func
sanitizeFrontendRedirectPath
(
path
string
)
string
{
path
=
strings
.
TrimSpace
(
path
)
if
path
==
""
{
return
""
}
if
len
(
path
)
>
linuxDoOAuthMaxRedirectLen
{
return
""
}
// 只允许同源相对路径(避免开放重定向)。
if
!
strings
.
HasPrefix
(
path
,
"/"
)
{
return
""
}
if
strings
.
HasPrefix
(
path
,
"//"
)
{
return
""
}
if
strings
.
Contains
(
path
,
"://"
)
{
return
""
}
if
strings
.
ContainsAny
(
path
,
"
\r\n
"
)
{
return
""
}
return
path
}
func
isRequestHTTPS
(
c
*
gin
.
Context
)
bool
{
if
c
.
Request
.
TLS
!=
nil
{
return
true
}
proto
:=
strings
.
ToLower
(
strings
.
TrimSpace
(
c
.
GetHeader
(
"X-Forwarded-Proto"
)))
return
proto
==
"https"
}
func
encodeCookieValue
(
value
string
)
string
{
return
base64
.
RawURLEncoding
.
EncodeToString
([]
byte
(
value
))
}
func
decodeCookieValue
(
value
string
)
(
string
,
error
)
{
raw
,
err
:=
base64
.
RawURLEncoding
.
DecodeString
(
value
)
if
err
!=
nil
{
return
""
,
err
}
return
string
(
raw
),
nil
}
func
readCookieDecoded
(
c
*
gin
.
Context
,
name
string
)
(
string
,
error
)
{
ck
,
err
:=
c
.
Request
.
Cookie
(
name
)
if
err
!=
nil
{
return
""
,
err
}
return
decodeCookieValue
(
ck
.
Value
)
}
func
setCookie
(
c
*
gin
.
Context
,
name
string
,
value
string
,
maxAgeSec
int
,
secure
bool
)
{
http
.
SetCookie
(
c
.
Writer
,
&
http
.
Cookie
{
Name
:
name
,
Value
:
value
,
Path
:
linuxDoOAuthCookiePath
,
MaxAge
:
maxAgeSec
,
HttpOnly
:
true
,
Secure
:
secure
,
SameSite
:
http
.
SameSiteLaxMode
,
})
}
func
clearCookie
(
c
*
gin
.
Context
,
name
string
,
secure
bool
)
{
http
.
SetCookie
(
c
.
Writer
,
&
http
.
Cookie
{
Name
:
name
,
Value
:
""
,
Path
:
linuxDoOAuthCookiePath
,
MaxAge
:
-
1
,
HttpOnly
:
true
,
Secure
:
secure
,
SameSite
:
http
.
SameSiteLaxMode
,
})
}
func
truncateFragmentValue
(
value
string
)
string
{
value
=
strings
.
TrimSpace
(
value
)
if
value
==
""
{
return
""
}
if
len
(
value
)
>
linuxDoOAuthMaxFragmentValueLen
{
value
=
value
[
:
linuxDoOAuthMaxFragmentValueLen
]
for
!
utf8
.
ValidString
(
value
)
{
value
=
value
[
:
len
(
value
)
-
1
]
}
}
return
value
}
func
buildBearerAuthorization
(
tokenType
,
accessToken
string
)
(
string
,
error
)
{
tokenType
=
strings
.
TrimSpace
(
tokenType
)
if
tokenType
==
""
{
tokenType
=
"Bearer"
}
if
!
strings
.
EqualFold
(
tokenType
,
"Bearer"
)
{
return
""
,
fmt
.
Errorf
(
"unsupported token_type: %s"
,
tokenType
)
}
accessToken
=
strings
.
TrimSpace
(
accessToken
)
if
accessToken
==
""
{
return
""
,
errors
.
New
(
"missing access_token"
)
}
if
strings
.
ContainsAny
(
accessToken
,
"
\t\r\n
"
)
{
return
""
,
errors
.
New
(
"access_token contains whitespace"
)
}
return
"Bearer "
+
accessToken
,
nil
}
func
isSafeLinuxDoSubject
(
subject
string
)
bool
{
subject
=
strings
.
TrimSpace
(
subject
)
if
subject
==
""
||
len
(
subject
)
>
linuxDoOAuthMaxSubjectLen
{
return
false
}
for
_
,
r
:=
range
subject
{
switch
{
case
r
>=
'0'
&&
r
<=
'9'
:
case
r
>=
'a'
&&
r
<=
'z'
:
case
r
>=
'A'
&&
r
<=
'Z'
:
case
r
==
'_'
||
r
==
'-'
:
default
:
return
false
}
}
return
true
}
func
linuxDoSyntheticEmail
(
subject
string
)
string
{
subject
=
strings
.
TrimSpace
(
subject
)
if
subject
==
""
{
return
""
}
return
"linuxdo-"
+
subject
+
service
.
LinuxDoConnectSyntheticEmailDomain
}
backend/internal/handler/auth_linuxdo_oauth_test.go
0 → 100644
View file @
6b97a8be
package
handler
import
(
"strings"
"testing"
"github.com/Wei-Shaw/sub2api/internal/config"
"github.com/stretchr/testify/require"
)
func
TestSanitizeFrontendRedirectPath
(
t
*
testing
.
T
)
{
require
.
Equal
(
t
,
"/dashboard"
,
sanitizeFrontendRedirectPath
(
"/dashboard"
))
require
.
Equal
(
t
,
"/dashboard"
,
sanitizeFrontendRedirectPath
(
" /dashboard "
))
require
.
Equal
(
t
,
""
,
sanitizeFrontendRedirectPath
(
"dashboard"
))
require
.
Equal
(
t
,
""
,
sanitizeFrontendRedirectPath
(
"//evil.com"
))
require
.
Equal
(
t
,
""
,
sanitizeFrontendRedirectPath
(
"https://evil.com"
))
require
.
Equal
(
t
,
""
,
sanitizeFrontendRedirectPath
(
"/
\n
foo"
))
long
:=
"/"
+
strings
.
Repeat
(
"a"
,
linuxDoOAuthMaxRedirectLen
)
require
.
Equal
(
t
,
""
,
sanitizeFrontendRedirectPath
(
long
))
}
func
TestBuildBearerAuthorization
(
t
*
testing
.
T
)
{
auth
,
err
:=
buildBearerAuthorization
(
""
,
"token123"
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
"Bearer token123"
,
auth
)
auth
,
err
=
buildBearerAuthorization
(
"bearer"
,
"token123"
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
"Bearer token123"
,
auth
)
_
,
err
=
buildBearerAuthorization
(
"MAC"
,
"token123"
)
require
.
Error
(
t
,
err
)
_
,
err
=
buildBearerAuthorization
(
"Bearer"
,
"token 123"
)
require
.
Error
(
t
,
err
)
}
func
TestLinuxDoParseUserInfoParsesIDAndUsername
(
t
*
testing
.
T
)
{
cfg
:=
config
.
LinuxDoConnectConfig
{
UserInfoURL
:
"https://connect.linux.do/api/user"
,
}
email
,
username
,
subject
,
err
:=
linuxDoParseUserInfo
(
`{"id":123,"username":"alice"}`
,
cfg
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
"123"
,
subject
)
require
.
Equal
(
t
,
"alice"
,
username
)
require
.
Equal
(
t
,
"linuxdo-123@linuxdo-connect.invalid"
,
email
)
}
func
TestLinuxDoParseUserInfoDefaultsUsername
(
t
*
testing
.
T
)
{
cfg
:=
config
.
LinuxDoConnectConfig
{
UserInfoURL
:
"https://connect.linux.do/api/user"
,
}
email
,
username
,
subject
,
err
:=
linuxDoParseUserInfo
(
`{"id":"123"}`
,
cfg
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
"123"
,
subject
)
require
.
Equal
(
t
,
"linuxdo_123"
,
username
)
require
.
Equal
(
t
,
"linuxdo-123@linuxdo-connect.invalid"
,
email
)
}
func
TestLinuxDoParseUserInfoRejectsUnsafeSubject
(
t
*
testing
.
T
)
{
cfg
:=
config
.
LinuxDoConnectConfig
{
UserInfoURL
:
"https://connect.linux.do/api/user"
,
}
_
,
_
,
_
,
err
:=
linuxDoParseUserInfo
(
`{"id":"123@456"}`
,
cfg
)
require
.
Error
(
t
,
err
)
tooLong
:=
strings
.
Repeat
(
"a"
,
linuxDoOAuthMaxSubjectLen
+
1
)
_
,
_
,
_
,
err
=
linuxDoParseUserInfo
(
`{"id":"`
+
tooLong
+
`"}`
,
cfg
)
require
.
Error
(
t
,
err
)
}
func
TestParseOAuthProviderErrorJSON
(
t
*
testing
.
T
)
{
code
,
desc
:=
parseOAuthProviderError
(
`{"error":"invalid_client","error_description":"bad secret"}`
)
require
.
Equal
(
t
,
"invalid_client"
,
code
)
require
.
Equal
(
t
,
"bad secret"
,
desc
)
}
func
TestParseOAuthProviderErrorForm
(
t
*
testing
.
T
)
{
code
,
desc
:=
parseOAuthProviderError
(
"error=invalid_request&error_description=Missing+code_verifier"
)
require
.
Equal
(
t
,
"invalid_request"
,
code
)
require
.
Equal
(
t
,
"Missing code_verifier"
,
desc
)
}
func
TestParseLinuxDoTokenResponseJSON
(
t
*
testing
.
T
)
{
token
,
ok
:=
parseLinuxDoTokenResponse
(
`{"access_token":"t1","token_type":"Bearer","expires_in":3600,"scope":"user"}`
)
require
.
True
(
t
,
ok
)
require
.
Equal
(
t
,
"t1"
,
token
.
AccessToken
)
require
.
Equal
(
t
,
"Bearer"
,
token
.
TokenType
)
require
.
Equal
(
t
,
int64
(
3600
),
token
.
ExpiresIn
)
require
.
Equal
(
t
,
"user"
,
token
.
Scope
)
}
func
TestParseLinuxDoTokenResponseForm
(
t
*
testing
.
T
)
{
token
,
ok
:=
parseLinuxDoTokenResponse
(
"access_token=t2&token_type=bearer&expires_in=60"
)
require
.
True
(
t
,
ok
)
require
.
Equal
(
t
,
"t2"
,
token
.
AccessToken
)
require
.
Equal
(
t
,
"bearer"
,
token
.
TokenType
)
require
.
Equal
(
t
,
int64
(
60
),
token
.
ExpiresIn
)
}
func
TestSingleLineStripsWhitespace
(
t
*
testing
.
T
)
{
require
.
Equal
(
t
,
"hello world"
,
singleLine
(
"hello
\r\n
world"
))
require
.
Equal
(
t
,
""
,
singleLine
(
"
\n\t\r
"
))
}
backend/internal/handler/dto/settings.go
View file @
6b97a8be
...
@@ -17,6 +17,11 @@ type SystemSettings struct {
...
@@ -17,6 +17,11 @@ type SystemSettings struct {
TurnstileSiteKey
string
`json:"turnstile_site_key"`
TurnstileSiteKey
string
`json:"turnstile_site_key"`
TurnstileSecretKeyConfigured
bool
`json:"turnstile_secret_key_configured"`
TurnstileSecretKeyConfigured
bool
`json:"turnstile_secret_key_configured"`
LinuxDoConnectEnabled
bool
`json:"linuxdo_connect_enabled"`
LinuxDoConnectClientID
string
`json:"linuxdo_connect_client_id"`
LinuxDoConnectClientSecretConfigured
bool
`json:"linuxdo_connect_client_secret_configured"`
LinuxDoConnectRedirectURL
string
`json:"linuxdo_connect_redirect_url"`
SiteName
string
`json:"site_name"`
SiteName
string
`json:"site_name"`
SiteLogo
string
`json:"site_logo"`
SiteLogo
string
`json:"site_logo"`
SiteSubtitle
string
`json:"site_subtitle"`
SiteSubtitle
string
`json:"site_subtitle"`
...
@@ -50,5 +55,6 @@ type PublicSettings struct {
...
@@ -50,5 +55,6 @@ type PublicSettings struct {
APIBaseURL
string
`json:"api_base_url"`
APIBaseURL
string
`json:"api_base_url"`
ContactInfo
string
`json:"contact_info"`
ContactInfo
string
`json:"contact_info"`
DocURL
string
`json:"doc_url"`
DocURL
string
`json:"doc_url"`
LinuxDoOAuthEnabled
bool
`json:"linuxdo_oauth_enabled"`
Version
string
`json:"version"`
Version
string
`json:"version"`
}
}
backend/internal/handler/setting_handler.go
View file @
6b97a8be
...
@@ -42,6 +42,7 @@ func (h *SettingHandler) GetPublicSettings(c *gin.Context) {
...
@@ -42,6 +42,7 @@ func (h *SettingHandler) GetPublicSettings(c *gin.Context) {
APIBaseURL
:
settings
.
APIBaseURL
,
APIBaseURL
:
settings
.
APIBaseURL
,
ContactInfo
:
settings
.
ContactInfo
,
ContactInfo
:
settings
.
ContactInfo
,
DocURL
:
settings
.
DocURL
,
DocURL
:
settings
.
DocURL
,
LinuxDoOAuthEnabled
:
settings
.
LinuxDoOAuthEnabled
,
Version
:
h
.
version
,
Version
:
h
.
version
,
})
})
}
}
backend/internal/pkg/antigravity/client.go
View file @
6b97a8be
...
@@ -5,8 +5,11 @@ import (
...
@@ -5,8 +5,11 @@ import (
"bytes"
"bytes"
"context"
"context"
"encoding/json"
"encoding/json"
"errors"
"fmt"
"fmt"
"io"
"io"
"log"
"net"
"net/http"
"net/http"
"net/url"
"net/url"
"strings"
"strings"
...
@@ -22,10 +25,10 @@ func resolveHost(urlStr string) string {
...
@@ -22,10 +25,10 @@ func resolveHost(urlStr string) string {
return
parsed
.
Host
return
parsed
.
Host
}
}
// NewAPIRequest 创建 Antigravity API 请求(v1internal 端点)
// NewAPIRequest
WithURL 使用指定的 base URL
创建 Antigravity API 请求(v1internal 端点)
func
NewAPIRequest
(
ctx
context
.
Context
,
action
,
accessToken
string
,
body
[]
byte
)
(
*
http
.
Request
,
error
)
{
func
NewAPIRequest
WithURL
(
ctx
context
.
Context
,
baseURL
,
action
,
accessToken
string
,
body
[]
byte
)
(
*
http
.
Request
,
error
)
{
// 构建 URL,流式请求添加 ?alt=sse 参数
// 构建 URL,流式请求添加 ?alt=sse 参数
apiURL
:=
fmt
.
Sprintf
(
"%s/v1internal:%s"
,
B
aseURL
,
action
)
apiURL
:=
fmt
.
Sprintf
(
"%s/v1internal:%s"
,
b
aseURL
,
action
)
isStream
:=
action
==
"streamGenerateContent"
isStream
:=
action
==
"streamGenerateContent"
if
isStream
{
if
isStream
{
apiURL
+=
"?alt=sse"
apiURL
+=
"?alt=sse"
...
@@ -53,11 +56,15 @@ func NewAPIRequest(ctx context.Context, action, accessToken string, body []byte)
...
@@ -53,11 +56,15 @@ func NewAPIRequest(ctx context.Context, action, accessToken string, body []byte)
req
.
Host
=
host
req
.
Host
=
host
}
}
// 注意:requestType 已在 JSON body 的 V1InternalRequest 中设置,不需要 HTTP Header
return
req
,
nil
return
req
,
nil
}
}
// NewAPIRequest 使用默认 URL 创建 Antigravity API 请求(v1internal 端点)
// 向后兼容:仅使用默认 BaseURL
func
NewAPIRequest
(
ctx
context
.
Context
,
action
,
accessToken
string
,
body
[]
byte
)
(
*
http
.
Request
,
error
)
{
return
NewAPIRequestWithURL
(
ctx
,
BaseURL
,
action
,
accessToken
,
body
)
}
// TokenResponse Google OAuth token 响应
// TokenResponse Google OAuth token 响应
type
TokenResponse
struct
{
type
TokenResponse
struct
{
AccessToken
string
`json:"access_token"`
AccessToken
string
`json:"access_token"`
...
@@ -164,6 +171,38 @@ func NewClient(proxyURL string) *Client {
...
@@ -164,6 +171,38 @@ func NewClient(proxyURL string) *Client {
}
}
}
}
// isConnectionError 判断是否为连接错误(网络超时、DNS 失败、连接拒绝)
func
isConnectionError
(
err
error
)
bool
{
if
err
==
nil
{
return
false
}
// 检查超时错误
var
netErr
net
.
Error
if
errors
.
As
(
err
,
&
netErr
)
&&
netErr
.
Timeout
()
{
return
true
}
// 检查连接错误(DNS 失败、连接拒绝)
var
opErr
*
net
.
OpError
if
errors
.
As
(
err
,
&
opErr
)
{
return
true
}
// 检查 URL 错误
var
urlErr
*
url
.
Error
return
errors
.
As
(
err
,
&
urlErr
)
}
// shouldFallbackToNextURL 判断是否应切换到下一个 URL
// 仅连接错误和 HTTP 429 触发 URL 降级
func
shouldFallbackToNextURL
(
err
error
,
statusCode
int
)
bool
{
if
isConnectionError
(
err
)
{
return
true
}
return
statusCode
==
http
.
StatusTooManyRequests
}
// ExchangeCode 用 authorization code 交换 token
// ExchangeCode 用 authorization code 交换 token
func
(
c
*
Client
)
ExchangeCode
(
ctx
context
.
Context
,
code
,
codeVerifier
string
)
(
*
TokenResponse
,
error
)
{
func
(
c
*
Client
)
ExchangeCode
(
ctx
context
.
Context
,
code
,
codeVerifier
string
)
(
*
TokenResponse
,
error
)
{
params
:=
url
.
Values
{}
params
:=
url
.
Values
{}
...
@@ -272,6 +311,7 @@ func (c *Client) GetUserInfo(ctx context.Context, accessToken string) (*UserInfo
...
@@ -272,6 +311,7 @@ func (c *Client) GetUserInfo(ctx context.Context, accessToken string) (*UserInfo
}
}
// LoadCodeAssist 获取账户信息,返回解析后的结构体和原始 JSON
// LoadCodeAssist 获取账户信息,返回解析后的结构体和原始 JSON
// 支持 URL fallback:sandbox → daily → prod
func
(
c
*
Client
)
LoadCodeAssist
(
ctx
context
.
Context
,
accessToken
string
)
(
*
LoadCodeAssistResponse
,
map
[
string
]
any
,
error
)
{
func
(
c
*
Client
)
LoadCodeAssist
(
ctx
context
.
Context
,
accessToken
string
)
(
*
LoadCodeAssistResponse
,
map
[
string
]
any
,
error
)
{
reqBody
:=
LoadCodeAssistRequest
{}
reqBody
:=
LoadCodeAssistRequest
{}
reqBody
.
Metadata
.
IDEType
=
"ANTIGRAVITY"
reqBody
.
Metadata
.
IDEType
=
"ANTIGRAVITY"
...
@@ -281,40 +321,65 @@ func (c *Client) LoadCodeAssist(ctx context.Context, accessToken string) (*LoadC
...
@@ -281,40 +321,65 @@ func (c *Client) LoadCodeAssist(ctx context.Context, accessToken string) (*LoadC
return
nil
,
nil
,
fmt
.
Errorf
(
"序列化请求失败: %w"
,
err
)
return
nil
,
nil
,
fmt
.
Errorf
(
"序列化请求失败: %w"
,
err
)
}
}
url
:=
BaseURL
+
"/v1internal:loadCodeAssist"
// 获取可用的 URL 列表
req
,
err
:=
http
.
NewRequestWithContext
(
ctx
,
http
.
MethodPost
,
url
,
strings
.
NewReader
(
string
(
bodyBytes
))
)
availableURLs
:=
DefaultURLAvailability
.
GetAvailableURLs
(
)
if
err
!=
nil
{
if
len
(
availableURLs
)
==
0
{
return
nil
,
nil
,
fmt
.
Errorf
(
"创建请求失败: %w"
,
err
)
availableURLs
=
BaseURLs
// 所有 URL 都不可用时,重试所有
}
}
req
.
Header
.
Set
(
"Authorization"
,
"Bearer "
+
accessToken
)
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
req
.
Header
.
Set
(
"User-Agent"
,
UserAgent
)
resp
,
err
:=
c
.
httpClient
.
Do
(
req
)
var
lastErr
error
if
err
!=
nil
{
for
urlIdx
,
baseURL
:=
range
availableURLs
{
return
nil
,
nil
,
fmt
.
Errorf
(
"loadCodeAssist 请求失败: %w"
,
err
)
apiURL
:=
baseURL
+
"/v1internal:loadCodeAssist"
}
req
,
err
:=
http
.
NewRequestWithContext
(
ctx
,
http
.
MethodPost
,
apiURL
,
strings
.
NewReader
(
string
(
bodyBytes
)))
defer
func
()
{
_
=
resp
.
Body
.
Close
()
}()
if
err
!=
nil
{
lastErr
=
fmt
.
Errorf
(
"创建请求失败: %w"
,
err
)
continue
}
req
.
Header
.
Set
(
"Authorization"
,
"Bearer "
+
accessToken
)
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
req
.
Header
.
Set
(
"User-Agent"
,
UserAgent
)
resp
,
err
:=
c
.
httpClient
.
Do
(
req
)
if
err
!=
nil
{
lastErr
=
fmt
.
Errorf
(
"loadCodeAssist 请求失败: %w"
,
err
)
if
shouldFallbackToNextURL
(
err
,
0
)
&&
urlIdx
<
len
(
availableURLs
)
-
1
{
DefaultURLAvailability
.
MarkUnavailable
(
baseURL
)
log
.
Printf
(
"[antigravity] loadCodeAssist URL fallback: %s -> %s"
,
baseURL
,
availableURLs
[
urlIdx
+
1
])
continue
}
return
nil
,
nil
,
lastErr
}
respBodyBytes
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
respBodyBytes
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
_
=
resp
.
Body
.
Close
()
// 立即关闭,避免循环内 defer 导致的资源泄漏
return
nil
,
nil
,
fmt
.
Errorf
(
"读取响应失败: %w"
,
err
)
if
err
!=
nil
{
}
return
nil
,
nil
,
fmt
.
Errorf
(
"读取响应失败: %w"
,
err
)
}
if
resp
.
StatusCode
!=
http
.
StatusOK
{
// 检查是否需要 URL 降级
return
nil
,
nil
,
fmt
.
Errorf
(
"loadCodeAssist 失败 (HTTP %d): %s"
,
resp
.
StatusCode
,
string
(
respBodyBytes
))
if
shouldFallbackToNextURL
(
nil
,
resp
.
StatusCode
)
&&
urlIdx
<
len
(
availableURLs
)
-
1
{
}
DefaultURLAvailability
.
MarkUnavailable
(
baseURL
)
log
.
Printf
(
"[antigravity] loadCodeAssist URL fallback (HTTP %d): %s -> %s"
,
resp
.
StatusCode
,
baseURL
,
availableURLs
[
urlIdx
+
1
])
continue
}
var
loadResp
LoadCodeAssistResponse
if
resp
.
StatusCode
!=
http
.
StatusOK
{
if
err
:=
json
.
Unmarshal
(
respBodyBytes
,
&
loadResp
);
err
!=
nil
{
return
nil
,
nil
,
fmt
.
Errorf
(
"loadCodeAssist 失败 (HTTP %d): %s"
,
resp
.
StatusCode
,
string
(
respBodyBytes
))
return
nil
,
nil
,
fmt
.
Errorf
(
"响应解析失败: %w"
,
err
)
}
}
// 解析原始 JSON 为 map
var
loadResp
LoadCodeAssistResponse
var
rawResp
map
[
string
]
any
if
err
:=
json
.
Unmarshal
(
respBodyBytes
,
&
loadResp
);
err
!=
nil
{
_
=
json
.
Unmarshal
(
respBodyBytes
,
&
rawResp
)
return
nil
,
nil
,
fmt
.
Errorf
(
"响应解析失败: %w"
,
err
)
}
// 解析原始 JSON 为 map
var
rawResp
map
[
string
]
any
_
=
json
.
Unmarshal
(
respBodyBytes
,
&
rawResp
)
return
&
loadResp
,
rawResp
,
nil
}
return
&
loadResp
,
rawResp
,
nil
return
nil
,
nil
,
lastErr
}
}
// ModelQuotaInfo 模型配额信息
// ModelQuotaInfo 模型配额信息
...
@@ -339,6 +404,7 @@ type FetchAvailableModelsResponse struct {
...
@@ -339,6 +404,7 @@ type FetchAvailableModelsResponse struct {
}
}
// FetchAvailableModels 获取可用模型和配额信息,返回解析后的结构体和原始 JSON
// FetchAvailableModels 获取可用模型和配额信息,返回解析后的结构体和原始 JSON
// 支持 URL fallback:sandbox → daily → prod
func
(
c
*
Client
)
FetchAvailableModels
(
ctx
context
.
Context
,
accessToken
,
projectID
string
)
(
*
FetchAvailableModelsResponse
,
map
[
string
]
any
,
error
)
{
func
(
c
*
Client
)
FetchAvailableModels
(
ctx
context
.
Context
,
accessToken
,
projectID
string
)
(
*
FetchAvailableModelsResponse
,
map
[
string
]
any
,
error
)
{
reqBody
:=
FetchAvailableModelsRequest
{
Project
:
projectID
}
reqBody
:=
FetchAvailableModelsRequest
{
Project
:
projectID
}
bodyBytes
,
err
:=
json
.
Marshal
(
reqBody
)
bodyBytes
,
err
:=
json
.
Marshal
(
reqBody
)
...
@@ -346,38 +412,63 @@ func (c *Client) FetchAvailableModels(ctx context.Context, accessToken, projectI
...
@@ -346,38 +412,63 @@ func (c *Client) FetchAvailableModels(ctx context.Context, accessToken, projectI
return
nil
,
nil
,
fmt
.
Errorf
(
"序列化请求失败: %w"
,
err
)
return
nil
,
nil
,
fmt
.
Errorf
(
"序列化请求失败: %w"
,
err
)
}
}
apiURL
:=
BaseURL
+
"/v1internal:fetchAvailableModels"
// 获取可用的 URL 列表
req
,
err
:=
http
.
NewRequestWithContext
(
ctx
,
http
.
MethodPost
,
apiURL
,
strings
.
NewReader
(
string
(
bodyBytes
))
)
availableURLs
:=
DefaultURLAvailability
.
GetAvailableURLs
(
)
if
err
!=
nil
{
if
len
(
availableURLs
)
==
0
{
return
nil
,
nil
,
fmt
.
Errorf
(
"创建请求失败: %w"
,
err
)
availableURLs
=
BaseURLs
// 所有 URL 都不可用时,重试所有
}
}
req
.
Header
.
Set
(
"Authorization"
,
"Bearer "
+
accessToken
)
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
req
.
Header
.
Set
(
"User-Agent"
,
UserAgent
)
resp
,
err
:=
c
.
httpClient
.
Do
(
req
)
var
lastErr
error
if
err
!=
nil
{
for
urlIdx
,
baseURL
:=
range
availableURLs
{
return
nil
,
nil
,
fmt
.
Errorf
(
"fetchAvailableModels 请求失败: %w"
,
err
)
apiURL
:=
baseURL
+
"/v1internal:fetchAvailableModels"
}
req
,
err
:=
http
.
NewRequestWithContext
(
ctx
,
http
.
MethodPost
,
apiURL
,
strings
.
NewReader
(
string
(
bodyBytes
)))
defer
func
()
{
_
=
resp
.
Body
.
Close
()
}()
if
err
!=
nil
{
lastErr
=
fmt
.
Errorf
(
"创建请求失败: %w"
,
err
)
continue
}
req
.
Header
.
Set
(
"Authorization"
,
"Bearer "
+
accessToken
)
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
req
.
Header
.
Set
(
"User-Agent"
,
UserAgent
)
resp
,
err
:=
c
.
httpClient
.
Do
(
req
)
if
err
!=
nil
{
lastErr
=
fmt
.
Errorf
(
"fetchAvailableModels 请求失败: %w"
,
err
)
if
shouldFallbackToNextURL
(
err
,
0
)
&&
urlIdx
<
len
(
availableURLs
)
-
1
{
DefaultURLAvailability
.
MarkUnavailable
(
baseURL
)
log
.
Printf
(
"[antigravity] fetchAvailableModels URL fallback: %s -> %s"
,
baseURL
,
availableURLs
[
urlIdx
+
1
])
continue
}
return
nil
,
nil
,
lastErr
}
respBodyBytes
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
respBodyBytes
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
_
=
resp
.
Body
.
Close
()
// 立即关闭,避免循环内 defer 导致的资源泄漏
return
nil
,
nil
,
fmt
.
Errorf
(
"读取响应失败: %w"
,
err
)
if
err
!=
nil
{
}
return
nil
,
nil
,
fmt
.
Errorf
(
"读取响应失败: %w"
,
err
)
}
if
resp
.
StatusCode
!=
http
.
StatusOK
{
// 检查是否需要 URL 降级
return
nil
,
nil
,
fmt
.
Errorf
(
"fetchAvailableModels 失败 (HTTP %d): %s"
,
resp
.
StatusCode
,
string
(
respBodyBytes
))
if
shouldFallbackToNextURL
(
nil
,
resp
.
StatusCode
)
&&
urlIdx
<
len
(
availableURLs
)
-
1
{
}
DefaultURLAvailability
.
MarkUnavailable
(
baseURL
)
log
.
Printf
(
"[antigravity] fetchAvailableModels URL fallback (HTTP %d): %s -> %s"
,
resp
.
StatusCode
,
baseURL
,
availableURLs
[
urlIdx
+
1
])
continue
}
var
modelsResp
FetchAvailableModelsResponse
if
resp
.
StatusCode
!=
http
.
StatusOK
{
if
err
:=
json
.
Unmarshal
(
respBodyBytes
,
&
modelsResp
);
err
!=
nil
{
return
nil
,
nil
,
fmt
.
Errorf
(
"fetchAvailableModels 失败 (HTTP %d): %s"
,
resp
.
StatusCode
,
string
(
respBodyBytes
))
return
nil
,
nil
,
fmt
.
Errorf
(
"响应解析失败: %w"
,
err
)
}
}
// 解析原始 JSON 为 map
var
modelsResp
FetchAvailableModelsResponse
var
rawResp
map
[
string
]
any
if
err
:=
json
.
Unmarshal
(
respBodyBytes
,
&
modelsResp
);
err
!=
nil
{
_
=
json
.
Unmarshal
(
respBodyBytes
,
&
rawResp
)
return
nil
,
nil
,
fmt
.
Errorf
(
"响应解析失败: %w"
,
err
)
}
// 解析原始 JSON 为 map
var
rawResp
map
[
string
]
any
_
=
json
.
Unmarshal
(
respBodyBytes
,
&
rawResp
)
return
&
modelsResp
,
rawResp
,
nil
}
return
&
modelsResp
,
rawResp
,
nil
return
nil
,
nil
,
lastErr
}
}
backend/internal/pkg/antigravity/oauth.go
View file @
6b97a8be
...
@@ -32,17 +32,79 @@ const (
...
@@ -32,17 +32,79 @@ const (
"https://www.googleapis.com/auth/cclog "
+
"https://www.googleapis.com/auth/cclog "
+
"https://www.googleapis.com/auth/experimentsandconfigs"
"https://www.googleapis.com/auth/experimentsandconfigs"
// API 端点
// 优先使用 sandbox daily URL,配额更宽松
BaseURL
=
"https://daily-cloudcode-pa.sandbox.googleapis.com"
// User-Agent(模拟官方客户端)
// User-Agent(模拟官方客户端)
UserAgent
=
"antigravity/1.104.0 darwin/arm64"
UserAgent
=
"antigravity/1.104.0 darwin/arm64"
// Session 过期时间
// Session 过期时间
SessionTTL
=
30
*
time
.
Minute
SessionTTL
=
30
*
time
.
Minute
// URL 可用性 TTL(不可用 URL 的恢复时间)
URLAvailabilityTTL
=
5
*
time
.
Minute
)
)
// BaseURLs 定义 Antigravity API 端点,按优先级排序
// fallback 顺序: sandbox → daily → prod
var
BaseURLs
=
[]
string
{
"https://daily-cloudcode-pa.sandbox.googleapis.com"
,
// sandbox
"https://daily-cloudcode-pa.googleapis.com"
,
// daily
"https://cloudcode-pa.googleapis.com"
,
// prod
}
// BaseURL 默认 URL(保持向后兼容)
var
BaseURL
=
BaseURLs
[
0
]
// URLAvailability 管理 URL 可用性状态(带 TTL 自动恢复)
type
URLAvailability
struct
{
mu
sync
.
RWMutex
unavailable
map
[
string
]
time
.
Time
// URL -> 恢复时间
ttl
time
.
Duration
}
// DefaultURLAvailability 全局 URL 可用性管理器
var
DefaultURLAvailability
=
NewURLAvailability
(
URLAvailabilityTTL
)
// NewURLAvailability 创建 URL 可用性管理器
func
NewURLAvailability
(
ttl
time
.
Duration
)
*
URLAvailability
{
return
&
URLAvailability
{
unavailable
:
make
(
map
[
string
]
time
.
Time
),
ttl
:
ttl
,
}
}
// MarkUnavailable 标记 URL 临时不可用
func
(
u
*
URLAvailability
)
MarkUnavailable
(
url
string
)
{
u
.
mu
.
Lock
()
defer
u
.
mu
.
Unlock
()
u
.
unavailable
[
url
]
=
time
.
Now
()
.
Add
(
u
.
ttl
)
}
// IsAvailable 检查 URL 是否可用
func
(
u
*
URLAvailability
)
IsAvailable
(
url
string
)
bool
{
u
.
mu
.
RLock
()
defer
u
.
mu
.
RUnlock
()
expiry
,
exists
:=
u
.
unavailable
[
url
]
if
!
exists
{
return
true
}
return
time
.
Now
()
.
After
(
expiry
)
}
// GetAvailableURLs 返回可用的 URL 列表(保持优先级顺序)
func
(
u
*
URLAvailability
)
GetAvailableURLs
()
[]
string
{
u
.
mu
.
RLock
()
defer
u
.
mu
.
RUnlock
()
now
:=
time
.
Now
()
result
:=
make
([]
string
,
0
,
len
(
BaseURLs
))
for
_
,
url
:=
range
BaseURLs
{
expiry
,
exists
:=
u
.
unavailable
[
url
]
if
!
exists
||
now
.
After
(
expiry
)
{
result
=
append
(
result
,
url
)
}
}
return
result
}
// OAuthSession 保存 OAuth 授权流程的临时状态
// OAuthSession 保存 OAuth 授权流程的临时状态
type
OAuthSession
struct
{
type
OAuthSession
struct
{
State
string
`json:"state"`
State
string
`json:"state"`
...
...
backend/internal/pkg/geminicli/constants.go
View file @
6b97a8be
...
@@ -27,10 +27,9 @@ const (
...
@@ -27,10 +27,9 @@ const (
// https://www.googleapis.com/auth/generative-language.retriever (often with cloud-platform).
// https://www.googleapis.com/auth/generative-language.retriever (often with cloud-platform).
DefaultAIStudioScopes
=
"https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/generative-language.retriever"
DefaultAIStudioScopes
=
"https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/generative-language.retriever"
// DefaultScopes for Google One (personal Google accounts with Gemini access)
// DefaultGoogleOneScopes (DEPRECATED, no longer used)
// Only used when a custom OAuth client is configured. When using the built-in Gemini CLI client,
// Google One now always uses the built-in Gemini CLI client with DefaultCodeAssistScopes.
// Google One uses DefaultCodeAssistScopes (same as code_assist) because the built-in client
// This constant is kept for backward compatibility but is not actively used.
// cannot request restricted scopes like generative-language.retriever or drive.readonly.
DefaultGoogleOneScopes
=
"https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/generative-language.retriever https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
DefaultGoogleOneScopes
=
"https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/generative-language.retriever https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
// GeminiCLIRedirectURI is the redirect URI used by Gemini CLI for Code Assist OAuth.
// GeminiCLIRedirectURI is the redirect URI used by Gemini CLI for Code Assist OAuth.
...
...
backend/internal/pkg/geminicli/oauth.go
View file @
6b97a8be
...
@@ -185,13 +185,9 @@ func EffectiveOAuthConfig(cfg OAuthConfig, oauthType string) (OAuthConfig, error
...
@@ -185,13 +185,9 @@ func EffectiveOAuthConfig(cfg OAuthConfig, oauthType string) (OAuthConfig, error
effective
.
Scopes
=
DefaultAIStudioScopes
effective
.
Scopes
=
DefaultAIStudioScopes
}
}
case
"google_one"
:
case
"google_one"
:
// Google One uses built-in Gemini CLI client (same as code_assist)
// Google One always uses built-in Gemini CLI client (same as code_assist)
// Built-in client can't request restricted scopes like generative-language.retriever
// Built-in client can't request restricted scopes like generative-language.retriever or drive.readonly
if
isBuiltinClient
{
effective
.
Scopes
=
DefaultCodeAssistScopes
effective
.
Scopes
=
DefaultCodeAssistScopes
}
else
{
effective
.
Scopes
=
DefaultGoogleOneScopes
}
default
:
default
:
// Default to Code Assist scopes
// Default to Code Assist scopes
effective
.
Scopes
=
DefaultCodeAssistScopes
effective
.
Scopes
=
DefaultCodeAssistScopes
...
...
Prev
1
2
3
4
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment