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.Helpers.AuthHelper
12 alias Pleroma.MFA.TOTP
15 alias Pleroma.Web.OAuth.Authorization
16 alias Pleroma.Web.OAuth.OAuthController
17 alias Pleroma.Web.OAuth.Token
22 signing_salt: "cooldude"
25 clear_config([:instance, :account_activation_required])
26 clear_config([:instance, :account_approval_required])
29 describe "in OAuth consumer mode, " do
32 app: insert(:oauth_app),
35 |> Plug.Session.call(Plug.Session.init(@session_opts))
40 setup do: clear_config([:auth, :oauth_consumer_strategies], ~w(twitter facebook))
42 test "GET /oauth/authorize renders auth forms, including OAuth consumer form", %{
51 "response_type" => "code",
52 "client_id" => app.client_id,
53 "redirect_uri" => OAuthController.default_redirect_uri(app),
58 assert response = html_response(conn, 200)
59 assert response =~ "Sign in with Twitter"
60 assert response =~ o_auth_path(conn, :prepare_request)
63 test "GET /oauth/prepare_request encodes parameters as `state` and redirects", %{
70 "/oauth/prepare_request",
72 "provider" => "twitter",
74 "scope" => "read follow",
75 "client_id" => app.client_id,
76 "redirect_uri" => OAuthController.default_redirect_uri(app),
82 assert html_response(conn, 302)
84 redirect_query = URI.parse(redirected_to(conn)).query
85 assert %{"state" => state_param} = URI.decode_query(redirect_query)
86 assert {:ok, state_components} = Jason.decode(state_param)
88 expected_client_id = app.client_id
89 expected_redirect_uri = app.redirect_uris
92 "scope" => "read follow",
93 "client_id" => ^expected_client_id,
94 "redirect_uri" => ^expected_redirect_uri,
99 test "with user-bound registration, GET /oauth/<provider>/callback redirects to `redirect_uri` with `code`",
100 %{app: app, conn: conn} do
101 registration = insert(:registration)
102 redirect_uri = OAuthController.default_redirect_uri(app)
105 "scope" => Enum.join(app.scopes, " "),
106 "client_id" => app.client_id,
107 "redirect_uri" => redirect_uri,
113 |> assign(:ueberauth_auth, %{provider: registration.provider, uid: registration.uid})
115 "/oauth/twitter/callback",
117 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
118 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
119 "provider" => "twitter",
120 "state" => Jason.encode!(state_params)
124 assert html_response(conn, 302)
125 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
128 test "with user-unbound registration, GET /oauth/<provider>/callback renders registration_details page",
129 %{app: app, conn: conn} do
133 "scope" => "read write",
134 "client_id" => app.client_id,
135 "redirect_uri" => OAuthController.default_redirect_uri(app),
141 |> assign(:ueberauth_auth, %{
144 info: %{nickname: user.nickname, email: user.email, name: user.name, description: nil}
147 "/oauth/twitter/callback",
149 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
150 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
151 "provider" => "twitter",
152 "state" => Jason.encode!(state_params)
156 assert response = html_response(conn, 200)
157 assert response =~ ~r/name="op" type="submit" value="register"/
158 assert response =~ ~r/name="op" type="submit" value="connect"/
159 assert response =~ user.email
160 assert response =~ user.nickname
163 test "on authentication error, GET /oauth/<provider>/callback redirects to `redirect_uri`", %{
168 "scope" => Enum.join(app.scopes, " "),
169 "client_id" => app.client_id,
170 "redirect_uri" => OAuthController.default_redirect_uri(app),
176 |> assign(:ueberauth_failure, %{errors: [%{message: "(error description)"}]})
178 "/oauth/twitter/callback",
180 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
181 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
182 "provider" => "twitter",
183 "state" => Jason.encode!(state_params)
187 assert html_response(conn, 302)
188 assert redirected_to(conn) == app.redirect_uris
189 assert get_flash(conn, :error) == "Failed to authenticate: (error description)."
192 test "GET /oauth/registration_details renders registration details form", %{
199 "/oauth/registration_details",
201 "authorization" => %{
202 "scopes" => app.scopes,
203 "client_id" => app.client_id,
204 "redirect_uri" => OAuthController.default_redirect_uri(app),
205 "state" => "a_state",
207 "email" => "john@doe.com"
212 assert response = html_response(conn, 200)
213 assert response =~ ~r/name="op" type="submit" value="register"/
214 assert response =~ ~r/name="op" type="submit" value="connect"/
217 test "with valid params, POST /oauth/register?op=register redirects to `redirect_uri` with `code`",
222 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
223 redirect_uri = OAuthController.default_redirect_uri(app)
227 |> put_session(:registration_id, registration.id)
232 "authorization" => %{
233 "scopes" => app.scopes,
234 "client_id" => app.client_id,
235 "redirect_uri" => redirect_uri,
236 "state" => "a_state",
237 "nickname" => "availablenick",
238 "email" => "available@email.com"
243 assert html_response(conn, 302)
244 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
247 test "with unlisted `redirect_uri`, POST /oauth/register?op=register results in HTTP 401",
252 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
253 unlisted_redirect_uri = "http://cross-site-request.com"
257 |> put_session(:registration_id, registration.id)
262 "authorization" => %{
263 "scopes" => app.scopes,
264 "client_id" => app.client_id,
265 "redirect_uri" => unlisted_redirect_uri,
266 "state" => "a_state",
267 "nickname" => "availablenick",
268 "email" => "available@email.com"
273 assert html_response(conn, 401)
276 test "with invalid params, POST /oauth/register?op=register renders registration_details page",
281 another_user = insert(:user)
282 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
286 "authorization" => %{
287 "scopes" => app.scopes,
288 "client_id" => app.client_id,
289 "redirect_uri" => OAuthController.default_redirect_uri(app),
290 "state" => "a_state",
291 "nickname" => "availablenickname",
292 "email" => "available@email.com"
296 for {bad_param, bad_param_value} <-
297 [{"nickname", another_user.nickname}, {"email", another_user.email}] do
298 bad_registration_attrs = %{
299 "authorization" => Map.put(params["authorization"], bad_param, bad_param_value)
302 bad_params = Map.merge(params, bad_registration_attrs)
306 |> put_session(:registration_id, registration.id)
307 |> post("/oauth/register", bad_params)
309 assert html_response(conn, 403) =~ ~r/name="op" type="submit" value="register"/
310 assert get_flash(conn, :error) == "Error: #{bad_param} has already been taken."
314 test "with valid params, POST /oauth/register?op=connect redirects to `redirect_uri` with `code`",
319 user = insert(:user, password_hash: Pbkdf2.hash_pwd_salt("testpassword"))
320 registration = insert(:registration, user: nil)
321 redirect_uri = OAuthController.default_redirect_uri(app)
325 |> put_session(:registration_id, registration.id)
330 "authorization" => %{
331 "scopes" => app.scopes,
332 "client_id" => app.client_id,
333 "redirect_uri" => redirect_uri,
334 "state" => "a_state",
335 "name" => user.nickname,
336 "password" => "testpassword"
341 assert html_response(conn, 302)
342 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
345 test "with unlisted `redirect_uri`, POST /oauth/register?op=connect results in HTTP 401`",
350 user = insert(:user, password_hash: Pbkdf2.hash_pwd_salt("testpassword"))
351 registration = insert(:registration, user: nil)
352 unlisted_redirect_uri = "http://cross-site-request.com"
356 |> put_session(:registration_id, registration.id)
361 "authorization" => %{
362 "scopes" => app.scopes,
363 "client_id" => app.client_id,
364 "redirect_uri" => unlisted_redirect_uri,
365 "state" => "a_state",
366 "name" => user.nickname,
367 "password" => "testpassword"
372 assert html_response(conn, 401)
375 test "with invalid params, POST /oauth/register?op=connect renders registration_details page",
381 registration = insert(:registration, user: nil)
385 "authorization" => %{
386 "scopes" => app.scopes,
387 "client_id" => app.client_id,
388 "redirect_uri" => OAuthController.default_redirect_uri(app),
389 "state" => "a_state",
390 "name" => user.nickname,
391 "password" => "wrong password"
397 |> put_session(:registration_id, registration.id)
398 |> post("/oauth/register", params)
400 assert html_response(conn, 401) =~ ~r/name="op" type="submit" value="connect"/
401 assert get_flash(conn, :error) == "Invalid Username/Password"
405 describe "GET /oauth/authorize" do
408 app: insert(:oauth_app, redirect_uris: "https://redirect.url"),
411 |> Plug.Session.call(Plug.Session.init(@session_opts))
416 test "renders authentication page", %{app: app, conn: conn} do
422 "response_type" => "code",
423 "client_id" => app.client_id,
424 "redirect_uri" => OAuthController.default_redirect_uri(app),
429 assert html_response(conn, 200) =~ ~s(type="submit")
432 test "properly handles internal calls with `authorization`-wrapped params", %{
441 "authorization" => %{
442 "response_type" => "code",
443 "client_id" => app.client_id,
444 "redirect_uri" => OAuthController.default_redirect_uri(app),
450 assert html_response(conn, 200) =~ ~s(type="submit")
453 test "renders authentication page if user is already authenticated but `force_login` is tru-ish",
454 %{app: app, conn: conn} do
455 token = insert(:oauth_token, app: app)
459 |> AuthHelper.put_session_token(token.token)
463 "response_type" => "code",
464 "client_id" => app.client_id,
465 "redirect_uri" => OAuthController.default_redirect_uri(app),
467 "force_login" => "true"
471 assert html_response(conn, 200) =~ ~s(type="submit")
474 test "renders authentication page if user is already authenticated but user request with another client",
479 token = insert(:oauth_token, app: app)
483 |> AuthHelper.put_session_token(token.token)
487 "response_type" => "code",
488 "client_id" => "another_client_id",
489 "redirect_uri" => OAuthController.default_redirect_uri(app),
494 assert html_response(conn, 200) =~ ~s(type="submit")
497 test "with existing authentication and non-OOB `redirect_uri`, redirects to app with `token` and `state` params",
502 token = insert(:oauth_token, app: app)
506 |> AuthHelper.put_session_token(token.token)
510 "response_type" => "code",
511 "client_id" => app.client_id,
512 "redirect_uri" => OAuthController.default_redirect_uri(app),
513 "state" => "specific_client_state",
518 assert URI.decode(redirected_to(conn)) ==
519 "https://redirect.url?access_token=#{token.token}&state=specific_client_state"
522 test "with existing authentication and unlisted non-OOB `redirect_uri`, redirects without credentials",
527 unlisted_redirect_uri = "http://cross-site-request.com"
528 token = insert(:oauth_token, app: app)
532 |> AuthHelper.put_session_token(token.token)
536 "response_type" => "code",
537 "client_id" => app.client_id,
538 "redirect_uri" => unlisted_redirect_uri,
539 "state" => "specific_client_state",
544 assert redirected_to(conn) == unlisted_redirect_uri
547 test "with existing authentication and OOB `redirect_uri`, redirects to app with `token` and `state` params",
552 token = insert(:oauth_token, app: app)
556 |> AuthHelper.put_session_token(token.token)
560 "response_type" => "code",
561 "client_id" => app.client_id,
562 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
567 assert html_response(conn, 200) =~ "Authorization exists"
571 describe "POST /oauth/authorize" do
572 test "redirects with oauth authorization, " <>
573 "granting requested app-supported scopes to both admin- and non-admin users" do
574 app_scopes = ["read", "write", "admin", "secret_scope"]
575 app = insert(:oauth_app, scopes: app_scopes)
576 redirect_uri = OAuthController.default_redirect_uri(app)
578 non_admin = insert(:user, is_admin: false)
579 admin = insert(:user, is_admin: true)
580 scopes_subset = ["read:subscope", "write", "admin"]
582 # In case scope param is missing, expecting _all_ app-supported scopes to be granted
583 for user <- [non_admin, admin],
584 {requested_scopes, expected_scopes} <-
585 %{scopes_subset => scopes_subset, nil: app_scopes} do
591 "authorization" => %{
592 "name" => user.nickname,
593 "password" => "test",
594 "client_id" => app.client_id,
595 "redirect_uri" => redirect_uri,
596 "scope" => requested_scopes,
597 "state" => "statepassed"
602 target = redirected_to(conn)
603 assert target =~ redirect_uri
605 query = URI.parse(target).query |> URI.query_decoder() |> Map.new()
607 assert %{"state" => "statepassed", "code" => code} = query
608 auth = Repo.get_by(Authorization, token: code)
610 assert auth.scopes == expected_scopes
614 test "authorize from cookie" do
616 app = insert(:oauth_app)
617 oauth_token = insert(:oauth_token, user: user, app: app)
618 redirect_uri = OAuthController.default_redirect_uri(app)
622 |> Plug.Session.call(Plug.Session.init(@session_opts))
624 |> AuthHelper.put_session_token(oauth_token.token)
628 "authorization" => %{
629 "name" => user.nickname,
630 "client_id" => app.client_id,
631 "redirect_uri" => redirect_uri,
632 "scope" => app.scopes,
633 "state" => "statepassed"
638 target = redirected_to(conn)
639 assert target =~ redirect_uri
641 query = URI.parse(target).query |> URI.query_decoder() |> Map.new()
643 assert %{"state" => "statepassed", "code" => code} = query
644 auth = Repo.get_by(Authorization, token: code)
646 assert auth.scopes == app.scopes
649 test "redirect to on two-factor auth page" do
650 otp_secret = TOTP.generate_secret()
654 multi_factor_authentication_settings: %MFA.Settings{
656 totp: %MFA.Settings.TOTP{secret: otp_secret, confirmed: true}
660 app = insert(:oauth_app, scopes: ["read", "write", "follow"])
664 |> post("/oauth/authorize", %{
665 "authorization" => %{
666 "name" => user.nickname,
667 "password" => "test",
668 "client_id" => app.client_id,
669 "redirect_uri" => app.redirect_uris,
670 "scope" => "read write",
671 "state" => "statepassed"
675 result = html_response(conn, 200)
677 mfa_token = Repo.get_by(MFA.Token, user_id: user.id)
678 assert result =~ app.redirect_uris
679 assert result =~ "statepassed"
680 assert result =~ mfa_token.token
681 assert result =~ "Two-factor authentication"
684 test "returns 401 for wrong credentials", %{conn: conn} do
686 app = insert(:oauth_app)
687 redirect_uri = OAuthController.default_redirect_uri(app)
691 |> post("/oauth/authorize", %{
692 "authorization" => %{
693 "name" => user.nickname,
694 "password" => "wrong",
695 "client_id" => app.client_id,
696 "redirect_uri" => redirect_uri,
697 "state" => "statepassed",
698 "scope" => Enum.join(app.scopes, " ")
701 |> html_response(:unauthorized)
704 assert result =~ app.client_id
705 assert result =~ redirect_uri
708 assert result =~ "Invalid Username/Password"
711 test "returns 401 for missing scopes" do
712 user = insert(:user, is_admin: false)
713 app = insert(:oauth_app, scopes: ["read", "write", "admin"])
714 redirect_uri = OAuthController.default_redirect_uri(app)
718 |> post("/oauth/authorize", %{
719 "authorization" => %{
720 "name" => user.nickname,
721 "password" => "test",
722 "client_id" => app.client_id,
723 "redirect_uri" => redirect_uri,
724 "state" => "statepassed",
728 |> html_response(:unauthorized)
731 assert result =~ app.client_id
732 assert result =~ redirect_uri
735 assert result =~ "This action is outside the authorized scopes"
738 test "returns 401 for scopes beyond app scopes hierarchy", %{conn: conn} do
740 app = insert(:oauth_app, scopes: ["read", "write"])
741 redirect_uri = OAuthController.default_redirect_uri(app)
745 |> post("/oauth/authorize", %{
746 "authorization" => %{
747 "name" => user.nickname,
748 "password" => "test",
749 "client_id" => app.client_id,
750 "redirect_uri" => redirect_uri,
751 "state" => "statepassed",
752 "scope" => "read write follow"
755 |> html_response(:unauthorized)
758 assert result =~ app.client_id
759 assert result =~ redirect_uri
762 assert result =~ "This action is outside the authorized scopes"
766 describe "POST /oauth/token" do
767 test "issues a token for an all-body request" do
769 app = insert(:oauth_app, scopes: ["read", "write"])
771 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
775 |> post("/oauth/token", %{
776 "grant_type" => "authorization_code",
777 "code" => auth.token,
778 "redirect_uri" => OAuthController.default_redirect_uri(app),
779 "client_id" => app.client_id,
780 "client_secret" => app.client_secret
783 assert %{"access_token" => token, "me" => ap_id} = json_response(conn, 200)
785 token = Repo.get_by(Token, token: token)
787 assert token.scopes == auth.scopes
788 assert user.ap_id == ap_id
791 test "issues a token for `password` grant_type with valid credentials, with full permissions by default" do
792 password = "testpassword"
793 user = insert(:user, password_hash: Pbkdf2.hash_pwd_salt(password))
795 app = insert(:oauth_app, scopes: ["read", "write"])
797 # Note: "scope" param is intentionally omitted
800 |> post("/oauth/token", %{
801 "grant_type" => "password",
802 "username" => user.nickname,
803 "password" => password,
804 "client_id" => app.client_id,
805 "client_secret" => app.client_secret
808 assert %{"access_token" => token} = json_response(conn, 200)
810 token = Repo.get_by(Token, token: token)
812 assert token.scopes == app.scopes
815 test "issues a mfa token for `password` grant_type, when MFA enabled" do
816 password = "testpassword"
817 otp_secret = TOTP.generate_secret()
821 password_hash: Pbkdf2.hash_pwd_salt(password),
822 multi_factor_authentication_settings: %MFA.Settings{
824 totp: %MFA.Settings.TOTP{secret: otp_secret, confirmed: true}
828 app = insert(:oauth_app, scopes: ["read", "write"])
832 |> post("/oauth/token", %{
833 "grant_type" => "password",
834 "username" => user.nickname,
835 "password" => password,
836 "client_id" => app.client_id,
837 "client_secret" => app.client_secret
839 |> json_response(403)
843 "supported_challenge_types" => "totp",
845 "error" => "mfa_required"
850 token = Repo.get_by(MFA.Token, token: response["mfa_token"])
851 assert token.user_id == user.id
852 assert token.authorization_id
855 test "issues a token for request with HTTP basic auth client credentials" do
857 app = insert(:oauth_app, scopes: ["scope1", "scope2", "scope3"])
859 {:ok, auth} = Authorization.create_authorization(app, user, ["scope1", "scope2"])
860 assert auth.scopes == ["scope1", "scope2"]
863 (URI.encode_www_form(app.client_id) <> ":" <> URI.encode_www_form(app.client_secret))
868 |> put_req_header("authorization", "Basic " <> app_encoded)
869 |> post("/oauth/token", %{
870 "grant_type" => "authorization_code",
871 "code" => auth.token,
872 "redirect_uri" => OAuthController.default_redirect_uri(app)
875 assert %{"access_token" => token, "scope" => scope} = json_response(conn, 200)
877 assert scope == "scope1 scope2"
879 token = Repo.get_by(Token, token: token)
881 assert token.scopes == ["scope1", "scope2"]
884 test "issue a token for client_credentials grant type" do
885 app = insert(:oauth_app, scopes: ["read", "write"])
889 |> post("/oauth/token", %{
890 "grant_type" => "client_credentials",
891 "client_id" => app.client_id,
892 "client_secret" => app.client_secret
895 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
896 json_response(conn, 200)
899 token_from_db = Repo.get_by(Token, token: token)
902 assert scope == "read write"
905 test "rejects token exchange with invalid client credentials" do
907 app = insert(:oauth_app)
909 {:ok, auth} = Authorization.create_authorization(app, user)
913 |> put_req_header("authorization", "Basic JTIxOiVGMCU5RiVBNCVCNwo=")
914 |> post("/oauth/token", %{
915 "grant_type" => "authorization_code",
916 "code" => auth.token,
917 "redirect_uri" => OAuthController.default_redirect_uri(app)
920 assert resp = json_response(conn, 400)
921 assert %{"error" => _} = resp
922 refute Map.has_key?(resp, "access_token")
925 test "rejects token exchange for valid credentials belonging to unconfirmed user and confirmation is required" do
926 Pleroma.Config.put([:instance, :account_activation_required], true)
927 password = "testpassword"
930 insert(:user, password_hash: Pbkdf2.hash_pwd_salt(password))
931 |> User.confirmation_changeset(need_confirmation: true)
932 |> User.update_and_set_cache()
934 refute Pleroma.User.account_status(user) == :active
936 app = insert(:oauth_app)
940 |> post("/oauth/token", %{
941 "grant_type" => "password",
942 "username" => user.nickname,
943 "password" => password,
944 "client_id" => app.client_id,
945 "client_secret" => app.client_secret
948 assert resp = json_response(conn, 403)
949 assert %{"error" => _} = resp
950 refute Map.has_key?(resp, "access_token")
953 test "rejects token exchange for valid credentials belonging to deactivated user" do
954 password = "testpassword"
958 password_hash: Pbkdf2.hash_pwd_salt(password),
962 app = insert(:oauth_app)
966 |> post("/oauth/token", %{
967 "grant_type" => "password",
968 "username" => user.nickname,
969 "password" => password,
970 "client_id" => app.client_id,
971 "client_secret" => app.client_secret
973 |> json_response(403)
976 "error" => "Your account is currently disabled",
977 "identifier" => "account_is_disabled"
981 test "rejects token exchange for user with password_reset_pending set to true" do
982 password = "testpassword"
986 password_hash: Pbkdf2.hash_pwd_salt(password),
987 password_reset_pending: true
990 app = insert(:oauth_app, scopes: ["read", "write"])
994 |> post("/oauth/token", %{
995 "grant_type" => "password",
996 "username" => user.nickname,
997 "password" => password,
998 "client_id" => app.client_id,
999 "client_secret" => app.client_secret
1001 |> json_response(403)
1004 "error" => "Password reset is required",
1005 "identifier" => "password_reset_required"
1009 test "rejects token exchange for user with confirmation_pending set to true" do
1010 Pleroma.Config.put([:instance, :account_activation_required], true)
1011 password = "testpassword"
1015 password_hash: Pbkdf2.hash_pwd_salt(password),
1016 confirmation_pending: true
1019 app = insert(:oauth_app, scopes: ["read", "write"])
1023 |> post("/oauth/token", %{
1024 "grant_type" => "password",
1025 "username" => user.nickname,
1026 "password" => password,
1027 "client_id" => app.client_id,
1028 "client_secret" => app.client_secret
1030 |> json_response(403)
1033 "error" => "Your login is missing a confirmed e-mail address",
1034 "identifier" => "missing_confirmed_email"
1038 test "rejects token exchange for valid credentials belonging to an unapproved user" do
1039 password = "testpassword"
1041 user = insert(:user, password_hash: Pbkdf2.hash_pwd_salt(password), approval_pending: true)
1043 refute Pleroma.User.account_status(user) == :active
1045 app = insert(:oauth_app)
1049 |> post("/oauth/token", %{
1050 "grant_type" => "password",
1051 "username" => user.nickname,
1052 "password" => password,
1053 "client_id" => app.client_id,
1054 "client_secret" => app.client_secret
1057 assert resp = json_response(conn, 403)
1058 assert %{"error" => _} = resp
1059 refute Map.has_key?(resp, "access_token")
1062 test "rejects an invalid authorization code" do
1063 app = insert(:oauth_app)
1067 |> post("/oauth/token", %{
1068 "grant_type" => "authorization_code",
1069 "code" => "Imobviouslyinvalid",
1070 "redirect_uri" => OAuthController.default_redirect_uri(app),
1071 "client_id" => app.client_id,
1072 "client_secret" => app.client_secret
1075 assert resp = json_response(conn, 400)
1076 assert %{"error" => _} = json_response(conn, 400)
1077 refute Map.has_key?(resp, "access_token")
1081 describe "POST /oauth/token - refresh token" do
1082 setup do: clear_config([:oauth2, :issue_new_refresh_token])
1084 test "issues a new access token with keep fresh token" do
1085 Pleroma.Config.put([:oauth2, :issue_new_refresh_token], true)
1086 user = insert(:user)
1087 app = insert(:oauth_app, scopes: ["read", "write"])
1089 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1090 {:ok, token} = Token.exchange_token(app, auth)
1094 |> post("/oauth/token", %{
1095 "grant_type" => "refresh_token",
1096 "refresh_token" => token.refresh_token,
1097 "client_id" => app.client_id,
1098 "client_secret" => app.client_secret
1100 |> json_response(200)
1107 "token_type" => "Bearer",
1108 "access_token" => _,
1109 "refresh_token" => _,
1115 refute Repo.get_by(Token, token: token.token)
1116 new_token = Repo.get_by(Token, token: response["access_token"])
1117 assert new_token.refresh_token == token.refresh_token
1118 assert new_token.scopes == auth.scopes
1119 assert new_token.user_id == user.id
1120 assert new_token.app_id == app.id
1123 test "issues a new access token with new fresh token" do
1124 Pleroma.Config.put([:oauth2, :issue_new_refresh_token], false)
1125 user = insert(:user)
1126 app = insert(:oauth_app, scopes: ["read", "write"])
1128 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1129 {:ok, token} = Token.exchange_token(app, auth)
1133 |> post("/oauth/token", %{
1134 "grant_type" => "refresh_token",
1135 "refresh_token" => token.refresh_token,
1136 "client_id" => app.client_id,
1137 "client_secret" => app.client_secret
1139 |> json_response(200)
1146 "token_type" => "Bearer",
1147 "access_token" => _,
1148 "refresh_token" => _,
1154 refute Repo.get_by(Token, token: token.token)
1155 new_token = Repo.get_by(Token, token: response["access_token"])
1156 refute new_token.refresh_token == token.refresh_token
1157 assert new_token.scopes == auth.scopes
1158 assert new_token.user_id == user.id
1159 assert new_token.app_id == app.id
1162 test "returns 400 if we try use access token" do
1163 user = insert(:user)
1164 app = insert(:oauth_app, scopes: ["read", "write"])
1166 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1167 {:ok, token} = Token.exchange_token(app, auth)
1171 |> post("/oauth/token", %{
1172 "grant_type" => "refresh_token",
1173 "refresh_token" => token.token,
1174 "client_id" => app.client_id,
1175 "client_secret" => app.client_secret
1177 |> json_response(400)
1179 assert %{"error" => "Invalid credentials"} == response
1182 test "returns 400 if refresh_token invalid" do
1183 app = insert(:oauth_app, scopes: ["read", "write"])
1187 |> post("/oauth/token", %{
1188 "grant_type" => "refresh_token",
1189 "refresh_token" => "token.refresh_token",
1190 "client_id" => app.client_id,
1191 "client_secret" => app.client_secret
1193 |> json_response(400)
1195 assert %{"error" => "Invalid credentials"} == response
1198 test "issues a new token if token expired" do
1199 user = insert(:user)
1200 app = insert(:oauth_app, scopes: ["read", "write"])
1202 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1203 {:ok, token} = Token.exchange_token(app, auth)
1206 Ecto.Changeset.change(
1208 %{valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -86_400 * 30)}
1211 {:ok, access_token} = Repo.update(change)
1215 |> post("/oauth/token", %{
1216 "grant_type" => "refresh_token",
1217 "refresh_token" => access_token.refresh_token,
1218 "client_id" => app.client_id,
1219 "client_secret" => app.client_secret
1221 |> json_response(200)
1228 "token_type" => "Bearer",
1229 "access_token" => _,
1230 "refresh_token" => _,
1236 refute Repo.get_by(Token, token: token.token)
1237 token = Repo.get_by(Token, token: response["access_token"])
1239 assert token.scopes == auth.scopes
1240 assert token.user_id == user.id
1241 assert token.app_id == app.id
1245 describe "POST /oauth/token - bad request" do
1246 test "returns 500" do
1249 |> post("/oauth/token", %{})
1250 |> json_response(500)
1252 assert %{"error" => "Bad request"} == response
1256 describe "POST /oauth/revoke" do
1257 test "when authenticated with request token, revokes it and clears it from session" do
1258 oauth_token = insert(:oauth_token)
1262 |> Plug.Session.call(Plug.Session.init(@session_opts))
1264 |> AuthHelper.put_session_token(oauth_token.token)
1265 |> post("/oauth/revoke", %{"token" => oauth_token.token})
1267 assert json_response(conn, 200)
1269 refute AuthHelper.get_session_token(conn)
1270 assert Token.get_by_token(oauth_token.token) == {:error, :not_found}
1273 test "if request is authenticated with a different token, " <>
1274 "revokes requested token but keeps session token" do
1275 user = insert(:user)
1276 oauth_token = insert(:oauth_token, user: user)
1277 other_app_oauth_token = insert(:oauth_token, user: user)
1281 |> Plug.Session.call(Plug.Session.init(@session_opts))
1283 |> AuthHelper.put_session_token(oauth_token.token)
1284 |> post("/oauth/revoke", %{"token" => other_app_oauth_token.token})
1286 assert json_response(conn, 200)
1288 assert AuthHelper.get_session_token(conn) == oauth_token.token
1289 assert Token.get_by_token(other_app_oauth_token.token) == {:error, :not_found}
1292 test "returns 500 on bad request" do
1295 |> post("/oauth/revoke", %{})
1296 |> json_response(500)
1298 assert %{"error" => "Bad request"} == response