Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh token helper #238

Merged
merged 2 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/gmailctl/cmd/api_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ type APIKeyProvider interface {
APIKey() string
}

// TokenRefresher allows to refresh API tokens if they are expired.
type TokenRefresher interface {
// RefreshToken refreshes the token if it is expired.
RefreshToken(ctx context.Context, cfgDir string) error
}

func openAPI() (*api.GmailAPI, error) {
srv, err := APIProvider.Service(context.Background(), cfgDir)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/gmailctl/cmd/apply_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const renameLabelWarning = `Warning: You are going to delete labels. This operat
irreversible, because it also removes those labels from messages.

If you are looking for renaming labels, please use the GMail UI.

`

// applyCmd represents the apply command
Expand Down Expand Up @@ -84,7 +85,7 @@ func apply(path string, interactive, test bool) error {
}

if len(diff.LabelsDiff.Removed) > 0 {
fmt.Println(renameLabelWarning)
fmt.Print(renameLabelWarning)
if !applyRemoveLabels {
return errors.WithDetails(errors.New("no changes have been made"),
"To protect you, deletion is disabled unless you\n"+
Expand Down
16 changes: 15 additions & 1 deletion cmd/gmailctl/cmd/init_cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"fmt"
"os"
"path"
Expand All @@ -10,7 +11,10 @@ import (
"github.com/mbrt/gmailctl/internal/data"
)

var initReset bool
var (
initReset bool
initRefreshExpired bool
)

// initCmd represents the init command
var initCmd = &cobra.Command{
Expand All @@ -23,6 +27,8 @@ setting up the API authorizations and initial settings.`,
var err error
if initReset {
err = resetConfig()
} else if initRefreshExpired {
err = refreshToken()
} else {
err = continueConfig()
}
Expand All @@ -37,6 +43,7 @@ func init() {

// Flags and configuration settings
initCmd.Flags().BoolVar(&initReset, "reset", false, "Reset the configuration.")
initCmd.Flags().BoolVar(&initRefreshExpired, "refresh-expired", false, "Refresh auth token if expired.")
}

func resetConfig() error {
Expand All @@ -58,6 +65,13 @@ func continueConfig() error {
return nil
}

func refreshToken() error {
if rt, ok := APIProvider.(TokenRefresher); ok {
return rt.RefreshToken(context.Background(), cfgDir)
}
return nil
}

func handleCfgDir() (err error) {
// Create the config dir
if _, err := os.Stat(cfgDir); err != nil {
Expand Down
18 changes: 18 additions & 0 deletions cmd/gmailctl/localcred/local_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/mbrt/gmailctl/cmd/gmailctl/cmd"
"github.com/mbrt/gmailctl/internal/engine/api"
"github.com/mbrt/gmailctl/internal/errors"
)

const (
Expand Down Expand Up @@ -88,6 +89,23 @@ func (Provider) ResetConfig(cfgDir string) error {
return nil
}

func (Provider) RefreshToken(ctx context.Context, cfgDir string) error {
auth, err := openCredentials(credentialsPath(cfgDir))
if err != nil {
return errors.WithDetails(fmt.Errorf("invalid credentials: %w", err),
"Please run 'gmailctl init' to initialize the credentials.")
}
svc, err := openToken(ctx, auth, tokenPath(cfgDir))
if err != nil {
return setupToken(auth, tokenPath(cfgDir))
}
// Check whether the token works by getting a label.
if _, err := svc.Users.Labels.Get("me", "INBOX").Context(ctx).Do(); err != nil {
return setupToken(auth, tokenPath(cfgDir))
}
return nil
}

func openCredentials(path string) (*api.Authenticator, error) {
cred, err := os.Open(path)
if err != nil {
Expand Down