mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-18 01:26:37 +08:00
214 lines
6.2 KiB
Go
214 lines
6.2 KiB
Go
package controller
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"github.com/songquanpeng/one-api/common"
|
||
"github.com/songquanpeng/one-api/common/config"
|
||
"github.com/songquanpeng/one-api/common/message"
|
||
"github.com/songquanpeng/one-api/model"
|
||
"net/http"
|
||
"strings"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
func GetStatus(c *gin.Context) {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"success": true,
|
||
"message": "",
|
||
"data": gin.H{
|
||
"version": common.Version,
|
||
"start_time": common.StartTime,
|
||
"email_verification": config.EmailVerificationEnabled,
|
||
"github_oauth": config.GitHubOAuthEnabled,
|
||
"github_client_id": config.GitHubClientId,
|
||
"lark_client_id": config.LarkClientId,
|
||
"system_name": config.SystemName,
|
||
"logo": config.Logo,
|
||
"footer_html": config.Footer,
|
||
"wechat_qrcode": config.WeChatAccountQRCodeImageURL,
|
||
"wechat_login": config.WeChatAuthEnabled,
|
||
"server_address": config.ServerAddress,
|
||
"turnstile_check": config.TurnstileCheckEnabled,
|
||
"turnstile_site_key": config.TurnstileSiteKey,
|
||
"top_up_link": config.TopUpLink,
|
||
"chat_link": config.ChatLink,
|
||
"quota_per_unit": config.QuotaPerUnit,
|
||
"display_in_currency": config.DisplayInCurrencyEnabled,
|
||
"oidc": config.OidcEnabled,
|
||
"oidc_client_id": config.OidcClientId,
|
||
"oidc_well_known": config.OidcWellKnown,
|
||
"oidc_authorization_endpoint": config.OidcAuthorizationEndpoint,
|
||
"oidc_token_endpoint": config.OidcTokenEndpoint,
|
||
"oidc_userinfo_endpoint": config.OidcUserinfoEndpoint,
|
||
},
|
||
})
|
||
return
|
||
}
|
||
|
||
func GetNotice(c *gin.Context) {
|
||
config.OptionMapRWMutex.RLock()
|
||
defer config.OptionMapRWMutex.RUnlock()
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"success": true,
|
||
"message": "",
|
||
"data": config.OptionMap["Notice"],
|
||
})
|
||
return
|
||
}
|
||
|
||
func GetAbout(c *gin.Context) {
|
||
config.OptionMapRWMutex.RLock()
|
||
defer config.OptionMapRWMutex.RUnlock()
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"success": true,
|
||
"message": "",
|
||
"data": config.OptionMap["About"],
|
||
})
|
||
return
|
||
}
|
||
|
||
func GetHomePageContent(c *gin.Context) {
|
||
config.OptionMapRWMutex.RLock()
|
||
defer config.OptionMapRWMutex.RUnlock()
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"success": true,
|
||
"message": "",
|
||
"data": config.OptionMap["HomePageContent"],
|
||
})
|
||
return
|
||
}
|
||
|
||
func SendEmailVerification(c *gin.Context) {
|
||
email := c.Query("email")
|
||
if err := common.Validate.Var(email, "required,email"); err != nil {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"success": false,
|
||
"message": "Invalid parameter",
|
||
})
|
||
return
|
||
}
|
||
if config.EmailDomainRestrictionEnabled {
|
||
allowed := false
|
||
for _, domain := range config.EmailDomainWhitelist {
|
||
if strings.HasSuffix(email, "@"+domain) {
|
||
allowed = true
|
||
break
|
||
}
|
||
}
|
||
if !allowed {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"success": false,
|
||
"message": "AdministratorEnable了邮箱域名白名单,您的Email address的域名不在白名单中",
|
||
})
|
||
return
|
||
}
|
||
}
|
||
if model.IsEmailAlreadyTaken(email) {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"success": false,
|
||
"message": "Email address is occupied",
|
||
})
|
||
return
|
||
}
|
||
code := common.GenerateVerificationCode(6)
|
||
common.RegisterVerificationCodeWithKey(email, code, common.EmailVerificationPurpose)
|
||
subject := fmt.Sprintf("%s Email verification email", config.SystemName)
|
||
content := fmt.Sprintf("<p>Hello, you are verifying %s email.</p>"+
|
||
"<p>Your verification code is: <strong>%s</strong></p>"+
|
||
"<p>The verification code is valid within %d minutes. If it is not your operation, please ignore it.</p>", config.SystemName, code, common.VerificationValidMinutes)
|
||
err := message.SendEmail(subject, email, content)
|
||
if err != nil {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"success": false,
|
||
"message": err.Error(),
|
||
})
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"success": true,
|
||
"message": "",
|
||
})
|
||
return
|
||
}
|
||
|
||
func SendPasswordResetEmail(c *gin.Context) {
|
||
email := c.Query("email")
|
||
if err := common.Validate.Var(email, "required,email"); err != nil {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"success": false,
|
||
"message": "Invalid parameter",
|
||
})
|
||
return
|
||
}
|
||
if !model.IsEmailAlreadyTaken(email) {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"success": false,
|
||
"message": "The email address is not registered",
|
||
})
|
||
return
|
||
}
|
||
code := common.GenerateVerificationCode(0)
|
||
common.RegisterVerificationCodeWithKey(email, code, common.PasswordResetPurpose)
|
||
link := fmt.Sprintf("%s/user/reset?email=%s&token=%s", config.ServerAddress, email, code)
|
||
subject := fmt.Sprintf("%s Password reset", config.SystemName)
|
||
content := fmt.Sprintf("<p>Hello, you are resetting %s password.</p>"+
|
||
"<p>点击 <a href='%s'>此处</a> 进行Password reset。</p>"+
|
||
"<p>如果链接None法点击,请尝试点击下面的链接或将其Copy到浏览器中打开:<br> %s </p>"+
|
||
"<p>The reset link is valid within %d minutes. If it is not your operation, please ignore it.</p>", config.SystemName, link, link, common.VerificationValidMinutes)
|
||
err := message.SendEmail(subject, email, content)
|
||
if err != nil {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"success": false,
|
||
"message": err.Error(),
|
||
})
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"success": true,
|
||
"message": "",
|
||
})
|
||
return
|
||
}
|
||
|
||
type PasswordResetRequest struct {
|
||
Email string `json:"email"`
|
||
Token string `json:"token"`
|
||
}
|
||
|
||
func ResetPassword(c *gin.Context) {
|
||
var req PasswordResetRequest
|
||
err := json.NewDecoder(c.Request.Body).Decode(&req)
|
||
if req.Email == "" || req.Token == "" {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"success": false,
|
||
"message": "Invalid parameter",
|
||
})
|
||
return
|
||
}
|
||
if !common.VerifyCodeWithKey(req.Email, req.Token, common.PasswordResetPurpose) {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"success": false,
|
||
"message": "Reset link is illegal or expired",
|
||
})
|
||
return
|
||
}
|
||
password := common.GenerateVerificationCode(12)
|
||
err = model.ResetUserPasswordByEmail(req.Email, password)
|
||
if err != nil {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"success": false,
|
||
"message": err.Error(),
|
||
})
|
||
return
|
||
}
|
||
common.DeleteKey(req.Email, common.PasswordResetPurpose)
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"success": true,
|
||
"message": "",
|
||
"data": password,
|
||
})
|
||
return
|
||
}
|