Merge branch 'fix/attachments-cleanup' into 'develop'
[akkoma] / test / web / oauth / oauth_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.OAuth.OAuthControllerTest do
6 use Pleroma.Web.ConnCase
7 import Pleroma.Factory
8
9 alias Pleroma.Repo
10 alias Pleroma.User
11 alias Pleroma.Web.OAuth.Authorization
12 alias Pleroma.Web.OAuth.OAuthController
13 alias Pleroma.Web.OAuth.Token
14
15 @session_opts [
16 store: :cookie,
17 key: "_test",
18 signing_salt: "cooldude"
19 ]
20 clear_config_all([:instance, :account_activation_required])
21
22 describe "in OAuth consumer mode, " do
23 setup do
24 [
25 app: insert(:oauth_app),
26 conn:
27 build_conn()
28 |> Plug.Session.call(Plug.Session.init(@session_opts))
29 |> fetch_session()
30 ]
31 end
32
33 clear_config([:auth, :oauth_consumer_strategies]) do
34 Pleroma.Config.put(
35 [:auth, :oauth_consumer_strategies],
36 ~w(twitter facebook)
37 )
38 end
39
40 test "GET /oauth/authorize renders auth forms, including OAuth consumer form", %{
41 app: app,
42 conn: conn
43 } do
44 conn =
45 get(
46 conn,
47 "/oauth/authorize",
48 %{
49 "response_type" => "code",
50 "client_id" => app.client_id,
51 "redirect_uri" => OAuthController.default_redirect_uri(app),
52 "scope" => "read"
53 }
54 )
55
56 assert response = html_response(conn, 200)
57 assert response =~ "Sign in with Twitter"
58 assert response =~ o_auth_path(conn, :prepare_request)
59 end
60
61 test "GET /oauth/prepare_request encodes parameters as `state` and redirects", %{
62 app: app,
63 conn: conn
64 } do
65 conn =
66 get(
67 conn,
68 "/oauth/prepare_request",
69 %{
70 "provider" => "twitter",
71 "authorization" => %{
72 "scope" => "read follow",
73 "client_id" => app.client_id,
74 "redirect_uri" => OAuthController.default_redirect_uri(app),
75 "state" => "a_state"
76 }
77 }
78 )
79
80 assert response = html_response(conn, 302)
81
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)
85
86 expected_client_id = app.client_id
87 expected_redirect_uri = app.redirect_uris
88
89 assert %{
90 "scope" => "read follow",
91 "client_id" => ^expected_client_id,
92 "redirect_uri" => ^expected_redirect_uri,
93 "state" => "a_state"
94 } = state_components
95 end
96
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)
101
102 state_params = %{
103 "scope" => Enum.join(app.scopes, " "),
104 "client_id" => app.client_id,
105 "redirect_uri" => redirect_uri,
106 "state" => ""
107 }
108
109 conn =
110 conn
111 |> assign(:ueberauth_auth, %{provider: registration.provider, uid: registration.uid})
112 |> get(
113 "/oauth/twitter/callback",
114 %{
115 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
116 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
117 "provider" => "twitter",
118 "state" => Poison.encode!(state_params)
119 }
120 )
121
122 assert response = html_response(conn, 302)
123 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
124 end
125
126 test "with user-unbound registration, GET /oauth/<provider>/callback renders registration_details page",
127 %{app: app, conn: conn} do
128 user = insert(:user)
129
130 state_params = %{
131 "scope" => "read write",
132 "client_id" => app.client_id,
133 "redirect_uri" => OAuthController.default_redirect_uri(app),
134 "state" => "a_state"
135 }
136
137 conn =
138 conn
139 |> assign(:ueberauth_auth, %{
140 provider: "twitter",
141 uid: "171799000",
142 info: %{nickname: user.nickname, email: user.email, name: user.name, description: nil}
143 })
144 |> get(
145 "/oauth/twitter/callback",
146 %{
147 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
148 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
149 "provider" => "twitter",
150 "state" => Poison.encode!(state_params)
151 }
152 )
153
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
159 end
160
161 test "on authentication error, GET /oauth/<provider>/callback redirects to `redirect_uri`", %{
162 app: app,
163 conn: conn
164 } do
165 state_params = %{
166 "scope" => Enum.join(app.scopes, " "),
167 "client_id" => app.client_id,
168 "redirect_uri" => OAuthController.default_redirect_uri(app),
169 "state" => ""
170 }
171
172 conn =
173 conn
174 |> assign(:ueberauth_failure, %{errors: [%{message: "(error description)"}]})
175 |> get(
176 "/oauth/twitter/callback",
177 %{
178 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
179 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
180 "provider" => "twitter",
181 "state" => Poison.encode!(state_params)
182 }
183 )
184
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)."
188 end
189
190 test "GET /oauth/registration_details renders registration details form", %{
191 app: app,
192 conn: conn
193 } do
194 conn =
195 get(
196 conn,
197 "/oauth/registration_details",
198 %{
199 "authorization" => %{
200 "scopes" => app.scopes,
201 "client_id" => app.client_id,
202 "redirect_uri" => OAuthController.default_redirect_uri(app),
203 "state" => "a_state",
204 "nickname" => nil,
205 "email" => "john@doe.com"
206 }
207 }
208 )
209
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"/
213 end
214
215 test "with valid params, POST /oauth/register?op=register redirects to `redirect_uri` with `code`",
216 %{
217 app: app,
218 conn: conn
219 } do
220 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
221 redirect_uri = OAuthController.default_redirect_uri(app)
222
223 conn =
224 conn
225 |> put_session(:registration_id, registration.id)
226 |> post(
227 "/oauth/register",
228 %{
229 "op" => "register",
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"
237 }
238 }
239 )
240
241 assert response = html_response(conn, 302)
242 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
243 end
244
245 test "with unlisted `redirect_uri`, POST /oauth/register?op=register results in HTTP 401",
246 %{
247 app: app,
248 conn: conn
249 } do
250 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
251 unlisted_redirect_uri = "http://cross-site-request.com"
252
253 conn =
254 conn
255 |> put_session(:registration_id, registration.id)
256 |> post(
257 "/oauth/register",
258 %{
259 "op" => "register",
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"
267 }
268 }
269 )
270
271 assert response = html_response(conn, 401)
272 end
273
274 test "with invalid params, POST /oauth/register?op=register renders registration_details page",
275 %{
276 app: app,
277 conn: conn
278 } do
279 another_user = insert(:user)
280 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
281
282 params = %{
283 "op" => "register",
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"
291 }
292 }
293
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)
298 }
299
300 bad_params = Map.merge(params, bad_registration_attrs)
301
302 conn =
303 conn
304 |> put_session(:registration_id, registration.id)
305 |> post("/oauth/register", bad_params)
306
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."
309 end
310 end
311
312 test "with valid params, POST /oauth/register?op=connect redirects to `redirect_uri` with `code`",
313 %{
314 app: app,
315 conn: conn
316 } do
317 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt("testpassword"))
318 registration = insert(:registration, user: nil)
319 redirect_uri = OAuthController.default_redirect_uri(app)
320
321 conn =
322 conn
323 |> put_session(:registration_id, registration.id)
324 |> post(
325 "/oauth/register",
326 %{
327 "op" => "connect",
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"
335 }
336 }
337 )
338
339 assert response = html_response(conn, 302)
340 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
341 end
342
343 test "with unlisted `redirect_uri`, POST /oauth/register?op=connect results in HTTP 401`",
344 %{
345 app: app,
346 conn: conn
347 } do
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"
351
352 conn =
353 conn
354 |> put_session(:registration_id, registration.id)
355 |> post(
356 "/oauth/register",
357 %{
358 "op" => "connect",
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"
366 }
367 }
368 )
369
370 assert response = html_response(conn, 401)
371 end
372
373 test "with invalid params, POST /oauth/register?op=connect renders registration_details page",
374 %{
375 app: app,
376 conn: conn
377 } do
378 user = insert(:user)
379 registration = insert(:registration, user: nil)
380
381 params = %{
382 "op" => "connect",
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"
390 }
391 }
392
393 conn =
394 conn
395 |> put_session(:registration_id, registration.id)
396 |> post("/oauth/register", params)
397
398 assert html_response(conn, 401) =~ ~r/name="op" type="submit" value="connect"/
399 assert get_flash(conn, :error) == "Invalid Username/Password"
400 end
401 end
402
403 describe "GET /oauth/authorize" do
404 setup do
405 [
406 app: insert(:oauth_app, redirect_uris: "https://redirect.url"),
407 conn:
408 build_conn()
409 |> Plug.Session.call(Plug.Session.init(@session_opts))
410 |> fetch_session()
411 ]
412 end
413
414 test "renders authentication page", %{app: app, conn: conn} do
415 conn =
416 get(
417 conn,
418 "/oauth/authorize",
419 %{
420 "response_type" => "code",
421 "client_id" => app.client_id,
422 "redirect_uri" => OAuthController.default_redirect_uri(app),
423 "scope" => "read"
424 }
425 )
426
427 assert html_response(conn, 200) =~ ~s(type="submit")
428 end
429
430 test "properly handles internal calls with `authorization`-wrapped params", %{
431 app: app,
432 conn: conn
433 } do
434 conn =
435 get(
436 conn,
437 "/oauth/authorize",
438 %{
439 "authorization" => %{
440 "response_type" => "code",
441 "client_id" => app.client_id,
442 "redirect_uri" => OAuthController.default_redirect_uri(app),
443 "scope" => "read"
444 }
445 }
446 )
447
448 assert html_response(conn, 200) =~ ~s(type="submit")
449 end
450
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)
454
455 conn =
456 conn
457 |> put_session(:oauth_token, token.token)
458 |> get(
459 "/oauth/authorize",
460 %{
461 "response_type" => "code",
462 "client_id" => app.client_id,
463 "redirect_uri" => OAuthController.default_redirect_uri(app),
464 "scope" => "read",
465 "force_login" => "true"
466 }
467 )
468
469 assert html_response(conn, 200) =~ ~s(type="submit")
470 end
471
472 test "renders authentication page if user is already authenticated but user request with another client",
473 %{
474 app: app,
475 conn: conn
476 } do
477 token = insert(:oauth_token, app: app)
478
479 conn =
480 conn
481 |> put_session(:oauth_token, token.token)
482 |> get(
483 "/oauth/authorize",
484 %{
485 "response_type" => "code",
486 "client_id" => "another_client_id",
487 "redirect_uri" => OAuthController.default_redirect_uri(app),
488 "scope" => "read"
489 }
490 )
491
492 assert html_response(conn, 200) =~ ~s(type="submit")
493 end
494
495 test "with existing authentication and non-OOB `redirect_uri`, redirects to app with `token` and `state` params",
496 %{
497 app: app,
498 conn: conn
499 } do
500 token = insert(:oauth_token, app: app)
501
502 conn =
503 conn
504 |> put_session(:oauth_token, token.token)
505 |> get(
506 "/oauth/authorize",
507 %{
508 "response_type" => "code",
509 "client_id" => app.client_id,
510 "redirect_uri" => OAuthController.default_redirect_uri(app),
511 "state" => "specific_client_state",
512 "scope" => "read"
513 }
514 )
515
516 assert URI.decode(redirected_to(conn)) ==
517 "https://redirect.url?access_token=#{token.token}&state=specific_client_state"
518 end
519
520 test "with existing authentication and unlisted non-OOB `redirect_uri`, redirects without credentials",
521 %{
522 app: app,
523 conn: conn
524 } do
525 unlisted_redirect_uri = "http://cross-site-request.com"
526 token = insert(:oauth_token, app: app)
527
528 conn =
529 conn
530 |> put_session(:oauth_token, token.token)
531 |> get(
532 "/oauth/authorize",
533 %{
534 "response_type" => "code",
535 "client_id" => app.client_id,
536 "redirect_uri" => unlisted_redirect_uri,
537 "state" => "specific_client_state",
538 "scope" => "read"
539 }
540 )
541
542 assert redirected_to(conn) == unlisted_redirect_uri
543 end
544
545 test "with existing authentication and OOB `redirect_uri`, redirects to app with `token` and `state` params",
546 %{
547 app: app,
548 conn: conn
549 } do
550 token = insert(:oauth_token, app: app)
551
552 conn =
553 conn
554 |> put_session(:oauth_token, token.token)
555 |> get(
556 "/oauth/authorize",
557 %{
558 "response_type" => "code",
559 "client_id" => app.client_id,
560 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
561 "scope" => "read"
562 }
563 )
564
565 assert html_response(conn, 200) =~ "Authorization exists"
566 end
567 end
568
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)
575
576 non_admin = insert(:user, is_admin: false)
577 admin = insert(:user, is_admin: true)
578 scopes_subset = ["read:subscope", "write", "admin"]
579
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
584 conn =
585 post(
586 build_conn(),
587 "/oauth/authorize",
588 %{
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"
596 }
597 }
598 )
599
600 target = redirected_to(conn)
601 assert target =~ redirect_uri
602
603 query = URI.parse(target).query |> URI.query_decoder() |> Map.new()
604
605 assert %{"state" => "statepassed", "code" => code} = query
606 auth = Repo.get_by(Authorization, token: code)
607 assert auth
608 assert auth.scopes == expected_scopes
609 end
610 end
611
612 test "returns 401 for wrong credentials", %{conn: conn} do
613 user = insert(:user)
614 app = insert(:oauth_app)
615 redirect_uri = OAuthController.default_redirect_uri(app)
616
617 result =
618 conn
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, " ")
627 }
628 })
629 |> html_response(:unauthorized)
630
631 # Keep the details
632 assert result =~ app.client_id
633 assert result =~ redirect_uri
634
635 # Error message
636 assert result =~ "Invalid Username/Password"
637 end
638
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)
643
644 result =
645 build_conn()
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",
653 "scope" => ""
654 }
655 })
656 |> html_response(:unauthorized)
657
658 # Keep the details
659 assert result =~ app.client_id
660 assert result =~ redirect_uri
661
662 # Error message
663 assert result =~ "This action is outside the authorized scopes"
664 end
665
666 test "returns 401 for scopes beyond app scopes hierarchy", %{conn: conn} do
667 user = insert(:user)
668 app = insert(:oauth_app, scopes: ["read", "write"])
669 redirect_uri = OAuthController.default_redirect_uri(app)
670
671 result =
672 conn
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"
681 }
682 })
683 |> html_response(:unauthorized)
684
685 # Keep the details
686 assert result =~ app.client_id
687 assert result =~ redirect_uri
688
689 # Error message
690 assert result =~ "This action is outside the authorized scopes"
691 end
692 end
693
694 describe "POST /oauth/token" do
695 test "issues a token for an all-body request" do
696 user = insert(:user)
697 app = insert(:oauth_app, scopes: ["read", "write"])
698
699 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
700
701 conn =
702 build_conn()
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
709 })
710
711 assert %{"access_token" => token, "me" => ap_id} = json_response(conn, 200)
712
713 token = Repo.get_by(Token, token: token)
714 assert token
715 assert token.scopes == auth.scopes
716 assert user.ap_id == ap_id
717 end
718
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))
722
723 app = insert(:oauth_app, scopes: ["read", "write"])
724
725 # Note: "scope" param is intentionally omitted
726 conn =
727 build_conn()
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
734 })
735
736 assert %{"access_token" => token} = json_response(conn, 200)
737
738 token = Repo.get_by(Token, token: token)
739 assert token
740 assert token.scopes == app.scopes
741 end
742
743 test "issues a token for request with HTTP basic auth client credentials" do
744 user = insert(:user)
745 app = insert(:oauth_app, scopes: ["scope1", "scope2", "scope3"])
746
747 {:ok, auth} = Authorization.create_authorization(app, user, ["scope1", "scope2"])
748 assert auth.scopes == ["scope1", "scope2"]
749
750 app_encoded =
751 (URI.encode_www_form(app.client_id) <> ":" <> URI.encode_www_form(app.client_secret))
752 |> Base.encode64()
753
754 conn =
755 build_conn()
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)
761 })
762
763 assert %{"access_token" => token, "scope" => scope} = json_response(conn, 200)
764
765 assert scope == "scope1 scope2"
766
767 token = Repo.get_by(Token, token: token)
768 assert token
769 assert token.scopes == ["scope1", "scope2"]
770 end
771
772 test "issue a token for client_credentials grant type" do
773 app = insert(:oauth_app, scopes: ["read", "write"])
774
775 conn =
776 build_conn()
777 |> post("/oauth/token", %{
778 "grant_type" => "client_credentials",
779 "client_id" => app.client_id,
780 "client_secret" => app.client_secret
781 })
782
783 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
784 json_response(conn, 200)
785
786 assert token
787 token_from_db = Repo.get_by(Token, token: token)
788 assert token_from_db
789 assert refresh
790 assert scope == "read write"
791 end
792
793 test "rejects token exchange with invalid client credentials" do
794 user = insert(:user)
795 app = insert(:oauth_app)
796
797 {:ok, auth} = Authorization.create_authorization(app, user)
798
799 conn =
800 build_conn()
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)
806 })
807
808 assert resp = json_response(conn, 400)
809 assert %{"error" => _} = resp
810 refute Map.has_key?(resp, "access_token")
811 end
812
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"
816
817 {:ok, user} =
818 insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
819 |> User.confirmation_changeset(need_confirmation: true)
820 |> User.update_and_set_cache()
821
822 refute Pleroma.User.account_status(user) == :active
823
824 app = insert(:oauth_app)
825
826 conn =
827 build_conn()
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
834 })
835
836 assert resp = json_response(conn, 403)
837 assert %{"error" => _} = resp
838 refute Map.has_key?(resp, "access_token")
839 end
840
841 test "rejects token exchange for valid credentials belonging to deactivated user" do
842 password = "testpassword"
843
844 user =
845 insert(:user,
846 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
847 deactivated: true
848 )
849
850 app = insert(:oauth_app)
851
852 resp =
853 build_conn()
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
860 })
861 |> json_response(403)
862
863 assert resp == %{
864 "error" => "Your account is currently disabled",
865 "identifier" => "account_is_disabled"
866 }
867 end
868
869 test "rejects token exchange for user with password_reset_pending set to true" do
870 password = "testpassword"
871
872 user =
873 insert(:user,
874 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
875 password_reset_pending: true
876 )
877
878 app = insert(:oauth_app, scopes: ["read", "write"])
879
880 resp =
881 build_conn()
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
888 })
889 |> json_response(403)
890
891 assert resp == %{
892 "error" => "Password reset is required",
893 "identifier" => "password_reset_required"
894 }
895 end
896
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"
900
901 user =
902 insert(:user,
903 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
904 confirmation_pending: true
905 )
906
907 app = insert(:oauth_app, scopes: ["read", "write"])
908
909 resp =
910 build_conn()
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
917 })
918 |> json_response(403)
919
920 assert resp == %{
921 "error" => "Your login is missing a confirmed e-mail address",
922 "identifier" => "missing_confirmed_email"
923 }
924 end
925
926 test "rejects an invalid authorization code" do
927 app = insert(:oauth_app)
928
929 conn =
930 build_conn()
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
937 })
938
939 assert resp = json_response(conn, 400)
940 assert %{"error" => _} = json_response(conn, 400)
941 refute Map.has_key?(resp, "access_token")
942 end
943 end
944
945 describe "POST /oauth/token - refresh token" do
946 clear_config([:oauth2, :issue_new_refresh_token])
947
948 test "issues a new access token with keep fresh token" do
949 Pleroma.Config.put([:oauth2, :issue_new_refresh_token], true)
950 user = insert(:user)
951 app = insert(:oauth_app, scopes: ["read", "write"])
952
953 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
954 {:ok, token} = Token.exchange_token(app, auth)
955
956 response =
957 build_conn()
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
963 })
964 |> json_response(200)
965
966 ap_id = user.ap_id
967
968 assert match?(
969 %{
970 "scope" => "write",
971 "token_type" => "Bearer",
972 "expires_in" => 600,
973 "access_token" => _,
974 "refresh_token" => _,
975 "me" => ^ap_id
976 },
977 response
978 )
979
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
986 end
987
988 test "issues a new access token with new fresh token" do
989 Pleroma.Config.put([:oauth2, :issue_new_refresh_token], false)
990 user = insert(:user)
991 app = insert(:oauth_app, scopes: ["read", "write"])
992
993 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
994 {:ok, token} = Token.exchange_token(app, auth)
995
996 response =
997 build_conn()
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
1003 })
1004 |> json_response(200)
1005
1006 ap_id = user.ap_id
1007
1008 assert match?(
1009 %{
1010 "scope" => "write",
1011 "token_type" => "Bearer",
1012 "expires_in" => 600,
1013 "access_token" => _,
1014 "refresh_token" => _,
1015 "me" => ^ap_id
1016 },
1017 response
1018 )
1019
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
1026 end
1027
1028 test "returns 400 if we try use access token" do
1029 user = insert(:user)
1030 app = insert(:oauth_app, scopes: ["read", "write"])
1031
1032 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1033 {:ok, token} = Token.exchange_token(app, auth)
1034
1035 response =
1036 build_conn()
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
1042 })
1043 |> json_response(400)
1044
1045 assert %{"error" => "Invalid credentials"} == response
1046 end
1047
1048 test "returns 400 if refresh_token invalid" do
1049 app = insert(:oauth_app, scopes: ["read", "write"])
1050
1051 response =
1052 build_conn()
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
1058 })
1059 |> json_response(400)
1060
1061 assert %{"error" => "Invalid credentials"} == response
1062 end
1063
1064 test "issues a new token if token expired" do
1065 user = insert(:user)
1066 app = insert(:oauth_app, scopes: ["read", "write"])
1067
1068 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1069 {:ok, token} = Token.exchange_token(app, auth)
1070
1071 change =
1072 Ecto.Changeset.change(
1073 token,
1074 %{valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -86_400 * 30)}
1075 )
1076
1077 {:ok, access_token} = Repo.update(change)
1078
1079 response =
1080 build_conn()
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
1086 })
1087 |> json_response(200)
1088
1089 ap_id = user.ap_id
1090
1091 assert match?(
1092 %{
1093 "scope" => "write",
1094 "token_type" => "Bearer",
1095 "expires_in" => 600,
1096 "access_token" => _,
1097 "refresh_token" => _,
1098 "me" => ^ap_id
1099 },
1100 response
1101 )
1102
1103 refute Repo.get_by(Token, token: token.token)
1104 token = Repo.get_by(Token, token: response["access_token"])
1105 assert token
1106 assert token.scopes == auth.scopes
1107 assert token.user_id == user.id
1108 assert token.app_id == app.id
1109 end
1110 end
1111
1112 describe "POST /oauth/token - bad request" do
1113 test "returns 500" do
1114 response =
1115 build_conn()
1116 |> post("/oauth/token", %{})
1117 |> json_response(500)
1118
1119 assert %{"error" => "Bad request"} == response
1120 end
1121 end
1122
1123 describe "POST /oauth/revoke - bad request" do
1124 test "returns 500" do
1125 response =
1126 build_conn()
1127 |> post("/oauth/revoke", %{})
1128 |> json_response(500)
1129
1130 assert %{"error" => "Bad request"} == response
1131 end
1132 end
1133 end