Skip to content

Commit

Permalink
feat: add HTTPClientFromContext + improved OtelHTTPClientFromContex…
Browse files Browse the repository at this point in the history
…t func
  • Loading branch information
BarcoMasile committed Jul 4, 2024
1 parent b36e498 commit fa1b3e8
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions pkg/authentication/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,31 @@ func PrincipalFromContext(ctx context.Context) *Principal {
return nil
}

value := ctx.Value(PrincipalContextKey)
if value == nil {
return nil
if value, ok := ctx.Value(PrincipalContextKey).(*Principal); ok {
return value
}

return value.(*Principal)
return nil
}

func OtelHTTPClientContext(ctx context.Context) context.Context {
return context.WithValue(ctx, oauth2.HTTPClient, otelHTTPClient)
}

func HTTPClientFromContext(ctx context.Context) *http.Client {
client := http.DefaultClient

if ctx == nil {
return client
}

if c, ok := ctx.Value(oauth2.HTTPClient).(*http.Client); ok {
client = c
}

return client
}

type OIDCProviderSupplier = func(ctx context.Context, issuer string) (*oidc.Provider, error)

type OAuth2Context struct {
Expand Down

0 comments on commit fa1b3e8

Please sign in to comment.