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

Adds a warning when API keys will expire soon #1086

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Enhances login to use the key expiration returned from API result
  • Loading branch information
fhats-stripe committed Jun 15, 2023
commit 738b51a50561c50f4e8ca2c702e2622b4a0f2846
3 changes: 0 additions & 3 deletions pkg/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ const (
// DateStringFormat is the format for expiredAt date
DateStringFormat = "2006-01-02"

// KeyValidInDays is the number of days the API key is valid for
KeyValidInDays = 90

// KeyManagementService is the key management service name
KeyManagementService = "Stripe CLI"
)
Expand Down
12 changes: 11 additions & 1 deletion pkg/login/client_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"fmt"
"github.com/stripe/stripe-cli/pkg/config"
"math"
"os"
"time"

"github.com/briandowns/spinner"

Expand Down Expand Up @@ -90,7 +92,15 @@ func (a *Authenticator) Login(ctx context.Context, links *Links) error {
} else {
ansi.StopSpinner(s, message, os.Stdout)
}
fmt.Println(ansi.Italic("Please note: this key will expire after 90 days, at which point you'll need to re-authenticate."))

keyValidityDurationDays := 90
if !res.KeyExpiration.IsZero() {
keyValidityDurationHours := res.KeyExpiration.Sub(time.Now()).Hours()
keyValidityDurationDays = int(math.Round(keyValidityDurationHours / 24.0))
}

keyDurationCourtesyMessage := fmt.Sprintf("Please note: this key will expire after %d days, at which point you'll need to re-authenticate.", keyValidityDurationDays)
fmt.Println(ansi.Italic(keyDurationCourtesyMessage))
return nil
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/login/keys/keytransfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type AsyncPollResult struct {
TestModeAPIKey string
Account *acct.Account
KeyExpiration time.Time
Err error
}

Expand Down Expand Up @@ -57,6 +58,7 @@ func (rt *RAKTransfer) AsyncPollKey(ctx context.Context, pollURL string, interva

ch <- AsyncPollResult{
TestModeAPIKey: response.TestModeAPIKey,
KeyExpiration: time.Unix(response.TestModeAPIKeyExpiry, 0),
Account: account,
Err: nil,
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/login/keys/polling.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ type PollAPIKeyResponse struct {
AccountID string `json:"account_id"`
AccountDisplayName string `json:"account_display_name"`
LiveModeAPIKey string `json:"livemode_key_secret"`
LiveModeAPIKeyExpiry int64 `json:"livemode_key_expiry"`
LiveModePublishableKey string `json:"livemode_key_publishable"`
TestModeAPIKey string `json:"testmode_key_secret"`
TestModeAPIKeyExpiry int64 `json:"testmode_key_expiry"`
TestModePublishableKey string `json:"testmode_key_publishable"`
}

Expand Down