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

Conditionally fetch email #78

Merged
merged 3 commits into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v8.8.2
## v0.8.3

* add user agent header [#76](https://github.com/ueberauth/ueberauth_github/pull/76)
* Fix empty scope not allowing to proceed with an expected missing email [#77](https://github.com/ueberauth/ueberauth_github/issues/77)

## v0.8.2

* Add user agent header [#76](https://github.com/ueberauth/ueberauth_github/pull/76)
* Update version in README [#70](https://github.com/ueberauth/ueberauth_github/pull/70)
* Fix typos [#72](https://github.com/ueberauth/ueberauth_github/pull/72)
* Relax version constraint on ueberauth [#71](https://github.com/ueberauth/ueberauth_github/pull/71)
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ Or with options:

/auth/github?scope=user,public_repo

By default the requested scope is "user,public\_repo". This provides both read
By default the requested scope is `"user,public\_repo"`. This provides both read
and write access to the GitHub user profile details and public repos. For a
read-only scope, either use "user:email" or an empty scope "". See more at
[GitHub's OAuth Documentation](https://developer.github.com/apps/building-integrations/setting-up-and-registering-oauth-apps/about-scopes-for-oauth-apps/).
read-only scope, either use `"user:email"` or an empty scope `""`. Empty scope
will only request minimum public information which even excludes user's email address
which results in a `nil` for `email` inside returned `%Ueberauth.Auth.Info{}`.
See more at [GitHub's OAuth Documentation](https://developer.github.com/apps/building-integrations/setting-up-and-registering-oauth-apps/about-scopes-for-oauth-apps/).

Scope can be configured either explicitly as a `scope` query value on the
request path or in your configuration:
Expand Down
16 changes: 10 additions & 6 deletions lib/ueberauth/strategy/github.ex
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ defmodule Ueberauth.Strategy.Github do
name: user["name"],
description: user["bio"],
nickname: user["login"],
email: fetch_email!(user, allow_private_emails),
email: maybe_fetch_email(user, allow_private_emails),
Copy link
Member

Choose a reason for hiding this comment

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

Is there any reason we couldn't update fetch_email! to avoid raising an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is one of two places where fetch_email! used to be used. The other one seems to be a legit place for raising with the expectation of the field existence.

Also, get_primary_email! and get_private_email! do no raise, so I could clean them up to have no ! in the names and call maybe_fetch_email from fetch_email!.

location: user["location"],
image: user["avatar_url"],
urls: %{
Expand Down Expand Up @@ -220,19 +220,23 @@ defmodule Ueberauth.Strategy.Github do
end

defp fetch_email!(user, allow_private_emails) do
user["email"] ||
get_primary_email!(user) ||
get_private_email!(user, allow_private_emails) ||
maybe_fetch_email(user, allow_private_emails) ||
raise "Unable to access the user's email address"
end

defp get_primary_email!(user) do
defp maybe_fetch_email(user, allow_private_emails) do
user["email"] ||
maybe_get_primary_email(user) ||
maybe_get_private_email(user, allow_private_emails)
end

defp maybe_get_primary_email(user) do
if user["emails"] && Enum.count(user["emails"]) > 0 do
Enum.find(user["emails"], & &1["primary"])["email"]
end
end

defp get_private_email!(user, allow_private_emails) do
defp maybe_get_private_email(user, allow_private_emails) do
if allow_private_emails do
"#{user["id"]}+#{user["login"]}@users.noreply.github.com"
end
Expand Down