[#923] OAuthController#callback adjustments (with tests).
This commit is contained in:
parent
45765918c3
commit
3e7f2bfc2f
2 changed files with 14 additions and 21 deletions
|
@ -249,13 +249,7 @@ def callback(conn, params) do
|
||||||
|
|
||||||
with {:ok, registration} <- Authenticator.get_registration(conn, params) do
|
with {:ok, registration} <- Authenticator.get_registration(conn, params) do
|
||||||
user = Repo.preload(registration, :user).user
|
user = Repo.preload(registration, :user).user
|
||||||
|
auth_params = Map.take(params, ~w(client_id redirect_uri scope scopes state))
|
||||||
auth_params = %{
|
|
||||||
"client_id" => params["client_id"],
|
|
||||||
"redirect_uri" => params["redirect_uri"],
|
|
||||||
"state" => params["state"],
|
|
||||||
"scopes" => oauth_scopes(params, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
if user do
|
if user do
|
||||||
create_authorization(
|
create_authorization(
|
||||||
|
|
|
@ -73,7 +73,7 @@ test "GET /oauth/prepare_request encodes parameters as `state` and redirects", %
|
||||||
"/oauth/prepare_request",
|
"/oauth/prepare_request",
|
||||||
%{
|
%{
|
||||||
"provider" => "twitter",
|
"provider" => "twitter",
|
||||||
"scope" => app.scopes,
|
"scope" => "read follow",
|
||||||
"client_id" => app.client_id,
|
"client_id" => app.client_id,
|
||||||
"redirect_uri" => app.redirect_uris,
|
"redirect_uri" => app.redirect_uris,
|
||||||
"state" => "a_state"
|
"state" => "a_state"
|
||||||
|
@ -81,21 +81,20 @@ test "GET /oauth/prepare_request encodes parameters as `state` and redirects", %
|
||||||
)
|
)
|
||||||
|
|
||||||
assert response = html_response(conn, 302)
|
assert response = html_response(conn, 302)
|
||||||
redirected_to = redirected_to(conn)
|
|
||||||
[state] = Regex.run(~r/(?<=state=).*?(?=\Z|&)/, redirected_to)
|
|
||||||
state = URI.decode(state)
|
|
||||||
assert {:ok, state_params} = Poison.decode(state)
|
|
||||||
|
|
||||||
expected_scope_param = Enum.join(app.scopes, "+")
|
redirect_query = URI.parse(redirected_to(conn)).query
|
||||||
expected_client_id_param = app.client_id
|
assert %{"state" => state_param} = URI.decode_query(redirect_query)
|
||||||
expected_redirect_uri_param = app.redirect_uris
|
assert {:ok, state_components} = Poison.decode(state_param)
|
||||||
|
|
||||||
|
expected_client_id = app.client_id
|
||||||
|
expected_redirect_uri = app.redirect_uris
|
||||||
|
|
||||||
assert %{
|
assert %{
|
||||||
"scope" => ^expected_scope_param,
|
"scope" => "read follow",
|
||||||
"client_id" => ^expected_client_id_param,
|
"client_id" => ^expected_client_id,
|
||||||
"redirect_uri" => ^expected_redirect_uri_param,
|
"redirect_uri" => ^expected_redirect_uri,
|
||||||
"state" => "a_state"
|
"state" => "a_state"
|
||||||
} = state_params
|
} = state_components
|
||||||
end
|
end
|
||||||
|
|
||||||
test "on authentication error, redirects to `redirect_uri`", %{app: app, conn: conn} do
|
test "on authentication error, redirects to `redirect_uri`", %{app: app, conn: conn} do
|
||||||
|
@ -158,7 +157,7 @@ test "with user-unbound registration, GET /oauth/<provider>/callback redirects t
|
||||||
registration = insert(:registration, user: nil)
|
registration = insert(:registration, user: nil)
|
||||||
|
|
||||||
state_params = %{
|
state_params = %{
|
||||||
"scope" => "read",
|
"scope" => "read write",
|
||||||
"client_id" => app.client_id,
|
"client_id" => app.client_id,
|
||||||
"redirect_uri" => app.redirect_uris,
|
"redirect_uri" => app.redirect_uris,
|
||||||
"state" => "a_state"
|
"state" => "a_state"
|
||||||
|
@ -182,7 +181,7 @@ test "with user-unbound registration, GET /oauth/<provider>/callback redirects t
|
||||||
state_params
|
state_params
|
||||||
|> Map.delete("scope")
|
|> Map.delete("scope")
|
||||||
|> Map.merge(%{
|
|> Map.merge(%{
|
||||||
"scopes" => ["read"],
|
"scope" => "read write",
|
||||||
"email" => Registration.email(registration),
|
"email" => Registration.email(registration),
|
||||||
"nickname" => Registration.nickname(registration)
|
"nickname" => Registration.nickname(registration)
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue