Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions envsec/internal/envcli/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package envcli
import (
"fmt"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.jetpack.io/envsec/internal/envvar"
"go.jetpack.io/pkg/sandbox/auth"
Expand Down Expand Up @@ -101,10 +102,9 @@ func whoAmICmd() *cobra.Command {
return err
}

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

Expand Down
5 changes: 3 additions & 2 deletions envsec/internal/envcli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type cmdConfig struct {

func (f *configFlags) genConfig(ctx context.Context) (*cmdConfig, error) {
var tok *session.Token
var ok bool
var err error

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

tok = client.GetSession()
if tok == nil {
tok, ok = client.GetSession()
if !ok {
return nil, errors.Errorf(
"To use envsec you must log in (`envsec auth login`) or specify --project-id and --org-id",
)
Expand Down
5 changes: 4 additions & 1 deletion envsec/internal/envcli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ func initCmd() *cobra.Command {
if err != nil {
return err
}
tok := client.GetSession()
tok, ok := client.GetSession()
if !ok {
return errors.New("not logged in, run `envsec auth login`")
}

wd, err := os.Getwd()
if err != nil {
Expand Down
13 changes: 10 additions & 3 deletions pkg/sandbox/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,16 @@ func (c *Client) LogoutFlow() error {
return c.RevokeSession()
}

func (c *Client) GetSession() *session.Token {
// TODO: automatically refresh token as needed
return c.store.ReadToken(c.issuer, c.clientID)
// GetSession returns the current valid session token, if any. If token is expired,
// it will attempt to refresh it. If no token is found, or is unable to be refreshed,
// it will return nil and false.
// TODO: automatically refresh token as needed
func (c *Client) GetSession() (*session.Token, bool) {
tok := c.store.ReadToken(c.issuer, c.clientID)
if tok == nil || !tok.Valid() {
return nil, false
}
return tok, true
}

func (c *Client) RefreshSession() *session.Token {
Expand Down