Skip to content

Commit 8f7decd

Browse files
committed
fix: better mapping of claims to %Ueberauth.Auth.Info{}
1 parent 3691107 commit 8f7decd

3 files changed

Lines changed: 45 additions & 9 deletions

File tree

‎lib/ueberauth/strategy/oidc.ex‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,26 @@ defmodule Ueberauth.Strategy.OIDC do
177177
userinfo = conn.private[:ueberauth_oidc_userinfo] || %{}
178178
claims = Map.merge(conn.private.ueberauth_oidc_claims, userinfo)
179179

180-
%Info{}
181-
|> Map.from_struct()
182-
|> Enum.reduce(%Info{}, fn {k, v}, struct ->
183-
string_key = Atom.to_string(k)
184-
Map.put(struct, k, Map.get(claims, string_key, v))
185-
end)
180+
urls =
181+
%{}
182+
|> add_optional_url(:profile, claims["profile"])
183+
|> add_optional_url(:website, claims["website"])
184+
185+
# https://openid.net/specs/openid-connect-core-1_0.html#Claims
186+
%Info{
187+
name: claims["name"],
188+
first_name: claims["first_name"],
189+
last_name: claims["last_name"],
190+
nickname: claims["nickname"],
191+
email: claims["email"],
192+
# address claim is a JSON blob
193+
location: nil,
194+
description: nil,
195+
image: claims["picture"],
196+
phone: claims["phone_number"],
197+
birthday: claims["birthdate"],
198+
urls: urls
199+
}
186200
end
187201

188202
defp set_error!(conn, key, message) do
@@ -213,4 +227,8 @@ defmodule Ueberauth.Strategy.OIDC do
213227
end
214228

215229
defp expires_at(expires_at), do: expires_at
230+
231+
defp add_optional_url(urls, field, value)
232+
defp add_optional_url(urls, _field, nil), do: urls
233+
defp add_optional_url(urls, field, value), do: Map.put(urls, field, value)
216234
end

‎test/support/fake_openid_connect.ex‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,15 @@ defmodule FakeOpenIDConnect do
9292
%{
9393
"sub" => "userinfo_sub",
9494
"name" => "Full Name",
95-
"email" => "userinfo_email"
95+
"first_name" => "First",
96+
"last_name" => "Last",
97+
"nickname" => "Nickname",
98+
"email" => "test@email.example",
99+
"picture" => "http://photo.example",
100+
"phone_number" => "phone_number_value",
101+
"birthdate" => "1970-01-01",
102+
"profile" => "http://profile.example",
103+
"website" => "http://website.example"
96104
}}
97105
end
98106

‎test/ueberauth_oidc/strategy/oidc_test.exs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,20 @@ defmodule Ueberauth.Strategy.OIDCTest do
179179
conn = run_request_and_callback(conn, options)
180180

181181
assert %Ueberauth.Auth{
182-
uid: "userinfo_email",
182+
uid: "test@email.example",
183183
info: %Ueberauth.Auth.Info{
184184
name: "Full Name",
185-
email: "userinfo_email"
185+
first_name: "First",
186+
last_name: "Last",
187+
nickname: "Nickname",
188+
email: "test@email.example",
189+
image: "http://photo.example",
190+
phone: "phone_number_value",
191+
birthday: "1970-01-01",
192+
urls: %{
193+
profile: "http://profile.example",
194+
website: "http://website.example"
195+
}
186196
}
187197
} = conn.assigns.ueberauth_auth
188198
end

0 commit comments

Comments
 (0)