Skip to content

Commit 429adaf

Browse files
authored
[auth] change GetSession so it returns ok (#158)
## Summary This will make code more readable and reduce mistakes when using library. An invalid token is not super useful to client. Updated error messages for new interface. I still think an error is better (can help with refresh failures, etc) but @loreto you mentioned preferring this. ## How was it tested? Untested.
1 parent 19cc446 commit 429adaf

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

envsec/internal/envcli/auth.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package envcli
66
import (
77
"fmt"
88

9+
"github.com/pkg/errors"
910
"github.com/spf13/cobra"
1011
"go.jetpack.io/envsec/internal/envvar"
1112
"go.jetpack.io/pkg/sandbox/auth"
@@ -101,10 +102,9 @@ func whoAmICmd() *cobra.Command {
101102
return err
102103
}
103104

104-
tok := client.GetSession()
105-
if tok == nil {
106-
fmt.Fprintln(cmd.OutOrStdout(), "Not logged in")
107-
return nil
105+
tok, ok := client.GetSession()
106+
if !ok {
107+
return errors.New("not logged in. Run `envsec auth login` to log in")
108108
}
109109
idClaims := tok.IDClaims()
110110

envsec/internal/envcli/flags.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ type cmdConfig struct {
8181

8282
func (f *configFlags) genConfig(ctx context.Context) (*cmdConfig, error) {
8383
var tok *session.Token
84+
var ok bool
8485
var err error
8586

8687
if f.orgID == "" {
@@ -89,8 +90,8 @@ func (f *configFlags) genConfig(ctx context.Context) (*cmdConfig, error) {
8990
return nil, err
9091
}
9192

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

envsec/internal/envcli/init.go

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

2427
wd, err := os.Getwd()
2528
if err != nil {

pkg/sandbox/auth/auth.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,16 @@ func (c *Client) LogoutFlow() error {
5555
return c.RevokeSession()
5656
}
5757

58-
func (c *Client) GetSession() *session.Token {
59-
// TODO: automatically refresh token as needed
60-
return c.store.ReadToken(c.issuer, c.clientID)
58+
// GetSession returns the current valid session token, if any. If token is expired,
59+
// it will attempt to refresh it. If no token is found, or is unable to be refreshed,
60+
// it will return nil and false.
61+
// TODO: automatically refresh token as needed
62+
func (c *Client) GetSession() (*session.Token, bool) {
63+
tok := c.store.ReadToken(c.issuer, c.clientID)
64+
if tok == nil || !tok.Valid() {
65+
return nil, false
66+
}
67+
return tok, true
6168
}
6269

6370
func (c *Client) RefreshSession() *session.Token {

0 commit comments

Comments
 (0)