Skip to content

Commit 2934232

Browse files
authored
Conditionally fetch email (#78)
* Conditionally fetch email * Adapt functions to naming conventions * Add to changelog and readme
1 parent 8b96898 commit 2934232

3 files changed

Lines changed: 21 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## v8.8.2
8+
## v0.8.3
99

10-
* add user agent header [#76](https://github.com/ueberauth/ueberauth_github/pull/76)
10+
* Fix empty scope not allowing to proceed with an expected missing email [#77](https://github.com/ueberauth/ueberauth_github/issues/77)
11+
12+
## v0.8.2
13+
14+
* Add user agent header [#76](https://github.com/ueberauth/ueberauth_github/pull/76)
1115
* Update version in README [#70](https://github.com/ueberauth/ueberauth_github/pull/70)
1216
* Fix typos [#72](https://github.com/ueberauth/ueberauth_github/pull/72)
1317
* Relax version constraint on ueberauth [#71](https://github.com/ueberauth/ueberauth_github/pull/71)

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,12 @@ Or with options:
8787
8888
/auth/github?scope=user,public_repo
8989
90-
By default the requested scope is "user,public\_repo". This provides both read
90+
By default the requested scope is `"user,public\_repo"`. This provides both read
9191
and write access to the GitHub user profile details and public repos. For a
92-
read-only scope, either use "user:email" or an empty scope "". See more at
93-
[GitHub's OAuth Documentation](https://developer.github.com/apps/building-integrations/setting-up-and-registering-oauth-apps/about-scopes-for-oauth-apps/).
92+
read-only scope, either use `"user:email"` or an empty scope `""`. Empty scope
93+
will only request minimum public information which even excludes user's email address
94+
which results in a `nil` for `email` inside returned `%Ueberauth.Auth.Info{}`.
95+
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/).
9496
9597
Scope can be configured either explicitly as a `scope` query value on the
9698
request path or in your configuration:

lib/ueberauth/strategy/github.ex

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ defmodule Ueberauth.Strategy.Github do
175175
name: user["name"],
176176
description: user["bio"],
177177
nickname: user["login"],
178-
email: fetch_email!(user, allow_private_emails),
178+
email: maybe_fetch_email(user, allow_private_emails),
179179
location: user["location"],
180180
image: user["avatar_url"],
181181
urls: %{
@@ -220,19 +220,23 @@ defmodule Ueberauth.Strategy.Github do
220220
end
221221

222222
defp fetch_email!(user, allow_private_emails) do
223-
user["email"] ||
224-
get_primary_email!(user) ||
225-
get_private_email!(user, allow_private_emails) ||
223+
maybe_fetch_email(user, allow_private_emails) ||
226224
raise "Unable to access the user's email address"
227225
end
228226

229-
defp get_primary_email!(user) do
227+
defp maybe_fetch_email(user, allow_private_emails) do
228+
user["email"] ||
229+
maybe_get_primary_email(user) ||
230+
maybe_get_private_email(user, allow_private_emails)
231+
end
232+
233+
defp maybe_get_primary_email(user) do
230234
if user["emails"] && Enum.count(user["emails"]) > 0 do
231235
Enum.find(user["emails"], & &1["primary"])["email"]
232236
end
233237
end
234238

235-
defp get_private_email!(user, allow_private_emails) do
239+
defp maybe_get_private_email(user, allow_private_emails) do
236240
if allow_private_emails do
237241
"#{user["id"]}+#{user["login"]}@users.noreply.github.com"
238242
end

0 commit comments

Comments
 (0)