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

Adding custom JSON deserializer for UserInfo type #851

Merged

Conversation

willvedd
Copy link
Contributor

@willvedd willvedd commented Sep 19, 2023

🔧 Changes

As reported by #840, when testing sign-in with an Apple through the auth0 test login command, users will incur this error:

 failed to fetch user info: cannot decode response: json: cannot unmarshal string into Go struct field UserInfo.email_verified of type bool

This is caused by the /userinfo Authentication endpoint returning a string-encoded boolean value for Apple social connections; all other IdPs return a standard boolean value.

The solution here is to implement a custom JSON deserializer for the UserInfo type. The logic is largely ripped from the Auth0 Go SDK.

📚 References

🔬 Testing

Added unit test cases for normal boolean value, string-encoded boolean and error case.

📝 Checklist

  • All new/changed/fixed functionality is covered by tests (or N/A)
  • I have added documentation for all new/changed functionality (or N/A)

@willvedd willvedd requested a review from a team as a code owner September 19, 2023 21:22
@willvedd willvedd requested a review from ewanharris September 19, 2023 21:22
@codecov-commenter
Copy link

codecov-commenter commented Sep 19, 2023

Codecov Report

Patch coverage: 77.77% and project coverage change: +0.01% 🎉

Comparison is base (9cc4f17) 73.34% compared to head (286541e) 73.36%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #851      +/-   ##
==========================================
+ Coverage   73.34%   73.36%   +0.01%     
==========================================
  Files          92       92              
  Lines       11847    11874      +27     
==========================================
+ Hits         8689     8711      +22     
- Misses       2644     2647       +3     
- Partials      514      516       +2     
Files Changed Coverage Δ
internal/auth/authutil/user_info.go 76.47% <77.77%> (+1.47%) ⬆️

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +52 to +69
if alias.RawEmailVerified != nil {
var emailVerified bool
switch rawEmailVerified := alias.RawEmailVerified.(type) {
case bool:
emailVerified = rawEmailVerified
case string:
emailVerified, err = strconv.ParseBool(rawEmailVerified)
if err != nil {
return err
}
default:
return fmt.Errorf("email_verified field expected to be bool or string, got: %s", reflect.TypeOf(rawEmailVerified))
}
alias.EmailVerified = &emailVerified
}

return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😞 It's unfortunate that we have to do this...

@willvedd willvedd merged commit 66252ab into main Sep 20, 2023
6 checks passed
@willvedd willvedd deleted the gh-840-userInfo-deserializer-for-variable-email-verified-field branch September 20, 2023 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants