fix(breaking): update the credentials filename

Save the credentials to the credentials.json file in the config
directory.
This commit is contained in:
Dan Anglin 2024-02-27 09:31:17 +00:00
parent e574ccaf2b
commit c38689fe28
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
5 changed files with 38 additions and 34 deletions

View file

@ -177,7 +177,7 @@ image::assets/images/consent_form.png[A screenshot of the consent form]
6. Paste the token into the prompt and press `ENTER`. 6. Paste the token into the prompt and press `ENTER`.
Enbas will then exchange the token for an access token which will be used to authentication to the Enbas will then exchange the token for an access token which will be used to authentication to the
GTS server on your behalf. GTS server on your behalf.
Enbas will then verify the access token, save the credentials to a file in your configuration directory, Enbas will then verify the access token, save the credentials to the `credentials.json` file in your configuration directory,
and confirm that you have successfully logged into your account. and confirm that you have successfully logged into your account.
+ +
[source,console] [source,console]

View file

@ -48,11 +48,11 @@ func (c *loginCommand) Execute() error {
instance = instance[:len(instance)-1] instance = instance[:len(instance)-1]
} }
authentication := config.Authentication{ credentials := config.Credentials{
Instance: instance, Instance: instance,
} }
gtsClient := client.NewClient(authentication) gtsClient := client.NewClient(credentials)
if err := gtsClient.Register(); err != nil { if err := gtsClient.Register(); err != nil {
return fmt.Errorf("unable to register the application; %w", err) return fmt.Errorf("unable to register the application; %w", err)
@ -91,7 +91,7 @@ Once you have the code please copy and paste it below.
return fmt.Errorf("unable to verify the credentials; %w", err) return fmt.Errorf("unable to verify the credentials; %w", err)
} }
loginName, err := config.SaveAuthentication(account.Username, gtsClient.Authentication) loginName, err := config.SaveCredentials(account.Username, gtsClient.Authentication)
if err != nil { if err != nil {
return fmt.Errorf("unable to save the authentication details; %w", err) return fmt.Errorf("unable to save the authentication details; %w", err)
} }

View file

@ -15,7 +15,7 @@ import (
) )
type Client struct { type Client struct {
Authentication config.Authentication Authentication config.Credentials
HTTPClient http.Client HTTPClient http.Client
UserAgent string UserAgent string
Timeout time.Duration Timeout time.Duration
@ -27,12 +27,12 @@ func NewClientFromConfig() (*Client, error) {
return nil, fmt.Errorf("unable to get the authentication configuration; %w", err) return nil, fmt.Errorf("unable to get the authentication configuration; %w", err)
} }
currentAuthentication := config.Authentications[config.CurrentAccount] currentAuthentication := config.Credentials[config.CurrentAccount]
return NewClient(currentAuthentication), nil return NewClient(currentAuthentication), nil
} }
func NewClient(authentication config.Authentication) *Client { func NewClient(authentication config.Credentials) *Client {
httpClient := http.Client{} httpClient := http.Client{}
gtsClient := Client{ gtsClient := Client{

View file

@ -11,33 +11,37 @@ import (
"codeflow.dananglin.me.uk/apollo/enbas/internal" "codeflow.dananglin.me.uk/apollo/enbas/internal"
) )
type AuthenticationConfig struct { const (
credentialsFileName = "credentials.json"
)
type CredentialsConfig struct {
CurrentAccount string `json:"currentAccount"` CurrentAccount string `json:"currentAccount"`
Authentications map[string]Authentication `json:"authentications"` Credentials map[string]Credentials `json:"credentials"`
} }
type Authentication struct { type Credentials struct {
Instance string `json:"instance"` Instance string `json:"instance"`
ClientID string `json:"clientId"` ClientID string `json:"clientId"`
ClientSecret string `json:"clientSecret"` ClientSecret string `json:"clientSecret"`
AccessToken string `json:"accessToken"` AccessToken string `json:"accessToken"`
} }
func SaveAuthentication(username string, authentication Authentication) (string, error) { func SaveCredentials(username string, credentials Credentials) (string, error) {
if err := ensureConfigDir(); err != nil { if err := ensureConfigDir(); err != nil {
return "", fmt.Errorf("unable to ensure the configuration directory; %w", err) return "", fmt.Errorf("unable to ensure the configuration directory; %w", err)
} }
var authConfig AuthenticationConfig var authConfig CredentialsConfig
filepath := authenticationConfigFile() filepath := credentialsConfigFile()
if _, err := os.Stat(filepath); err != nil { if _, err := os.Stat(filepath); err != nil {
if !errors.Is(err, os.ErrNotExist) { if !errors.Is(err, os.ErrNotExist) {
return "", fmt.Errorf("unknown error received when running stat on %s; %w", filepath, err) return "", fmt.Errorf("unknown error received when running stat on %s; %w", filepath, err)
} }
authConfig.Authentications = make(map[string]Authentication) authConfig.Credentials = make(map[string]Credentials)
} else { } else {
authConfig, err = NewAuthenticationConfigFromFile() authConfig, err = NewAuthenticationConfigFromFile()
if err != nil { if err != nil {
@ -47,38 +51,38 @@ func SaveAuthentication(username string, authentication Authentication) (string,
instance := "" instance := ""
if strings.HasPrefix(authentication.Instance, "https://") { if strings.HasPrefix(credentials.Instance, "https://") {
instance = strings.TrimPrefix(authentication.Instance, "https://") instance = strings.TrimPrefix(credentials.Instance, "https://")
} else if strings.HasPrefix(authentication.Instance, "http://") { } else if strings.HasPrefix(credentials.Instance, "http://") {
instance = strings.TrimPrefix(authentication.Instance, "http://") instance = strings.TrimPrefix(credentials.Instance, "http://")
} }
authenticationName := username + "@" + instance authenticationName := username + "@" + instance
authConfig.CurrentAccount = authenticationName authConfig.CurrentAccount = authenticationName
authConfig.Authentications[authenticationName] = authentication authConfig.Credentials[authenticationName] = credentials
if err := saveAuthenticationFile(authConfig); err != nil { if err := saveCredentialsConfigFile(authConfig); err != nil {
return "", fmt.Errorf("unable to save the authentication configuration to file; %w", err) return "", fmt.Errorf("unable to save the authentication configuration to file; %w", err)
} }
return authenticationName, nil return authenticationName, nil
} }
func NewAuthenticationConfigFromFile() (AuthenticationConfig, error) { func NewAuthenticationConfigFromFile() (CredentialsConfig, error) {
path := authenticationConfigFile() path := credentialsConfigFile()
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {
return AuthenticationConfig{}, fmt.Errorf("unable to open %s, %w", path, err) return CredentialsConfig{}, fmt.Errorf("unable to open %s, %w", path, err)
} }
defer file.Close() defer file.Close()
var authConfig AuthenticationConfig var authConfig CredentialsConfig
if err := json.NewDecoder(file).Decode(&authConfig); err != nil { if err := json.NewDecoder(file).Decode(&authConfig); err != nil {
return AuthenticationConfig{}, fmt.Errorf("unable to decode the JSON data; %w", err) return CredentialsConfig{}, fmt.Errorf("unable to decode the JSON data; %w", err)
} }
return authConfig, nil return authConfig, nil
@ -90,21 +94,21 @@ func UpdateCurrentAccount(account string) error {
return fmt.Errorf("unable to retrieve the existing authentication configuration; %w", err) return fmt.Errorf("unable to retrieve the existing authentication configuration; %w", err)
} }
if _, ok := authConfig.Authentications[account]; !ok { if _, ok := authConfig.Credentials[account]; !ok {
return fmt.Errorf("account %s is not found", account) return fmt.Errorf("account %s is not found", account)
} }
authConfig.CurrentAccount = account authConfig.CurrentAccount = account
if err := saveAuthenticationFile(authConfig); err != nil { if err := saveCredentialsConfigFile(authConfig); err != nil {
return fmt.Errorf("unable to save the authentication configuration to file; %w", err) return fmt.Errorf("unable to save the authentication configuration to file; %w", err)
} }
return nil return nil
} }
func authenticationConfigFile() string { func credentialsConfigFile() string {
return filepath.Join(configDir(), "authentications.json") return filepath.Join(configDir(), credentialsFileName)
} }
func configDir() string { func configDir() string {
@ -132,8 +136,8 @@ func ensureConfigDir() error {
return nil return nil
} }
func saveAuthenticationFile(authConfig AuthenticationConfig) error { func saveCredentialsConfigFile(authConfig CredentialsConfig) error {
file, err := os.Create(authenticationConfigFile()) file, err := os.Create(credentialsConfigFile())
if err != nil { if err != nil {
return fmt.Errorf("unable to open the config file; %w", err) return fmt.Errorf("unable to open the config file; %w", err)
} }

View file

@ -77,8 +77,8 @@ func (a Account) String() string {
%s %s %s %s
%s %s
%s %s`
`
metadata := "" metadata := ""
for _, field := range a.Fields { for _, field := range a.Fields {