Skip to content

Commit

Permalink
fix: add missing tracing & attributes in oidc strategy (#3429)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-jonas authored Aug 16, 2023
1 parent ec85751 commit 09bcb71
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
3 changes: 3 additions & 0 deletions persistence/sql/persister_errorx.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/gofrs/uuid"
"github.com/pkg/errors"
"go.opentelemetry.io/otel/attribute"

"github.com/ory/jsonschema/v3"

Expand Down Expand Up @@ -44,6 +45,8 @@ func (p *Persister) CreateErrorContainer(ctx context.Context, csrfToken string,
return uuid.Nil, sqlcon.HandleError(err)
}

span.SetAttributes(attribute.String("id", c.ID.String()))

return c.ID, nil
}

Expand Down
33 changes: 23 additions & 10 deletions selfservice/strategy/oidc/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ import (
"path/filepath"
"strings"

"go.opentelemetry.io/otel/attribute"
"golang.org/x/oauth2"

"github.com/ory/kratos/cipher"
"github.com/ory/kratos/selfservice/sessiontokenexchange"
"github.com/ory/x/jsonnetsecure"
"github.com/ory/x/otelx"

"github.com/ory/kratos/text"

Expand Down Expand Up @@ -396,16 +400,7 @@ func (s *Strategy) handleCallback(w http.ResponseWriter, r *http.Request, ps htt
return
}

te, ok := provider.(TokenExchanger)
if !ok {
te, err = provider.OAuth2(r.Context())
if err != nil {
s.forwardError(w, r, req, s.handleError(w, r, req, pid, nil, err))
return
}
}

token, err := te.Exchange(r.Context(), code)
token, err := s.ExchangeCode(r.Context(), provider, code)
if err != nil {
s.forwardError(w, r, req, s.handleError(w, r, req, pid, nil, err))
return
Expand Down Expand Up @@ -460,6 +455,24 @@ func (s *Strategy) handleCallback(w http.ResponseWriter, r *http.Request, ps htt
}
}

func (s *Strategy) ExchangeCode(ctx context.Context, provider Provider, code string) (token *oauth2.Token, err error) {
ctx, span := s.d.Tracer(ctx).Tracer().Start(ctx, "strategy.oidc.ExchangeCode")
defer otelx.End(span, &err)
span.SetAttributes(attribute.String("provider_id", provider.Config().ID))
span.SetAttributes(attribute.String("provider_label", provider.Config().Label))

te, ok := provider.(TokenExchanger)
if !ok {
te, err = provider.OAuth2(ctx)
if err != nil {
return nil, err
}
}

token, err = te.Exchange(ctx, code)
return token, err
}

func (s *Strategy) populateMethod(r *http.Request, c *container.Container, message func(provider string) *text.Message) error {
conf, err := s.Config(r.Context())
if err != nil {
Expand Down

0 comments on commit 09bcb71

Please sign in to comment.