Skip to content

Commit 9a7c82a

Browse files
committed
feat(auth): Optimized device session handling logic
- Introduced middleware to handle device sessions - Changed `handleSession` to `HandleSession` in multiple places in `auth.go` to maintain consistent naming - Updated response structure to return `device_key` and `token`
1 parent 8623da5 commit 9a7c82a

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

server/handles/auth.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/alist-org/alist/v3/internal/session"
1616
"github.com/alist-org/alist/v3/internal/setting"
1717
"github.com/alist-org/alist/v3/server/common"
18+
"github.com/alist-org/alist/v3/server/middlewares"
1819
"github.com/gin-gonic/gin"
1920
"github.com/pquerna/otp/totp"
2021
)
@@ -82,13 +83,18 @@ func loginHash(c *gin.Context, req *LoginReq) {
8283
return
8384
}
8485
}
86+
// generate device session
87+
if !middlewares.HandleSession(c, user) {
88+
return
89+
}
8590
// generate token
8691
token, err := common.GenerateToken(user)
8792
if err != nil {
8893
common.ErrorResp(c, err, 400, true)
8994
return
9095
}
91-
common.SuccessResp(c, gin.H{"token": token})
96+
key := c.GetString("device_key")
97+
common.SuccessResp(c, gin.H{"token": token, "device_key": key})
9298
loginCache.Del(ip)
9399
}
94100

server/middlewares/auth.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func Auth(c *gin.Context) {
2626
c.Abort()
2727
return
2828
}
29-
if !handleSession(c, admin) {
29+
if !HandleSession(c, admin) {
3030
return
3131
}
3232
log.Debugf("use admin token: %+v", admin)
@@ -54,7 +54,7 @@ func Auth(c *gin.Context) {
5454
}
5555
guest.RolesDetail = roles
5656
}
57-
if !handleSession(c, guest) {
57+
if !HandleSession(c, guest) {
5858
return
5959
}
6060
log.Debugf("use empty token: %+v", guest)
@@ -93,14 +93,15 @@ func Auth(c *gin.Context) {
9393
}
9494
user.RolesDetail = roles
9595
}
96-
if !handleSession(c, user) {
96+
if !HandleSession(c, user) {
9797
return
9898
}
9999
log.Debugf("use login token: %+v", user)
100100
c.Next()
101101
}
102102

103-
func handleSession(c *gin.Context, user *model.User) bool {
103+
// HandleSession verifies device sessions and stores context values.
104+
func HandleSession(c *gin.Context, user *model.User) bool {
104105
clientID := c.GetHeader("Client-Id")
105106
if clientID == "" {
106107
clientID = c.Query("client_id")

0 commit comments

Comments
 (0)