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"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server"
|
2024-02-13 15:19:02 +00:00
|
|
|
forge_types "go.woodpecker-ci.org/woodpecker/v2/server/forge/types"
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store/types"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/shared/httputil"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/shared/token"
|
2016-05-02 00:33:22 +00:00
|
|
|
)
|
|
|
|
|
2017-07-31 19:15:05 +00:00
|
|
|
func HandleLogin(c *gin.Context) {
|
2023-08-07 14:05:18 +00:00
|
|
|
if err := c.Request.FormValue("error"); err != "" {
|
|
|
|
c.Redirect(http.StatusSeeOther, server.Config.Server.RootPath+"/login/error?code="+err)
|
2017-07-31 19:15:05 +00:00
|
|
|
} else {
|
2023-08-07 14:05:18 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, server.Config.Server.RootPath+"/authorize")
|
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)
|
2024-04-16 06:04:55 +00:00
|
|
|
_forge, err := server.Config.Services.Manager.ForgeMain()
|
|
|
|
if err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
forgeID := int64(1) // TODO: replace with forge id when multiple forges are supported
|
2016-05-02 00:33:22 +00:00
|
|
|
|
2024-01-10 19:57:12 +00:00
|
|
|
// when dealing with redirects, we may need to adjust the content type. I
|
2016-05-02 00:33:22 +00:00
|
|
|
// cannot, however, remember why, so need to revisit this line.
|
|
|
|
c.Writer.Header().Del("Content-Type")
|
|
|
|
|
2024-02-13 15:19:02 +00:00
|
|
|
tmpuser, redirectURL, err := _forge.Login(c, &forge_types.OAuthRequest{
|
|
|
|
Error: c.Request.FormValue("error"),
|
|
|
|
ErrorURI: c.Request.FormValue("error_uri"),
|
|
|
|
ErrorDescription: c.Request.FormValue("error_description"),
|
|
|
|
Code: c.Request.FormValue("code"),
|
|
|
|
})
|
2016-05-02 00:33:22 +00:00
|
|
|
if err != nil {
|
2024-01-10 19:57:12 +00:00
|
|
|
log.Error().Err(err).Msg("cannot authenticate user")
|
2023-08-07 14:05:18 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, server.Config.Server.RootPath+"/login?error=oauth_error")
|
2016-05-02 00:33:22 +00:00
|
|
|
return
|
|
|
|
}
|
2024-02-13 15:19:02 +00:00
|
|
|
// The user is not authorized yet -> redirect
|
2016-05-02 00:33:22 +00:00
|
|
|
if tmpuser == nil {
|
2024-02-13 15:19:02 +00:00
|
|
|
http.Redirect(c.Writer, c.Request, redirectURL, http.StatusSeeOther)
|
2016-05-02 00:33:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the user from the database
|
2023-05-11 03:19:35 +00:00
|
|
|
u, err := _store.GetUserRemoteID(tmpuser.ForgeRemoteID, tmpuser.Login)
|
2024-04-16 06:04:55 +00:00
|
|
|
if err != nil && !errors.Is(err, types.RecordNotExist) {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2023-03-19 12:52:58 +00:00
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
if errors.Is(err, types.RecordNotExist) {
|
2016-05-02 00:33:22 +00:00
|
|
|
// if self-registration is disabled we should return a not authorized error
|
2023-11-12 13:39:41 +00:00
|
|
|
if !server.Config.Permissions.Open && !server.Config.Permissions.Admins.IsAdmin(tmpuser) {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("cannot register %s. registration closed", tmpuser.Login)
|
2023-08-07 14:05:18 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, server.Config.Server.RootPath+"/login?error=access_denied")
|
2016-05-02 00:33:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-12 13:39:41 +00:00
|
|
|
// if self-registration is enabled for allowed organizations we need to
|
2016-05-03 00:47:58 +00:00
|
|
|
// check the user's organization membership.
|
2023-11-12 13:39:41 +00:00
|
|
|
if server.Config.Permissions.Orgs.IsConfigured {
|
2023-07-07 13:43:11 +00:00
|
|
|
teams, terr := _forge.Teams(c, tmpuser)
|
2023-11-12 13:39:41 +00:00
|
|
|
if terr != nil || !server.Config.Permissions.Orgs.IsMember(teams) {
|
2024-03-15 17:00:25 +00:00
|
|
|
log.Error().Err(terr).Msgf("cannot verify team membership for %s.", u.Login)
|
|
|
|
c.Redirect(http.StatusSeeOther, server.Config.Server.RootPath+"/login?error=access_denied")
|
2016-05-03 00:47:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:33:22 +00:00
|
|
|
// create the user account
|
|
|
|
u = &model.User{
|
2023-05-11 03:19:35 +00:00
|
|
|
Login: tmpuser.Login,
|
|
|
|
ForgeRemoteID: tmpuser.ForgeRemoteID,
|
|
|
|
Token: tmpuser.Token,
|
|
|
|
Secret: tmpuser.Secret,
|
|
|
|
Email: tmpuser.Email,
|
|
|
|
Avatar: tmpuser.Avatar,
|
2024-04-16 06:04:55 +00:00
|
|
|
ForgeID: forgeID,
|
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 {
|
2024-01-10 19:57:12 +00:00
|
|
|
log.Error().Err(err).Msgf("cannot insert %s", u.Login)
|
2023-08-07 14:05:18 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, server.Config.Server.RootPath+"/login?error=internal_error")
|
2016-05-02 00:33:22 +00:00
|
|
|
return
|
|
|
|
}
|
2023-07-21 17:45:32 +00:00
|
|
|
|
|
|
|
// if another user already have activated repos on behave of that user,
|
|
|
|
// the user was stored as org. now we adopt it to the user.
|
|
|
|
if org, err := _store.OrgFindByName(u.Login); err == nil && org != nil {
|
|
|
|
org.IsUser = true
|
2024-02-21 18:56:37 +00:00
|
|
|
u.OrgID = org.ID
|
2023-07-21 17:45:32 +00:00
|
|
|
if err := _store.OrgUpdate(org); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("on user creation, could not mark org as user")
|
|
|
|
}
|
2024-02-21 18:56:37 +00:00
|
|
|
} else {
|
|
|
|
if err != nil && !errors.Is(err, types.RecordNotExist) {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
org = &model.Org{
|
|
|
|
Name: u.Login,
|
|
|
|
IsUser: true,
|
|
|
|
Private: false,
|
2024-04-16 06:04:55 +00:00
|
|
|
ForgeID: u.ForgeID,
|
2024-02-21 18:56:37 +00:00
|
|
|
}
|
|
|
|
if err := _store.OrgCreate(org); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("on user creation, could create org for user")
|
|
|
|
}
|
|
|
|
u.OrgID = org.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update org name
|
|
|
|
if u.Login != tmpuser.Login {
|
|
|
|
org, err := _store.OrgGet(u.OrgID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("cannot get org %s", u.Login)
|
|
|
|
c.Redirect(http.StatusSeeOther, server.Config.Server.RootPath+"/login?error=internal_error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
org.Name = u.Login
|
|
|
|
if err := _store.OrgUpdate(org); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("on user creation, could not mark org as user")
|
2023-07-21 17:45:32 +00:00
|
|
|
}
|
2016-05-02 00:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-05-11 03:19:35 +00:00
|
|
|
u.ForgeRemoteID = tmpuser.ForgeRemoteID
|
|
|
|
u.Login = tmpuser.Login
|
2023-11-12 13:39:41 +00:00
|
|
|
u.Admin = u.Admin || server.Config.Permissions.Admins.IsAdmin(tmpuser)
|
2016-05-02 00:33:22 +00:00
|
|
|
|
2023-11-12 13:39:41 +00:00
|
|
|
// if self-registration is enabled for allowed organizations we need to
|
2016-05-03 00:47:58 +00:00
|
|
|
// check the user's organization membership.
|
2023-11-12 13:39:41 +00:00
|
|
|
if server.Config.Permissions.Orgs.IsConfigured {
|
2023-07-07 13:43:11 +00:00
|
|
|
teams, terr := _forge.Teams(c, u)
|
2023-11-12 13:39:41 +00:00
|
|
|
if terr != nil || !server.Config.Permissions.Orgs.IsMember(teams) {
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Error().Err(terr).Msgf("cannot verify team membership for %s", u.Login)
|
2023-08-07 14:05:18 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, server.Config.Server.RootPath+"/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 {
|
2024-01-10 19:57:12 +00:00
|
|
|
log.Error().Err(err).Msgf("cannot update %s", u.Login)
|
2023-08-07 14:05:18 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, server.Config.Server.RootPath+"/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 {
|
2024-01-10 19:57:12 +00:00
|
|
|
log.Error().Msgf("cannot create token for %s", u.Login)
|
2023-08-07 14:05:18 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, server.Config.Server.RootPath+"/login?error=internal_error")
|
2016-05-02 00:33:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-07 13:43:11 +00:00
|
|
|
repos, _ := _forge.Repos(c, u)
|
2023-06-24 08:08:34 +00:00
|
|
|
for _, forgeRepo := range repos {
|
|
|
|
dbRepo, err := _store.GetRepoForgeID(forgeRepo.ForgeRemoteID)
|
|
|
|
if err != nil && errors.Is(err, types.RecordNotExist) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
2024-01-10 19:57:12 +00:00
|
|
|
log.Error().Err(err).Msgf("cannot list repos for %s", u.Login)
|
2023-06-24 08:08:34 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, "/login?error=internal_error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !dbRepo.IsActive {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Debug().Msgf("synced user permission for %s %s", u.Login, dbRepo.FullName)
|
2023-06-24 08:08:34 +00:00
|
|
|
perm := forgeRepo.Perm
|
|
|
|
perm.Repo = dbRepo
|
|
|
|
perm.RepoID = dbRepo.ID
|
|
|
|
perm.UserID = u.ID
|
|
|
|
perm.Synced = time.Now().Unix()
|
|
|
|
if err := _store.PermUpsert(perm); err != nil {
|
2024-01-10 19:57:12 +00:00
|
|
|
log.Error().Err(err).Msgf("cannot update permissions for %s", u.Login)
|
2023-06-24 08:08:34 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, "/login?error=internal_error")
|
|
|
|
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-08-07 14:05:18 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, server.Config.Server.RootPath+"/")
|
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-08-07 14:05:18 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, server.Config.Server.RootPath+"/")
|
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
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
_forge, err := server.Config.Services.Manager.ForgeMain() // TODO: get selected forge from auth request
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Cannot get main forge")
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:33:22 +00:00
|
|
|
in := &tokenPayload{}
|
2024-04-16 06:04:55 +00:00
|
|
|
err = c.Bind(in)
|
2016-05-02 00:33:22 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
login, err := _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 {
|
2024-01-10 21:56:42 +00:00
|
|
|
handleDBError(c, 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"`
|
|
|
|
}
|