11package auth
22
33import (
4+ "errors"
45 "fmt"
56 "io"
67 "time"
@@ -10,6 +11,7 @@ import (
1011 "github.com/signadot/cli/internal/config"
1112 "github.com/signadot/cli/internal/spinner"
1213 sdkauth "github.com/signadot/go-sdk/client/auth"
14+ "github.com/signadot/go-sdk/client/orgs"
1315 "github.com/signadot/go-sdk/models"
1416 "github.com/spf13/cobra"
1517)
@@ -24,12 +26,64 @@ func newLogin(cfg *config.Auth) *cobra.Command {
2426 return runLogin (loginCfg , cmd .OutOrStdout ())
2527 },
2628 }
29+ loginCfg .AddFlags (cmd )
2730
2831 return cmd
2932}
3033
3134func runLogin (cfg * config.AuthLogin , out io.Writer ) error {
32- if err := cfg .UnauthInitAPIConfig (); err != nil {
35+ var err error
36+ if cfg .WithAPIKey != "" {
37+ err = apiKeyLogin (cfg , out )
38+ } else {
39+ err = bearerTokenLogin (cfg , out )
40+ }
41+ if err != nil {
42+ return err
43+ }
44+
45+ green := color .New (color .FgGreen ).SprintFunc ()
46+ fmt .Fprintf (out , "%s Successfully logged in\n " , green ("✓" ))
47+ return nil
48+ }
49+
50+ func apiKeyLogin (cfg * config.AuthLogin , out io.Writer ) error {
51+ // init the API client with the provided api key
52+ if err := cfg .InitAPIConfigWithApiKey (cfg .WithAPIKey ); err != nil {
53+ return err
54+ }
55+
56+ spin := spinner .Start (out , "Checking provided API key" )
57+ defer spin .Stop ()
58+
59+ // resolve the org from the api key
60+ res , err := cfg .Client .Orgs .GetOrgName (& orgs.GetOrgNameParams {}, nil )
61+ if err != nil {
62+ spin .StopFail ()
63+ return err
64+ }
65+ orgInfo := res .Payload
66+ if len (orgInfo .Orgs ) == 0 {
67+ spin .StopFail ()
68+ return errors .New ("could not resolve org from API key" )
69+ }
70+ org := orgInfo .Orgs [0 ]
71+
72+ // store the auth info
73+ err = auth .StoreAuthInKeyring (& auth.Auth {
74+ APIKey : cfg .WithAPIKey ,
75+ OrgName : org .Name ,
76+ })
77+ if err != nil {
78+ spin .StopFail ()
79+ return fmt .Errorf ("failed to store auth info: %w" , err )
80+ }
81+ return nil
82+ }
83+
84+ func bearerTokenLogin (cfg * config.AuthLogin , out io.Writer ) error {
85+ // init an unauthirezed API client
86+ if err := cfg .InitUnauthAPIConfig (); err != nil {
3387 return err
3488 }
3589
@@ -45,16 +99,16 @@ func runLogin(cfg *config.AuthLogin, out io.Writer) error {
4599 return err
46100 }
47101
48- // store the access token and org name
49- if err := auth .StoreToken (token .AccessToken ); err != nil {
50- return fmt .Errorf ("failed to store access token: %w" , err )
51- }
52- if err := auth .StoreOrg (token .OrgName ); err != nil {
53- return fmt .Errorf ("failed to store org name: %w" , err )
102+ // store the auth info
103+ expiresAt := time .Now ().Add (time .Duration (token .ExpiresIn ) * time .Second )
104+ err = auth .StoreAuthInKeyring (& auth.Auth {
105+ BearerToken : token .AccessToken ,
106+ OrgName : token .OrgName ,
107+ ExpiresAt : & expiresAt ,
108+ })
109+ if err != nil {
110+ return fmt .Errorf ("failed to store auth info: %w" , err )
54111 }
55-
56- green := color .New (color .FgGreen ).SprintFunc ()
57- fmt .Fprintf (out , "%s Successfully logged in\n " , green ("✓" ))
58112 return nil
59113}
60114
@@ -85,6 +139,7 @@ func waitForUserAuth(cfg *config.AuthLogin, out io.Writer,
85139
86140 res , err := cfg .Client .Auth .AuthDeviceGetToken (param )
87141 if err != nil {
142+ spin .StopFail ()
88143 return nil , fmt .Errorf ("couldn't get device token: %w" , err )
89144 }
90145 token := res .Payload
0 commit comments