1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-12 08:23:48 +02:00
woodpecker/server/router/middleware/session/user.go

131 lines
2.8 KiB
Go
Raw Normal View History

2018-02-20 00:24:10 +02:00
// Copyright 2018 Drone.IO Inc.
2018-03-21 15:02:17 +02:00
//
2018-02-20 00:24:10 +02: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 15:02:17 +02:00
//
2018-02-20 00:24:10 +02:00
// http://www.apache.org/licenses/LICENSE-2.0
2018-03-21 15:02:17 +02:00
//
2018-02-20 00:24:10 +02: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 03:21:17 +02:00
package session
import (
"net/http"
"github.com/woodpecker-ci/woodpecker/model"
"github.com/woodpecker-ci/woodpecker/server/store"
"github.com/woodpecker-ci/woodpecker/shared/token"
2015-09-30 03:21:17 +02:00
"github.com/gin-gonic/gin"
)
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 Token(c *gin.Context) *token.Token {
v, ok := c.Get("token")
if !ok {
return nil
}
u, ok := v.(*token.Token)
if !ok {
return nil
}
return u
}
func SetUser() gin.HandlerFunc {
return func(c *gin.Context) {
var user *model.User
t, err := token.ParseRequest(c.Request, func(t *token.Token) (string, error) {
var err error
user, err = store.GetUserLogin(c, t.Text)
2015-09-30 03:21:17 +02:00
return user.Hash, err
})
if err == nil {
2016-05-02 21:21:25 +02:00
confv := c.MustGet("config")
2017-05-05 18:59:37 +02:00
if conf, ok := confv.(*model.Settings); ok {
2016-05-02 21:21:25 +02:00
user.Admin = conf.IsAdmin(user)
}
2015-09-30 03:21:17 +02:00
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.
if t.Kind == token.SessToken {
err = token.CheckCsrf(c.Request, func(t *token.Token) (string, error) {
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:
2016-04-22 02:10:19 +02:00
c.String(401, "User not authorized")
c.Abort()
2015-09-30 03:21:17 +02:00
case user.Admin == false:
2020-11-27 05:29:07 +02:00
c.String(403, "User not authorized")
2016-04-22 02:10:19 +02:00
c.Abort()
2015-09-30 03:21:17 +02:00
default:
c.Next()
}
}
}
func MustRepoAdmin() gin.HandlerFunc {
return func(c *gin.Context) {
user := User(c)
perm := Perm(c)
switch {
case user == nil:
c.String(401, "User not authorized")
c.Abort()
case perm.Admin == false:
c.String(403, "User not authorized")
c.Abort()
default:
c.Next()
}
}
}
2015-09-30 03:21:17 +02:00
func MustUser() gin.HandlerFunc {
return func(c *gin.Context) {
user := User(c)
switch {
case user == nil:
2016-04-22 02:10:19 +02:00
c.String(401, "User not authorized")
c.Abort()
2015-09-30 03:21:17 +02:00
default:
c.Next()
}
}
}