diff --git a/README.asciidoc b/README.asciidoc index b89ccd9..6fd09f6 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -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`. Enbas will then exchange the token for an access token which will be used to authentication to the 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. + [source,console] diff --git a/cmd/enbas/login.go b/cmd/enbas/login.go index 37ad419..1fa5107 100644 --- a/cmd/enbas/login.go +++ b/cmd/enbas/login.go @@ -48,11 +48,11 @@ func (c *loginCommand) Execute() error { instance = instance[:len(instance)-1] } - authentication := config.Authentication{ + credentials := config.Credentials{ Instance: instance, } - gtsClient := client.NewClient(authentication) + gtsClient := client.NewClient(credentials) if err := gtsClient.Register(); err != nil { 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) } - loginName, err := config.SaveAuthentication(account.Username, gtsClient.Authentication) + loginName, err := config.SaveCredentials(account.Username, gtsClient.Authentication) if err != nil { return fmt.Errorf("unable to save the authentication details; %w", err) } diff --git a/internal/client/client.go b/internal/client/client.go index d2ba66b..1878a73 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -15,7 +15,7 @@ import ( ) type Client struct { - Authentication config.Authentication + Authentication config.Credentials HTTPClient http.Client UserAgent string Timeout time.Duration @@ -27,12 +27,12 @@ func NewClientFromConfig() (*Client, error) { 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 } -func NewClient(authentication config.Authentication) *Client { +func NewClient(authentication config.Credentials) *Client { httpClient := http.Client{} gtsClient := Client{ diff --git a/internal/config/config.go b/internal/config/credentials.go similarity index 63% rename from internal/config/config.go rename to internal/config/credentials.go index d98538e..b228fa0 100644 --- a/internal/config/config.go +++ b/internal/config/credentials.go @@ -11,33 +11,37 @@ import ( "codeflow.dananglin.me.uk/apollo/enbas/internal" ) -type AuthenticationConfig struct { - CurrentAccount string `json:"currentAccount"` - Authentications map[string]Authentication `json:"authentications"` +const ( + credentialsFileName = "credentials.json" +) + +type CredentialsConfig struct { + CurrentAccount string `json:"currentAccount"` + Credentials map[string]Credentials `json:"credentials"` } -type Authentication struct { +type Credentials struct { Instance string `json:"instance"` ClientID string `json:"clientId"` ClientSecret string `json:"clientSecret"` 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 { 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 !errors.Is(err, os.ErrNotExist) { 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 { authConfig, err = NewAuthenticationConfigFromFile() if err != nil { @@ -47,38 +51,38 @@ func SaveAuthentication(username string, authentication Authentication) (string, instance := "" - if strings.HasPrefix(authentication.Instance, "https://") { - instance = strings.TrimPrefix(authentication.Instance, "https://") - } else if strings.HasPrefix(authentication.Instance, "http://") { - instance = strings.TrimPrefix(authentication.Instance, "http://") + if strings.HasPrefix(credentials.Instance, "https://") { + instance = strings.TrimPrefix(credentials.Instance, "https://") + } else if strings.HasPrefix(credentials.Instance, "http://") { + instance = strings.TrimPrefix(credentials.Instance, "http://") } authenticationName := username + "@" + instance 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 authenticationName, nil } -func NewAuthenticationConfigFromFile() (AuthenticationConfig, error) { - path := authenticationConfigFile() +func NewAuthenticationConfigFromFile() (CredentialsConfig, error) { + path := credentialsConfigFile() file, err := os.Open(path) 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() - var authConfig AuthenticationConfig + var authConfig CredentialsConfig 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 @@ -90,21 +94,21 @@ func UpdateCurrentAccount(account string) error { 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) } 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 nil } -func authenticationConfigFile() string { - return filepath.Join(configDir(), "authentications.json") +func credentialsConfigFile() string { + return filepath.Join(configDir(), credentialsFileName) } func configDir() string { @@ -132,8 +136,8 @@ func ensureConfigDir() error { return nil } -func saveAuthenticationFile(authConfig AuthenticationConfig) error { - file, err := os.Create(authenticationConfigFile()) +func saveCredentialsConfigFile(authConfig CredentialsConfig) error { + file, err := os.Create(credentialsConfigFile()) if err != nil { return fmt.Errorf("unable to open the config file; %w", err) } diff --git a/internal/model/account.go b/internal/model/account.go index 1dae43f..a87e29e 100644 --- a/internal/model/account.go +++ b/internal/model/account.go @@ -77,8 +77,8 @@ func (a Account) String() string { %s %s %s - %s -` + %s` + metadata := "" for _, field := range a.Fields {