Skip to content

Commit 2f602fa

Browse files
committed
fix(webhook): fix webhook authorization
1 parent 6b72cff commit 2f602fa

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

backend/server/api/middlewares.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/apache/incubator-devlake/core/errors"
3434
"github.com/apache/incubator-devlake/core/models/common"
3535
"github.com/apache/incubator-devlake/helpers/apikeyhelper"
36+
"github.com/apache/incubator-devlake/server/api/shared"
3637
"github.com/gin-gonic/gin"
3738
)
3839

@@ -272,9 +273,13 @@ func CheckAuthorizationHeader(c *gin.Context, logger log.Logger, db dal.Dal, api
272273

273274
logger.Info("redirect path: %s to: %s", c.Request.URL.Path, path)
274275
c.Request.URL.Path = path
275-
c.Set(common.USER, &common.User{
276+
user := &common.User{
276277
Name: apiKey.Creator.Creator,
277278
Email: apiKey.Creator.CreatorEmail,
278-
})
279+
}
280+
c.Set(common.USER, user)
281+
// Also store in the request context so the user survives gin's HandleContext
282+
// resetting c.Keys when rerouting from /rest/... to /plugins/...
283+
c.Request = shared.SetRestAuthUser(c.Request, user)
279284
return true
280285
}

backend/server/api/shared/gin_utils.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,36 @@ limitations under the License.
1818
package shared
1919

2020
import (
21+
"context"
22+
"net/http"
23+
2124
"github.com/apache/incubator-devlake/core/models/common"
2225
"github.com/gin-gonic/gin"
2326
)
2427

28+
// restAuthKey is an unexported type used as a request-context key so it cannot
29+
// collide with keys set by other packages.
30+
type restAuthKey struct{}
31+
32+
// SetRestAuthUser stores the authenticated user in the HTTP request context.
33+
// This is necessary because gin's HandleContext calls c.reset(), which clears
34+
// c.Keys but leaves c.Request (and its context) intact. RestAuthentication
35+
// calls this before rerouting so the user survives the reset.
36+
func SetRestAuthUser(r *http.Request, user *common.User) *http.Request {
37+
return r.WithContext(context.WithValue(r.Context(), restAuthKey{}, user))
38+
}
39+
2540
func GetUser(c *gin.Context) (*common.User, bool) {
2641
userObj, exist := c.Get(common.USER)
27-
if !exist {
28-
return nil, false
42+
if exist {
43+
if user, ok := userObj.(*common.User); ok {
44+
return user, true
45+
}
46+
}
47+
// Fallback: RestAuthentication stores the user here before calling
48+
// HandleContext, which resets c.Keys but preserves c.Request.
49+
if user, ok := c.Request.Context().Value(restAuthKey{}).(*common.User); ok && user != nil {
50+
return user, true
2951
}
30-
user := userObj.(*common.User)
31-
return user, true
52+
return nil, false
3253
}

0 commit comments

Comments
 (0)