1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.OAuth.OAuthControllerTest do
6 use Pleroma.Web.ConnCase
11 alias Pleroma.Web.OAuth.Authorization
12 alias Pleroma.Web.OAuth.OAuthController
13 alias Pleroma.Web.OAuth.Token
18 signing_salt: "cooldude"
20 clear_config_all([:instance, :account_activation_required])
22 describe "in OAuth consumer mode, " do
25 app: insert(:oauth_app),
28 |> Plug.Session.call(Plug.Session.init(@session_opts))
33 clear_config([:auth, :oauth_consumer_strategies]) do
35 [:auth, :oauth_consumer_strategies],
40 test "GET /oauth/authorize renders auth forms, including OAuth consumer form", %{
49 "response_type" => "code",
50 "client_id" => app.client_id,
51 "redirect_uri" => OAuthController.default_redirect_uri(app),
56 assert response = html_response(conn, 200)
57 assert response =~ "Sign in with Twitter"
58 assert response =~ o_auth_path(conn, :prepare_request)
61 test "GET /oauth/prepare_request encodes parameters as `state` and redirects", %{
68 "/oauth/prepare_request",
70 "provider" => "twitter",
72 "scope" => "read follow",
73 "client_id" => app.client_id,
74 "redirect_uri" => OAuthController.default_redirect_uri(app),
80 assert response = html_response(conn, 302)
82 redirect_query = URI.parse(redirected_to(conn)).query
83 assert %{"state" => state_param} = URI.decode_query(redirect_query)
84 assert {:ok, state_components} = Poison.decode(state_param)
86 expected_client_id = app.client_id
87 expected_redirect_uri = app.redirect_uris
90 "scope" => "read follow",
91 "client_id" => ^expected_client_id,
92 "redirect_uri" => ^expected_redirect_uri,
97 test "with user-bound registration, GET /oauth/<provider>/callback redirects to `redirect_uri` with `code`",
98 %{app: app, conn: conn} do
99 registration = insert(:registration)
100 redirect_uri = OAuthController.default_redirect_uri(app)
103 "scope" => Enum.join(app.scopes, " "),
104 "client_id" => app.client_id,
105 "redirect_uri" => redirect_uri,
111 |> assign(:ueberauth_auth, %{provider: registration.provider, uid: registration.uid})
113 "/oauth/twitter/callback",
115 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
116 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
117 "provider" => "twitter",
118 "state" => Poison.encode!(state_params)
122 assert response = html_response(conn, 302)
123 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
126 test "with user-unbound registration, GET /oauth/<provider>/callback renders registration_details page",
127 %{app: app, conn: conn} do
131 "scope" => "read write",
132 "client_id" => app.client_id,
133 "redirect_uri" => OAuthController.default_redirect_uri(app),
139 |> assign(:ueberauth_auth, %{
142 info: %{nickname: user.nickname, email: user.email, name: user.name, description: nil}
145 "/oauth/twitter/callback",
147 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
148 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
149 "provider" => "twitter",
150 "state" => Poison.encode!(state_params)
154 assert response = html_response(conn, 200)
155 assert response =~ ~r/name="op" type="submit" value="register"/
156 assert response =~ ~r/name="op" type="submit" value="connect"/
157 assert response =~ user.email
158 assert response =~ user.nickname
161 test "on authentication error, GET /oauth/<provider>/callback redirects to `redirect_uri`", %{
166 "scope" => Enum.join(app.scopes, " "),
167 "client_id" => app.client_id,
168 "redirect_uri" => OAuthController.default_redirect_uri(app),
174 |> assign(:ueberauth_failure, %{errors: [%{message: "(error description)"}]})
176 "/oauth/twitter/callback",
178 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
179 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
180 "provider" => "twitter",
181 "state" => Poison.encode!(state_params)
185 assert response = html_response(conn, 302)
186 assert redirected_to(conn) == app.redirect_uris
187 assert get_flash(conn, :error) == "Failed to authenticate: (error description)."
190 test "GET /oauth/registration_details renders registration details form", %{
197 "/oauth/registration_details",
199 "authorization" => %{
200 "scopes" => app.scopes,
201 "client_id" => app.client_id,
202 "redirect_uri" => OAuthController.default_redirect_uri(app),
203 "state" => "a_state",
205 "email" => "john@doe.com"
210 assert response = html_response(conn, 200)
211 assert response =~ ~r/name="op" type="submit" value="register"/
212 assert response =~ ~r/name="op" type="submit" value="connect"/
215 test "with valid params, POST /oauth/register?op=register redirects to `redirect_uri` with `code`",
220 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
221 redirect_uri = OAuthController.default_redirect_uri(app)
225 |> put_session(:registration_id, registration.id)
230 "authorization" => %{
231 "scopes" => app.scopes,
232 "client_id" => app.client_id,
233 "redirect_uri" => redirect_uri,
234 "state" => "a_state",
235 "nickname" => "availablenick",
236 "email" => "available@email.com"
241 assert response = html_response(conn, 302)
242 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
245 test "with unlisted `redirect_uri`, POST /oauth/register?op=register results in HTTP 401",
250 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
251 unlisted_redirect_uri = "http://cross-site-request.com"
255 |> put_session(:registration_id, registration.id)
260 "authorization" => %{
261 "scopes" => app.scopes,
262 "client_id" => app.client_id,
263 "redirect_uri" => unlisted_redirect_uri,
264 "state" => "a_state",
265 "nickname" => "availablenick",
266 "email" => "available@email.com"
271 assert response = html_response(conn, 401)
274 test "with invalid params, POST /oauth/register?op=register renders registration_details page",
279 another_user = insert(:user)
280 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
284 "authorization" => %{
285 "scopes" => app.scopes,
286 "client_id" => app.client_id,
287 "redirect_uri" => OAuthController.default_redirect_uri(app),
288 "state" => "a_state",
289 "nickname" => "availablenickname",
290 "email" => "available@email.com"
294 for {bad_param, bad_param_value} <-
295 [{"nickname", another_user.nickname}, {"email", another_user.email}] do
296 bad_registration_attrs = %{
297 "authorization" => Map.put(params["authorization"], bad_param, bad_param_value)
300 bad_params = Map.merge(params, bad_registration_attrs)
304 |> put_session(:registration_id, registration.id)
305 |> post("/oauth/register", bad_params)
307 assert html_response(conn, 403) =~ ~r/name="op" type="submit" value="register"/
308 assert get_flash(conn, :error) == "Error: #{bad_param} has already been taken."
312 test "with valid params, POST /oauth/register?op=connect redirects to `redirect_uri` with `code`",
317 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt("testpassword"))
318 registration = insert(:registration, user: nil)
319 redirect_uri = OAuthController.default_redirect_uri(app)
323 |> put_session(:registration_id, registration.id)
328 "authorization" => %{
329 "scopes" => app.scopes,
330 "client_id" => app.client_id,
331 "redirect_uri" => redirect_uri,
332 "state" => "a_state",
333 "name" => user.nickname,
334 "password" => "testpassword"
339 assert response = html_response(conn, 302)
340 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
343 test "with unlisted `redirect_uri`, POST /oauth/register?op=connect results in HTTP 401`",
348 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt("testpassword"))
349 registration = insert(:registration, user: nil)
350 unlisted_redirect_uri = "http://cross-site-request.com"
354 |> put_session(:registration_id, registration.id)
359 "authorization" => %{
360 "scopes" => app.scopes,
361 "client_id" => app.client_id,
362 "redirect_uri" => unlisted_redirect_uri,
363 "state" => "a_state",
364 "name" => user.nickname,
365 "password" => "testpassword"
370 assert response = html_response(conn, 401)
373 test "with invalid params, POST /oauth/register?op=connect renders registration_details page",
379 registration = insert(:registration, user: nil)
383 "authorization" => %{
384 "scopes" => app.scopes,
385 "client_id" => app.client_id,
386 "redirect_uri" => OAuthController.default_redirect_uri(app),
387 "state" => "a_state",
388 "name" => user.nickname,
389 "password" => "wrong password"
395 |> put_session(:registration_id, registration.id)
396 |> post("/oauth/register", params)
398 assert html_response(conn, 401) =~ ~r/name="op" type="submit" value="connect"/
399 assert get_flash(conn, :error) == "Invalid Username/Password"
403 describe "GET /oauth/authorize" do
406 app: insert(:oauth_app, redirect_uris: "https://redirect.url"),
409 |> Plug.Session.call(Plug.Session.init(@session_opts))
414 test "renders authentication page", %{app: app, conn: conn} do
420 "response_type" => "code",
421 "client_id" => app.client_id,
422 "redirect_uri" => OAuthController.default_redirect_uri(app),
427 assert html_response(conn, 200) =~ ~s(type="submit")
430 test "properly handles internal calls with `authorization`-wrapped params", %{
439 "authorization" => %{
440 "response_type" => "code",
441 "client_id" => app.client_id,
442 "redirect_uri" => OAuthController.default_redirect_uri(app),
448 assert html_response(conn, 200) =~ ~s(type="submit")
451 test "renders authentication page if user is already authenticated but `force_login` is tru-ish",
452 %{app: app, conn: conn} do
453 token = insert(:oauth_token, app: app)
457 |> put_session(:oauth_token, token.token)
461 "response_type" => "code",
462 "client_id" => app.client_id,
463 "redirect_uri" => OAuthController.default_redirect_uri(app),
465 "force_login" => "true"
469 assert html_response(conn, 200) =~ ~s(type="submit")
472 test "renders authentication page if user is already authenticated but user request with another client",
477 token = insert(:oauth_token, app: app)
481 |> put_session(:oauth_token, token.token)
485 "response_type" => "code",
486 "client_id" => "another_client_id",
487 "redirect_uri" => OAuthController.default_redirect_uri(app),
492 assert html_response(conn, 200) =~ ~s(type="submit")
495 test "with existing authentication and non-OOB `redirect_uri`, redirects to app with `token` and `state` params",
500 token = insert(:oauth_token, app: app)
504 |> put_session(:oauth_token, token.token)
508 "response_type" => "code",
509 "client_id" => app.client_id,
510 "redirect_uri" => OAuthController.default_redirect_uri(app),
511 "state" => "specific_client_state",
516 assert URI.decode(redirected_to(conn)) ==
517 "https://redirect.url?access_token=#{token.token}&state=specific_client_state"
520 test "with existing authentication and unlisted non-OOB `redirect_uri`, redirects without credentials",
525 unlisted_redirect_uri = "http://cross-site-request.com"
526 token = insert(:oauth_token, app: app)
530 |> put_session(:oauth_token, token.token)
534 "response_type" => "code",
535 "client_id" => app.client_id,
536 "redirect_uri" => unlisted_redirect_uri,
537 "state" => "specific_client_state",
542 assert redirected_to(conn) == unlisted_redirect_uri
545 test "with existing authentication and OOB `redirect_uri`, redirects to app with `token` and `state` params",
550 token = insert(:oauth_token, app: app)
554 |> put_session(:oauth_token, token.token)
558 "response_type" => "code",
559 "client_id" => app.client_id,
560 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
565 assert html_response(conn, 200) =~ "Authorization exists"
569 describe "POST /oauth/authorize" do
570 test "redirects with oauth authorization, " <>
571 "granting requested app-supported scopes to both admin- and non-admin users" do
572 app_scopes = ["read", "write", "admin", "secret_scope"]
573 app = insert(:oauth_app, scopes: app_scopes)
574 redirect_uri = OAuthController.default_redirect_uri(app)
576 non_admin = insert(:user, is_admin: false)
577 admin = insert(:user, is_admin: true)
578 scopes_subset = ["read:subscope", "write", "admin"]
580 # In case scope param is missing, expecting _all_ app-supported scopes to be granted
581 for user <- [non_admin, admin],
582 {requested_scopes, expected_scopes} <-
583 %{scopes_subset => scopes_subset, nil => app_scopes} do
589 "authorization" => %{
590 "name" => user.nickname,
591 "password" => "test",
592 "client_id" => app.client_id,
593 "redirect_uri" => redirect_uri,
594 "scope" => requested_scopes,
595 "state" => "statepassed"
600 target = redirected_to(conn)
601 assert target =~ redirect_uri
603 query = URI.parse(target).query |> URI.query_decoder() |> Map.new()
605 assert %{"state" => "statepassed", "code" => code} = query
606 auth = Repo.get_by(Authorization, token: code)
608 assert auth.scopes == expected_scopes
612 test "returns 401 for wrong credentials", %{conn: conn} do
614 app = insert(:oauth_app)
615 redirect_uri = OAuthController.default_redirect_uri(app)
619 |> post("/oauth/authorize", %{
620 "authorization" => %{
621 "name" => user.nickname,
622 "password" => "wrong",
623 "client_id" => app.client_id,
624 "redirect_uri" => redirect_uri,
625 "state" => "statepassed",
626 "scope" => Enum.join(app.scopes, " ")
629 |> html_response(:unauthorized)
632 assert result =~ app.client_id
633 assert result =~ redirect_uri
636 assert result =~ "Invalid Username/Password"
639 test "returns 401 for missing scopes" do
640 user = insert(:user, is_admin: false)
641 app = insert(:oauth_app, scopes: ["read", "write", "admin"])
642 redirect_uri = OAuthController.default_redirect_uri(app)
646 |> post("/oauth/authorize", %{
647 "authorization" => %{
648 "name" => user.nickname,
649 "password" => "test",
650 "client_id" => app.client_id,
651 "redirect_uri" => redirect_uri,
652 "state" => "statepassed",
656 |> html_response(:unauthorized)
659 assert result =~ app.client_id
660 assert result =~ redirect_uri
663 assert result =~ "This action is outside the authorized scopes"
666 test "returns 401 for scopes beyond app scopes hierarchy", %{conn: conn} do
668 app = insert(:oauth_app, scopes: ["read", "write"])
669 redirect_uri = OAuthController.default_redirect_uri(app)
673 |> post("/oauth/authorize", %{
674 "authorization" => %{
675 "name" => user.nickname,
676 "password" => "test",
677 "client_id" => app.client_id,
678 "redirect_uri" => redirect_uri,
679 "state" => "statepassed",
680 "scope" => "read write follow"
683 |> html_response(:unauthorized)
686 assert result =~ app.client_id
687 assert result =~ redirect_uri
690 assert result =~ "This action is outside the authorized scopes"
694 describe "POST /oauth/token" do
695 test "issues a token for an all-body request" do
697 app = insert(:oauth_app, scopes: ["read", "write"])
699 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
703 |> post("/oauth/token", %{
704 "grant_type" => "authorization_code",
705 "code" => auth.token,
706 "redirect_uri" => OAuthController.default_redirect_uri(app),
707 "client_id" => app.client_id,
708 "client_secret" => app.client_secret
711 assert %{"access_token" => token, "me" => ap_id} = json_response(conn, 200)
713 token = Repo.get_by(Token, token: token)
715 assert token.scopes == auth.scopes
716 assert user.ap_id == ap_id
719 test "issues a token for `password` grant_type with valid credentials, with full permissions by default" do
720 password = "testpassword"
721 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
723 app = insert(:oauth_app, scopes: ["read", "write"])
725 # Note: "scope" param is intentionally omitted
728 |> post("/oauth/token", %{
729 "grant_type" => "password",
730 "username" => user.nickname,
731 "password" => password,
732 "client_id" => app.client_id,
733 "client_secret" => app.client_secret
736 assert %{"access_token" => token} = json_response(conn, 200)
738 token = Repo.get_by(Token, token: token)
740 assert token.scopes == app.scopes
743 test "issues a token for request with HTTP basic auth client credentials" do
745 app = insert(:oauth_app, scopes: ["scope1", "scope2", "scope3"])
747 {:ok, auth} = Authorization.create_authorization(app, user, ["scope1", "scope2"])
748 assert auth.scopes == ["scope1", "scope2"]
751 (URI.encode_www_form(app.client_id) <> ":" <> URI.encode_www_form(app.client_secret))
756 |> put_req_header("authorization", "Basic " <> app_encoded)
757 |> post("/oauth/token", %{
758 "grant_type" => "authorization_code",
759 "code" => auth.token,
760 "redirect_uri" => OAuthController.default_redirect_uri(app)
763 assert %{"access_token" => token, "scope" => scope} = json_response(conn, 200)
765 assert scope == "scope1 scope2"
767 token = Repo.get_by(Token, token: token)
769 assert token.scopes == ["scope1", "scope2"]
772 test "issue a token for client_credentials grant type" do
773 app = insert(:oauth_app, scopes: ["read", "write"])
777 |> post("/oauth/token", %{
778 "grant_type" => "client_credentials",
779 "client_id" => app.client_id,
780 "client_secret" => app.client_secret
783 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
784 json_response(conn, 200)
787 token_from_db = Repo.get_by(Token, token: token)
790 assert scope == "read write"
793 test "rejects token exchange with invalid client credentials" do
795 app = insert(:oauth_app)
797 {:ok, auth} = Authorization.create_authorization(app, user)
801 |> put_req_header("authorization", "Basic JTIxOiVGMCU5RiVBNCVCNwo=")
802 |> post("/oauth/token", %{
803 "grant_type" => "authorization_code",
804 "code" => auth.token,
805 "redirect_uri" => OAuthController.default_redirect_uri(app)
808 assert resp = json_response(conn, 400)
809 assert %{"error" => _} = resp
810 refute Map.has_key?(resp, "access_token")
813 test "rejects token exchange for valid credentials belonging to unconfirmed user and confirmation is required" do
814 Pleroma.Config.put([:instance, :account_activation_required], true)
815 password = "testpassword"
818 insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
819 |> User.confirmation_changeset(need_confirmation: true)
820 |> User.update_and_set_cache()
822 refute Pleroma.User.account_status(user) == :active
824 app = insert(:oauth_app)
828 |> post("/oauth/token", %{
829 "grant_type" => "password",
830 "username" => user.nickname,
831 "password" => password,
832 "client_id" => app.client_id,
833 "client_secret" => app.client_secret
836 assert resp = json_response(conn, 403)
837 assert %{"error" => _} = resp
838 refute Map.has_key?(resp, "access_token")
841 test "rejects token exchange for valid credentials belonging to deactivated user" do
842 password = "testpassword"
846 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
850 app = insert(:oauth_app)
854 |> post("/oauth/token", %{
855 "grant_type" => "password",
856 "username" => user.nickname,
857 "password" => password,
858 "client_id" => app.client_id,
859 "client_secret" => app.client_secret
861 |> json_response(403)
864 "error" => "Your account is currently disabled",
865 "identifier" => "account_is_disabled"
869 test "rejects token exchange for user with password_reset_pending set to true" do
870 password = "testpassword"
874 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
875 password_reset_pending: true
878 app = insert(:oauth_app, scopes: ["read", "write"])
882 |> post("/oauth/token", %{
883 "grant_type" => "password",
884 "username" => user.nickname,
885 "password" => password,
886 "client_id" => app.client_id,
887 "client_secret" => app.client_secret
889 |> json_response(403)
892 "error" => "Password reset is required",
893 "identifier" => "password_reset_required"
897 test "rejects token exchange for user with confirmation_pending set to true" do
898 Pleroma.Config.put([:instance, :account_activation_required], true)
899 password = "testpassword"
903 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
904 confirmation_pending: true
907 app = insert(:oauth_app, scopes: ["read", "write"])
911 |> post("/oauth/token", %{
912 "grant_type" => "password",
913 "username" => user.nickname,
914 "password" => password,
915 "client_id" => app.client_id,
916 "client_secret" => app.client_secret
918 |> json_response(403)
921 "error" => "Your login is missing a confirmed e-mail address",
922 "identifier" => "missing_confirmed_email"
926 test "rejects an invalid authorization code" do
927 app = insert(:oauth_app)
931 |> post("/oauth/token", %{
932 "grant_type" => "authorization_code",
933 "code" => "Imobviouslyinvalid",
934 "redirect_uri" => OAuthController.default_redirect_uri(app),
935 "client_id" => app.client_id,
936 "client_secret" => app.client_secret
939 assert resp = json_response(conn, 400)
940 assert %{"error" => _} = json_response(conn, 400)
941 refute Map.has_key?(resp, "access_token")
945 describe "POST /oauth/token - refresh token" do
946 clear_config([:oauth2, :issue_new_refresh_token])
948 test "issues a new access token with keep fresh token" do
949 Pleroma.Config.put([:oauth2, :issue_new_refresh_token], true)
951 app = insert(:oauth_app, scopes: ["read", "write"])
953 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
954 {:ok, token} = Token.exchange_token(app, auth)
958 |> post("/oauth/token", %{
959 "grant_type" => "refresh_token",
960 "refresh_token" => token.refresh_token,
961 "client_id" => app.client_id,
962 "client_secret" => app.client_secret
964 |> json_response(200)
971 "token_type" => "Bearer",
974 "refresh_token" => _,
980 refute Repo.get_by(Token, token: token.token)
981 new_token = Repo.get_by(Token, token: response["access_token"])
982 assert new_token.refresh_token == token.refresh_token
983 assert new_token.scopes == auth.scopes
984 assert new_token.user_id == user.id
985 assert new_token.app_id == app.id
988 test "issues a new access token with new fresh token" do
989 Pleroma.Config.put([:oauth2, :issue_new_refresh_token], false)
991 app = insert(:oauth_app, scopes: ["read", "write"])
993 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
994 {:ok, token} = Token.exchange_token(app, auth)
998 |> post("/oauth/token", %{
999 "grant_type" => "refresh_token",
1000 "refresh_token" => token.refresh_token,
1001 "client_id" => app.client_id,
1002 "client_secret" => app.client_secret
1004 |> json_response(200)
1011 "token_type" => "Bearer",
1012 "expires_in" => 600,
1013 "access_token" => _,
1014 "refresh_token" => _,
1020 refute Repo.get_by(Token, token: token.token)
1021 new_token = Repo.get_by(Token, token: response["access_token"])
1022 refute new_token.refresh_token == token.refresh_token
1023 assert new_token.scopes == auth.scopes
1024 assert new_token.user_id == user.id
1025 assert new_token.app_id == app.id
1028 test "returns 400 if we try use access token" do
1029 user = insert(:user)
1030 app = insert(:oauth_app, scopes: ["read", "write"])
1032 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1033 {:ok, token} = Token.exchange_token(app, auth)
1037 |> post("/oauth/token", %{
1038 "grant_type" => "refresh_token",
1039 "refresh_token" => token.token,
1040 "client_id" => app.client_id,
1041 "client_secret" => app.client_secret
1043 |> json_response(400)
1045 assert %{"error" => "Invalid credentials"} == response
1048 test "returns 400 if refresh_token invalid" do
1049 app = insert(:oauth_app, scopes: ["read", "write"])
1053 |> post("/oauth/token", %{
1054 "grant_type" => "refresh_token",
1055 "refresh_token" => "token.refresh_token",
1056 "client_id" => app.client_id,
1057 "client_secret" => app.client_secret
1059 |> json_response(400)
1061 assert %{"error" => "Invalid credentials"} == response
1064 test "issues a new token if token expired" do
1065 user = insert(:user)
1066 app = insert(:oauth_app, scopes: ["read", "write"])
1068 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1069 {:ok, token} = Token.exchange_token(app, auth)
1072 Ecto.Changeset.change(
1074 %{valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -86_400 * 30)}
1077 {:ok, access_token} = Repo.update(change)
1081 |> post("/oauth/token", %{
1082 "grant_type" => "refresh_token",
1083 "refresh_token" => access_token.refresh_token,
1084 "client_id" => app.client_id,
1085 "client_secret" => app.client_secret
1087 |> json_response(200)
1094 "token_type" => "Bearer",
1095 "expires_in" => 600,
1096 "access_token" => _,
1097 "refresh_token" => _,
1103 refute Repo.get_by(Token, token: token.token)
1104 token = Repo.get_by(Token, token: response["access_token"])
1106 assert token.scopes == auth.scopes
1107 assert token.user_id == user.id
1108 assert token.app_id == app.id
1112 describe "POST /oauth/token - bad request" do
1113 test "returns 500" do
1116 |> post("/oauth/token", %{})
1117 |> json_response(500)
1119 assert %{"error" => "Bad request"} == response
1123 describe "POST /oauth/revoke - bad request" do
1124 test "returns 500" do
1127 |> post("/oauth/revoke", %{})
1128 |> json_response(500)
1130 assert %{"error" => "Bad request"} == response