Skip to content

Commit

Permalink
More human friendly token expiry date messages (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaperis authored Jan 31, 2025
2 parents 12d3ebc + 8476d35 commit d1639db
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
33 changes: 23 additions & 10 deletions helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,21 +398,34 @@ func CheckTokenExpiration(accessToken string) error {
}

switch untilExp := time.Until(expiration); {
case untilExp < 0:
return fmt.Errorf("the provided access token has expired, please renew it")
case untilExp > 0 && untilExp < 24*time.Hour:
fmt.Fprintln(
case untilExp >= 1*time.Minute && untilExp < time.Hour:
fmt.Fprintf(
os.Stderr,
"The provided access token expires in",
time.Until(expiration).Truncate(time.Second),
"WARNING! The provided access token expires in only %d minutes.\n",
int(untilExp.Minutes())-(int(untilExp.Hours())*60),
)
fmt.Fprintln(os.Stderr, "Consider renewing the token.")
default:
fmt.Fprintln(
case untilExp >= 1*time.Hour && untilExp < 2*time.Hour:
fmt.Fprintf(
os.Stderr,
"WARNING! The provided access token expires in only 1 hour and %d minutes.\n",
int(untilExp.Minutes())-(int(untilExp.Hours())*60),
)
fmt.Fprintln(os.Stderr, "Consider renewing the token.")
case untilExp >= 2*time.Hour && untilExp <= 24*time.Hour:
fmt.Fprintf(
os.Stderr,
"The provided access token expires in",
time.Until(expiration).Truncate(time.Second),
"The provided access token expires in %d hours and %d minutes.\n",
int(untilExp.Hours()), int(untilExp.Minutes())-(int(untilExp.Hours())*60),
)
fmt.Fprintln(os.Stderr, "Consider renewing the token.")
case untilExp > 24*time.Hour:
fmt.Fprintf(
os.Stderr,
"The provided access token expires on %s.\n", expiration.Format(time.RFC1123),
)
default:
return fmt.Errorf("the provided access token has expired, please renew it")
}

return nil
Expand Down
35 changes: 33 additions & 2 deletions helpers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/rand"
"crypto/rsa"
"fmt"
"io"
"log"
"os"
"runtime"
Expand Down Expand Up @@ -326,10 +327,40 @@ func (suite *HelperTests) TestTokenExpiration() {
err = CheckTokenExpiration(token)
assert.EqualError(suite.T(), err, "the provided access token has expired, please renew it")

// Token with valid expiration
token = generateDummyToken(time.Now().Add(time.Hour * 72).Unix())
// Token with valid expiration, less than 2 hours
token = generateDummyToken(time.Now().Add(time.Hour).Unix())

storeStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w

err = CheckTokenExpiration(token)
assert.NoError(suite.T(), err)

w.Close()
out, _ := io.ReadAll(r)
os.Stderr = storeStderr

msg := "WARNING! The provided access token expires in only 59 minutes."
assert.Contains(suite.T(), string(out), msg)

// Token with valid expiration, more than a day
exp := time.Now().Add(time.Hour * 72)
token = generateDummyToken(exp.Unix())

storeStderr = os.Stderr
r, w, _ = os.Pipe()
os.Stderr = w

err = CheckTokenExpiration(token)
assert.NoError(suite.T(), err)

w.Close()
out, _ = io.ReadAll(r)
os.Stderr = storeStderr

msg = "The provided access token expires on " + exp.Format(time.RFC1123)
assert.Contains(suite.T(), string(out), msg)
}

func (suite *HelperTests) TestPubKeyEmptyField() {
Expand Down

0 comments on commit d1639db

Please sign in to comment.