mirror of
https://github.com/jsiebens/ionscale.git
synced 2026-03-31 15:07:49 +01:00
fix: send user and login in registration response
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package migration
|
||||
|
||||
import (
|
||||
"github.com/go-gormigrate/gormigrate/v2"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func m202212201300_add_user_id_column() *gormigrate.Migration {
|
||||
return &gormigrate.Migration{
|
||||
ID: "202212201300",
|
||||
Migrate: func(db *gorm.DB) error {
|
||||
type RegistrationRequest struct {
|
||||
Key string `gorm:"type:varchar(64);uniqueIndex"`
|
||||
UserID uint64
|
||||
}
|
||||
|
||||
return db.AutoMigrate(
|
||||
&RegistrationRequest{},
|
||||
)
|
||||
},
|
||||
Rollback: nil,
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ func Migrations() []*gormigrate.Migration {
|
||||
m202210070814_add_filesharing_and_servicecollection_columns(),
|
||||
m202210080700_ssh_action_request(),
|
||||
m202211031100_add_authorized_column(),
|
||||
m202212201300_add_user_id_column(),
|
||||
}
|
||||
return migrations
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ type RegistrationRequest struct {
|
||||
CreatedAt time.Time
|
||||
Authenticated bool
|
||||
Error string
|
||||
UserID uint64
|
||||
}
|
||||
|
||||
func (r *RegistrationRequest) IsFinished() bool {
|
||||
|
||||
@@ -100,7 +100,7 @@ func (r *repository) GetOrCreateUserWithAccount(ctx context.Context, tailnet *Ta
|
||||
|
||||
func (r *repository) GetUser(ctx context.Context, userID uint64) (*User, error) {
|
||||
var m User
|
||||
tx := r.withContext(ctx).Preload("Tailnet").Preload("Account").First(&m, "id = ? and user_type = ?", userID, UserTypePerson)
|
||||
tx := r.withContext(ctx).Preload("Tailnet").Preload("Account").First(&m, "id = ?", userID)
|
||||
|
||||
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
|
||||
@@ -515,6 +515,7 @@ func (h *AuthenticationHandlers) endMachineRegistrationFlow(c echo.Context, regi
|
||||
err = h.repository.Transaction(func(rp domain.Repository) error {
|
||||
registrationRequest.Authenticated = true
|
||||
registrationRequest.Error = ""
|
||||
registrationRequest.UserID = user.ID
|
||||
|
||||
if err := rp.SaveMachine(ctx, m); err != nil {
|
||||
return err
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/jsiebens/ionscale/internal/config"
|
||||
"github.com/jsiebens/ionscale/internal/domain"
|
||||
"github.com/jsiebens/ionscale/internal/errors"
|
||||
"github.com/jsiebens/ionscale/internal/mapping"
|
||||
"github.com/jsiebens/ionscale/internal/util"
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
@@ -103,7 +104,14 @@ func (h *RegistrationHandlers) Register(c echo.Context) error {
|
||||
return errors.Wrap(err, 0)
|
||||
}
|
||||
|
||||
response := tailcfg.RegisterResponse{MachineAuthorized: true}
|
||||
tUser, tLogin := mapping.ToUser(m.User)
|
||||
|
||||
response := tailcfg.RegisterResponse{
|
||||
MachineAuthorized: m.Authorized,
|
||||
User: tUser,
|
||||
Login: tLogin,
|
||||
}
|
||||
|
||||
return binder.WriteResponse(c, http.StatusOK, response)
|
||||
}
|
||||
|
||||
@@ -240,7 +248,13 @@ func (h *RegistrationHandlers) authenticateMachineWithAuthKey(c echo.Context, bi
|
||||
return errors.Wrap(err, 0)
|
||||
}
|
||||
|
||||
response := tailcfg.RegisterResponse{MachineAuthorized: true}
|
||||
tUser, tLogin := mapping.ToUser(m.User)
|
||||
response := tailcfg.RegisterResponse{
|
||||
MachineAuthorized: true,
|
||||
User: tUser,
|
||||
Login: tLogin,
|
||||
}
|
||||
|
||||
return binder.WriteResponse(c, http.StatusOK, response)
|
||||
}
|
||||
|
||||
@@ -265,7 +279,19 @@ func (h *RegistrationHandlers) followup(c echo.Context, binder bind.Binder, req
|
||||
}
|
||||
|
||||
if m != nil && m.IsFinished() {
|
||||
response := tailcfg.RegisterResponse{MachineAuthorized: len(m.Error) != 0, Error: m.Error}
|
||||
user, err := h.repository.GetUser(ctx, m.UserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
u, l := mapping.ToUser(*user)
|
||||
|
||||
response := tailcfg.RegisterResponse{
|
||||
MachineAuthorized: len(m.Error) != 0,
|
||||
Error: m.Error,
|
||||
User: u,
|
||||
Login: l,
|
||||
}
|
||||
return binder.WriteResponse(c, http.StatusOK, response)
|
||||
}
|
||||
case <-notify:
|
||||
|
||||
@@ -226,6 +226,23 @@ func ToUserProfile(u domain.User) tailcfg.UserProfile {
|
||||
return profile
|
||||
}
|
||||
|
||||
func ToUser(u domain.User) (tailcfg.User, tailcfg.Login) {
|
||||
user := tailcfg.User{
|
||||
ID: tailcfg.UserID(u.ID),
|
||||
LoginName: u.Name,
|
||||
DisplayName: u.Name,
|
||||
Logins: []tailcfg.LoginID{tailcfg.LoginID(u.ID)},
|
||||
Domain: u.Tailnet.Name,
|
||||
}
|
||||
login := tailcfg.Login{
|
||||
ID: tailcfg.LoginID(u.ID),
|
||||
LoginName: u.Name,
|
||||
DisplayName: u.Name,
|
||||
Domain: u.Tailnet.Name,
|
||||
}
|
||||
return user, login
|
||||
}
|
||||
|
||||
func filterServices(services []tailcfg.Service) []tailcfg.Service {
|
||||
result := []tailcfg.Service{}
|
||||
for _, s := range services {
|
||||
|
||||
@@ -47,7 +47,7 @@ func (s *Service) DeleteUser(ctx context.Context, req *connect.Request[api.Delet
|
||||
principal := CurrentPrincipal(ctx)
|
||||
|
||||
if !principal.IsSystemAdmin() && principal.UserMatches(req.Msg.UserId) {
|
||||
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("unable delete yourself"))
|
||||
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("unable to delete yourself"))
|
||||
}
|
||||
|
||||
user, err := s.repository.GetUser(ctx, req.Msg.UserId)
|
||||
@@ -63,6 +63,10 @@ func (s *Service) DeleteUser(ctx context.Context, req *connect.Request[api.Delet
|
||||
return nil, connect.NewError(connect.CodePermissionDenied, fmt.Errorf("permission denied"))
|
||||
}
|
||||
|
||||
if user.UserType == domain.UserTypeService {
|
||||
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("unable delete service account"))
|
||||
}
|
||||
|
||||
err = s.repository.Transaction(func(tx domain.Repository) error {
|
||||
if err := tx.DeleteMachineByUser(ctx, req.Msg.UserId); err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user