reduce scope to read for now; confirm successful login

This commit is contained in:
Dan Anglin 2024-02-21 01:26:25 +00:00
parent d3c67562ec
commit 6c297e5242
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 12 additions and 9 deletions

View file

@ -21,9 +21,9 @@ type Authentication struct {
AccessToken string `json:"accessToken"`
}
func saveAuthenticationConfig(username string, authentication Authentication) error {
func saveAuthenticationConfig(username string, authentication Authentication) (string, error) {
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 config AuthenticationConfig
@ -32,14 +32,14 @@ func saveAuthenticationConfig(username string, authentication Authentication) er
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)
return "", fmt.Errorf("unknown error received when running stat on %s; %w", filepath, err)
}
config.Authentications = make(map[string]Authentication)
} else {
config, err = newAuthenticationConfigFromFile()
if err != nil {
return fmt.Errorf("unable to retrieve the existing authentication configuration; %w", err)
return "", fmt.Errorf("unable to retrieve the existing authentication configuration; %w", err)
}
}
@ -59,14 +59,14 @@ func saveAuthenticationConfig(username string, authentication Authentication) er
file, err := os.Create(authenticationConfigFile())
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)
}
if err := json.NewEncoder(file).Encode(config); err != nil {
return fmt.Errorf("unable to save the JSON data to the authentication config file; %w", err)
return "", fmt.Errorf("unable to save the JSON data to the authentication config file; %w", err)
}
return nil
return authenticationName, nil
}
func newAuthenticationConfigFromFile() (AuthenticationConfig, error) {

View file

@ -76,10 +76,13 @@ func loginWithOauth2() error {
return fmt.Errorf("unable to verify the credentials; %w", err)
}
if err := saveAuthenticationConfig(account.Username, client.authentication); err != nil {
loginName, err := saveAuthenticationConfig(account.Username, client.authentication)
if err != nil {
return fmt.Errorf("unable to save the authentication details; %w", err)
}
fmt.Printf("Successfully logged into %s\n", loginName)
return nil
}
@ -87,7 +90,7 @@ func authCodeURL(account Authentication) string {
config := oauth2.Config{
ClientID: account.ClientID,
ClientSecret: account.ClientSecret,
Scopes: []string{"read", "write"},
Scopes: []string{"read"},
RedirectURL: redirectUri,
Endpoint: oauth2.Endpoint{
AuthURL: account.Instance + "/oauth/authorize",