|
1 | 1 | 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 |
| - |
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 | + |
| 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 | + |
| 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" |
30 | 30 | }
|
31 |
| - @user @user_response |
32 | 31 |
|
33 | 32 | 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" |
43 | 36 | end
|
44 | 37 |
|
45 | 38 | 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"]) |
51 | 43 |
|
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") |
57 | 45 |
|
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 | + ) |
60 | 52 |
|
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"]]) |
63 | 55 |
|
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 |
69 | 58 | end
|
70 | 59 | end
|
0 commit comments