Skip to content

Commit

Permalink
Dump request and response for token exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
toddkazakov committed Jun 6, 2024
1 parent 4c1d4f1 commit 86451e9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
26 changes: 26 additions & 0 deletions auth/service/api/v1/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package v1

import (
"fmt"
"github.com/tidepool-org/platform/log"
"net/http"
"net/http/httputil"
)

type DebuggingTransport struct {
Logger log.Logger
}

func (s *DebuggingTransport) RoundTrip(r *http.Request) (*http.Response, error) {
bytes, _ := httputil.DumpRequestOut(r, true)

resp, err := http.DefaultTransport.RoundTrip(r)
// err is returned after dumping the response

respBytes, _ := httputil.DumpResponse(resp, true)
bytes = append(bytes, respBytes...)

s.Logger.Debug(fmt.Sprintf("%s\n", string(bytes)))

return resp, err
}
6 changes: 6 additions & 0 deletions auth/service/api/v1/oauth.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package v1

import (
"context"
"fmt"
"golang.org/x/oauth2"
"net/http"
"net/url"
"path"
Expand Down Expand Up @@ -176,6 +178,10 @@ func (r *Router) OAuthProviderRedirectGet(res rest.ResponseWriter, req *rest.Req
}
}

// TODO: Remove.
transport := DebuggingTransport{Logger: log.LoggerFromContext(ctx)}
ctx = context.WithValue(ctx, oauth2.HTTPClient, http.Client{Transport: &transport})

oauthToken, err := prvdr.ExchangeAuthorizationCodeForToken(ctx, query.Get("code"))
if err != nil {
r.htmlOnError(res, req, err)
Expand Down
4 changes: 2 additions & 2 deletions auth/service/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ func (s *Service) initializeProviderFactory() error {
}

if prvdr, prvdrErr := twiistProvider.New(s.ConfigReporter().WithScopes("provider"), s.DataSourceClient(), s.TaskClient()); prvdrErr != nil {
s.Logger().WithError(prvdrErr).Warn("Unable to create dexcom provider")
s.Logger().WithError(prvdrErr).Warn("Unable to create twiist provider")
} else if prvdrErr = prvdrFctry.Add(prvdr); prvdrErr != nil {
return errors.Wrap(prvdrErr, "unable to add dexcom provider")
return errors.Wrap(prvdrErr, "unable to add twiist provider")
}

return nil
Expand Down
5 changes: 5 additions & 0 deletions oauth/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func NewProvider(name string, configReporter config.Reporter) (*Provider, error)
}
cfg.Scopes = SplitScopes(configReporter.GetWithDefault("scopes", ""))

authStyleInParams := configReporter.GetWithDefault("auth_style_in_params", "")
if authStyleInParams == "true" {
cfg.Endpoint.AuthStyle = oauth2.AuthStyleInParams
}

stateSalt := configReporter.GetWithDefault("state_salt", "")
if stateSalt == "" {
return nil, errors.New("state salt is missing")
Expand Down

0 comments on commit 86451e9

Please sign in to comment.