@@ -41,20 +41,28 @@ var (
41
41
42
42
func PersonalAccessToken (gdb * gorm.DB ) gin.HandlerFunc {
43
43
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 ]
54
64
}
55
65
56
- // Check if the personal access token is valid.
57
- personalAccessToken := tokenFields [1 ]
58
66
var token models.PersonalAccessToken
59
67
if err := gdb .WithContext (c ).Where ("token = ?" , personalAccessToken ).First (& token ).Error ; err != nil {
60
68
logger .Errorf ("invalid personal access token attempt: %s, error: %v" , c .Request .URL .Path , err )
0 commit comments