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
10 alias Pleroma.MFA.TOTP
13 alias Pleroma.Web.OAuth.Authorization
14 alias Pleroma.Web.OAuth.OAuthController
15 alias Pleroma.Web.OAuth.Token
20 signing_salt: "cooldude"
23 clear_config([:instance, :account_activation_required])
24 clear_config([:instance, :account_approval_required])
27 describe "in OAuth consumer mode, " do
30 app: insert(:oauth_app),
33 |> Plug.Session.call(Plug.Session.init(@session_opts))
38 setup do: clear_config([:auth, :oauth_consumer_strategies], ~w(twitter facebook))
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: Pbkdf2.hash_pwd_salt("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: Pbkdf2.hash_pwd_salt("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 "redirect to on two-factor auth page" do
613 otp_secret = TOTP.generate_secret()
617 multi_factor_authentication_settings: %MFA.Settings{
619 totp: %MFA.Settings.TOTP{secret: otp_secret, confirmed: true}
623 app = insert(:oauth_app, scopes: ["read", "write", "follow"])
627 |> post("/oauth/authorize", %{
628 "authorization" => %{
629 "name" => user.nickname,
630 "password" => "test",
631 "client_id" => app.client_id,
632 "redirect_uri" => app.redirect_uris,
633 "scope" => "read write",
634 "state" => "statepassed"
638 result = html_response(conn, 200)
640 mfa_token = Repo.get_by(MFA.Token, user_id: user.id)
641 assert result =~ app.redirect_uris
642 assert result =~ "statepassed"
643 assert result =~ mfa_token.token
644 assert result =~ "Two-factor authentication"
647 test "returns 401 for wrong credentials", %{conn: conn} do
649 app = insert(:oauth_app)
650 redirect_uri = OAuthController.default_redirect_uri(app)
654 |> post("/oauth/authorize", %{
655 "authorization" => %{
656 "name" => user.nickname,
657 "password" => "wrong",
658 "client_id" => app.client_id,
659 "redirect_uri" => redirect_uri,
660 "state" => "statepassed",
661 "scope" => Enum.join(app.scopes, " ")
664 |> html_response(:unauthorized)
667 assert result =~ app.client_id
668 assert result =~ redirect_uri
671 assert result =~ "Invalid Username/Password"
674 test "returns 401 for missing scopes" do
675 user = insert(:user, is_admin: false)
676 app = insert(:oauth_app, scopes: ["read", "write", "admin"])
677 redirect_uri = OAuthController.default_redirect_uri(app)
681 |> post("/oauth/authorize", %{
682 "authorization" => %{
683 "name" => user.nickname,
684 "password" => "test",
685 "client_id" => app.client_id,
686 "redirect_uri" => redirect_uri,
687 "state" => "statepassed",
691 |> html_response(:unauthorized)
694 assert result =~ app.client_id
695 assert result =~ redirect_uri
698 assert result =~ "This action is outside the authorized scopes"
701 test "returns 401 for scopes beyond app scopes hierarchy", %{conn: conn} do
703 app = insert(:oauth_app, scopes: ["read", "write"])
704 redirect_uri = OAuthController.default_redirect_uri(app)
708 |> post("/oauth/authorize", %{
709 "authorization" => %{
710 "name" => user.nickname,
711 "password" => "test",
712 "client_id" => app.client_id,
713 "redirect_uri" => redirect_uri,
714 "state" => "statepassed",
715 "scope" => "read write follow"
718 |> html_response(:unauthorized)
721 assert result =~ app.client_id
722 assert result =~ redirect_uri
725 assert result =~ "This action is outside the authorized scopes"
729 describe "POST /oauth/token" do
730 test "issues a token for an all-body request" do
732 app = insert(:oauth_app, scopes: ["read", "write"])
734 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
738 |> post("/oauth/token", %{
739 "grant_type" => "authorization_code",
740 "code" => auth.token,
741 "redirect_uri" => OAuthController.default_redirect_uri(app),
742 "client_id" => app.client_id,
743 "client_secret" => app.client_secret
746 assert %{"access_token" => token, "me" => ap_id} = json_response(conn, 200)
748 token = Repo.get_by(Token, token: token)
750 assert token.scopes == auth.scopes
751 assert user.ap_id == ap_id
754 test "issues a token for `password` grant_type with valid credentials, with full permissions by default" do
755 password = "testpassword"
756 user = insert(:user, password_hash: Pbkdf2.hash_pwd_salt(password))
758 app = insert(:oauth_app, scopes: ["read", "write"])
760 # Note: "scope" param is intentionally omitted
763 |> post("/oauth/token", %{
764 "grant_type" => "password",
765 "username" => user.nickname,
766 "password" => password,
767 "client_id" => app.client_id,
768 "client_secret" => app.client_secret
771 assert %{"access_token" => token} = json_response(conn, 200)
773 token = Repo.get_by(Token, token: token)
775 assert token.scopes == app.scopes
778 test "issues a mfa token for `password` grant_type, when MFA enabled" do
779 password = "testpassword"
780 otp_secret = TOTP.generate_secret()
784 password_hash: Pbkdf2.hash_pwd_salt(password),
785 multi_factor_authentication_settings: %MFA.Settings{
787 totp: %MFA.Settings.TOTP{secret: otp_secret, confirmed: true}
791 app = insert(:oauth_app, scopes: ["read", "write"])
795 |> post("/oauth/token", %{
796 "grant_type" => "password",
797 "username" => user.nickname,
798 "password" => password,
799 "client_id" => app.client_id,
800 "client_secret" => app.client_secret
802 |> json_response(403)
806 "supported_challenge_types" => "totp",
808 "error" => "mfa_required"
813 token = Repo.get_by(MFA.Token, token: response["mfa_token"])
814 assert token.user_id == user.id
815 assert token.authorization_id
818 test "issues a token for request with HTTP basic auth client credentials" do
820 app = insert(:oauth_app, scopes: ["scope1", "scope2", "scope3"])
822 {:ok, auth} = Authorization.create_authorization(app, user, ["scope1", "scope2"])
823 assert auth.scopes == ["scope1", "scope2"]
826 (URI.encode_www_form(app.client_id) <> ":" <> URI.encode_www_form(app.client_secret))
831 |> put_req_header("authorization", "Basic " <> app_encoded)
832 |> post("/oauth/token", %{
833 "grant_type" => "authorization_code",
834 "code" => auth.token,
835 "redirect_uri" => OAuthController.default_redirect_uri(app)
838 assert %{"access_token" => token, "scope" => scope} = json_response(conn, 200)
840 assert scope == "scope1 scope2"
842 token = Repo.get_by(Token, token: token)
844 assert token.scopes == ["scope1", "scope2"]
847 test "issue a token for client_credentials grant type" do
848 app = insert(:oauth_app, scopes: ["read", "write"])
852 |> post("/oauth/token", %{
853 "grant_type" => "client_credentials",
854 "client_id" => app.client_id,
855 "client_secret" => app.client_secret
858 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
859 json_response(conn, 200)
862 token_from_db = Repo.get_by(Token, token: token)
865 assert scope == "read write"
868 test "rejects token exchange with invalid client credentials" do
870 app = insert(:oauth_app)
872 {:ok, auth} = Authorization.create_authorization(app, user)
876 |> put_req_header("authorization", "Basic JTIxOiVGMCU5RiVBNCVCNwo=")
877 |> post("/oauth/token", %{
878 "grant_type" => "authorization_code",
879 "code" => auth.token,
880 "redirect_uri" => OAuthController.default_redirect_uri(app)
883 assert resp = json_response(conn, 400)
884 assert %{"error" => _} = resp
885 refute Map.has_key?(resp, "access_token")
888 test "rejects token exchange for valid credentials belonging to unconfirmed user and confirmation is required" do
889 Pleroma.Config.put([:instance, :account_activation_required], true)
890 password = "testpassword"
893 insert(:user, password_hash: Pbkdf2.hash_pwd_salt(password))
894 |> User.confirmation_changeset(need_confirmation: true)
895 |> User.update_and_set_cache()
897 refute Pleroma.User.account_status(user) == :active
899 app = insert(:oauth_app)
903 |> post("/oauth/token", %{
904 "grant_type" => "password",
905 "username" => user.nickname,
906 "password" => password,
907 "client_id" => app.client_id,
908 "client_secret" => app.client_secret
911 assert resp = json_response(conn, 403)
912 assert %{"error" => _} = resp
913 refute Map.has_key?(resp, "access_token")
916 test "rejects token exchange for valid credentials belonging to deactivated user" do
917 password = "testpassword"
921 password_hash: Pbkdf2.hash_pwd_salt(password),
925 app = insert(:oauth_app)
929 |> post("/oauth/token", %{
930 "grant_type" => "password",
931 "username" => user.nickname,
932 "password" => password,
933 "client_id" => app.client_id,
934 "client_secret" => app.client_secret
936 |> json_response(403)
939 "error" => "Your account is currently disabled",
940 "identifier" => "account_is_disabled"
944 test "rejects token exchange for user with password_reset_pending set to true" do
945 password = "testpassword"
949 password_hash: Pbkdf2.hash_pwd_salt(password),
950 password_reset_pending: true
953 app = insert(:oauth_app, scopes: ["read", "write"])
957 |> post("/oauth/token", %{
958 "grant_type" => "password",
959 "username" => user.nickname,
960 "password" => password,
961 "client_id" => app.client_id,
962 "client_secret" => app.client_secret
964 |> json_response(403)
967 "error" => "Password reset is required",
968 "identifier" => "password_reset_required"
972 test "rejects token exchange for user with confirmation_pending set to true" do
973 Pleroma.Config.put([:instance, :account_activation_required], true)
974 password = "testpassword"
978 password_hash: Pbkdf2.hash_pwd_salt(password),
979 confirmation_pending: true
982 app = insert(:oauth_app, scopes: ["read", "write"])
986 |> post("/oauth/token", %{
987 "grant_type" => "password",
988 "username" => user.nickname,
989 "password" => password,
990 "client_id" => app.client_id,
991 "client_secret" => app.client_secret
993 |> json_response(403)
996 "error" => "Your login is missing a confirmed e-mail address",
997 "identifier" => "missing_confirmed_email"
1001 test "rejects token exchange for valid credentials belonging to an unapproved user" do
1002 password = "testpassword"
1004 user = insert(:user, password_hash: Pbkdf2.hash_pwd_salt(password), approval_pending: true)
1006 refute Pleroma.User.account_status(user) == :active
1008 app = insert(:oauth_app)
1012 |> post("/oauth/token", %{
1013 "grant_type" => "password",
1014 "username" => user.nickname,
1015 "password" => password,
1016 "client_id" => app.client_id,
1017 "client_secret" => app.client_secret
1020 assert resp = json_response(conn, 403)
1021 assert %{"error" => _} = resp
1022 refute Map.has_key?(resp, "access_token")
1025 test "rejects an invalid authorization code" do
1026 app = insert(:oauth_app)
1030 |> post("/oauth/token", %{
1031 "grant_type" => "authorization_code",
1032 "code" => "Imobviouslyinvalid",
1033 "redirect_uri" => OAuthController.default_redirect_uri(app),
1034 "client_id" => app.client_id,
1035 "client_secret" => app.client_secret
1038 assert resp = json_response(conn, 400)
1039 assert %{"error" => _} = json_response(conn, 400)
1040 refute Map.has_key?(resp, "access_token")
1044 describe "POST /oauth/token - refresh token" do
1045 setup do: clear_config([:oauth2, :issue_new_refresh_token])
1047 test "issues a new access token with keep fresh token" do
1048 Pleroma.Config.put([:oauth2, :issue_new_refresh_token], true)
1049 user = insert(:user)
1050 app = insert(:oauth_app, scopes: ["read", "write"])
1052 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1053 {:ok, token} = Token.exchange_token(app, auth)
1057 |> post("/oauth/token", %{
1058 "grant_type" => "refresh_token",
1059 "refresh_token" => token.refresh_token,
1060 "client_id" => app.client_id,
1061 "client_secret" => app.client_secret
1063 |> json_response(200)
1070 "token_type" => "Bearer",
1071 "expires_in" => 600,
1072 "access_token" => _,
1073 "refresh_token" => _,
1079 refute Repo.get_by(Token, token: token.token)
1080 new_token = Repo.get_by(Token, token: response["access_token"])
1081 assert new_token.refresh_token == token.refresh_token
1082 assert new_token.scopes == auth.scopes
1083 assert new_token.user_id == user.id
1084 assert new_token.app_id == app.id
1087 test "issues a new access token with new fresh token" do
1088 Pleroma.Config.put([:oauth2, :issue_new_refresh_token], false)
1089 user = insert(:user)
1090 app = insert(:oauth_app, scopes: ["read", "write"])
1092 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1093 {:ok, token} = Token.exchange_token(app, auth)
1097 |> post("/oauth/token", %{
1098 "grant_type" => "refresh_token",
1099 "refresh_token" => token.refresh_token,
1100 "client_id" => app.client_id,
1101 "client_secret" => app.client_secret
1103 |> json_response(200)
1110 "token_type" => "Bearer",
1111 "expires_in" => 600,
1112 "access_token" => _,
1113 "refresh_token" => _,
1119 refute Repo.get_by(Token, token: token.token)
1120 new_token = Repo.get_by(Token, token: response["access_token"])
1121 refute new_token.refresh_token == token.refresh_token
1122 assert new_token.scopes == auth.scopes
1123 assert new_token.user_id == user.id
1124 assert new_token.app_id == app.id
1127 test "returns 400 if we try use access token" do
1128 user = insert(:user)
1129 app = insert(:oauth_app, scopes: ["read", "write"])
1131 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1132 {:ok, token} = Token.exchange_token(app, auth)
1136 |> post("/oauth/token", %{
1137 "grant_type" => "refresh_token",
1138 "refresh_token" => token.token,
1139 "client_id" => app.client_id,
1140 "client_secret" => app.client_secret
1142 |> json_response(400)
1144 assert %{"error" => "Invalid credentials"} == response
1147 test "returns 400 if refresh_token invalid" do
1148 app = insert(:oauth_app, scopes: ["read", "write"])
1152 |> post("/oauth/token", %{
1153 "grant_type" => "refresh_token",
1154 "refresh_token" => "token.refresh_token",
1155 "client_id" => app.client_id,
1156 "client_secret" => app.client_secret
1158 |> json_response(400)
1160 assert %{"error" => "Invalid credentials"} == response
1163 test "issues a new token if token expired" do
1164 user = insert(:user)
1165 app = insert(:oauth_app, scopes: ["read", "write"])
1167 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1168 {:ok, token} = Token.exchange_token(app, auth)
1171 Ecto.Changeset.change(
1173 %{valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -86_400 * 30)}
1176 {:ok, access_token} = Repo.update(change)
1180 |> post("/oauth/token", %{
1181 "grant_type" => "refresh_token",
1182 "refresh_token" => access_token.refresh_token,
1183 "client_id" => app.client_id,
1184 "client_secret" => app.client_secret
1186 |> json_response(200)
1193 "token_type" => "Bearer",
1194 "expires_in" => 600,
1195 "access_token" => _,
1196 "refresh_token" => _,
1202 refute Repo.get_by(Token, token: token.token)
1203 token = Repo.get_by(Token, token: response["access_token"])
1205 assert token.scopes == auth.scopes
1206 assert token.user_id == user.id
1207 assert token.app_id == app.id
1211 describe "POST /oauth/token - bad request" do
1212 test "returns 500" do
1215 |> post("/oauth/token", %{})
1216 |> json_response(500)
1218 assert %{"error" => "Bad request"} == response
1222 describe "POST /oauth/revoke - bad request" do
1223 test "returns 500" do
1226 |> post("/oauth/revoke", %{})
1227 |> json_response(500)
1229 assert %{"error" => "Bad request"} == response