Skip to content

Commit

Permalink
fix: handling of tokens revoked before expiration (jsdelivr#134)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Kolárik <[email protected]>
  • Loading branch information
radulucut and MartinKolarik authored Sep 25, 2024
1 parent be19e61 commit 8cfd0de
Show file tree
Hide file tree
Showing 12 changed files with 546 additions and 150 deletions.
4 changes: 3 additions & 1 deletion cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"errors"
"math"
"syscall"

"github.com/jsdelivr/globalping-cli/globalping"
Expand Down Expand Up @@ -89,7 +90,7 @@ func (r *Root) RunAuthStatus(cmd *cobra.Command, args []string) error {
res, err := r.client.TokenIntrospection("")
if err != nil {
e, ok := err.(*globalping.AuthorizeError)
if ok && e.ErrorType == "not_authorized" {
if ok && e.ErrorType == globalping.ErrTypeNotAuthorized {
r.printer.Println("Not logged in.")
return nil
}
Expand Down Expand Up @@ -131,6 +132,7 @@ func (r *Root) loginWithToken() error {
profile := r.storage.GetProfile()
profile.Token = &globalping.Token{
AccessToken: token,
Expiry: r.utils.Now().Add(math.MaxInt64),
}
err = r.storage.SaveConfig()
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion cmd/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"bytes"
"context"
"math"
"os"
"syscall"
"testing"
Expand All @@ -23,6 +24,9 @@ func Test_Auth_Login_WithToken(t *testing.T) {

gbMock := mocks.NewMockClient(ctrl)

utilsMock := mocks.NewMockUtils(ctrl)
utilsMock.EXPECT().Now().Return(defaultCurrentTime).AnyTimes()

w := new(bytes.Buffer)
r := new(bytes.Buffer)
r.WriteString("token\n")
Expand All @@ -39,7 +43,7 @@ func Test_Auth_Login_WithToken(t *testing.T) {
RefreshToken: "oldRefreshToken",
}

root := NewRoot(printer, ctx, nil, nil, gbMock, nil, _storage)
root := NewRoot(printer, ctx, nil, utilsMock, gbMock, nil, _storage)

gbMock.EXPECT().TokenIntrospection("token").Return(&globalping.IntrospectionResponse{
Active: true,
Expand All @@ -59,6 +63,7 @@ Logged in as test.
assert.Equal(t, &storage.Profile{
Token: &globalping.Token{
AccessToken: "token",
Expiry: defaultCurrentTime.Add(math.MaxInt64),
},
}, profile)
}
Expand Down
18 changes: 13 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"math"
"os"
"os/signal"
"syscall"
Expand Down Expand Up @@ -49,12 +50,19 @@ func Execute() {
Limit: 1,
}
t := time.NewTicker(10 * time.Second)
token := profile.Token
if config.GlobalpingToken != "" {
token = &globalping.Token{
AccessToken: config.GlobalpingToken,
ExpiresIn: math.MaxInt64,
Expiry: time.Now().Add(math.MaxInt64),
}
}
globalpingClient := globalping.NewClientWithCacheCleanup(globalping.Config{
APIURL: config.GlobalpingAPIURL,
AuthURL: config.GlobalpingAuthURL,
DashboardURL: config.GlobalpingDashboardURL,
AuthAccessToken: config.GlobalpingToken,
AuthToken: profile.Token,
APIURL: config.GlobalpingAPIURL,
AuthURL: config.GlobalpingAuthURL,
DashboardURL: config.GlobalpingDashboardURL,
AuthToken: token,
OnTokenRefresh: func(token *globalping.Token) {
profile.Token = token
err := localStorage.SaveConfig()
Expand Down
Loading

0 comments on commit 8cfd0de

Please sign in to comment.