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: 6 additions & 2 deletions envsec/internal/envcli/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ func refreshCmd() *cobra.Command {
return err
}

_ = client.RefreshSession()
_, ok := client.GetSession(cmd.Context())
if !ok {
return errors.New("Failed to refresh: not logged in. Run `envsec auth login` to log in")
}
fmt.Fprintln(cmd.OutOrStdout(), "Refreshed successfully")
return nil
},
}
Expand All @@ -102,7 +106,7 @@ func whoAmICmd() *cobra.Command {
return err
}

tok, ok := client.GetSession()
tok, ok := client.GetSession(cmd.Context())
if !ok {
return errors.New("not logged in. Run `envsec auth login` to log in")
}
Expand Down
2 changes: 1 addition & 1 deletion envsec/internal/envcli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (f *configFlags) genConfig(ctx context.Context) (*cmdConfig, error) {
return nil, err
}

tok, ok = client.GetSession()
tok, ok = client.GetSession(ctx)
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
2 changes: 1 addition & 1 deletion envsec/internal/envcli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func initCmd() *cobra.Command {
if err != nil {
return err
}
tok, ok := client.GetSession()
tok, ok := client.GetSession(cmd.Context())
if !ok {
return errors.New("not logged in, run `envsec auth login`")
}
Expand Down
55 changes: 51 additions & 4 deletions pkg/sandbox/auth/auth.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package auth

import (
"context"
"fmt"
"os"
"path/filepath"

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

"go.jetpack.io/pkg/sandbox/auth/internal/authflow"
"go.jetpack.io/pkg/sandbox/auth/internal/callbackserver"
Expand Down Expand Up @@ -59,16 +62,60 @@ func (c *Client) LogoutFlow() error {
// 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) {
func (c *Client) GetSession(ctx context.Context) (*session.Token, bool) {
tok := c.store.ReadToken(c.issuer, c.clientID)
if tok == nil || !tok.Valid() {
if tok == nil {
return nil, false
}

// Refresh if the token is no longer valid:
if !tok.Valid() {
tok = c.refresh(ctx, tok)
if !tok.Valid() {
return nil, false
}
}

return tok, true
}

func (c *Client) RefreshSession() *session.Token {
panic("refresh session not implemented")
func (c *Client) refresh(
ctx context.Context,
tok *session.Token,
) *session.Token {
if tok == nil {
return nil
}

// TODO: figure out how to share oidc provider and oauth2 client
// with auth flow:
provider, err := oidc.NewProvider(ctx, c.issuer)
if err != nil {
return tok
}

conf := oauth2.Config{
ClientID: c.clientID,
Endpoint: provider.Endpoint(),
Scopes: []string{"openid", "offline_access"},
}

// Refresh logic:
tokenSource := conf.TokenSource(ctx, &tok.Token)
newToken, err := tokenSource.Token()
if err != nil {
return tok
}

if newToken.AccessToken != tok.AccessToken {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this check needed?

tok.Token = *newToken
err = c.store.WriteToken(c.issuer, c.clientID, tok)
if err != nil {
return tok
}
}

return tok
}

func (c *Client) RevokeSession() error {
Expand Down