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
|
package register
import (
"context"
"log"
"time"
"code.crute.us/mcrute/golib/cli"
"code.crute.us/mcrute/golib/db/mongodb/v2"
glecho "code.crute.us/mcrute/golib/echo"
"code.crute.us/mcrute/ssh-proxy/app"
"code.crute.us/mcrute/ssh-proxy/app/models"
"github.com/spf13/cobra"
)
func Register(root *cobra.Command) {
registerCmd := &cobra.Command{
Use: "register username",
Short: "Create registration invite for user",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
cfg := app.Config{}
cli.MustGetConfig(c, &cfg)
registerMain(cfg, args[0])
},
}
cli.AddFlags(registerCmd, &app.Config{}, app.DefaultConfig, "register")
root.AddCommand(registerCmd)
}
func registerMain(cfg app.Config, username string) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
vc, err := glecho.MakeVaultSecretsClient(ctx)
if err != nil {
log.Fatalf("Error making vault client %s", err)
}
mongo, err := mongodb.Connect(ctx, cfg.MongoDbUri, vc)
if err != nil {
log.Fatalf("Error connecting to mongodb: %s", err)
}
userStore := &mongodb.MongoDbBasicStore[*models.User]{
Db: mongo,
CollectionName: "users",
}
authSessionStore := &models.AuthSessionStoreMongodb{
MongoDbBasicStore: &mongodb.MongoDbBasicStore[*models.AuthSession]{
Db: mongo,
CollectionName: "oauth_sessions",
},
}
if _, err := userStore.Get(ctx, username); err != nil {
log.Fatalf("User %s does not exist", username)
}
authSession := models.NewAuthSession("invite-only", time.Now().Add(cfg.InviteTimeout))
authSession.IsRegistration = true
authSession.UserId = username
if err := authSessionStore.Upsert(ctx, authSession); err != nil {
log.Fatalf("Error inserting registration: %s", err)
}
log.Printf("Invitation created, user code is: %s", authSession.UserCode)
}
|