Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(auth): use 'Token' as authentication header #7894

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func DelUserCacheOnline(username string) {
}
u = fmt.Sprintf("https://localhost:%d/api/admin/user/del_cache", conf.Conf.Scheme.HttpsPort)
}
res, err := client.R().SetHeader("Authorization", token).SetQueryParam("username", username).Post(u)
res, err := client.R().SetHeader("X-Token", token).SetQueryParam("username", username).Post(u)
if err != nil {
utils.Log.Warnf("[del_user_cache_online] failed: %+v", err)
return
Expand Down
2 changes: 1 addition & 1 deletion drivers/alist_v3/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (d *AListV3) Put(ctx context.Context, dstDir model.Obj, stream model.FileSt
if err != nil {
return err
}
req.Header.Set("Authorization", d.Token)
req.Header.Set(d.Addition.AuthHeader, d.Token)
req.Header.Set("File-Path", path.Join(dstDir.GetPath(), stream.GetName()))
req.Header.Set("Password", d.MetaPassword)
if md5 := stream.GetHash().GetHash(utils.MD5); len(md5) > 0 {
Expand Down
1 change: 1 addition & 0 deletions drivers/alist_v3/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type Addition struct {
driver.RootPath
Address string `json:"url" required:"true"`
AuthHeader string `json:"auth_header" type:"select" options:"Authorization,X-Token" default:"X-Token"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的修改需要在init函数中判断下AuthHeader ,如果为空则设置默认值

MetaPassword string `json:"meta_password"`
Username string `json:"username"`
Password string `json:"password"`
Expand Down
2 changes: 1 addition & 1 deletion drivers/alist_v3/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (d *AListV3) login() error {
func (d *AListV3) request(api, method string, callback base.ReqCallback, retry ...bool) ([]byte, error) {
url := d.Address + "/api" + api
req := base.RestyClient.R()
req.SetHeader("Authorization", d.Token)
req.SetHeader(d.Addition.AuthHeader, d.Token)
if callback != nil {
callback(req)
}
Expand Down
8 changes: 8 additions & 0 deletions server/common/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/model"
"github.com/golang-jwt/jwt/v4"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -64,6 +65,13 @@ func ParseToken(tokenString string) (*UserClaims, error) {
return nil, errors.New("couldn't handle this token")
}

func GetToken(c *gin.Context) string {
if token := c.GetHeader("X-Token"); token != "" {
return token
}
return c.GetHeader("Authorization")
}

func InvalidateToken(tokenString string) error {
if tokenString == "" {
return nil // don't invalidate empty guest token
Expand Down
3 changes: 2 additions & 1 deletion server/handles/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ func Verify2FA(c *gin.Context) {
}

func LogOut(c *gin.Context) {
err := common.InvalidateToken(c.GetHeader("Authorization"))
token := common.GetToken(c)
err := common.InvalidateToken(token)
if err != nil {
common.ErrorResp(c, err, 500)
} else {
Expand Down
4 changes: 2 additions & 2 deletions server/middlewares/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// Auth is a middleware that checks if the user is logged in.
// if token is empty, set user to guest
func Auth(c *gin.Context) {
token := c.GetHeader("Authorization")
token := common.GetToken(c)
if subtle.ConstantTimeCompare([]byte(token), []byte(setting.GetStr(conf.Token))) == 1 {
admin, err := op.GetAdmin()
if err != nil {
Expand Down Expand Up @@ -74,7 +74,7 @@ func Auth(c *gin.Context) {
}

func Authn(c *gin.Context) {
token := c.GetHeader("Authorization")
token := common.GetToken(c)
if subtle.ConstantTimeCompare([]byte(token), []byte(setting.GetStr(conf.Token))) == 1 {
admin, err := op.GetAdmin()
if err != nil {
Expand Down
Loading