@@ -13,6 +13,7 @@ import (
1313 internal "github.com/UpCloudLtd/upcloud-cli/v3/internal/service"
1414 "github.com/zalando/go-keyring"
1515
16+ "github.com/UpCloudLtd/upcloud-go-api/credentials"
1617 "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/client"
1718 "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/service"
1819 "github.com/adrg/xdg"
@@ -35,9 +36,6 @@ const (
3536
3637 // env vars custom prefix
3738 envPrefix = "UPCLOUD"
38-
39- // keyringServiceName is the name of the service to use when using the system keyring
40- keyringServiceName = "UpCloud"
4139)
4240
4341var (
@@ -100,24 +98,15 @@ func (s *Config) Load() error {
10098 }
10199 }
102100
103- // If no credentials are provided, check if token is stored in keyring
104- if v .GetString ("token" ) == "" && v .GetString ("username" ) == "" && v .GetString ("password" ) == "" {
105- token , err := keyring .Get (keyringServiceName , "" )
106- if err == nil {
107- if err := v .MergeConfigMap (map [string ]interface {}{"token" : token }); err != nil {
108- return fmt .Errorf ("unable to merge token from keyring: %w" , err )
109- }
110- }
111- }
112-
113- // If only username is provided, check if password is stored in keyring
114- if v .GetString ("username" ) != "" && v .GetString ("token" ) == "" && v .GetString ("password" ) == "" {
115- password , err := keyring .Get (keyringServiceName , v .GetString ("username" ))
116- if err == nil {
117- if err := v .MergeConfigMap (map [string ]interface {}{"password" : password }); err != nil {
118- return fmt .Errorf ("unable to merge password from keyring: %w" , err )
119- }
120- }
101+ creds , err := credentials .Parse (credentials.Credentials {
102+ Username : v .GetString ("username" ),
103+ Password : v .GetString ("password" ),
104+ Token : v .GetString ("token" ),
105+ })
106+ if err == nil {
107+ v .Set ("username" , creds .Username )
108+ v .Set ("password" , creds .Password )
109+ v .Set ("token" , creds .Token )
121110 }
122111
123112 v .Set ("config" , v .ConfigFileUsed ())
@@ -224,7 +213,7 @@ func (s *Config) CreateService() (internal.AllServices, error) {
224213 if s .GetString ("config" ) != "" {
225214 configDetails = fmt .Sprintf ("used %s" , s .GetString ("config" ))
226215 }
227- return nil , clierrors.MissingCredentialsError {ConfigFile : configDetails , ServiceName : keyringServiceName }
216+ return nil , clierrors.MissingCredentialsError {ConfigFile : configDetails , ServiceName : credentials . KeyringServiceName }
228217 }
229218
230219 configs := []client.ConfigFn {
@@ -253,7 +242,7 @@ func GetVersion() string {
253242}
254243
255244func SaveTokenToKeyring (token string ) error {
256- return keyring .Set (keyringServiceName , "" , token )
245+ return keyring .Set (credentials . KeyringServiceName , credentials . KeyringTokenUser , token )
257246}
258247
259248func getVersion () string {
0 commit comments