Skip to content

Commit

Permalink
Support private email address that are not in api responses (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonydenyer authored and doomspork committed Nov 25, 2018
1 parent 689dcd1 commit c9e6ed0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
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

0 comments on commit c9e6ed0

Please sign in to comment.