This repository was archived by the owner on Jul 18, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathadmin.go
More file actions
122 lines (93 loc) · 2.69 KB
/
Copy pathadmin.go
File metadata and controls
122 lines (93 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"context"
"errors"
"fmt"
"log/slog"
"github.com/urfave/cli/v3"
"github.com/SecurityBrewery/catalyst/app/auth/password"
"github.com/SecurityBrewery/catalyst/app/database/sqlc"
)
func adminCreate(ctx context.Context, command *cli.Command) error {
catalyst, cleanup, err := setup(ctx, command)
if err != nil {
return fmt.Errorf("failed to setup catalyst: %w", err)
}
defer cleanup()
if command.Args().Len() != 2 {
return errors.New("usage: catalyst admin create <email> <password>")
}
name, email := command.Args().Get(0), command.Args().Get(0)
pw := command.Args().Get(1)
passwordHash, tokenKey, err := password.Hash(pw)
if err != nil {
return errors.New("failed to hash password: " + err.Error())
}
user, err := catalyst.Queries.CreateUser(ctx, sqlc.CreateUserParams{
Name: &name,
Email: &email,
Username: "admin",
PasswordHash: passwordHash,
TokenKey: tokenKey,
Active: true,
})
if err != nil {
return err
}
if err := catalyst.Queries.AssignGroupToUser(ctx, sqlc.AssignGroupToUserParams{
UserID: user.ID,
GroupID: "admin",
}); err != nil {
return err
}
slog.InfoContext(ctx, "Creating admin", "id", user.ID, "email", user.Email)
return nil
}
func adminSetPassword(ctx context.Context, command *cli.Command) error {
catalyst, cleanup, err := setup(ctx, command)
if err != nil {
return fmt.Errorf("failed to setup catalyst: %w", err)
}
defer cleanup()
if command.Args().Len() != 2 {
return errors.New("usage: catalyst admin set-password <email> <password>")
}
mail := command.Args().Get(0)
user, err := catalyst.Queries.UserByEmail(ctx, &mail)
if err != nil {
return err
}
passwordHash, tokenKey, err := password.Hash(command.Args().Get(1))
if err != nil {
return errors.New("failed to hash password: " + err.Error())
}
if _, err := catalyst.Queries.UpdateUser(ctx, sqlc.UpdateUserParams{
ID: user.ID,
PasswordHash: &passwordHash,
TokenKey: &tokenKey,
}); err != nil {
return err
}
slog.InfoContext(ctx, "Setting password for admin", "id", user.ID, "email", user.Email)
return nil
}
func adminDelete(ctx context.Context, command *cli.Command) error {
catalyst, cleanup, err := setup(ctx, command)
if err != nil {
return fmt.Errorf("failed to setup catalyst: %w", err)
}
defer cleanup()
if command.Args().Len() != 1 {
return errors.New("usage: catalyst admin delete <email>")
}
mail := command.Args().Get(0)
user, err := catalyst.Queries.UserByEmail(ctx, &mail)
if err != nil {
return err
}
if err := catalyst.Queries.DeleteUser(ctx, user.ID); err != nil {
return err
}
slog.InfoContext(ctx, "Deleted admin", "id", user.ID, "email", mail)
return nil
}