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.
|
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
package api
|
2016-05-02 00:33:22 +00:00
|
|
|
|
|
|
|
import (
|
2016-05-03 00:47:58 +00:00
|
|
|
"encoding/base32"
|
2023-03-19 12:52:58 +00:00
|
|
|
"errors"
|
2016-05-02 00:33:22 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2019-06-01 07:45:20 +00:00
|
|
|
"github.com/gorilla/securecookie"
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2021-09-27 17:51:55 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2021-09-23 11:33:59 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
2023-03-19 17:32:19 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store/types"
|
2021-05-25 12:08:27 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/shared/httputil"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/shared/token"
|
2016-05-02 00:33:22 +00:00
|
|
|
)
|
|
|
|
|
2017-07-31 19:15:05 +00:00
|
|
|
func HandleLogin(c *gin.Context) {
|
|
|
|
var (
|
|
|
|
w = c.Writer
|
|
|
|
r = c.Request
|
|
|
|
)
|
|
|
|
if err := r.FormValue("error"); err != "" {
|
|
|
|
http.Redirect(w, r, "/login/error?code="+err, 303)
|
|
|
|
} else {
|
2022-05-14 15:45:01 +00:00
|
|
|
http.Redirect(w, r, "/authorize", 303)
|
2017-07-31 19:15:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleAuth(c *gin.Context) {
|
2021-12-01 13:22:06 +00:00
|
|
|
_store := store.FromContext(c)
|
2016-05-02 00:33:22 +00:00
|
|
|
|
|
|
|
// when dealing with redirects we may need to adjust the content type. I
|
|
|
|
// cannot, however, remember why, so need to revisit this line.
|
|
|
|
c.Writer.Header().Del("Content-Type")
|
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
tmpuser, err := server.Config.Services.Forge.Login(c, c.Writer, c.Request)
|
2016-05-02 00:33:22 +00:00
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("cannot authenticate user. %s", err)
|
2023-03-19 12:52:58 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, "/login?error=oauth_error")
|
2016-05-02 00:33:22 +00:00
|
|
|
return
|
|
|
|
}
|
2022-11-04 23:35:06 +00:00
|
|
|
// this will happen when the user is redirected by the forge as
|
2016-05-02 00:33:22 +00:00
|
|
|
// part of the authorization workflow.
|
|
|
|
if tmpuser == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
config := ToConfig(c)
|
|
|
|
|
|
|
|
// get the user from the database
|
2021-12-01 13:22:06 +00:00
|
|
|
u, err := _store.GetUserLogin(tmpuser.Login)
|
2016-05-02 00:33:22 +00:00
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
if !errors.Is(err, types.RecordNotExist) {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:33:22 +00:00
|
|
|
// if self-registration is disabled we should return a not authorized error
|
2016-05-03 00:47:58 +00:00
|
|
|
if !config.Open && !config.IsAdmin(tmpuser) {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("cannot register %s. registration closed", tmpuser.Login)
|
2023-03-19 12:52:58 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, "/login?error=access_denied")
|
2016-05-02 00:33:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-03 00:47:58 +00:00
|
|
|
// if self-registration is enabled for whitelisted organizations we need to
|
|
|
|
// check the user's organization membership.
|
|
|
|
if len(config.Orgs) != 0 {
|
2022-11-04 23:35:06 +00:00
|
|
|
teams, terr := server.Config.Services.Forge.Teams(c, tmpuser)
|
2021-11-23 14:36:52 +00:00
|
|
|
if terr != nil || !config.IsMember(teams) {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("cannot verify team membership for %s.", u.Login)
|
2016-05-03 00:47:58 +00:00
|
|
|
c.Redirect(303, "/login?error=access_denied")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:33:22 +00:00
|
|
|
// create the user account
|
|
|
|
u = &model.User{
|
|
|
|
Login: tmpuser.Login,
|
|
|
|
Token: tmpuser.Token,
|
|
|
|
Secret: tmpuser.Secret,
|
|
|
|
Email: tmpuser.Email,
|
|
|
|
Avatar: tmpuser.Avatar,
|
2016-05-03 00:47:58 +00:00
|
|
|
Hash: base32.StdEncoding.EncodeToString(
|
|
|
|
securecookie.GenerateRandomKey(32),
|
|
|
|
),
|
2016-05-02 00:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// insert the user into the database
|
2021-12-01 13:22:06 +00:00
|
|
|
if err := _store.CreateUser(u); err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("cannot insert %s. %s", u.Login, err)
|
2023-03-19 12:52:58 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, "/login?error=internal_error")
|
2016-05-02 00:33:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the user meta data and authorization data.
|
|
|
|
u.Token = tmpuser.Token
|
|
|
|
u.Secret = tmpuser.Secret
|
|
|
|
u.Email = tmpuser.Email
|
|
|
|
u.Avatar = tmpuser.Avatar
|
2023-03-18 20:21:20 +00:00
|
|
|
u.Admin = u.Admin || config.IsAdmin(tmpuser)
|
2016-05-02 00:33:22 +00:00
|
|
|
|
2016-05-03 00:47:58 +00:00
|
|
|
// if self-registration is enabled for whitelisted organizations we need to
|
|
|
|
// check the user's organization membership.
|
2016-05-02 00:33:22 +00:00
|
|
|
if len(config.Orgs) != 0 {
|
2022-11-04 23:35:06 +00:00
|
|
|
teams, terr := server.Config.Services.Forge.Teams(c, u)
|
2021-11-23 14:36:52 +00:00
|
|
|
if terr != nil || !config.IsMember(teams) {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("cannot verify team membership for %s.", u.Login)
|
2023-03-19 12:52:58 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, "/login?error=access_denied")
|
2016-05-02 00:33:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
if err := _store.UpdateUser(u); err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("cannot update %s. %s", u.Login, err)
|
2023-03-19 12:52:58 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, "/login?error=internal_error")
|
2016-05-03 00:47:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
exp := time.Now().Add(server.Config.Server.SessionExpires).Unix()
|
2021-09-24 14:29:26 +00:00
|
|
|
tokenString, err := token.New(token.SessToken, u.Login).SignExpires(u.Hash, exp)
|
2016-05-02 00:33:22 +00:00
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("cannot create token for %s. %s", u.Login, err)
|
2023-03-19 12:52:58 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, "/login?error=internal_error")
|
2016-05-02 00:33:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-24 14:29:26 +00:00
|
|
|
httputil.SetCookie(c.Writer, c.Request, "user_sess", tokenString)
|
2016-05-02 00:33:22 +00:00
|
|
|
|
2023-03-19 12:52:58 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, "/")
|
2016-05-02 00:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetLogout(c *gin.Context) {
|
|
|
|
httputil.DelCookie(c.Writer, c.Request, "user_sess")
|
|
|
|
httputil.DelCookie(c.Writer, c.Request, "user_last")
|
2023-03-19 12:52:58 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, "/")
|
2016-05-02 00:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetLoginToken(c *gin.Context) {
|
2021-12-01 13:22:06 +00:00
|
|
|
_store := store.FromContext(c)
|
2021-10-28 09:12:58 +00:00
|
|
|
|
2016-05-02 00:33:22 +00:00
|
|
|
in := &tokenPayload{}
|
|
|
|
err := c.Bind(in)
|
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2016-05-02 00:33:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
login, err := server.Config.Services.Forge.Auth(c, in.Access, in.Refresh)
|
2016-05-02 00:33:22 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusUnauthorized, err)
|
2016-05-02 00:33:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
user, err := _store.GetUserLogin(login)
|
2016-05-02 00:33:22 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusNotFound, err)
|
2016-05-02 00:33:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
exp := time.Now().Add(server.Config.Server.SessionExpires).Unix()
|
2021-11-23 14:36:52 +00:00
|
|
|
newToken := token.New(token.SessToken, user.Login)
|
|
|
|
tokenStr, err := newToken.SignExpires(user.Hash, exp)
|
2016-05-02 00:33:22 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2016-05-02 00:33:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-14 21:01:20 +00:00
|
|
|
c.JSON(http.StatusOK, &tokenPayload{
|
2021-11-23 14:36:52 +00:00
|
|
|
Access: tokenStr,
|
2016-05-02 00:33:22 +00:00
|
|
|
Expires: exp - time.Now().Unix(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type tokenPayload struct {
|
|
|
|
Access string `json:"access_token,omitempty"`
|
|
|
|
Refresh string `json:"refresh_token,omitempty"`
|
|
|
|
Expires int64 `json:"expires_in,omitempty"`
|
|
|
|
}
|
2016-05-02 19:21:25 +00:00
|
|
|
|
|
|
|
// ToConfig returns the config from the Context
|
2017-05-05 16:59:37 +00:00
|
|
|
func ToConfig(c *gin.Context) *model.Settings {
|
2016-05-02 19:21:25 +00:00
|
|
|
v := c.MustGet("config")
|
2017-05-05 16:59:37 +00:00
|
|
|
return v.(*model.Settings)
|
2016-05-02 19:21:25 +00:00
|
|
|
}
|