alist/server/handles/auth.go

167 lines
3.7 KiB
Go
Raw Normal View History

2022-07-11 09:12:50 +00:00
package handles
2022-06-25 13:34:44 +00:00
import (
2022-08-05 17:22:13 +00:00
"bytes"
"encoding/base64"
"image/png"
2022-06-28 10:12:53 +00:00
"time"
2022-06-25 13:34:44 +00:00
"github.com/Xhofe/go-cache"
2022-06-26 08:39:02 +00:00
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
2022-06-26 11:20:19 +00:00
"github.com/alist-org/alist/v3/server/common"
2022-06-25 13:34:44 +00:00
"github.com/gin-gonic/gin"
2022-08-05 17:22:13 +00:00
"github.com/pquerna/otp/totp"
2022-06-25 13:34:44 +00:00
)
var loginCache = cache.NewMemCache[int]()
var (
defaultDuration = time.Minute * 5
defaultTimes = 5
)
type LoginReq struct {
2022-07-23 12:42:12 +00:00
Username string `json:"username" binding:"required"`
Password string `json:"password"`
2022-08-06 09:21:32 +00:00
OtpCode string `json:"otp_code"`
2022-06-25 13:34:44 +00:00
}
func Login(c *gin.Context) {
// check count of login
ip := c.ClientIP()
count, ok := loginCache.Get(ip)
2022-06-26 08:39:02 +00:00
if ok && count >= defaultTimes {
2022-08-05 17:22:13 +00:00
common.ErrorStrResp(c, "Too many unsuccessful sign-in attempts have been made using an incorrect username or password, Try again later.", 429)
2022-06-25 13:34:44 +00:00
loginCache.Expire(ip, defaultDuration)
return
}
// check username
var req LoginReq
if err := c.ShouldBind(&req); err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 400)
2022-06-25 13:34:44 +00:00
return
}
user, err := op.GetUserByName(req.Username)
2022-06-25 13:34:44 +00:00
if err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 400)
2022-07-07 13:31:43 +00:00
loginCache.Set(ip, count+1)
2022-06-25 13:34:44 +00:00
return
}
// validate password
if err := user.ValidatePassword(req.Password); err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 400)
2022-06-25 13:34:44 +00:00
loginCache.Set(ip, count+1)
return
}
2022-08-05 17:22:13 +00:00
// check 2FA
if user.OtpSecret != "" {
2022-08-06 09:21:32 +00:00
if !totp.Validate(req.OtpCode, user.OtpSecret) {
2022-08-05 17:22:13 +00:00
common.ErrorStrResp(c, "Invalid 2FA code", 402)
loginCache.Set(ip, count+1)
return
}
}
2022-06-25 13:34:44 +00:00
// generate token
2022-06-26 11:20:19 +00:00
token, err := common.GenerateToken(user.Username)
2022-06-25 13:34:44 +00:00
if err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 400, true)
2022-06-25 13:34:44 +00:00
return
}
2022-06-26 11:20:19 +00:00
common.SuccessResp(c, gin.H{"token": token})
2022-06-25 13:34:44 +00:00
loginCache.Del(ip)
}
2022-06-26 08:39:02 +00:00
2022-08-06 09:21:32 +00:00
type UserResp struct {
model.User
Otp bool `json:"otp"`
}
2022-06-26 08:39:02 +00:00
// CurrentUser get current user by token
// if token is empty, return guest user
func CurrentUser(c *gin.Context) {
user := c.MustGet("user").(*model.User)
2022-08-06 09:21:32 +00:00
userResp := UserResp{
User: *user,
}
userResp.Password = ""
2022-08-06 09:21:32 +00:00
if userResp.OtpSecret != "" {
userResp.Otp = true
}
common.SuccessResp(c, userResp)
2022-06-26 08:39:02 +00:00
}
2022-07-23 12:42:12 +00:00
func UpdateCurrent(c *gin.Context) {
var req LoginReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
user := c.MustGet("user").(*model.User)
user.Username = req.Username
if req.Password != "" {
user.Password = req.Password
}
if err := op.UpdateUser(user); err != nil {
2022-07-23 12:42:12 +00:00
common.ErrorResp(c, err, 500)
} else {
common.SuccessResp(c)
}
}
2022-08-05 17:22:13 +00:00
func Generate2FA(c *gin.Context) {
user := c.MustGet("user").(*model.User)
if user.IsGuest() {
common.ErrorStrResp(c, "Guest user can not generate 2FA code", 403)
return
}
key, err := totp.Generate(totp.GenerateOpts{
Issuer: "Alist",
AccountName: user.Username,
})
if err != nil {
common.ErrorResp(c, err, 500)
return
}
img, err := key.Image(400, 400)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
// to base64
var buf bytes.Buffer
png.Encode(&buf, img)
2022-09-06 06:39:21 +00:00
b64 := base64.StdEncoding.EncodeToString(buf.Bytes())
2022-08-05 17:22:13 +00:00
common.SuccessResp(c, gin.H{
2022-09-06 06:39:21 +00:00
"qr": "data:image/png;base64," + b64,
2022-08-05 17:22:13 +00:00
"secret": key.Secret(),
})
}
type Verify2FAReq struct {
Code string `json:"code" binding:"required"`
Secret string `json:"secret" binding:"required"`
}
func Verify2FA(c *gin.Context) {
var req Verify2FAReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
user := c.MustGet("user").(*model.User)
if user.IsGuest() {
common.ErrorStrResp(c, "Guest user can not generate 2FA code", 403)
return
}
if !totp.Validate(req.Code, req.Secret) {
common.ErrorStrResp(c, "Invalid 2FA code", 400)
return
}
user.OtpSecret = req.Secret
if err := op.UpdateUser(user); err != nil {
2022-08-05 17:22:13 +00:00
common.ErrorResp(c, err, 500)
} else {
common.SuccessResp(c)
}
}