Skip to content

Commit 584a612

Browse files
authored
feat(middlewares): support token from query parameter (#4294)
Signed-off-by: Gaius <gaius.qi@gmail.com>
1 parent 7374114 commit 584a612

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

manager/middlewares/personal_access_token.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,28 @@ var (
4141

4242
func PersonalAccessToken(gdb *gorm.DB) gin.HandlerFunc {
4343
return func(c *gin.Context) {
44-
// Get bearer token from Authorization header.
45-
authorization := c.GetHeader(headers.Authorization)
46-
tokenFields := strings.Fields(authorization)
47-
if len(tokenFields) != 2 || tokenFields[0] != "Bearer" {
48-
c.JSON(http.StatusUnauthorized, ErrorResponse{
49-
Message: http.StatusText(http.StatusUnauthorized),
50-
})
51-
52-
c.Abort()
53-
return
44+
// Extract personal access token from either query parameter or Authorization header.
45+
// First, try to get the token from the "access_token" query parameter.
46+
// If not found, extract it from the "Authorization" header using Bearer token format,
47+
// return 401 Unauthorized if the Authorization header format is invalid.
48+
var personalAccessToken string
49+
if accessToken := strings.TrimSpace(c.Query("access_token")); accessToken != "" {
50+
personalAccessToken = accessToken
51+
} else {
52+
authorization := c.GetHeader(headers.Authorization)
53+
tokenFields := strings.Fields(authorization)
54+
if len(tokenFields) != 2 || !strings.EqualFold(tokenFields[0], "Bearer") {
55+
c.JSON(http.StatusUnauthorized, ErrorResponse{
56+
Message: http.StatusText(http.StatusUnauthorized),
57+
})
58+
59+
c.Abort()
60+
return
61+
}
62+
63+
personalAccessToken = tokenFields[1]
5464
}
5565

56-
// Check if the personal access token is valid.
57-
personalAccessToken := tokenFields[1]
5866
var token models.PersonalAccessToken
5967
if err := gdb.WithContext(c).Where("token = ?", personalAccessToken).First(&token).Error; err != nil {
6068
logger.Errorf("invalid personal access token attempt: %s, error: %v", c.Request.URL.Path, err)

0 commit comments

Comments
 (0)