fix(breaking): update the credentials filename
Save the credentials to the credentials.json file in the config directory.
This commit is contained in:
parent
e574ccaf2b
commit
c38689fe28
5 changed files with 38 additions and 34 deletions
|
@ -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]
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -77,8 +77,8 @@ func (a Account) String() string {
|
|||
%s %s
|
||||
|
||||
%s
|
||||
%s
|
||||
`
|
||||
%s`
|
||||
|
||||
metadata := ""
|
||||
|
||||
for _, field := range a.Fields {
|
||||
|
|
Loading…
Reference in a new issue