-
Notifications
You must be signed in to change notification settings - Fork 6
/
auth_add_client.go
101 lines (82 loc) · 2.86 KB
/
auth_add_client.go
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
package cli
import (
"context"
authStore "github.com/gabor-boros/go-oauth2-pg"
"github.com/go-oauth2/oauth2/v4/models"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/spf13/cobra"
"go.uber.org/zap"
"github.com/opcotech/elemo/internal/model"
)
// authAddClient represents the addClient command
var authAddClient = &cobra.Command{
Use: "add-client",
Short: "Add new OAuth 2.0 client",
Long: `Add a new OAuth 2.0 client to the database. The client ID and secret will be
generated automatically. The client ID and secret will be printed to the
standard output.
Examples:
# Create a new client
elemo auth add-client --callback-url https://example.com/callback
# Create a new public client for the domain example.com
elemo auth add-client --domain example.com --public`,
Run: func(cmd *cobra.Command, _ []string) {
callbackURL, err := cmd.Flags().GetString("callback-url")
if err != nil {
logger.Fatal("failed to get callback-url flag value", zap.Error(err))
}
public, err := cmd.Flags().GetBool("public")
if err != nil {
logger.Fatal("failed to get public flag value", zap.Error(err))
}
userID, err := cmd.Flags().GetString("user-id")
if err != nil {
logger.Fatal("failed to get user-id flag value", zap.Error(err))
}
if callbackURL == "" {
logger.Fatal("callback-url is required")
}
initTracer("cli-auth-add-client")
_, relDBPool, err := initRelationalDatabase()
if err != nil {
logger.Fatal("failed to initialize relational database", zap.Error(err))
}
clientStore, err := authStore.NewClientStore(
authStore.WithClientStoreConnPool(relDBPool.(*pgxpool.Pool)),
authStore.WithClientStoreTable(authStore.DefaultClientStoreTable),
authStore.WithClientStoreLogger(&authStoreLogger{
logger: logger.Named("auth_store"),
}),
)
if err != nil {
logger.Fatal("failed to initialize client store", zap.Error(err))
}
if err := clientStore.InitTable(context.Background()); err != nil {
logger.Fatal("failed to initialize client store", zap.Error(err))
}
client := &models.Client{
ID: model.NewRawID(),
Secret: uuid.New().String(),
Domain: callbackURL,
Public: public,
UserID: userID,
}
if err = clientStore.Create(client); err != nil {
logger.Fatal("failed to create client", zap.Error(err))
}
logger.Info("client created successfully",
zap.String("callback-url", client.GetDomain()),
zap.Bool("public", client.IsPublic()),
zap.String("user-id", client.GetUserID()),
zap.String("client-id", client.GetID()),
zap.String("client-secret", client.GetSecret()),
)
},
}
func init() {
authCmd.AddCommand(authAddClient)
authAddClient.Flags().StringP("user-id", "u", "", "User ID of the client")
authAddClient.Flags().StringP("callback-url", "c", "", "Callback URL of the client")
authAddClient.Flags().BoolP("public", "p", false, "Set the client as public")
}