@@ -49,6 +49,37 @@ func (ag *AuthGroup) Login(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
// RegisterUser registers a new user with all permissions set to false.
|
||||
// @Summary Register user
|
||||
// @Description Registers a new user with login, password, name, last name. All permissions are set to false.
|
||||
// @Tags auth
|
||||
// @Accept json
|
||||
// @Param request body repository.UserRegister true "Registration data"
|
||||
// @Success 200 {object} map[string]string
|
||||
// @Failure 400 {object} map[string]string
|
||||
// @Failure 409 {object} map[string]string
|
||||
// @Failure 500 {object} map[string]string
|
||||
// @Router /auth/register [post]
|
||||
func (ag *AuthGroup) RegisterUser(c *gin.Context) {
|
||||
var req repository.UserRegister
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
if ag.Repo.ExistsByLogin(req.Login) {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "login already exists"})
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := ag.Repo.RegisterUser(req); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to register user"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "user registered"})
|
||||
}
|
||||
|
||||
// CreateToken creates a new user.
|
||||
// @Summary Create user
|
||||
// @Description Creates a new user with permissions
|
||||
|
||||
@@ -21,6 +21,22 @@ func NewScriptHandlers(svc *service.ScriptService, cmder *commander.Commander) S
|
||||
return ScriptHandlers{svc: svc, cmder: cmder}
|
||||
}
|
||||
|
||||
type RunScriptIn struct {
|
||||
AgentID string `json:"agent_id" binding:"required"`
|
||||
InterpreterID int64 `json:"interpreter_id" binding:"required"`
|
||||
ScriptText string `json:"script_text" binding:"required"`
|
||||
Stdin *string `json:"stdin"`
|
||||
}
|
||||
|
||||
type RunScriptOut struct {
|
||||
ID int64 `json:"id"`
|
||||
Command []string `json:"command"`
|
||||
Stdin *string `json:"stdin"`
|
||||
Stdout string `json:"stdout"`
|
||||
Stderr string `json:"stderr"`
|
||||
Status int32 `json:"status"`
|
||||
}
|
||||
|
||||
// RunScript executes a script on a target agent.
|
||||
// @Summary Run a script on an agent
|
||||
// @Description Resolves interpreter argv[] and sends the full command to the agent
|
||||
@@ -32,12 +48,6 @@ func NewScriptHandlers(svc *service.ScriptService, cmder *commander.Commander) S
|
||||
// @Router /scripts/run [post]
|
||||
func (self *ScriptHandlers) RunScript(c *gin.Context) {
|
||||
err := func() error {
|
||||
type RunScriptIn struct {
|
||||
AgentID string `json:"agent_id" binding:"required"`
|
||||
InterpreterID int64 `json:"interpreter_id" binding:"required"`
|
||||
ScriptText string `json:"script_text" binding:"required"`
|
||||
Stdin *string `json:"stdin"`
|
||||
}
|
||||
var in RunScriptIn
|
||||
if err := c.Bind(&in); err != nil {
|
||||
return err
|
||||
@@ -67,14 +77,6 @@ func (self *ScriptHandlers) RunScript(c *gin.Context) {
|
||||
return err
|
||||
}
|
||||
|
||||
type RunScriptOut struct {
|
||||
ID int64 `json:"id"`
|
||||
Command []string `json:"command"`
|
||||
Stdin *string `json:"stdin"`
|
||||
Stdout string `json:"stdout"`
|
||||
Stderr string `json:"stderr"`
|
||||
Status int32 `json:"status"`
|
||||
}
|
||||
c.JSON(http.StatusCreated, RunScriptOut{
|
||||
ID: job.ID,
|
||||
Command: job.Command,
|
||||
|
||||
@@ -25,6 +25,14 @@ type TokenCreate struct {
|
||||
IsActive bool `json:"is_active"`
|
||||
}
|
||||
|
||||
// UserRegister is the request body for public user registration (all permissions false).
|
||||
type UserRegister struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
LastName string `json:"last_name" binding:"required"`
|
||||
Login string `json:"login" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
|
||||
// TokenUpdate is the request body for updating an existing user.
|
||||
type TokenUpdate struct {
|
||||
Name string `json:"name"`
|
||||
|
||||
@@ -64,6 +64,34 @@ func (r *Repository) CreateToken(tc TokenCreate) (string, error) {
|
||||
return strconv.FormatInt(id, 10), nil
|
||||
}
|
||||
|
||||
// RegisterUser inserts a new user with all permissions set to false and is_active=false.
|
||||
func (r *Repository) RegisterUser(ur UserRegister) (string, error) {
|
||||
hashed, err := bcrypt.GenerateFromPassword([]byte(ur.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
token, err := utils.RandomToken()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
result, err := r.DB.Exec(
|
||||
`INSERT INTO tokens (name, last_name, login, password, token, permission_view, permission_manage_agent, permission_admin, is_active)
|
||||
VALUES (?, ?, ?, ?, ?, 0, 0, 0, 0)`,
|
||||
ur.Name, ur.LastName, ur.Login, string(hashed), token,
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strconv.FormatInt(id, 10), nil
|
||||
}
|
||||
|
||||
// Login authenticates by login/password, generates a new token, and returns LoginResponse.
|
||||
func (r *Repository) Login(login, password string) (*LoginResponse, error) {
|
||||
var t Tokens
|
||||
|
||||
Reference in New Issue
Block a user