gateway: move user la and login validation to command layer
This commit is contained in:
parent
75b5b65da3
commit
3e3a7a0ea5
@ -15,11 +15,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sorintlab/agola/internal/services/gateway/api"
|
"github.com/sorintlab/agola/internal/services/gateway/api"
|
||||||
@ -75,24 +71,10 @@ func userLACreate(cmd *cobra.Command, args []string) error {
|
|||||||
return errors.Wrapf(err, "failed to create linked account")
|
return errors.Wrapf(err, "failed to create linked account")
|
||||||
}
|
}
|
||||||
if resp.Oauth2Redirect != "" {
|
if resp.Oauth2Redirect != "" {
|
||||||
log.Infof("visit %s", resp.Oauth2Redirect)
|
log.Infof("visit %s to continue", resp.Oauth2Redirect)
|
||||||
|
} else {
|
||||||
reader := bufio.NewReader(os.Stdin)
|
|
||||||
fmt.Print("Enter code: ")
|
|
||||||
code, _ := reader.ReadString('\n')
|
|
||||||
code = strings.TrimSpace(code)
|
|
||||||
log.Infof("code: %s", code)
|
|
||||||
|
|
||||||
req := &api.CreateUserLARequest{
|
|
||||||
RemoteSourceName: userLACreateOpts.remoteSourceName,
|
|
||||||
}
|
|
||||||
resp, _, err = gwclient.CreateUserLA(context.TODO(), userLACreateOpts.username, req)
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrapf(err, "failed to create linked account")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Infof("linked account for user %q created, ID: %s", userLACreateOpts.username, resp.LinkedAccount.ID)
|
log.Infof("linked account for user %q created, ID: %s", userLACreateOpts.username, resp.LinkedAccount.ID)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -23,11 +23,9 @@ import (
|
|||||||
csapi "github.com/sorintlab/agola/internal/services/configstore/api"
|
csapi "github.com/sorintlab/agola/internal/services/configstore/api"
|
||||||
"github.com/sorintlab/agola/internal/services/gateway/command"
|
"github.com/sorintlab/agola/internal/services/gateway/command"
|
||||||
"github.com/sorintlab/agola/internal/services/types"
|
"github.com/sorintlab/agola/internal/services/types"
|
||||||
"github.com/sorintlab/agola/internal/util"
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type CreateUserRequest struct {
|
type CreateUserRequest struct {
|
||||||
@ -37,11 +35,10 @@ type CreateUserRequest struct {
|
|||||||
type CreateUserHandler struct {
|
type CreateUserHandler struct {
|
||||||
log *zap.SugaredLogger
|
log *zap.SugaredLogger
|
||||||
ch *command.CommandHandler
|
ch *command.CommandHandler
|
||||||
configstoreClient *csapi.Client
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCreateUserHandler(logger *zap.Logger, ch *command.CommandHandler, configstoreClient *csapi.Client) *CreateUserHandler {
|
func NewCreateUserHandler(logger *zap.Logger, ch *command.CommandHandler) *CreateUserHandler {
|
||||||
return &CreateUserHandler{log: logger.Sugar(), ch: ch, configstoreClient: configstoreClient}
|
return &CreateUserHandler{log: logger.Sugar(), ch: ch}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *CreateUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *CreateUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -281,11 +278,10 @@ type CreateUserLAResponse struct {
|
|||||||
type CreateUserLAHandler struct {
|
type CreateUserLAHandler struct {
|
||||||
log *zap.SugaredLogger
|
log *zap.SugaredLogger
|
||||||
ch *command.CommandHandler
|
ch *command.CommandHandler
|
||||||
configstoreClient *csapi.Client
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCreateUserLAHandler(logger *zap.Logger, ch *command.CommandHandler, configstoreClient *csapi.Client) *CreateUserLAHandler {
|
func NewCreateUserLAHandler(logger *zap.Logger, ch *command.CommandHandler) *CreateUserLAHandler {
|
||||||
return &CreateUserLAHandler{log: logger.Sugar(), ch: ch, configstoreClient: configstoreClient}
|
return &CreateUserLAHandler{log: logger.Sugar(), ch: ch}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *CreateUserLAHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *CreateUserLAHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -315,35 +311,13 @@ func (h *CreateUserLAHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *CreateUserLAHandler) createUserLA(ctx context.Context, userName string, req *CreateUserLARequest) (*CreateUserLAResponse, error) {
|
func (h *CreateUserLAHandler) createUserLA(ctx context.Context, userName string, req *CreateUserLARequest) (*CreateUserLAResponse, error) {
|
||||||
remoteSourceName := req.RemoteSourceName
|
|
||||||
user, _, err := h.configstoreClient.GetUserByName(ctx, userName)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrapf(err, "failed to get user %q", userName)
|
|
||||||
}
|
|
||||||
rs, _, err := h.configstoreClient.GetRemoteSourceByName(ctx, remoteSourceName)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrapf(err, "failed to get remote source %q", remoteSourceName)
|
|
||||||
}
|
|
||||||
h.log.Infof("rs: %s", util.Dump(rs))
|
|
||||||
var la *types.LinkedAccount
|
|
||||||
for _, v := range user.LinkedAccounts {
|
|
||||||
if v.RemoteSourceID == rs.ID {
|
|
||||||
la = v
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
h.log.Infof("la: %s", util.Dump(la))
|
|
||||||
if la != nil {
|
|
||||||
return nil, errors.Errorf("user %q already have a linked account for remote source %q", userName, rs.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
creq := &command.CreateUserLARequest{
|
creq := &command.CreateUserLARequest{
|
||||||
UserName: userName,
|
UserName: userName,
|
||||||
RemoteSourceName: rs.Name,
|
RemoteSourceName: req.RemoteSourceName,
|
||||||
}
|
}
|
||||||
|
|
||||||
h.log.Infof("creating linked account")
|
h.log.Infof("creating linked account")
|
||||||
cresp, err := h.ch.HandleRemoteSourceAuth(ctx, rs, req.RemoteSourceLoginName, req.RemoteSourceLoginPassword, "createuserla", creq)
|
cresp, err := h.ch.HandleRemoteSourceAuth(ctx, req.RemoteSourceName, req.RemoteSourceLoginName, req.RemoteSourceLoginPassword, "createuserla", creq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -448,11 +422,10 @@ type LoginUserResponse struct {
|
|||||||
type LoginUserHandler struct {
|
type LoginUserHandler struct {
|
||||||
log *zap.SugaredLogger
|
log *zap.SugaredLogger
|
||||||
ch *command.CommandHandler
|
ch *command.CommandHandler
|
||||||
configstoreClient *csapi.Client
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLoginUserHandler(logger *zap.Logger, ch *command.CommandHandler, configstoreClient *csapi.Client) *LoginUserHandler {
|
func NewLoginUserHandler(logger *zap.Logger, ch *command.CommandHandler) *LoginUserHandler {
|
||||||
return &LoginUserHandler{log: logger.Sugar(), ch: ch, configstoreClient: configstoreClient}
|
return &LoginUserHandler{log: logger.Sugar(), ch: ch}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *LoginUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *LoginUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -480,19 +453,13 @@ func (h *LoginUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *LoginUserHandler) loginUser(ctx context.Context, req *LoginUserRequest) (*LoginUserResponse, error) {
|
func (h *LoginUserHandler) loginUser(ctx context.Context, req *LoginUserRequest) (*LoginUserResponse, error) {
|
||||||
remoteSourceName := req.RemoteSourceName
|
|
||||||
rs, _, err := h.configstoreClient.GetRemoteSourceByName(ctx, remoteSourceName)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrapf(err, "failed to get remote source %q", remoteSourceName)
|
|
||||||
}
|
|
||||||
h.log.Infof("rs: %s", util.Dump(rs))
|
|
||||||
|
|
||||||
creq := &command.LoginUserRequest{
|
creq := &command.LoginUserRequest{
|
||||||
RemoteSourceName: rs.Name,
|
RemoteSourceName: req.RemoteSourceName,
|
||||||
}
|
}
|
||||||
|
|
||||||
h.log.Infof("logging in user")
|
h.log.Infof("logging in user")
|
||||||
cresp, err := h.ch.HandleRemoteSourceAuth(ctx, rs, req.LoginName, req.LoginPassword, "loginuser", creq)
|
cresp, err := h.ch.HandleRemoteSourceAuth(ctx, req.RemoteSourceName, req.LoginName, req.LoginPassword, "loginuser", creq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -213,7 +213,38 @@ type RemoteSourceAuthResponse struct {
|
|||||||
Response interface{}
|
Response interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CommandHandler) HandleRemoteSourceAuth(ctx context.Context, rs *types.RemoteSource, loginName, loginPassword, requestType string, req interface{}) (*RemoteSourceAuthResponse, error) {
|
func (c *CommandHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSourceName, loginName, loginPassword, requestType string, req interface{}) (*RemoteSourceAuthResponse, error) {
|
||||||
|
rs, _, err := c.configstoreClient.GetRemoteSourceByName(ctx, remoteSourceName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "failed to get remote source %q", remoteSourceName)
|
||||||
|
}
|
||||||
|
c.log.Infof("rs: %s", util.Dump(rs))
|
||||||
|
|
||||||
|
switch requestType {
|
||||||
|
case "createuserla":
|
||||||
|
req := req.(*CreateUserLARequest)
|
||||||
|
user, _, err := c.configstoreClient.GetUserByName(ctx, req.UserName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "failed to get user %q", req.UserName)
|
||||||
|
}
|
||||||
|
var la *types.LinkedAccount
|
||||||
|
for _, v := range user.LinkedAccounts {
|
||||||
|
if v.RemoteSourceID == rs.ID {
|
||||||
|
la = v
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.log.Infof("la: %s", util.Dump(la))
|
||||||
|
if la != nil {
|
||||||
|
return nil, errors.Errorf("user %q already have a linked account for remote source %q", req.UserName, rs.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
case "loginuser":
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, errors.Errorf("unknown request type: %q", requestType)
|
||||||
|
}
|
||||||
|
|
||||||
switch rs.AuthType {
|
switch rs.AuthType {
|
||||||
case types.RemoteSourceAuthTypeOauth2:
|
case types.RemoteSourceAuthTypeOauth2:
|
||||||
oauth2Source, err := common.GetOauth2Source(rs, "")
|
oauth2Source, err := common.GetOauth2Source(rs, "")
|
||||||
|
@ -165,10 +165,10 @@ func (g *Gateway) Run(ctx context.Context) error {
|
|||||||
userHandler := api.NewUserHandler(logger, g.configstoreClient)
|
userHandler := api.NewUserHandler(logger, g.configstoreClient)
|
||||||
userByNameHandler := api.NewUserByNameHandler(logger, g.configstoreClient)
|
userByNameHandler := api.NewUserByNameHandler(logger, g.configstoreClient)
|
||||||
usersHandler := api.NewUsersHandler(logger, g.configstoreClient)
|
usersHandler := api.NewUsersHandler(logger, g.configstoreClient)
|
||||||
createUserHandler := api.NewCreateUserHandler(logger, g.ch, g.configstoreClient)
|
createUserHandler := api.NewCreateUserHandler(logger, g.ch)
|
||||||
deleteUserHandler := api.NewDeleteUserHandler(logger, g.configstoreClient)
|
deleteUserHandler := api.NewDeleteUserHandler(logger, g.configstoreClient)
|
||||||
|
|
||||||
createUserLAHandler := api.NewCreateUserLAHandler(logger, g.ch, g.configstoreClient)
|
createUserLAHandler := api.NewCreateUserLAHandler(logger, g.ch)
|
||||||
deleteUserLAHandler := api.NewDeleteUserLAHandler(logger, g.configstoreClient)
|
deleteUserLAHandler := api.NewDeleteUserLAHandler(logger, g.configstoreClient)
|
||||||
createUserTokenHandler := api.NewCreateUserTokenHandler(logger, g.configstoreClient)
|
createUserTokenHandler := api.NewCreateUserTokenHandler(logger, g.configstoreClient)
|
||||||
|
|
||||||
@ -191,7 +191,7 @@ func (g *Gateway) Run(ctx context.Context) error {
|
|||||||
|
|
||||||
reposHandler := api.NewReposHandler(logger, g.configstoreClient)
|
reposHandler := api.NewReposHandler(logger, g.configstoreClient)
|
||||||
|
|
||||||
loginUserHandler := api.NewLoginUserHandler(logger, g.ch, g.configstoreClient)
|
loginUserHandler := api.NewLoginUserHandler(logger, g.ch)
|
||||||
oauth2callbackHandler := api.NewOAuth2CallbackHandler(logger, g.ch, g.configstoreClient)
|
oauth2callbackHandler := api.NewOAuth2CallbackHandler(logger, g.ch, g.configstoreClient)
|
||||||
|
|
||||||
router := mux.NewRouter()
|
router := mux.NewRouter()
|
||||||
|
Loading…
Reference in New Issue
Block a user