Skip to content

Commit b4f708a

Browse files
authored
Merge pull request #178 from pow-auth/auth0-oidc
Switch to Auth0 OIDC
2 parents 2676c79 + b4ecb4e commit b4f708a

File tree

3 files changed

+59
-84
lines changed

3 files changed

+59
-84
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
### Breaking changes
88

9+
* `Assent.Strategy.Auth0.authorize_url/2` no longer accepts `:domain` config, use `:base_url` instead
910
* `Assent.Strategy.Google` now return `hd` instead of `google_hd`
1011
* `:site` configuration option removed, use `:base_url` instead
1112
* `Assent.Strategy.OAuth2.authorize_url/2` no longer allows `:state` in `:authorization_params`
@@ -17,6 +18,7 @@
1718

1819
### Changes
1920

21+
* `Assent.Strategy.Auth0` now uses OIDC instead of OAuth 2.0 base strategy
2022
* `Assent.Strategy.Google` now uses OIDC instead of OAuth 2.0 base strategy
2123

2224
## v0.2

Diff for: lib/assent/strategies/auth0.ex

+11-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
defmodule Assent.Strategy.Auth0 do
22
@moduledoc """
3-
Auth0 OAuth 2.0 strategy.
3+
Auth0 OpenID Connect strategy.
4+
5+
## Configuration
6+
7+
- `:base_url` - The Auth0 base URL, required
48
59
## Usage
610
@@ -13,33 +17,13 @@ defmodule Assent.Strategy.Auth0 do
1317
1418
See `Assent.Strategy.OAuth2` for more.
1519
"""
16-
use Assent.Strategy.OAuth2.Base
20+
use Assent.Strategy.OIDC.Base
1721

1822
@impl true
19-
def default_config(config) do
20-
append_domain_config(config,
21-
authorize_url: "/authorize",
22-
token_url: "/oauth/token",
23-
user_url: "/userinfo",
24-
authorization_params: [scope: "openid profile email"],
25-
auth_method: :client_secret_post
26-
)
27-
end
28-
29-
defp append_domain_config(config, default) do
30-
case Assent.fetch_config(config, :domain) do
31-
{:ok, domain} ->
32-
IO.warn("`:domain` config is deprecated. Use `:base_url` instead.")
33-
Keyword.put(default, :base_url, prepend_scheme(domain))
34-
35-
_error ->
36-
default
37-
end
23+
def default_config(_config) do
24+
[
25+
authorization_params: [scope: "email profile"],
26+
client_authentication_method: "client_secret_post"
27+
]
3828
end
39-
40-
defp prepend_scheme("http" <> _ = domain), do: domain
41-
defp prepend_scheme(domain), do: "https://" <> domain
42-
43-
@impl true
44-
def normalize(_config, user), do: {:ok, user}
4529
end

Diff for: test/assent/strategies/auth0_test.exs

+46-57
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,59 @@
11
defmodule Assent.Strategy.Auth0Test do
2-
use Assent.Test.OAuth2TestCase
3-
4-
alias Assent.{MissingConfigError, Strategy.Auth0}
5-
6-
# From https://auth0.com/docs/api/authentication#user-profile
7-
@user_response %{
8-
"sub" => "248289761001",
9-
"name" => "Jane Josephine Doe",
10-
"given_name" => "Jane",
11-
"family_name" => "Doe",
12-
"middle_name" => "Josephine",
13-
"nickname" => "JJ",
14-
"preferred_username" => "j.doe",
15-
"profile" => "http://exampleco.com/janedoe",
16-
"picture" => "http://exampleco.com/janedoe/me.jpg",
17-
"website" => "http://exampleco.com",
18-
"email" => "[email protected]",
19-
"email_verified" => true,
20-
"gender" => "female",
21-
"birthdate" => "1972-03-31",
22-
"zoneinfo" => "America/Los_Angeles",
23-
"locale" => "en-US",
24-
"phone_number" => "+1 (111) 222-3434",
25-
"phone_number_verified" => false,
26-
"address" => %{
27-
"country" => "us"
28-
},
29-
"updated_at" => "1556845729"
2+
use Assent.Test.OIDCTestCase
3+
4+
alias Assent.Strategy.Auth0
5+
6+
# From https://auth0.com/docs/get-started/apis/scopes/sample-use-cases-scopes-and-claims#authenticate-a-user-and-request-standard-claims
7+
@id_token_claims %{
8+
"name" => "John Doe",
9+
"nickname" => "john.doe",
10+
"picture" => "https://myawesomeavatar.com/avatar.png",
11+
"updated_at" => "2017-03-30T15:13:40.474Z",
12+
"email" => "[email protected]",
13+
"email_verified" => false,
14+
"iss" => "https://{yourDomain}/",
15+
"sub" => "auth0|USER-ID",
16+
"aud" => "{yourClientId}",
17+
"exp" => :os.system_time(:second) + 60,
18+
"iat" => :os.system_time(:second),
19+
"nonce" => "crypto-value",
20+
"at_hash" => "IoS3ZGppJKUn3Bta_LgE2A"
21+
}
22+
@user %{
23+
"email" => "[email protected]",
24+
"email_verified" => false,
25+
"sub" => "auth0|USER-ID",
26+
"name" => "John Doe",
27+
"nickname" => "john.doe",
28+
"picture" => "https://myawesomeavatar.com/avatar.png",
29+
"updated_at" => "2017-03-30T15:13:40.474Z"
3030
}
31-
@user @user_response
3231

3332
test "authorize_url/2", %{config: config} do
34-
config = Keyword.delete(config, :base_url)
35-
36-
assert {:error, %MissingConfigError{} = error} = Auth0.authorize_url(config)
37-
assert error.key == :base_url
38-
39-
assert {:ok, %{url: url}} =
40-
Auth0.authorize_url(config ++ [base_url: "https://demo.auth0.com/authorize"])
41-
42-
assert url =~ "https://demo.auth0.com/authorize"
33+
assert {:ok, %{url: url}} = Auth0.authorize_url(config)
34+
assert url =~ "/oauth/authorize?client_id=id"
35+
assert url =~ "scope=openid+email+profile"
4336
end
4437

4538
test "callback/2", %{config: config, callback_params: params} do
46-
expect_oauth2_access_token_request([uri: "/oauth/token"], fn _conn, params ->
47-
assert params["client_secret"] == config[:client_secret]
48-
end)
49-
50-
expect_oauth2_user_request(@user_response, uri: "/userinfo")
39+
openid_config =
40+
config[:openid_configuration]
41+
|> Map.put("issuer", "https://{yourDomain}/")
42+
|> Map.put("token_endpoint_auth_methods_supported", ["client_secret_post"])
5143

52-
assert {:ok, %{user: user}} = Auth0.callback(config, params)
53-
assert user == @user
54-
end
55-
56-
### Deprecated
44+
session_params = Map.put(config[:session_params], :nonce, "crypto-value")
5745

58-
test "authorize_url/2 with `:domain` config", %{config: config} do
59-
config = Keyword.take(config, [:client_id, :redirect_uri])
46+
config =
47+
Keyword.merge(config,
48+
openid_configuration: openid_config,
49+
client_id: "{yourClientId}",
50+
session_params: session_params
51+
)
6052

61-
assert {:error, %MissingConfigError{} = error} = Auth0.authorize_url(config)
62-
assert error.key == :base_url
53+
[key | _rest] = expect_oidc_jwks_uri_request()
54+
expect_oidc_access_token_request(id_token_opts: [claims: @id_token_claims, kid: key["kid"]])
6355

64-
assert {:ok, %{url: url}} = Auth0.authorize_url(config ++ [domain: "demo.auth0.com"])
65-
assert url =~ "https://demo.auth0.com/authorize"
66-
67-
assert {:ok, %{url: url}} = Auth0.authorize_url(config ++ [domain: "http://demo.auth0.com"])
68-
assert url =~ "http://demo.auth0.com/authorize"
56+
assert {:ok, %{user: user}} = Auth0.callback(config, params)
57+
assert user == @user
6958
end
7059
end

0 commit comments

Comments
 (0)