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

Support private email address that are not in api responses #42

Merged
merged 1 commit into from
Nov 25, 2018
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ config :ueberauth, Ueberauth,
]
```

## Private Emails
Github now allows you to keep your email address private. If you don't mind that you won't know a users email address you can specify `allow_private_emails`. This will set the users email as `id+username@users.noreply.github.com`.

```elixir
config :ueberauth, Ueberauth,
providers: [
github: {Ueberauth.Strategy.Github, [allow_private_emails: true]}
]
```

## License

Please see [LICENSE](https://github.com/ueberauth/ueberauth_github/blob/master/LICENSE) for licensing details.
25 changes: 17 additions & 8 deletions lib/ueberauth/strategy/github.ex
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,13 @@ defmodule Ueberauth.Strategy.Github do
"""
def info(conn) do
user = conn.private.github_user
allow_private_emails = Keyword.get(options(conn), :allow_private_emails, false)

%Info{
name: user["name"],
description: user["bio"],
nickname: user["login"],
email: fetch_email!(user),
email: fetch_email!(user, allow_private_emails),
location: user["location"],
image: user["avatar_url"],
urls: %{
Expand Down Expand Up @@ -200,25 +201,33 @@ defmodule Ueberauth.Strategy.Github do
}
end

defp fetch_uid("email", %{private: %{github_user: user}}) do
defp fetch_uid("email", conn) do
# private email will not be available as :email and must be fetched
fetch_email!(user)
allow_private_emails = Keyword.get(options(conn), :allow_private_emails, false)
fetch_email!(conn.private.github_user.user, allow_private_emails)
end

defp fetch_uid(field, conn) do
conn.private.github_user[field]
end

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

defp get_primary_email!(user) do
unless user["emails"] && (Enum.count(user["emails"]) > 0) do
raise "Unable to access the user's email address"
if user["emails"] && (Enum.count(user["emails"]) > 0) do
Enum.find(user["emails"], &(&1["primary"]))["email"]
end
end

Enum.find(user["emails"], &(&1["primary"]))["email"]
defp get_private_email!(user, allow_private_emails) do
if allow_private_emails do
"#{user["id"]}+#{user["login"]}@users.noreply.github.com"
end
end

defp fetch_user(conn, token) do
Expand Down