Skip to content

Commit

Permalink
Check if ClientAssertionType is empty and add required check
Browse files Browse the repository at this point in the history
  • Loading branch information
ewanharris committed Dec 13, 2023
1 parent 62f9bd3 commit 716bdad
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
8 changes: 6 additions & 2 deletions authentication/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (a *Authentication) addClientAuthenticationToURLValues(params oauth.ClientA
body.Set("client_assertion", clientAssertion)
body.Set("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer")
break
case params.ClientAssertion != "":
case params.ClientAssertion != "" && params.ClientAssertionType != "":
body.Set("client_assertion", params.ClientAssertion)
body.Set("client_assertion_type", params.ClientAssertionType)
break
Expand All @@ -284,7 +284,7 @@ func (a *Authentication) addClientAuthenticationToURLValues(params oauth.ClientA
}

// Helper for adding client authentication to an oauth.ClientAuthentication struct.
func (a *Authentication) addClientAuthenticationToClientAuthStruct(params *oauth.ClientAuthentication) error {
func (a *Authentication) addClientAuthenticationToClientAuthStruct(params *oauth.ClientAuthentication, required bool) error {
if params.ClientID == "" {
params.ClientID = a.clientID
}
Expand All @@ -306,6 +306,10 @@ func (a *Authentication) addClientAuthenticationToClientAuthStruct(params *oauth
params.ClientSecret = a.clientSecret
}

if required && (params.ClientSecret == "" && params.ClientAssertion == "") {
return errors.New("client_secret or client_assertion is required but not provided")
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion authentication/mfa.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (m *MFA) Challenge(ctx context.Context, body mfa.ChallengeRequest, opts ...
return nil, fmt.Errorf("Missing required fields: %s", strings.Join(missing, ", "))
}

err = m.authentication.addClientAuthenticationToClientAuthStruct(&body.ClientAuthentication)
err = m.authentication.addClientAuthenticationToClientAuthStruct(&body.ClientAuthentication, false)

if err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions authentication/passwordless.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Passwordless manager
//
// See: https://auth0.com/docs/api/authentication?http#get-code-or-link
func (p *Passwordless) SendEmail(ctx context.Context, params passwordless.SendEmailRequest, opts ...RequestOption) (r *passwordless.SendEmailResponse, err error) {
err = p.authentication.addClientAuthenticationToClientAuthStruct(&params.ClientAuthentication)
err = p.authentication.addClientAuthenticationToClientAuthStruct(&params.ClientAuthentication, false)
if err != nil {
return nil, err
}
Expand All @@ -33,7 +33,7 @@ func (p *Passwordless) SendEmail(ctx context.Context, params passwordless.SendEm
//
// See: https://auth0.com/docs/api/authentication?http#authenticate-user
func (p *Passwordless) LoginWithEmail(ctx context.Context, params passwordless.LoginWithEmailRequest, validationOptions oauth.IDTokenValidationOptions, opts ...RequestOption) (t *oauth.TokenSet, err error) {
err = p.authentication.addClientAuthenticationToClientAuthStruct(&params.ClientAuthentication)
err = p.authentication.addClientAuthenticationToClientAuthStruct(&params.ClientAuthentication, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -64,7 +64,7 @@ func (p *Passwordless) LoginWithEmail(ctx context.Context, params passwordless.L
//
// See: https://auth0.com/docs/api/authentication?http#get-code-or-link
func (p *Passwordless) SendSMS(ctx context.Context, params passwordless.SendSMSRequest, opts ...RequestOption) (r *passwordless.SendSMSResponse, err error) {
err = p.authentication.addClientAuthenticationToClientAuthStruct(&params.ClientAuthentication)
err = p.authentication.addClientAuthenticationToClientAuthStruct(&params.ClientAuthentication, false)
if err != nil {
return nil, err
}
Expand All @@ -79,7 +79,7 @@ func (p *Passwordless) SendSMS(ctx context.Context, params passwordless.SendSMSR
//
// See: https://auth0.com/docs/api/authentication?http#authenticate-user
func (p *Passwordless) LoginWithSMS(ctx context.Context, params passwordless.LoginWithSMSRequest, validationOptions oauth.IDTokenValidationOptions, opts ...RequestOption) (t *oauth.TokenSet, err error) {
err = p.authentication.addClientAuthenticationToClientAuthStruct(&params.ClientAuthentication)
err = p.authentication.addClientAuthenticationToClientAuthStruct(&params.ClientAuthentication, false)

if err != nil {
return nil, err
Expand Down

0 comments on commit 716bdad

Please sign in to comment.