1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 setup do: clear_config([: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 setup do: clear_config([:auth, :oauth_consumer_strategies], ~w(twitter facebook))
35 test "GET /oauth/authorize renders auth forms, including OAuth consumer form", %{
44 "response_type" => "code",
45 "client_id" => app.client_id,
46 "redirect_uri" => OAuthController.default_redirect_uri(app),
51 assert response = html_response(conn, 200)
52 assert response =~ "Sign in with Twitter"
53 assert response =~ o_auth_path(conn, :prepare_request)
56 test "GET /oauth/prepare_request encodes parameters as `state` and redirects", %{
63 "/oauth/prepare_request",
65 "provider" => "twitter",
67 "scope" => "read follow",
68 "client_id" => app.client_id,
69 "redirect_uri" => OAuthController.default_redirect_uri(app),
75 assert response = html_response(conn, 302)
77 redirect_query = URI.parse(redirected_to(conn)).query
78 assert %{"state" => state_param} = URI.decode_query(redirect_query)
79 assert {:ok, state_components} = Poison.decode(state_param)
81 expected_client_id = app.client_id
82 expected_redirect_uri = app.redirect_uris
85 "scope" => "read follow",
86 "client_id" => ^expected_client_id,
87 "redirect_uri" => ^expected_redirect_uri,
92 test "with user-bound registration, GET /oauth/<provider>/callback redirects to `redirect_uri` with `code`",
93 %{app: app, conn: conn} do
94 registration = insert(:registration)
95 redirect_uri = OAuthController.default_redirect_uri(app)
98 "scope" => Enum.join(app.scopes, " "),
99 "client_id" => app.client_id,
100 "redirect_uri" => redirect_uri,
106 |> assign(:ueberauth_auth, %{provider: registration.provider, uid: registration.uid})
108 "/oauth/twitter/callback",
110 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
111 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
112 "provider" => "twitter",
113 "state" => Poison.encode!(state_params)
117 assert response = html_response(conn, 302)
118 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
121 test "with user-unbound registration, GET /oauth/<provider>/callback renders registration_details page",
122 %{app: app, conn: conn} do
126 "scope" => "read write",
127 "client_id" => app.client_id,
128 "redirect_uri" => OAuthController.default_redirect_uri(app),
134 |> assign(:ueberauth_auth, %{
137 info: %{nickname: user.nickname, email: user.email, name: user.name, description: nil}
140 "/oauth/twitter/callback",
142 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
143 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
144 "provider" => "twitter",
145 "state" => Poison.encode!(state_params)
149 assert response = html_response(conn, 200)
150 assert response =~ ~r/name="op" type="submit" value="register"/
151 assert response =~ ~r/name="op" type="submit" value="connect"/
152 assert response =~ user.email
153 assert response =~ user.nickname
156 test "on authentication error, GET /oauth/<provider>/callback redirects to `redirect_uri`", %{
161 "scope" => Enum.join(app.scopes, " "),
162 "client_id" => app.client_id,
163 "redirect_uri" => OAuthController.default_redirect_uri(app),
169 |> assign(:ueberauth_failure, %{errors: [%{message: "(error description)"}]})
171 "/oauth/twitter/callback",
173 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
174 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
175 "provider" => "twitter",
176 "state" => Poison.encode!(state_params)
180 assert response = html_response(conn, 302)
181 assert redirected_to(conn) == app.redirect_uris
182 assert get_flash(conn, :error) == "Failed to authenticate: (error description)."
185 test "GET /oauth/registration_details renders registration details form", %{
192 "/oauth/registration_details",
194 "authorization" => %{
195 "scopes" => app.scopes,
196 "client_id" => app.client_id,
197 "redirect_uri" => OAuthController.default_redirect_uri(app),
198 "state" => "a_state",
200 "email" => "john@doe.com"
205 assert response = html_response(conn, 200)
206 assert response =~ ~r/name="op" type="submit" value="register"/
207 assert response =~ ~r/name="op" type="submit" value="connect"/
210 test "with valid params, POST /oauth/register?op=register redirects to `redirect_uri` with `code`",
215 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
216 redirect_uri = OAuthController.default_redirect_uri(app)
220 |> put_session(:registration_id, registration.id)
225 "authorization" => %{
226 "scopes" => app.scopes,
227 "client_id" => app.client_id,
228 "redirect_uri" => redirect_uri,
229 "state" => "a_state",
230 "nickname" => "availablenick",
231 "email" => "available@email.com"
236 assert response = html_response(conn, 302)
237 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
240 test "with unlisted `redirect_uri`, POST /oauth/register?op=register results in HTTP 401",
245 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
246 unlisted_redirect_uri = "http://cross-site-request.com"
250 |> put_session(:registration_id, registration.id)
255 "authorization" => %{
256 "scopes" => app.scopes,
257 "client_id" => app.client_id,
258 "redirect_uri" => unlisted_redirect_uri,
259 "state" => "a_state",
260 "nickname" => "availablenick",
261 "email" => "available@email.com"
266 assert response = html_response(conn, 401)
269 test "with invalid params, POST /oauth/register?op=register renders registration_details page",
274 another_user = insert(:user)
275 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
279 "authorization" => %{
280 "scopes" => app.scopes,
281 "client_id" => app.client_id,
282 "redirect_uri" => OAuthController.default_redirect_uri(app),
283 "state" => "a_state",
284 "nickname" => "availablenickname",
285 "email" => "available@email.com"
289 for {bad_param, bad_param_value} <-
290 [{"nickname", another_user.nickname}, {"email", another_user.email}] do
291 bad_registration_attrs = %{
292 "authorization" => Map.put(params["authorization"], bad_param, bad_param_value)
295 bad_params = Map.merge(params, bad_registration_attrs)
299 |> put_session(:registration_id, registration.id)
300 |> post("/oauth/register", bad_params)
302 assert html_response(conn, 403) =~ ~r/name="op" type="submit" value="register"/
303 assert get_flash(conn, :error) == "Error: #{bad_param} has already been taken."
307 test "with valid params, POST /oauth/register?op=connect redirects to `redirect_uri` with `code`",
312 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt("testpassword"))
313 registration = insert(:registration, user: nil)
314 redirect_uri = OAuthController.default_redirect_uri(app)
318 |> put_session(:registration_id, registration.id)
323 "authorization" => %{
324 "scopes" => app.scopes,
325 "client_id" => app.client_id,
326 "redirect_uri" => redirect_uri,
327 "state" => "a_state",
328 "name" => user.nickname,
329 "password" => "testpassword"
334 assert response = html_response(conn, 302)
335 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
338 test "with unlisted `redirect_uri`, POST /oauth/register?op=connect results in HTTP 401`",
343 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt("testpassword"))
344 registration = insert(:registration, user: nil)
345 unlisted_redirect_uri = "http://cross-site-request.com"
349 |> put_session(:registration_id, registration.id)
354 "authorization" => %{
355 "scopes" => app.scopes,
356 "client_id" => app.client_id,
357 "redirect_uri" => unlisted_redirect_uri,
358 "state" => "a_state",
359 "name" => user.nickname,
360 "password" => "testpassword"
365 assert response = html_response(conn, 401)
368 test "with invalid params, POST /oauth/register?op=connect renders registration_details page",
374 registration = insert(:registration, user: nil)
378 "authorization" => %{
379 "scopes" => app.scopes,
380 "client_id" => app.client_id,
381 "redirect_uri" => OAuthController.default_redirect_uri(app),
382 "state" => "a_state",
383 "name" => user.nickname,
384 "password" => "wrong password"
390 |> put_session(:registration_id, registration.id)
391 |> post("/oauth/register", params)
393 assert html_response(conn, 401) =~ ~r/name="op" type="submit" value="connect"/
394 assert get_flash(conn, :error) == "Invalid Username/Password"
398 describe "GET /oauth/authorize" do
401 app: insert(:oauth_app, redirect_uris: "https://redirect.url"),
404 |> Plug.Session.call(Plug.Session.init(@session_opts))
409 test "renders authentication page", %{app: app, conn: conn} do
415 "response_type" => "code",
416 "client_id" => app.client_id,
417 "redirect_uri" => OAuthController.default_redirect_uri(app),
422 assert html_response(conn, 200) =~ ~s(type="submit")
425 test "properly handles internal calls with `authorization`-wrapped params", %{
434 "authorization" => %{
435 "response_type" => "code",
436 "client_id" => app.client_id,
437 "redirect_uri" => OAuthController.default_redirect_uri(app),
443 assert html_response(conn, 200) =~ ~s(type="submit")
446 test "renders authentication page if user is already authenticated but `force_login` is tru-ish",
447 %{app: app, conn: conn} do
448 token = insert(:oauth_token, app: app)
452 |> put_session(:oauth_token, token.token)
456 "response_type" => "code",
457 "client_id" => app.client_id,
458 "redirect_uri" => OAuthController.default_redirect_uri(app),
460 "force_login" => "true"
464 assert html_response(conn, 200) =~ ~s(type="submit")
467 test "renders authentication page if user is already authenticated but user request with another client",
472 token = insert(:oauth_token, app: app)
476 |> put_session(:oauth_token, token.token)
480 "response_type" => "code",
481 "client_id" => "another_client_id",
482 "redirect_uri" => OAuthController.default_redirect_uri(app),
487 assert html_response(conn, 200) =~ ~s(type="submit")
490 test "with existing authentication and non-OOB `redirect_uri`, redirects to app with `token` and `state` params",
495 token = insert(:oauth_token, app: app)
499 |> put_session(:oauth_token, token.token)
503 "response_type" => "code",
504 "client_id" => app.client_id,
505 "redirect_uri" => OAuthController.default_redirect_uri(app),
506 "state" => "specific_client_state",
511 assert URI.decode(redirected_to(conn)) ==
512 "https://redirect.url?access_token=#{token.token}&state=specific_client_state"
515 test "with existing authentication and unlisted non-OOB `redirect_uri`, redirects without credentials",
520 unlisted_redirect_uri = "http://cross-site-request.com"
521 token = insert(:oauth_token, app: app)
525 |> put_session(:oauth_token, token.token)
529 "response_type" => "code",
530 "client_id" => app.client_id,
531 "redirect_uri" => unlisted_redirect_uri,
532 "state" => "specific_client_state",
537 assert redirected_to(conn) == unlisted_redirect_uri
540 test "with existing authentication and OOB `redirect_uri`, redirects to app with `token` and `state` params",
545 token = insert(:oauth_token, app: app)
549 |> put_session(:oauth_token, token.token)
553 "response_type" => "code",
554 "client_id" => app.client_id,
555 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
560 assert html_response(conn, 200) =~ "Authorization exists"
564 describe "POST /oauth/authorize" do
565 test "redirects with oauth authorization, " <>
566 "granting requested app-supported scopes to both admin- and non-admin users" do
567 app_scopes = ["read", "write", "admin", "secret_scope"]
568 app = insert(:oauth_app, scopes: app_scopes)
569 redirect_uri = OAuthController.default_redirect_uri(app)
571 non_admin = insert(:user, is_admin: false)
572 admin = insert(:user, is_admin: true)
573 scopes_subset = ["read:subscope", "write", "admin"]
575 # In case scope param is missing, expecting _all_ app-supported scopes to be granted
576 for user <- [non_admin, admin],
577 {requested_scopes, expected_scopes} <-
578 %{scopes_subset => scopes_subset, nil => app_scopes} do
584 "authorization" => %{
585 "name" => user.nickname,
586 "password" => "test",
587 "client_id" => app.client_id,
588 "redirect_uri" => redirect_uri,
589 "scope" => requested_scopes,
590 "state" => "statepassed"
595 target = redirected_to(conn)
596 assert target =~ redirect_uri
598 query = URI.parse(target).query |> URI.query_decoder() |> Map.new()
600 assert %{"state" => "statepassed", "code" => code} = query
601 auth = Repo.get_by(Authorization, token: code)
603 assert auth.scopes == expected_scopes
607 test "returns 401 for wrong credentials", %{conn: conn} do
609 app = insert(:oauth_app)
610 redirect_uri = OAuthController.default_redirect_uri(app)
614 |> post("/oauth/authorize", %{
615 "authorization" => %{
616 "name" => user.nickname,
617 "password" => "wrong",
618 "client_id" => app.client_id,
619 "redirect_uri" => redirect_uri,
620 "state" => "statepassed",
621 "scope" => Enum.join(app.scopes, " ")
624 |> html_response(:unauthorized)
627 assert result =~ app.client_id
628 assert result =~ redirect_uri
631 assert result =~ "Invalid Username/Password"
634 test "returns 401 for missing scopes" do
635 user = insert(:user, is_admin: false)
636 app = insert(:oauth_app, scopes: ["read", "write", "admin"])
637 redirect_uri = OAuthController.default_redirect_uri(app)
641 |> post("/oauth/authorize", %{
642 "authorization" => %{
643 "name" => user.nickname,
644 "password" => "test",
645 "client_id" => app.client_id,
646 "redirect_uri" => redirect_uri,
647 "state" => "statepassed",
651 |> html_response(:unauthorized)
654 assert result =~ app.client_id
655 assert result =~ redirect_uri
658 assert result =~ "This action is outside the authorized scopes"
661 test "returns 401 for scopes beyond app scopes hierarchy", %{conn: conn} do
663 app = insert(:oauth_app, scopes: ["read", "write"])
664 redirect_uri = OAuthController.default_redirect_uri(app)
668 |> post("/oauth/authorize", %{
669 "authorization" => %{
670 "name" => user.nickname,
671 "password" => "test",
672 "client_id" => app.client_id,
673 "redirect_uri" => redirect_uri,
674 "state" => "statepassed",
675 "scope" => "read write follow"
678 |> html_response(:unauthorized)
681 assert result =~ app.client_id
682 assert result =~ redirect_uri
685 assert result =~ "This action is outside the authorized scopes"
689 describe "POST /oauth/token" do
690 test "issues a token for an all-body request" do
692 app = insert(:oauth_app, scopes: ["read", "write"])
694 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
698 |> post("/oauth/token", %{
699 "grant_type" => "authorization_code",
700 "code" => auth.token,
701 "redirect_uri" => OAuthController.default_redirect_uri(app),
702 "client_id" => app.client_id,
703 "client_secret" => app.client_secret
706 assert %{"access_token" => token, "me" => ap_id} = json_response(conn, 200)
708 token = Repo.get_by(Token, token: token)
710 assert token.scopes == auth.scopes
711 assert user.ap_id == ap_id
714 test "issues a token for `password` grant_type with valid credentials, with full permissions by default" do
715 password = "testpassword"
716 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
718 app = insert(:oauth_app, scopes: ["read", "write"])
720 # Note: "scope" param is intentionally omitted
723 |> post("/oauth/token", %{
724 "grant_type" => "password",
725 "username" => user.nickname,
726 "password" => password,
727 "client_id" => app.client_id,
728 "client_secret" => app.client_secret
731 assert %{"access_token" => token} = json_response(conn, 200)
733 token = Repo.get_by(Token, token: token)
735 assert token.scopes == app.scopes
738 test "issues a token for request with HTTP basic auth client credentials" do
740 app = insert(:oauth_app, scopes: ["scope1", "scope2", "scope3"])
742 {:ok, auth} = Authorization.create_authorization(app, user, ["scope1", "scope2"])
743 assert auth.scopes == ["scope1", "scope2"]
746 (URI.encode_www_form(app.client_id) <> ":" <> URI.encode_www_form(app.client_secret))
751 |> put_req_header("authorization", "Basic " <> app_encoded)
752 |> post("/oauth/token", %{
753 "grant_type" => "authorization_code",
754 "code" => auth.token,
755 "redirect_uri" => OAuthController.default_redirect_uri(app)
758 assert %{"access_token" => token, "scope" => scope} = json_response(conn, 200)
760 assert scope == "scope1 scope2"
762 token = Repo.get_by(Token, token: token)
764 assert token.scopes == ["scope1", "scope2"]
767 test "issue a token for client_credentials grant type" do
768 app = insert(:oauth_app, scopes: ["read", "write"])
772 |> post("/oauth/token", %{
773 "grant_type" => "client_credentials",
774 "client_id" => app.client_id,
775 "client_secret" => app.client_secret
778 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
779 json_response(conn, 200)
782 token_from_db = Repo.get_by(Token, token: token)
785 assert scope == "read write"
788 test "rejects token exchange with invalid client credentials" do
790 app = insert(:oauth_app)
792 {:ok, auth} = Authorization.create_authorization(app, user)
796 |> put_req_header("authorization", "Basic JTIxOiVGMCU5RiVBNCVCNwo=")
797 |> post("/oauth/token", %{
798 "grant_type" => "authorization_code",
799 "code" => auth.token,
800 "redirect_uri" => OAuthController.default_redirect_uri(app)
803 assert resp = json_response(conn, 400)
804 assert %{"error" => _} = resp
805 refute Map.has_key?(resp, "access_token")
808 test "rejects token exchange for valid credentials belonging to unconfirmed user and confirmation is required" do
809 Pleroma.Config.put([:instance, :account_activation_required], true)
810 password = "testpassword"
813 insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
814 |> User.confirmation_changeset(need_confirmation: true)
815 |> User.update_and_set_cache()
817 refute Pleroma.User.account_status(user) == :active
819 app = insert(:oauth_app)
823 |> post("/oauth/token", %{
824 "grant_type" => "password",
825 "username" => user.nickname,
826 "password" => password,
827 "client_id" => app.client_id,
828 "client_secret" => app.client_secret
831 assert resp = json_response(conn, 403)
832 assert %{"error" => _} = resp
833 refute Map.has_key?(resp, "access_token")
836 test "rejects token exchange for valid credentials belonging to deactivated user" do
837 password = "testpassword"
841 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
845 app = insert(:oauth_app)
849 |> post("/oauth/token", %{
850 "grant_type" => "password",
851 "username" => user.nickname,
852 "password" => password,
853 "client_id" => app.client_id,
854 "client_secret" => app.client_secret
856 |> json_response(403)
859 "error" => "Your account is currently disabled",
860 "identifier" => "account_is_disabled"
864 test "rejects token exchange for user with password_reset_pending set to true" do
865 password = "testpassword"
869 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
870 password_reset_pending: true
873 app = insert(:oauth_app, scopes: ["read", "write"])
877 |> post("/oauth/token", %{
878 "grant_type" => "password",
879 "username" => user.nickname,
880 "password" => password,
881 "client_id" => app.client_id,
882 "client_secret" => app.client_secret
884 |> json_response(403)
887 "error" => "Password reset is required",
888 "identifier" => "password_reset_required"
892 test "rejects token exchange for user with confirmation_pending set to true" do
893 Pleroma.Config.put([:instance, :account_activation_required], true)
894 password = "testpassword"
898 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
899 confirmation_pending: true
902 app = insert(:oauth_app, scopes: ["read", "write"])
906 |> post("/oauth/token", %{
907 "grant_type" => "password",
908 "username" => user.nickname,
909 "password" => password,
910 "client_id" => app.client_id,
911 "client_secret" => app.client_secret
913 |> json_response(403)
916 "error" => "Your login is missing a confirmed e-mail address",
917 "identifier" => "missing_confirmed_email"
921 test "rejects an invalid authorization code" do
922 app = insert(:oauth_app)
926 |> post("/oauth/token", %{
927 "grant_type" => "authorization_code",
928 "code" => "Imobviouslyinvalid",
929 "redirect_uri" => OAuthController.default_redirect_uri(app),
930 "client_id" => app.client_id,
931 "client_secret" => app.client_secret
934 assert resp = json_response(conn, 400)
935 assert %{"error" => _} = json_response(conn, 400)
936 refute Map.has_key?(resp, "access_token")
940 describe "POST /oauth/token - refresh token" do
941 setup do: clear_config([:oauth2, :issue_new_refresh_token])
943 test "issues a new access token with keep fresh token" do
944 Pleroma.Config.put([:oauth2, :issue_new_refresh_token], true)
946 app = insert(:oauth_app, scopes: ["read", "write"])
948 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
949 {:ok, token} = Token.exchange_token(app, auth)
953 |> post("/oauth/token", %{
954 "grant_type" => "refresh_token",
955 "refresh_token" => token.refresh_token,
956 "client_id" => app.client_id,
957 "client_secret" => app.client_secret
959 |> json_response(200)
966 "token_type" => "Bearer",
969 "refresh_token" => _,
975 refute Repo.get_by(Token, token: token.token)
976 new_token = Repo.get_by(Token, token: response["access_token"])
977 assert new_token.refresh_token == token.refresh_token
978 assert new_token.scopes == auth.scopes
979 assert new_token.user_id == user.id
980 assert new_token.app_id == app.id
983 test "issues a new access token with new fresh token" do
984 Pleroma.Config.put([:oauth2, :issue_new_refresh_token], false)
986 app = insert(:oauth_app, scopes: ["read", "write"])
988 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
989 {:ok, token} = Token.exchange_token(app, auth)
993 |> post("/oauth/token", %{
994 "grant_type" => "refresh_token",
995 "refresh_token" => token.refresh_token,
996 "client_id" => app.client_id,
997 "client_secret" => app.client_secret
999 |> json_response(200)
1006 "token_type" => "Bearer",
1007 "expires_in" => 600,
1008 "access_token" => _,
1009 "refresh_token" => _,
1015 refute Repo.get_by(Token, token: token.token)
1016 new_token = Repo.get_by(Token, token: response["access_token"])
1017 refute new_token.refresh_token == token.refresh_token
1018 assert new_token.scopes == auth.scopes
1019 assert new_token.user_id == user.id
1020 assert new_token.app_id == app.id
1023 test "returns 400 if we try use access token" do
1024 user = insert(:user)
1025 app = insert(:oauth_app, scopes: ["read", "write"])
1027 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1028 {:ok, token} = Token.exchange_token(app, auth)
1032 |> post("/oauth/token", %{
1033 "grant_type" => "refresh_token",
1034 "refresh_token" => token.token,
1035 "client_id" => app.client_id,
1036 "client_secret" => app.client_secret
1038 |> json_response(400)
1040 assert %{"error" => "Invalid credentials"} == response
1043 test "returns 400 if refresh_token invalid" do
1044 app = insert(:oauth_app, scopes: ["read", "write"])
1048 |> post("/oauth/token", %{
1049 "grant_type" => "refresh_token",
1050 "refresh_token" => "token.refresh_token",
1051 "client_id" => app.client_id,
1052 "client_secret" => app.client_secret
1054 |> json_response(400)
1056 assert %{"error" => "Invalid credentials"} == response
1059 test "issues a new token if token expired" do
1060 user = insert(:user)
1061 app = insert(:oauth_app, scopes: ["read", "write"])
1063 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1064 {:ok, token} = Token.exchange_token(app, auth)
1067 Ecto.Changeset.change(
1069 %{valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -86_400 * 30)}
1072 {:ok, access_token} = Repo.update(change)
1076 |> post("/oauth/token", %{
1077 "grant_type" => "refresh_token",
1078 "refresh_token" => access_token.refresh_token,
1079 "client_id" => app.client_id,
1080 "client_secret" => app.client_secret
1082 |> json_response(200)
1089 "token_type" => "Bearer",
1090 "expires_in" => 600,
1091 "access_token" => _,
1092 "refresh_token" => _,
1098 refute Repo.get_by(Token, token: token.token)
1099 token = Repo.get_by(Token, token: response["access_token"])
1101 assert token.scopes == auth.scopes
1102 assert token.user_id == user.id
1103 assert token.app_id == app.id
1107 describe "POST /oauth/token - bad request" do
1108 test "returns 500" do
1111 |> post("/oauth/token", %{})
1112 |> json_response(500)
1114 assert %{"error" => "Bad request"} == response
1118 describe "POST /oauth/revoke - bad request" do
1119 test "returns 500" do
1122 |> post("/oauth/revoke", %{})
1123 |> json_response(500)
1125 assert %{"error" => "Bad request"} == response