Skip to content

Commit de89e93

Browse files
loretomikeland73
andauthored
Adds token refresh logic (#162)
## Summary Adds token refresh logic ## How was it tested? I'm actually unsure how best to test this other than going through the auth flow, waiting for my token to expire, and running again. Any ideas on how best to shortcut that process? Is there a way to purposefully give myself an expired token on the first try? --------- Co-authored-by: Mike Landau <mikeland86@gmail.com>
1 parent cf58e90 commit de89e93

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

envsec/internal/envcli/auth.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ func refreshCmd() *cobra.Command {
8383
return err
8484
}
8585

86-
_ = client.RefreshSession()
86+
_, ok := client.GetSession(cmd.Context())
87+
if !ok {
88+
return errors.New("Failed to refresh: not logged in. Run `envsec auth login` to log in")
89+
}
90+
fmt.Fprintln(cmd.OutOrStdout(), "Refreshed successfully")
8791
return nil
8892
},
8993
}
@@ -102,7 +106,7 @@ func whoAmICmd() *cobra.Command {
102106
return err
103107
}
104108

105-
tok, ok := client.GetSession()
109+
tok, ok := client.GetSession(cmd.Context())
106110
if !ok {
107111
return errors.New("not logged in. Run `envsec auth login` to log in")
108112
}

envsec/internal/envcli/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (f *configFlags) genConfig(ctx context.Context) (*cmdConfig, error) {
9090
return nil, err
9191
}
9292

93-
tok, ok = client.GetSession()
93+
tok, ok = client.GetSession(ctx)
9494
if !ok {
9595
return nil, errors.Errorf(
9696
"To use envsec you must log in (`envsec auth login`) or specify --project-id and --org-id",

envsec/internal/envcli/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func initCmd() *cobra.Command {
1919
if err != nil {
2020
return err
2121
}
22-
tok, ok := client.GetSession()
22+
tok, ok := client.GetSession(cmd.Context())
2323
if !ok {
2424
return errors.New("not logged in, run `envsec auth login`")
2525
}

pkg/sandbox/auth/auth.go

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package auth
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"path/filepath"
78

9+
"github.com/coreos/go-oidc/v3/oidc"
810
"go.jetpack.io/pkg/sandbox/auth/session"
11+
"golang.org/x/oauth2"
912

1013
"go.jetpack.io/pkg/sandbox/auth/internal/authflow"
1114
"go.jetpack.io/pkg/sandbox/auth/internal/callbackserver"
@@ -59,16 +62,60 @@ func (c *Client) LogoutFlow() error {
5962
// it will attempt to refresh it. If no token is found, or is unable to be refreshed,
6063
// it will return nil and false.
6164
// TODO: automatically refresh token as needed
62-
func (c *Client) GetSession() (*session.Token, bool) {
65+
func (c *Client) GetSession(ctx context.Context) (*session.Token, bool) {
6366
tok := c.store.ReadToken(c.issuer, c.clientID)
64-
if tok == nil || !tok.Valid() {
67+
if tok == nil {
6568
return nil, false
6669
}
70+
71+
// Refresh if the token is no longer valid:
72+
if !tok.Valid() {
73+
tok = c.refresh(ctx, tok)
74+
if !tok.Valid() {
75+
return nil, false
76+
}
77+
}
78+
6779
return tok, true
6880
}
6981

70-
func (c *Client) RefreshSession() *session.Token {
71-
panic("refresh session not implemented")
82+
func (c *Client) refresh(
83+
ctx context.Context,
84+
tok *session.Token,
85+
) *session.Token {
86+
if tok == nil {
87+
return nil
88+
}
89+
90+
// TODO: figure out how to share oidc provider and oauth2 client
91+
// with auth flow:
92+
provider, err := oidc.NewProvider(ctx, c.issuer)
93+
if err != nil {
94+
return tok
95+
}
96+
97+
conf := oauth2.Config{
98+
ClientID: c.clientID,
99+
Endpoint: provider.Endpoint(),
100+
Scopes: []string{"openid", "offline_access"},
101+
}
102+
103+
// Refresh logic:
104+
tokenSource := conf.TokenSource(ctx, &tok.Token)
105+
newToken, err := tokenSource.Token()
106+
if err != nil {
107+
return tok
108+
}
109+
110+
if newToken.AccessToken != tok.AccessToken {
111+
tok.Token = *newToken
112+
err = c.store.WriteToken(c.issuer, c.clientID, tok)
113+
if err != nil {
114+
return tok
115+
}
116+
}
117+
118+
return tok
72119
}
73120

74121
func (c *Client) RevokeSession() error {

0 commit comments

Comments
 (0)