2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2023-07-21 17:45:32 +00:00
|
|
|
"strconv"
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2024-01-14 17:22:06 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/shared/token"
|
2015-09-30 01:21:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func User(c *gin.Context) *model.User {
|
|
|
|
v, ok := c.Get("user")
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
u, ok := v.(*model.User)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetUser() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
var user *model.User
|
|
|
|
|
2024-06-26 22:08:59 +00:00
|
|
|
t, err := token.ParseRequest([]token.Type{token.UserToken, token.SessToken}, c.Request, func(t *token.Token) (string, error) {
|
2015-09-30 01:21:17 +00:00
|
|
|
var err error
|
2024-05-27 09:52:43 +00:00
|
|
|
userID, err := strconv.ParseInt(t.Get("user-id"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
user, err = store.FromContext(c).GetUser(userID)
|
2015-09-30 01:21:17 +00:00
|
|
|
return user.Hash, err
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
c.Set("user", user)
|
|
|
|
|
|
|
|
// if this is a session token (ie not the API token)
|
|
|
|
// this means the user is accessing with a web browser,
|
|
|
|
// so we should implement CSRF protection measures.
|
2024-06-26 22:08:59 +00:00
|
|
|
if t.Type == token.SessToken {
|
2024-02-08 21:49:07 +00:00
|
|
|
err = token.CheckCsrf(c.Request, func(_ *token.Token) (string, error) {
|
2015-09-30 01:21:17 +00:00
|
|
|
return user.Hash, nil
|
|
|
|
})
|
|
|
|
// if csrf token validation fails, exit immediately
|
|
|
|
// with a not authorized error.
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func MustAdmin() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
user := User(c)
|
|
|
|
switch {
|
|
|
|
case user == nil:
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusUnauthorized, "User not authorized")
|
2016-04-22 00:10:19 +00:00
|
|
|
c.Abort()
|
2021-11-23 14:36:52 +00:00
|
|
|
case !user.Admin:
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusForbidden, "User not authorized")
|
2016-04-22 00:10:19 +00:00
|
|
|
c.Abort()
|
2015-09-30 01:21:17 +00:00
|
|
|
default:
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 17:43:29 +00:00
|
|
|
func MustRepoAdmin() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
user := User(c)
|
|
|
|
perm := Perm(c)
|
|
|
|
switch {
|
|
|
|
case user == nil:
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusUnauthorized, "User not authorized")
|
2016-07-21 17:43:29 +00:00
|
|
|
c.Abort()
|
2021-11-23 14:36:52 +00:00
|
|
|
case !perm.Admin:
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusForbidden, "User not authorized")
|
2016-07-21 17:43:29 +00:00
|
|
|
c.Abort()
|
|
|
|
default:
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
func MustUser() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
user := User(c)
|
|
|
|
switch {
|
|
|
|
case user == nil:
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusUnauthorized, "User not authorized")
|
2016-04-22 00:10:19 +00:00
|
|
|
c.Abort()
|
2015-09-30 01:21:17 +00:00
|
|
|
default:
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-25 01:09:35 +00:00
|
|
|
|
|
|
|
func MustOrgMember(admin bool) gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
user := User(c)
|
|
|
|
if user == nil {
|
|
|
|
c.String(http.StatusUnauthorized, "User not authorized")
|
|
|
|
c.Abort()
|
|
|
|
return
|
|
|
|
}
|
2023-07-21 17:45:32 +00:00
|
|
|
|
2024-10-09 10:05:01 +00:00
|
|
|
org := Org(c)
|
|
|
|
if org == nil {
|
|
|
|
c.String(http.StatusBadRequest, "Organization not loaded")
|
|
|
|
c.Abort()
|
2022-07-25 01:09:35 +00:00
|
|
|
return
|
|
|
|
}
|
2023-07-21 17:45:32 +00:00
|
|
|
|
2022-07-25 01:09:35 +00:00
|
|
|
// User can access his own, admin can access all
|
2023-07-21 17:45:32 +00:00
|
|
|
if (org.Name == user.Login) || user.Admin {
|
2022-07-25 01:09:35 +00:00
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
_forge, err := server.Config.Services.Manager.ForgeFromUser(user)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Cannot get forge from user")
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
perm, err := server.Config.Services.Membership.Get(c, _forge, user, org.Name)
|
2022-07-25 01:09:35 +00:00
|
|
|
if err != nil {
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Error().Err(err).Msg("failed to check membership")
|
2022-07-25 01:09:35 +00:00
|
|
|
c.String(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
|
|
|
|
c.Abort()
|
|
|
|
return
|
|
|
|
}
|
2023-07-21 17:45:32 +00:00
|
|
|
|
2022-07-25 01:09:35 +00:00
|
|
|
if perm == nil || (!admin && !perm.Member) || (admin && !perm.Admin) {
|
2024-01-11 18:17:07 +00:00
|
|
|
c.String(http.StatusForbidden, "user not authorized")
|
2022-07-25 01:09:35 +00:00
|
|
|
c.Abort()
|
|
|
|
return
|
|
|
|
}
|
2023-07-21 17:45:32 +00:00
|
|
|
|
2022-07-25 01:09:35 +00:00
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|