Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLOUDP-279516 Refresh bearer token inside store #3499

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion internal/cli/auth/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ package auth
import (
"context"
"io"
"net/http"

"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/oauth"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/transport"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/spf13/cobra"
atlas "go.mongodb.org/atlas/mongodbatlas"
Expand Down Expand Up @@ -53,7 +55,9 @@ type logoutOpts struct {

func (opts *logoutOpts) initFlow() error {
var err error
opts.flow, err = oauth.FlowWithConfig(config.Default())
client := http.DefaultClient
client.Transport = transport.Default()
opts.flow, err = oauth.FlowWithConfig(config.Default(), client)
return err
}

Expand Down
6 changes: 5 additions & 1 deletion internal/cli/refresher_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import (
"context"
"errors"
"fmt"
"net/http"

"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/oauth"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/transport"
atlasauth "go.mongodb.org/atlas/auth"
atlas "go.mongodb.org/atlas/mongodbatlas"
)
Expand All @@ -42,7 +44,9 @@ type Refresher interface {
func (opts *RefresherOpts) InitFlow(c oauth.ServiceGetter) func() error {
return func() error {
var err error
opts.flow, err = oauth.FlowWithConfig(c)
client := http.DefaultClient
client.Transport = transport.Default()
opts.flow, err = oauth.FlowWithConfig(c, client)
return err
}
}
Expand Down
5 changes: 1 addition & 4 deletions internal/oauth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"net/http"

"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/transport"
"go.mongodb.org/atlas/auth"
)

Expand All @@ -37,9 +36,7 @@ const (
GovClientID = "0oabtyfelbTBdoucy297" // GovClientID for production
)

func FlowWithConfig(c ServiceGetter) (*auth.Config, error) {
client := http.DefaultClient
client.Transport = transport.Default()
func FlowWithConfig(c ServiceGetter, client *http.Client) (*auth.Config, error) {
id := ClientID
if c.Service() == config.CloudGovService {
id = GovClientID
Expand Down
9 changes: 8 additions & 1 deletion internal/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ func (s *Store) httpClient(httpTransport http.RoundTripper) (*http.Client, error
return t.Client()
}
if s.accessToken != nil {
tr := transport.NewAccessTokenTransport(s.accessToken, httpTransport)
tr, err := transport.NewAccessTokenTransport(s.accessToken, httpTransport, func(t *atlasauth.Token) error {
config.SetAccessToken(t.AccessToken)
config.SetRefreshToken(t.RefreshToken)
return config.Save()
})
if err != nil {
return nil, err
}

return &http.Client{Transport: tr}, nil
}
Expand Down
39 changes: 33 additions & 6 deletions internal/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"time"

"github.com/mongodb-forks/digest"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/oauth"
atlasauth "go.mongodb.org/atlas/auth"
)

Expand Down Expand Up @@ -67,19 +69,44 @@ func NewDigestTransport(username, password string, base http.RoundTripper) *dige
}
}

func NewAccessTokenTransport(token *atlasauth.Token, base http.RoundTripper) http.RoundTripper {
return &tokenTransport{
token: token,
base: base,
func NewAccessTokenTransport(token *atlasauth.Token, base http.RoundTripper, saveToken func(*atlasauth.Token) error) (http.RoundTripper, error) {
client := http.DefaultClient
client.Transport = Default()

flow, err := oauth.FlowWithConfig(config.Default(), client)

if err != nil {
return nil, err
}

return &tokenTransport{
token: token,
base: base,
authConfig: flow,
saveToken: saveToken,
}, nil
}

type tokenTransport struct {
token *atlasauth.Token
base http.RoundTripper
token *atlasauth.Token
authConfig *atlasauth.Config
base http.RoundTripper
saveToken func(*atlasauth.Token) error
}

func (tr *tokenTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if !tr.token.Valid() {
token, _, err := tr.authConfig.RefreshToken(req.Context(), tr.token.RefreshToken)
if err != nil {
return nil, err
}
tr.token = token
if err := tr.saveToken(tr.token); err != nil {
return nil, err
}
}

tr.token.SetAuthHeader(req)

return tr.base.RoundTrip(req)
}
Loading