2019-02-21 16:58:25 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2019-04-03 09:07:54 +00:00
|
|
|
"sort"
|
2019-02-21 16:58:25 +00:00
|
|
|
"strconv"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/services/gateway/action"
|
2022-02-24 10:18:29 +00:00
|
|
|
"agola.io/agola/internal/services/gateway/common"
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/util"
|
2022-01-26 15:52:10 +00:00
|
|
|
csapitypes "agola.io/agola/services/configstore/api/types"
|
2019-07-31 13:39:07 +00:00
|
|
|
cstypes "agola.io/agola/services/configstore/types"
|
|
|
|
gwapitypes "agola.io/agola/services/gateway/api/types"
|
2019-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-02-21 17:07:58 +00:00
|
|
|
"github.com/rs/zerolog"
|
2019-05-23 09:23:14 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-02-21 16:58:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CreateUserHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-03 21:48:49 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewCreateUserHandler(log zerolog.Logger, ah *action.ActionHandler) *CreateUserHandler {
|
|
|
|
return &CreateUserHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req gwapitypes.CreateUserRequest
|
2019-02-21 16:58:25 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
creq := &action.CreateUserRequest{
|
2019-03-14 13:36:18 +00:00
|
|
|
UserName: req.UserName,
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
u, err := h.ah.CreateUser(ctx, creq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 13:36:18 +00:00
|
|
|
res := createUserResponse(u)
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteUserHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 12:45:19 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewDeleteUserHandler(log zerolog.Logger, ah *action.ActionHandler) *DeleteUserHandler {
|
|
|
|
return &DeleteUserHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DeleteUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
2019-05-03 09:07:53 +00:00
|
|
|
userRef := vars["userref"]
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-05-05 12:45:19 +00:00
|
|
|
err := h.ah.DeleteUser(ctx, userRef)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-09 12:53:00 +00:00
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusNoContent, nil); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-04-09 12:53:00 +00:00
|
|
|
}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CurrentUserHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 12:45:19 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewCurrentUserHandler(log zerolog.Logger, ah *action.ActionHandler) *CurrentUserHandler {
|
|
|
|
return &CurrentUserHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CurrentUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
2022-02-24 10:18:29 +00:00
|
|
|
userID := common.CurrentUserID(ctx)
|
|
|
|
if userID == "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("user not authenticated")))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-05 12:45:19 +00:00
|
|
|
user, err := h.ah.GetUser(ctx, userID)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := createUserResponse(user)
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 12:45:19 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewUserHandler(log zerolog.Logger, ah *action.ActionHandler) *UserHandler {
|
|
|
|
return &UserHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *UserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
2019-05-03 09:07:53 +00:00
|
|
|
userRef := vars["userref"]
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-05-05 12:45:19 +00:00
|
|
|
user, err := h.ah.GetUser(ctx, userRef)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := createUserResponse(user)
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func createUserResponse(u *cstypes.User) *gwapitypes.UserResponse {
|
|
|
|
user := &gwapitypes.UserResponse{
|
2019-05-03 07:54:47 +00:00
|
|
|
ID: u.ID,
|
|
|
|
UserName: u.Name,
|
|
|
|
Tokens: make([]string, 0, len(u.Tokens)),
|
2019-07-31 13:39:07 +00:00
|
|
|
LinkedAccounts: make([]*gwapitypes.LinkedAccountResponse, 0, len(u.LinkedAccounts)),
|
2019-04-03 09:07:54 +00:00
|
|
|
}
|
|
|
|
for tokenName := range u.Tokens {
|
|
|
|
user.Tokens = append(user.Tokens, tokenName)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
2019-07-02 13:35:14 +00:00
|
|
|
sort.Strings(user.Tokens)
|
2019-04-03 09:07:54 +00:00
|
|
|
|
2019-05-03 07:54:47 +00:00
|
|
|
for _, la := range u.LinkedAccounts {
|
2019-07-31 13:39:07 +00:00
|
|
|
user.LinkedAccounts = append(user.LinkedAccounts, &gwapitypes.LinkedAccountResponse{
|
2019-05-13 21:12:48 +00:00
|
|
|
ID: la.ID,
|
2019-05-09 14:49:02 +00:00
|
|
|
RemoteSourceID: la.RemoteSourceID,
|
|
|
|
RemoteUserName: la.RemoteUserName,
|
|
|
|
RemoteUserAvatarURL: la.RemoteUserAvatarURL,
|
2019-05-03 07:54:47 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
|
|
|
type UsersHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 12:45:19 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewUsersHandler(log zerolog.Logger, ah *action.ActionHandler) *UsersHandler {
|
|
|
|
return &UsersHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *UsersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
query := r.URL.Query()
|
|
|
|
|
|
|
|
limitS := query.Get("limit")
|
|
|
|
limit := DefaultRunsLimit
|
|
|
|
if limitS != "" {
|
|
|
|
var err error
|
|
|
|
limit, err = strconv.Atoi(limitS)
|
|
|
|
if err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("cannot parse limit: %w", err)))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if limit < 0 {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("limit must be greater or equal than 0")))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if limit > MaxRunsLimit {
|
|
|
|
limit = MaxRunsLimit
|
|
|
|
}
|
|
|
|
asc := false
|
|
|
|
if _, ok := query["asc"]; ok {
|
|
|
|
asc = true
|
|
|
|
}
|
|
|
|
|
|
|
|
start := query.Get("start")
|
|
|
|
|
2019-05-05 12:45:19 +00:00
|
|
|
areq := &action.GetUsersRequest{
|
|
|
|
Start: start,
|
|
|
|
Limit: limit,
|
|
|
|
Asc: asc,
|
|
|
|
}
|
|
|
|
csusers, err := h.ah.GetUsers(ctx, areq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
users := make([]*gwapitypes.UserResponse, len(csusers))
|
2019-02-21 16:58:25 +00:00
|
|
|
for i, p := range csusers {
|
|
|
|
users[i] = createUserResponse(p)
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, users); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateUserLAHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-03 21:48:49 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewCreateUserLAHandler(log zerolog.Logger, ah *action.ActionHandler) *CreateUserLAHandler {
|
|
|
|
return &CreateUserLAHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateUserLAHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
2019-05-03 09:07:53 +00:00
|
|
|
userRef := vars["userref"]
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req *gwapitypes.CreateUserLARequest
|
2019-02-21 16:58:25 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-03 09:07:53 +00:00
|
|
|
res, err := h.createUserLA(ctx, userRef, req)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (h *CreateUserLAHandler) createUserLA(ctx context.Context, userRef string, req *gwapitypes.CreateUserLARequest) (*gwapitypes.CreateUserLAResponse, error) {
|
2019-05-03 21:48:49 +00:00
|
|
|
creq := &action.CreateUserLARequest{
|
2019-05-03 09:07:53 +00:00
|
|
|
UserRef: userRef,
|
2019-03-29 13:33:41 +00:00
|
|
|
RemoteSourceName: req.RemoteSourceName,
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Info().Msgf("creating linked account")
|
2019-05-03 21:48:49 +00:00
|
|
|
cresp, err := h.ah.HandleRemoteSourceAuth(ctx, req.RemoteSourceName, req.RemoteSourceLoginName, req.RemoteSourceLoginPassword, action.RemoteSourceRequestTypeCreateUserLA, creq)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if cresp.Oauth2Redirect != "" {
|
2019-07-31 13:39:07 +00:00
|
|
|
return &gwapitypes.CreateUserLAResponse{
|
2019-02-21 16:58:25 +00:00
|
|
|
Oauth2Redirect: cresp.Oauth2Redirect,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-05-03 21:48:49 +00:00
|
|
|
authresp := cresp.Response.(*action.CreateUserLAResponse)
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
resp := &gwapitypes.CreateUserLAResponse{
|
|
|
|
LinkedAccount: &gwapitypes.LinkedAccount{
|
|
|
|
ID: authresp.LinkedAccount.ID,
|
|
|
|
RemoteUserID: authresp.LinkedAccount.RemoteUserID,
|
|
|
|
RemoteUserName: authresp.LinkedAccount.RemoteUserName,
|
|
|
|
RemoteUserAvatarURL: authresp.LinkedAccount.RemoteUserAvatarURL,
|
|
|
|
RemoteSourceID: authresp.LinkedAccount.RemoteUserID,
|
|
|
|
},
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Info().Msgf("linked account %q for user %q created", resp.LinkedAccount.ID, userRef)
|
2019-02-21 16:58:25 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteUserLAHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 12:45:19 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewDeleteUserLAHandler(log zerolog.Logger, ah *action.ActionHandler) *DeleteUserLAHandler {
|
|
|
|
return &DeleteUserLAHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DeleteUserLAHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
2019-05-03 09:07:53 +00:00
|
|
|
userRef := vars["userref"]
|
2019-02-21 16:58:25 +00:00
|
|
|
laID := vars["laid"]
|
|
|
|
|
2019-05-05 12:45:19 +00:00
|
|
|
err := h.ah.DeleteUserLA(ctx, userRef, laID)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-08 09:35:45 +00:00
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusNoContent, nil); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-04-08 09:35:45 +00:00
|
|
|
}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CreateUserTokenHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-03 21:48:49 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewCreateUserTokenHandler(log zerolog.Logger, ah *action.ActionHandler) *CreateUserTokenHandler {
|
|
|
|
return &CreateUserTokenHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateUserTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
2019-05-03 09:07:53 +00:00
|
|
|
userRef := vars["userref"]
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req gwapitypes.CreateUserTokenRequest
|
2019-02-21 16:58:25 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
creq := &action.CreateUserTokenRequest{
|
2019-05-03 09:07:53 +00:00
|
|
|
UserRef: userRef,
|
2019-02-21 16:58:25 +00:00
|
|
|
TokenName: req.TokenName,
|
|
|
|
}
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Info().Msgf("creating user %q token", userRef)
|
2019-05-03 21:48:49 +00:00
|
|
|
token, err := h.ah.CreateUserToken(ctx, creq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
res := &gwapitypes.CreateUserTokenResponse{
|
2019-04-05 13:56:06 +00:00
|
|
|
Token: token,
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
2019-04-08 09:35:45 +00:00
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 13:01:57 +00:00
|
|
|
type DeleteUserTokenHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 12:45:19 +00:00
|
|
|
ah *action.ActionHandler
|
2019-04-05 13:01:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewDeleteUserTokenHandler(log zerolog.Logger, ah *action.ActionHandler) *DeleteUserTokenHandler {
|
|
|
|
return &DeleteUserTokenHandler{log: log, ah: ah}
|
2019-04-05 13:01:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DeleteUserTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
2019-05-03 09:07:53 +00:00
|
|
|
userRef := vars["userref"]
|
2019-04-05 13:01:57 +00:00
|
|
|
tokenName := vars["tokenname"]
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Info().Msgf("deleting user %q token %q", userRef, tokenName)
|
2019-05-05 12:45:19 +00:00
|
|
|
err := h.ah.DeleteUserToken(ctx, userRef, tokenName)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-04-05 13:01:57 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-08 09:35:45 +00:00
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusNoContent, nil); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-04-08 09:35:45 +00:00
|
|
|
}
|
2019-04-05 13:01:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 16:53:15 +00:00
|
|
|
type RegisterUserHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-03 21:48:49 +00:00
|
|
|
ah *action.ActionHandler
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewRegisterUserHandler(log zerolog.Logger, ah *action.ActionHandler) *RegisterUserHandler {
|
|
|
|
return &RegisterUserHandler{log: log, ah: ah}
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RegisterUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req *gwapitypes.RegisterUserRequest
|
2019-03-29 16:53:15 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-03-29 16:53:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
res, err := h.registerUser(ctx, req)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-29 16:53:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (h *RegisterUserHandler) registerUser(ctx context.Context, req *gwapitypes.RegisterUserRequest) (*gwapitypes.RegisterUserResponse, error) {
|
2019-05-03 21:48:49 +00:00
|
|
|
creq := &action.RegisterUserRequest{
|
2019-03-29 16:53:15 +00:00
|
|
|
UserName: req.CreateUserRequest.UserName,
|
|
|
|
RemoteSourceName: req.CreateUserLARequest.RemoteSourceName,
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
cresp, err := h.ah.HandleRemoteSourceAuth(ctx, req.CreateUserLARequest.RemoteSourceName, req.CreateUserLARequest.RemoteSourceLoginName, req.CreateUserLARequest.RemoteSourceLoginPassword, action.RemoteSourceRequestTypeRegisterUser, creq)
|
2019-03-29 16:53:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if cresp.Oauth2Redirect != "" {
|
2019-07-31 13:39:07 +00:00
|
|
|
return &gwapitypes.RegisterUserResponse{
|
2019-03-29 16:53:15 +00:00
|
|
|
Oauth2Redirect: cresp.Oauth2Redirect,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-05-03 21:48:49 +00:00
|
|
|
//authresp := cresp.Response.(*action.RegisterUserResponse)
|
2019-03-29 16:53:15 +00:00
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
resp := &gwapitypes.RegisterUserResponse{}
|
2019-03-29 16:53:15 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type AuthorizeHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-03 21:48:49 +00:00
|
|
|
ah *action.ActionHandler
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewAuthorizeHandler(log zerolog.Logger, ah *action.ActionHandler) *AuthorizeHandler {
|
|
|
|
return &AuthorizeHandler{log: log, ah: ah}
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *AuthorizeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req *gwapitypes.LoginUserRequest
|
2019-03-29 16:53:15 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-03-29 16:53:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
res, err := h.authorize(ctx, req)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-29 16:53:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (h *AuthorizeHandler) authorize(ctx context.Context, req *gwapitypes.LoginUserRequest) (*gwapitypes.AuthorizeResponse, error) {
|
2019-05-03 21:48:49 +00:00
|
|
|
creq := &action.LoginUserRequest{
|
2019-03-29 16:53:15 +00:00
|
|
|
RemoteSourceName: req.RemoteSourceName,
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
cresp, err := h.ah.HandleRemoteSourceAuth(ctx, req.RemoteSourceName, req.LoginName, req.LoginPassword, action.RemoteSourceRequestTypeAuthorize, creq)
|
2019-03-29 16:53:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if cresp.Oauth2Redirect != "" {
|
2019-07-31 13:39:07 +00:00
|
|
|
return &gwapitypes.AuthorizeResponse{
|
2019-03-29 16:53:15 +00:00
|
|
|
Oauth2Redirect: cresp.Oauth2Redirect,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-05-03 21:48:49 +00:00
|
|
|
authresp := cresp.Response.(*action.AuthorizeResponse)
|
2019-03-29 16:53:15 +00:00
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
resp := &gwapitypes.AuthorizeResponse{
|
|
|
|
RemoteUserInfo: &gwapitypes.UserInfo{
|
|
|
|
ID: authresp.RemoteUserInfo.ID,
|
|
|
|
LoginName: authresp.RemoteUserInfo.LoginName,
|
|
|
|
Email: authresp.RemoteUserInfo.Email,
|
|
|
|
},
|
2019-03-29 16:53:15 +00:00
|
|
|
RemoteSourceName: authresp.RemoteSourceName,
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
type LoginUserHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-03 21:48:49 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewLoginUserHandler(log zerolog.Logger, ah *action.ActionHandler) *LoginUserHandler {
|
|
|
|
return &LoginUserHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *LoginUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req *gwapitypes.LoginUserRequest
|
2019-02-21 16:58:25 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
res, err := h.loginUser(ctx, req)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (h *LoginUserHandler) loginUser(ctx context.Context, req *gwapitypes.LoginUserRequest) (*gwapitypes.LoginUserResponse, error) {
|
2019-05-03 21:48:49 +00:00
|
|
|
creq := &action.LoginUserRequest{
|
2019-03-29 13:33:41 +00:00
|
|
|
RemoteSourceName: req.RemoteSourceName,
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Info().Msgf("logging in user")
|
2019-05-03 21:48:49 +00:00
|
|
|
cresp, err := h.ah.HandleRemoteSourceAuth(ctx, req.RemoteSourceName, req.LoginName, req.LoginPassword, action.RemoteSourceRequestTypeLoginUser, creq)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if cresp.Oauth2Redirect != "" {
|
2019-07-31 13:39:07 +00:00
|
|
|
return &gwapitypes.LoginUserResponse{
|
2019-02-21 16:58:25 +00:00
|
|
|
Oauth2Redirect: cresp.Oauth2Redirect,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-05-03 21:48:49 +00:00
|
|
|
authresp := cresp.Response.(*action.LoginUserResponse)
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
resp := &gwapitypes.LoginUserResponse{
|
2019-02-21 16:58:25 +00:00
|
|
|
Token: authresp.Token,
|
|
|
|
User: createUserResponse(authresp.User),
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
2019-06-11 13:09:41 +00:00
|
|
|
|
|
|
|
type UserCreateRunHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-06-11 13:09:41 +00:00
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewUserCreateRunHandler(log zerolog.Logger, ah *action.ActionHandler) *UserCreateRunHandler {
|
|
|
|
return &UserCreateRunHandler{log: log, ah: ah}
|
2019-06-11 13:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *UserCreateRunHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req gwapitypes.UserCreateRunRequest
|
2019-06-11 13:09:41 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-06-11 13:09:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-03 13:19:52 +00:00
|
|
|
creq := &action.UserCreateRunRequest{
|
2019-08-02 08:03:28 +00:00
|
|
|
RepoUUID: req.RepoUUID,
|
|
|
|
RepoPath: req.RepoPath,
|
|
|
|
Branch: req.Branch,
|
|
|
|
Tag: req.Tag,
|
|
|
|
Ref: req.Ref,
|
|
|
|
CommitSHA: req.CommitSHA,
|
|
|
|
Message: req.Message,
|
|
|
|
PullRequestRefRegexes: req.PullRequestRefRegexes,
|
2019-08-05 15:33:30 +00:00
|
|
|
Variables: req.Variables,
|
2019-07-03 13:19:52 +00:00
|
|
|
}
|
|
|
|
err := h.ah.UserCreateRun(ctx, creq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-06-11 13:09:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, nil); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-06-11 13:09:41 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-26 15:52:10 +00:00
|
|
|
|
|
|
|
type UserOrgsHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2022-01-26 15:52:10 +00:00
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewUserOrgsHandler(log zerolog.Logger, ah *action.ActionHandler) *UserOrgsHandler {
|
|
|
|
return &UserOrgsHandler{log: log, ah: ah}
|
2022-01-26 15:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *UserOrgsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
2022-02-24 10:18:29 +00:00
|
|
|
userID := common.CurrentUserID(ctx)
|
|
|
|
if userID == "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("user not authenticated")))
|
2022-01-26 15:52:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-24 10:18:29 +00:00
|
|
|
userOrgs, err := h.ah.GetUserOrgs(ctx, userID)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2022-01-26 15:52:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := make([]*gwapitypes.UserOrgsResponse, len(userOrgs))
|
|
|
|
for i, userOrg := range userOrgs {
|
|
|
|
res[i] = createUserOrgsResponse(userOrg)
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2022-01-26 15:52:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createUserOrgsResponse(o *csapitypes.UserOrgsResponse) *gwapitypes.UserOrgsResponse {
|
|
|
|
userOrgs := &gwapitypes.UserOrgsResponse{
|
|
|
|
Organization: createOrgResponse(o.Organization),
|
|
|
|
Role: gwapitypes.MemberRole(o.Role),
|
|
|
|
}
|
|
|
|
|
|
|
|
return userOrgs
|
|
|
|
}
|