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-03-29 16:53:15 +00:00
|
|
|
gitsource "github.com/sorintlab/agola/internal/gitsources"
|
2019-05-03 21:48:49 +00:00
|
|
|
"github.com/sorintlab/agola/internal/services/gateway/action"
|
2019-02-21 16:58:25 +00:00
|
|
|
"github.com/sorintlab/agola/internal/services/types"
|
2019-04-09 12:53:00 +00:00
|
|
|
"github.com/sorintlab/agola/internal/util"
|
2019-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2019-05-14 10:58:29 +00:00
|
|
|
"go.uber.org/zap"
|
2019-05-23 09:23:14 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-02-21 16:58:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CreateUserRequest struct {
|
|
|
|
UserName string `json:"username"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateUserHandler struct {
|
2019-03-29 13:33:41 +00:00
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:48:49 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
func NewCreateUserHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateUserHandler {
|
|
|
|
return &CreateUserHandler{log: logger.Sugar(), ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
var req CreateUserRequest
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(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)
|
2019-03-14 13:36:18 +00:00
|
|
|
if httpError(w, err) {
|
2019-04-05 13:56:06 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 13:36:18 +00:00
|
|
|
res := createUserResponse(u)
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusCreated, res); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteUserHandler struct {
|
2019-05-05 12:45:19 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 12:45:19 +00:00
|
|
|
func NewDeleteUserHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteUserHandler {
|
|
|
|
return &DeleteUserHandler{log: logger.Sugar(), 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)
|
|
|
|
if httpError(w, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-09 12:53:00 +00:00
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusNoContent, nil); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CurrentUserHandler struct {
|
2019-05-05 12:45:19 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 12:45:19 +00:00
|
|
|
func NewCurrentUserHandler(logger *zap.Logger, ah *action.ActionHandler) *CurrentUserHandler {
|
|
|
|
return &CurrentUserHandler{log: logger.Sugar(), ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CurrentUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
userIDVal := ctx.Value("userid")
|
|
|
|
if userIDVal == nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(errors.Errorf("user not authenticated")))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
userID := userIDVal.(string)
|
|
|
|
|
2019-05-05 12:45:19 +00:00
|
|
|
user, err := h.ah.GetUser(ctx, userID)
|
|
|
|
if httpError(w, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := createUserResponse(user)
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, res); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserHandler struct {
|
2019-05-05 12:45:19 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 12:45:19 +00:00
|
|
|
func NewUserHandler(logger *zap.Logger, ah *action.ActionHandler) *UserHandler {
|
|
|
|
return &UserHandler{log: logger.Sugar(), 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)
|
|
|
|
if httpError(w, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := createUserResponse(user)
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, res); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserResponse struct {
|
2019-05-09 14:48:28 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
UserName string `json:"username"`
|
|
|
|
Tokens []string `json:"tokens"`
|
|
|
|
LinkedAccounts []*LinkedAccountResponse `json:"linked_accounts"`
|
2019-05-03 07:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type LinkedAccountResponse struct {
|
2019-05-13 21:12:48 +00:00
|
|
|
ID string `json:"id"`
|
2019-05-09 14:49:02 +00:00
|
|
|
RemoteSourceID string `json:"remote_source_id"`
|
|
|
|
RemoteUserName string `json:"remote_user_name"`
|
|
|
|
RemoteUserAvatarURL string `json:"remote_user_avatar_url"`
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 09:07:54 +00:00
|
|
|
func createUserResponse(u *types.User) *UserResponse {
|
2019-02-21 16:58:25 +00:00
|
|
|
user := &UserResponse{
|
2019-05-03 07:54:47 +00:00
|
|
|
ID: u.ID,
|
|
|
|
UserName: u.Name,
|
|
|
|
Tokens: make([]string, 0, len(u.Tokens)),
|
|
|
|
LinkedAccounts: make([]*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-04-03 09:07:54 +00:00
|
|
|
sort.Sort(sort.StringSlice(user.Tokens))
|
|
|
|
|
2019-05-03 07:54:47 +00:00
|
|
|
for _, la := range u.LinkedAccounts {
|
|
|
|
user.LinkedAccounts = append(user.LinkedAccounts, &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 {
|
2019-05-05 12:45:19 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 12:45:19 +00:00
|
|
|
func NewUsersHandler(logger *zap.Logger, ah *action.ActionHandler) *UsersHandler {
|
|
|
|
return &UsersHandler{log: logger.Sugar(), 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 {
|
2019-05-23 09:23:14 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(errors.Errorf("cannot parse limit: %w", err)))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if limit < 0 {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(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)
|
|
|
|
if httpError(w, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
users := make([]*UserResponse, len(csusers))
|
|
|
|
for i, p := range csusers {
|
|
|
|
users[i] = createUserResponse(p)
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, users); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateUserLARequest struct {
|
|
|
|
RemoteSourceName string `json:"remote_source_name"`
|
2019-03-29 16:53:15 +00:00
|
|
|
RemoteSourceLoginName string `json:"remote_source_login_name"`
|
|
|
|
RemoteSourceLoginPassword string `json:"remote_source_login_password"`
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CreateUserLAResponse struct {
|
|
|
|
LinkedAccount *types.LinkedAccount `json:"linked_account"`
|
|
|
|
Oauth2Redirect string `json:"oauth2_redirect"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateUserLAHandler struct {
|
2019-03-29 13:33:41 +00:00
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:48:49 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
func NewCreateUserLAHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateUserLAHandler {
|
|
|
|
return &CreateUserLAHandler{log: logger.Sugar(), 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
|
|
|
|
|
|
|
var req *CreateUserLARequest
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(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)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpError(w, err) {
|
2019-02-21 16:58:25 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusCreated, res); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 09:07:53 +00:00
|
|
|
func (h *CreateUserLAHandler) createUserLA(ctx context.Context, userRef string, req *CreateUserLARequest) (*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
|
|
|
}
|
|
|
|
|
|
|
|
h.log.Infof("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 != "" {
|
|
|
|
return &CreateUserLAResponse{
|
|
|
|
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
|
|
|
|
|
|
|
resp := &CreateUserLAResponse{
|
|
|
|
LinkedAccount: authresp.LinkedAccount,
|
|
|
|
}
|
2019-05-03 09:07:53 +00:00
|
|
|
h.log.Infof("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 {
|
2019-05-05 12:45:19 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 12:45:19 +00:00
|
|
|
func NewDeleteUserLAHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteUserLAHandler {
|
|
|
|
return &DeleteUserLAHandler{log: logger.Sugar(), 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)
|
|
|
|
if httpError(w, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-08 09:35:45 +00:00
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusNoContent, nil); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CreateUserTokenRequest struct {
|
|
|
|
TokenName string `json:"token_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateUserTokenResponse struct {
|
|
|
|
Token string `json:"token"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateUserTokenHandler struct {
|
2019-04-05 13:56:06 +00:00
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:48:49 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
func NewCreateUserTokenHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateUserTokenHandler {
|
|
|
|
return &CreateUserTokenHandler{log: logger.Sugar(), 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
|
|
|
|
|
|
|
var req CreateUserTokenRequest
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(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,
|
|
|
|
}
|
2019-05-03 09:07:53 +00:00
|
|
|
h.log.Infof("creating user %q token", userRef)
|
2019-05-03 21:48:49 +00:00
|
|
|
token, err := h.ah.CreateUserToken(ctx, creq)
|
2019-04-05 13:56:06 +00:00
|
|
|
if httpError(w, err) {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
res := &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
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusCreated, res); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 13:01:57 +00:00
|
|
|
type DeleteUserTokenHandler struct {
|
2019-05-05 12:45:19 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
2019-04-05 13:01:57 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 12:45:19 +00:00
|
|
|
func NewDeleteUserTokenHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteUserTokenHandler {
|
|
|
|
return &DeleteUserTokenHandler{log: logger.Sugar(), 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"]
|
|
|
|
|
2019-05-03 09:07:53 +00:00
|
|
|
h.log.Infof("deleting user %q token %q", userRef, tokenName)
|
2019-05-05 12:45:19 +00:00
|
|
|
err := h.ah.DeleteUserToken(ctx, userRef, tokenName)
|
|
|
|
if httpError(w, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-04-05 13:01:57 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-08 09:35:45 +00:00
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusNoContent, nil); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-04-05 13:01:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 16:53:15 +00:00
|
|
|
type RegisterUserRequest struct {
|
|
|
|
CreateUserRequest
|
|
|
|
CreateUserLARequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type RegisterUserHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:48:49 +00:00
|
|
|
ah *action.ActionHandler
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RegisterUserResponse struct {
|
|
|
|
Oauth2Redirect string `json:"oauth2_redirect"`
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
func NewRegisterUserHandler(logger *zap.Logger, ah *action.ActionHandler) *RegisterUserHandler {
|
|
|
|
return &RegisterUserHandler{log: logger.Sugar(), ah: ah}
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RegisterUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
var req *RegisterUserRequest
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-03-29 16:53:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
res, err := h.registerUser(ctx, req)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpError(w, err) {
|
2019-03-29 16:53:15 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusCreated, res); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RegisterUserHandler) registerUser(ctx context.Context, req *RegisterUserRequest) (*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 != "" {
|
|
|
|
return &RegisterUserResponse{
|
|
|
|
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
|
|
|
|
|
|
|
resp := &RegisterUserResponse{}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type AuthorizeHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:48:49 +00:00
|
|
|
ah *action.ActionHandler
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AuthorizeResponse struct {
|
|
|
|
Oauth2Redirect string `json:"oauth2_redirect"`
|
|
|
|
RemoteUserInfo *gitsource.UserInfo `json:"remote_user_info"`
|
|
|
|
RemoteSourceName string `json:"remote_source_name"`
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
func NewAuthorizeHandler(logger *zap.Logger, ah *action.ActionHandler) *AuthorizeHandler {
|
|
|
|
return &AuthorizeHandler{log: logger.Sugar(), ah: ah}
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *AuthorizeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
var req *LoginUserRequest
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-03-29 16:53:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
res, err := h.authorize(ctx, req)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpError(w, err) {
|
2019-03-29 16:53:15 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusCreated, res); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *AuthorizeHandler) authorize(ctx context.Context, req *LoginUserRequest) (*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 != "" {
|
|
|
|
return &AuthorizeResponse{
|
|
|
|
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
|
|
|
|
|
|
|
resp := &AuthorizeResponse{
|
|
|
|
RemoteUserInfo: authresp.RemoteUserInfo,
|
|
|
|
RemoteSourceName: authresp.RemoteSourceName,
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
type LoginUserRequest struct {
|
|
|
|
RemoteSourceName string `json:"remote_source_name"`
|
|
|
|
LoginName string `json:"login_name"`
|
|
|
|
LoginPassword string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type LoginUserResponse struct {
|
|
|
|
Oauth2Redirect string `json:"oauth2_redirect"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
User *UserResponse `json:"user"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type LoginUserHandler struct {
|
2019-03-29 13:33:41 +00:00
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:48:49 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
func NewLoginUserHandler(logger *zap.Logger, ah *action.ActionHandler) *LoginUserHandler {
|
|
|
|
return &LoginUserHandler{log: logger.Sugar(), ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *LoginUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
var req *LoginUserRequest
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
res, err := h.loginUser(ctx, req)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpError(w, err) {
|
2019-02-21 16:58:25 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusCreated, res); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *LoginUserHandler) loginUser(ctx context.Context, req *LoginUserRequest) (*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
|
|
|
}
|
|
|
|
|
|
|
|
h.log.Infof("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 != "" {
|
|
|
|
return &LoginUserResponse{
|
|
|
|
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
|
|
|
|
|
|
|
resp := &LoginUserResponse{
|
|
|
|
Token: authresp.Token,
|
|
|
|
User: createUserResponse(authresp.User),
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
2019-06-11 13:09:41 +00:00
|
|
|
|
|
|
|
type UserCreateRunRequest struct {
|
|
|
|
RepoPath string `json:"repo_path,omitempty"`
|
|
|
|
Branch string `json:"branch,omitempty"`
|
|
|
|
CommitSHA string `json:"commit_sha,omitempty"`
|
|
|
|
Message string `json:"message,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserCreateRunHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserCreateRunHandler(logger *zap.Logger, ah *action.ActionHandler) *UserCreateRunHandler {
|
|
|
|
return &UserCreateRunHandler{log: logger.Sugar(), ah: ah}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *UserCreateRunHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
var req UserCreateRunRequest
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := h.ah.UserCreateRun(ctx, req.RepoPath, req.Branch, req.CommitSHA, req.Message)
|
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusCreated, nil); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|