|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "errors" |
5 | 4 | "fmt" |
6 | | - "log/slog" |
7 | 5 |
|
8 | 6 | "github.com/opentdf/otdfctl/pkg/cli" |
9 | 7 | "github.com/opentdf/otdfctl/pkg/handlers" |
10 | 8 | "github.com/opentdf/otdfctl/pkg/man" |
11 | 9 | "github.com/spf13/cobra" |
12 | 10 | ) |
13 | 11 |
|
14 | | -var ( |
15 | | - clientCredentialsCmd = man.Docs.GetCommand("auth/client-credentials", |
16 | | - man.WithRun(auth_clientCredentials), |
17 | | - ) |
18 | | - noCacheCreds bool |
| 12 | +var clientCredentialsCmd = man.Docs.GetCommand("auth/client-credentials", |
| 13 | + man.WithRun(auth_clientCredentials), |
| 14 | + man.WithHiddenFlags("with-client-creds", "with-client-creds-file"), |
19 | 15 | ) |
20 | 16 |
|
21 | 17 | func auth_clientCredentials(cmd *cobra.Command, args []string) { |
22 | | - var err error |
| 18 | + var c handlers.ClientCredentials |
23 | 19 |
|
24 | 20 | flagHelper := cli.NewFlagHelper(cmd) |
25 | 21 | host := flagHelper.GetRequiredString("host") |
26 | 22 | tlsNoVerify := flagHelper.GetOptionalBool("tls-no-verify") |
27 | | - clientID := flagHelper.GetOptionalString("client-id") |
28 | | - clientSecret := flagHelper.GetOptionalString("client-secret") |
29 | 23 |
|
30 | | - slog.Debug("Checking for client credentials file", slog.String("with-client-creds-file", clientCredsFile)) |
31 | | - if clientCredsFile != "" { |
32 | | - creds, err := handlers.GetClientCredsFromFile(clientCredsFile) |
33 | | - if err != nil { |
34 | | - cli.ExitWithError("Failed to parse client credentials JSON", err) |
35 | | - } |
36 | | - clientID = creds.ClientID |
37 | | - clientSecret = creds.ClientSecret |
38 | | - } |
| 24 | + p := cli.NewPrinter(true) |
39 | 25 |
|
40 | | - // if not provided by flag, check keyring cache for clientID |
41 | | - if clientID == "" { |
42 | | - slog.Debug("No client-id provided. Attempting to retrieve the default from keyring.") |
43 | | - clientID, err = handlers.GetClientIDFromCache(host) |
44 | | - if err != nil || clientID == "" { |
45 | | - cli.ExitWithError("Please provide required flag: (client-id)", errors.New("no client-id found")) |
46 | | - } else { |
47 | | - slog.Debug(cli.SuccessMessage("Retrieved stored client-id from keyring")) |
48 | | - } |
| 26 | + if len(args) > 0 { |
| 27 | + c.ClientId = args[0] |
| 28 | + } |
| 29 | + if len(args) > 1 { |
| 30 | + c.ClientSecret = args[1] |
49 | 31 | } |
50 | 32 |
|
51 | | - // check if we have a clientSecret in the keyring, if a null value is passed in |
52 | | - if clientSecret == "" { |
53 | | - clientSecret, err = handlers.GetClientSecretFromCache(host, clientID) |
54 | | - if err == nil || clientSecret == "" { |
55 | | - cli.ExitWithError("Please provide required flag: (client-secret)", errors.New("no client-secret found")) |
56 | | - } else { |
57 | | - slog.Debug("Retrieved stored client-secret from keyring") |
58 | | - } |
| 33 | + if c.ClientId == "" { |
| 34 | + c.ClientId = cli.AskForInput("Enter client id: ") |
| 35 | + } |
| 36 | + if c.ClientSecret == "" { |
| 37 | + c.ClientSecret = cli.AskForSecret("Enter client secret: ") |
59 | 38 | } |
60 | 39 |
|
61 | | - slog.Debug("Attempting to login with client credentials", slog.String("client-id", clientID)) |
62 | | - if err := handlers.GetTokenWithClientCreds(cmd.Context(), host, clientID, clientSecret, tlsNoVerify); err != nil { |
| 40 | + p.Printf("Logging in with client ID and secret for %s... ", host) |
| 41 | + if _, err := handlers.GetTokenWithClientCreds(cmd.Context(), host, c, tlsNoVerify); err != nil { |
| 42 | + fmt.Println("failed") |
63 | 43 | cli.ExitWithError("An error occurred during login. Please check your credentials and try again", err) |
64 | 44 | } |
| 45 | + p.Println("ok") |
65 | 46 |
|
66 | | - fmt.Println(cli.SuccessMessage("Successfully logged in with client ID and secret")) |
| 47 | + p.Print("Storing client ID and secret in keyring... ") |
| 48 | + if err := handlers.NewKeyring(host).SetClientCredentials(c); err != nil { |
| 49 | + fmt.Println("failed") |
| 50 | + cli.ExitWithError("Failed to cache client credentials", err) |
| 51 | + } |
| 52 | + p.Println("ok") |
67 | 53 | } |
68 | 54 |
|
69 | 55 | func init() { |
70 | | - clientCredentialsCmd := man.Docs.GetCommand("auth/client-credentials", |
71 | | - man.WithRun(auth_clientCredentials), |
72 | | - // use the individual client-id and client-secret flags here instead of the global with-client-creds flag |
73 | | - man.WithHiddenFlags("with-client-creds", "with-client-creds-file"), |
74 | | - ) |
75 | | - clientCredentialsCmd.Flags().StringP( |
76 | | - clientCredentialsCmd.GetDocFlag("client-id").Name, |
77 | | - clientCredentialsCmd.GetDocFlag("client-id").Shorthand, |
78 | | - clientCredentialsCmd.GetDocFlag("client-id").Default, |
79 | | - clientCredentialsCmd.GetDocFlag("client-id").Description, |
80 | | - ) |
81 | | - clientCredentialsCmd.Flags().StringP( |
82 | | - clientCredentialsCmd.GetDocFlag("client-secret").Name, |
83 | | - clientCredentialsCmd.GetDocFlag("client-secret").Shorthand, |
84 | | - clientCredentialsCmd.GetDocFlag("client-secret").Default, |
85 | | - clientCredentialsCmd.GetDocFlag("client-secret").Description, |
86 | | - ) |
87 | | - clientCredentialsCmd.Flags().BoolVarP( |
88 | | - &noCacheCreds, |
89 | | - clientCredentialsCmd.GetDocFlag("no-cache").Name, |
90 | | - clientCredentialsCmd.GetDocFlag("no-cache").Shorthand, |
91 | | - clientCredentialsCmd.GetDocFlag("no-cache").DefaultAsBool(), |
92 | | - clientCredentialsCmd.GetDocFlag("no-cache").Description, |
93 | | - ) |
| 56 | + authCmd.AddCommand(&clientCredentialsCmd.Command) |
94 | 57 | } |
0 commit comments