Merge branch 'develop' into issue/1276
[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
21 clear_config([:instance, :account_activation_required])
22
23 describe "in OAuth consumer mode, " do
24 setup do
25 [
26 app: insert(:oauth_app),
27 conn:
28 build_conn()
29 |> Plug.Session.call(Plug.Session.init(@session_opts))
30 |> fetch_session()
31 ]
32 end
33
34 clear_config([:auth, :oauth_consumer_strategies]) do
35 Pleroma.Config.put(
36 [:auth, :oauth_consumer_strategies],
37 ~w(twitter facebook)
38 )
39 end
40
41 test "GET /oauth/authorize renders auth forms, including OAuth consumer form", %{
42 app: app,
43 conn: conn
44 } do
45 conn =
46 get(
47 conn,
48 "/oauth/authorize",
49 %{
50 "response_type" => "code",
51 "client_id" => app.client_id,
52 "redirect_uri" => OAuthController.default_redirect_uri(app),
53 "scope" => "read"
54 }
55 )
56
57 assert response = html_response(conn, 200)
58 assert response =~ "Sign in with Twitter"
59 assert response =~ o_auth_path(conn, :prepare_request)
60 end
61
62 test "GET /oauth/prepare_request encodes parameters as `state` and redirects", %{
63 app: app,
64 conn: conn
65 } do
66 conn =
67 get(
68 conn,
69 "/oauth/prepare_request",
70 %{
71 "provider" => "twitter",
72 "authorization" => %{
73 "scope" => "read follow",
74 "client_id" => app.client_id,
75 "redirect_uri" => OAuthController.default_redirect_uri(app),
76 "state" => "a_state"
77 }
78 }
79 )
80
81 assert response = html_response(conn, 302)
82
83 redirect_query = URI.parse(redirected_to(conn)).query
84 assert %{"state" => state_param} = URI.decode_query(redirect_query)
85 assert {:ok, state_components} = Poison.decode(state_param)
86
87 expected_client_id = app.client_id
88 expected_redirect_uri = app.redirect_uris
89
90 assert %{
91 "scope" => "read follow",
92 "client_id" => ^expected_client_id,
93 "redirect_uri" => ^expected_redirect_uri,
94 "state" => "a_state"
95 } = state_components
96 end
97
98 test "with user-bound registration, GET /oauth/<provider>/callback redirects to `redirect_uri` with `code`",
99 %{app: app, conn: conn} do
100 registration = insert(:registration)
101 redirect_uri = OAuthController.default_redirect_uri(app)
102
103 state_params = %{
104 "scope" => Enum.join(app.scopes, " "),
105 "client_id" => app.client_id,
106 "redirect_uri" => redirect_uri,
107 "state" => ""
108 }
109
110 conn =
111 conn
112 |> assign(:ueberauth_auth, %{provider: registration.provider, uid: registration.uid})
113 |> get(
114 "/oauth/twitter/callback",
115 %{
116 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
117 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
118 "provider" => "twitter",
119 "state" => Poison.encode!(state_params)
120 }
121 )
122
123 assert response = html_response(conn, 302)
124 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
125 end
126
127 test "with user-unbound registration, GET /oauth/<provider>/callback renders registration_details page",
128 %{app: app, conn: conn} do
129 user = insert(:user)
130
131 state_params = %{
132 "scope" => "read write",
133 "client_id" => app.client_id,
134 "redirect_uri" => OAuthController.default_redirect_uri(app),
135 "state" => "a_state"
136 }
137
138 conn =
139 conn
140 |> assign(:ueberauth_auth, %{
141 provider: "twitter",
142 uid: "171799000",
143 info: %{nickname: user.nickname, email: user.email, name: user.name, description: nil}
144 })
145 |> get(
146 "/oauth/twitter/callback",
147 %{
148 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
149 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
150 "provider" => "twitter",
151 "state" => Poison.encode!(state_params)
152 }
153 )
154
155 assert response = html_response(conn, 200)
156 assert response =~ ~r/name="op" type="submit" value="register"/
157 assert response =~ ~r/name="op" type="submit" value="connect"/
158 assert response =~ user.email
159 assert response =~ user.nickname
160 end
161
162 test "on authentication error, GET /oauth/<provider>/callback redirects to `redirect_uri`", %{
163 app: app,
164 conn: conn
165 } do
166 state_params = %{
167 "scope" => Enum.join(app.scopes, " "),
168 "client_id" => app.client_id,
169 "redirect_uri" => OAuthController.default_redirect_uri(app),
170 "state" => ""
171 }
172
173 conn =
174 conn
175 |> assign(:ueberauth_failure, %{errors: [%{message: "(error description)"}]})
176 |> get(
177 "/oauth/twitter/callback",
178 %{
179 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
180 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
181 "provider" => "twitter",
182 "state" => Poison.encode!(state_params)
183 }
184 )
185
186 assert response = html_response(conn, 302)
187 assert redirected_to(conn) == app.redirect_uris
188 assert get_flash(conn, :error) == "Failed to authenticate: (error description)."
189 end
190
191 test "GET /oauth/registration_details renders registration details form", %{
192 app: app,
193 conn: conn
194 } do
195 conn =
196 get(
197 conn,
198 "/oauth/registration_details",
199 %{
200 "authorization" => %{
201 "scopes" => app.scopes,
202 "client_id" => app.client_id,
203 "redirect_uri" => OAuthController.default_redirect_uri(app),
204 "state" => "a_state",
205 "nickname" => nil,
206 "email" => "john@doe.com"
207 }
208 }
209 )
210
211 assert response = html_response(conn, 200)
212 assert response =~ ~r/name="op" type="submit" value="register"/
213 assert response =~ ~r/name="op" type="submit" value="connect"/
214 end
215
216 test "with valid params, POST /oauth/register?op=register redirects to `redirect_uri` with `code`",
217 %{
218 app: app,
219 conn: conn
220 } do
221 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
222 redirect_uri = OAuthController.default_redirect_uri(app)
223
224 conn =
225 conn
226 |> put_session(:registration_id, registration.id)
227 |> post(
228 "/oauth/register",
229 %{
230 "op" => "register",
231 "authorization" => %{
232 "scopes" => app.scopes,
233 "client_id" => app.client_id,
234 "redirect_uri" => redirect_uri,
235 "state" => "a_state",
236 "nickname" => "availablenick",
237 "email" => "available@email.com"
238 }
239 }
240 )
241
242 assert response = html_response(conn, 302)
243 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
244 end
245
246 test "with unlisted `redirect_uri`, POST /oauth/register?op=register results in HTTP 401",
247 %{
248 app: app,
249 conn: conn
250 } do
251 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
252 unlisted_redirect_uri = "http://cross-site-request.com"
253
254 conn =
255 conn
256 |> put_session(:registration_id, registration.id)
257 |> post(
258 "/oauth/register",
259 %{
260 "op" => "register",
261 "authorization" => %{
262 "scopes" => app.scopes,
263 "client_id" => app.client_id,
264 "redirect_uri" => unlisted_redirect_uri,
265 "state" => "a_state",
266 "nickname" => "availablenick",
267 "email" => "available@email.com"
268 }
269 }
270 )
271
272 assert response = html_response(conn, 401)
273 end
274
275 test "with invalid params, POST /oauth/register?op=register renders registration_details page",
276 %{
277 app: app,
278 conn: conn
279 } do
280 another_user = insert(:user)
281 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
282
283 params = %{
284 "op" => "register",
285 "authorization" => %{
286 "scopes" => app.scopes,
287 "client_id" => app.client_id,
288 "redirect_uri" => OAuthController.default_redirect_uri(app),
289 "state" => "a_state",
290 "nickname" => "availablenickname",
291 "email" => "available@email.com"
292 }
293 }
294
295 for {bad_param, bad_param_value} <-
296 [{"nickname", another_user.nickname}, {"email", another_user.email}] do
297 bad_registration_attrs = %{
298 "authorization" => Map.put(params["authorization"], bad_param, bad_param_value)
299 }
300
301 bad_params = Map.merge(params, bad_registration_attrs)
302
303 conn =
304 conn
305 |> put_session(:registration_id, registration.id)
306 |> post("/oauth/register", bad_params)
307
308 assert html_response(conn, 403) =~ ~r/name="op" type="submit" value="register"/
309 assert get_flash(conn, :error) == "Error: #{bad_param} has already been taken."
310 end
311 end
312
313 test "with valid params, POST /oauth/register?op=connect redirects to `redirect_uri` with `code`",
314 %{
315 app: app,
316 conn: conn
317 } do
318 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt("testpassword"))
319 registration = insert(:registration, user: nil)
320 redirect_uri = OAuthController.default_redirect_uri(app)
321
322 conn =
323 conn
324 |> put_session(:registration_id, registration.id)
325 |> post(
326 "/oauth/register",
327 %{
328 "op" => "connect",
329 "authorization" => %{
330 "scopes" => app.scopes,
331 "client_id" => app.client_id,
332 "redirect_uri" => redirect_uri,
333 "state" => "a_state",
334 "name" => user.nickname,
335 "password" => "testpassword"
336 }
337 }
338 )
339
340 assert response = html_response(conn, 302)
341 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
342 end
343
344 test "with unlisted `redirect_uri`, POST /oauth/register?op=connect results in HTTP 401`",
345 %{
346 app: app,
347 conn: conn
348 } do
349 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt("testpassword"))
350 registration = insert(:registration, user: nil)
351 unlisted_redirect_uri = "http://cross-site-request.com"
352
353 conn =
354 conn
355 |> put_session(:registration_id, registration.id)
356 |> post(
357 "/oauth/register",
358 %{
359 "op" => "connect",
360 "authorization" => %{
361 "scopes" => app.scopes,
362 "client_id" => app.client_id,
363 "redirect_uri" => unlisted_redirect_uri,
364 "state" => "a_state",
365 "name" => user.nickname,
366 "password" => "testpassword"
367 }
368 }
369 )
370
371 assert response = html_response(conn, 401)
372 end
373
374 test "with invalid params, POST /oauth/register?op=connect renders registration_details page",
375 %{
376 app: app,
377 conn: conn
378 } do
379 user = insert(:user)
380 registration = insert(:registration, user: nil)
381
382 params = %{
383 "op" => "connect",
384 "authorization" => %{
385 "scopes" => app.scopes,
386 "client_id" => app.client_id,
387 "redirect_uri" => OAuthController.default_redirect_uri(app),
388 "state" => "a_state",
389 "name" => user.nickname,
390 "password" => "wrong password"
391 }
392 }
393
394 conn =
395 conn
396 |> put_session(:registration_id, registration.id)
397 |> post("/oauth/register", params)
398
399 assert html_response(conn, 401) =~ ~r/name="op" type="submit" value="connect"/
400 assert get_flash(conn, :error) == "Invalid Username/Password"
401 end
402 end
403
404 describe "GET /oauth/authorize" do
405 setup do
406 [
407 app: insert(:oauth_app, redirect_uris: "https://redirect.url"),
408 conn:
409 build_conn()
410 |> Plug.Session.call(Plug.Session.init(@session_opts))
411 |> fetch_session()
412 ]
413 end
414
415 test "renders authentication page", %{app: app, conn: conn} do
416 conn =
417 get(
418 conn,
419 "/oauth/authorize",
420 %{
421 "response_type" => "code",
422 "client_id" => app.client_id,
423 "redirect_uri" => OAuthController.default_redirect_uri(app),
424 "scope" => "read"
425 }
426 )
427
428 assert html_response(conn, 200) =~ ~s(type="submit")
429 end
430
431 test "properly handles internal calls with `authorization`-wrapped params", %{
432 app: app,
433 conn: conn
434 } do
435 conn =
436 get(
437 conn,
438 "/oauth/authorize",
439 %{
440 "authorization" => %{
441 "response_type" => "code",
442 "client_id" => app.client_id,
443 "redirect_uri" => OAuthController.default_redirect_uri(app),
444 "scope" => "read"
445 }
446 }
447 )
448
449 assert html_response(conn, 200) =~ ~s(type="submit")
450 end
451
452 test "renders authentication page if user is already authenticated but `force_login` is tru-ish",
453 %{app: app, conn: conn} do
454 token = insert(:oauth_token, app: app)
455
456 conn =
457 conn
458 |> put_session(:oauth_token, token.token)
459 |> get(
460 "/oauth/authorize",
461 %{
462 "response_type" => "code",
463 "client_id" => app.client_id,
464 "redirect_uri" => OAuthController.default_redirect_uri(app),
465 "scope" => "read",
466 "force_login" => "true"
467 }
468 )
469
470 assert html_response(conn, 200) =~ ~s(type="submit")
471 end
472
473 test "renders authentication page if user is already authenticated but user request with another client",
474 %{
475 app: app,
476 conn: conn
477 } do
478 token = insert(:oauth_token, app: app)
479
480 conn =
481 conn
482 |> put_session(:oauth_token, token.token)
483 |> get(
484 "/oauth/authorize",
485 %{
486 "response_type" => "code",
487 "client_id" => "another_client_id",
488 "redirect_uri" => OAuthController.default_redirect_uri(app),
489 "scope" => "read"
490 }
491 )
492
493 assert html_response(conn, 200) =~ ~s(type="submit")
494 end
495
496 test "with existing authentication and non-OOB `redirect_uri`, redirects to app with `token` and `state` params",
497 %{
498 app: app,
499 conn: conn
500 } do
501 token = insert(:oauth_token, app: app)
502
503 conn =
504 conn
505 |> put_session(:oauth_token, token.token)
506 |> get(
507 "/oauth/authorize",
508 %{
509 "response_type" => "code",
510 "client_id" => app.client_id,
511 "redirect_uri" => OAuthController.default_redirect_uri(app),
512 "state" => "specific_client_state",
513 "scope" => "read"
514 }
515 )
516
517 assert URI.decode(redirected_to(conn)) ==
518 "https://redirect.url?access_token=#{token.token}&state=specific_client_state"
519 end
520
521 test "with existing authentication and unlisted non-OOB `redirect_uri`, redirects without credentials",
522 %{
523 app: app,
524 conn: conn
525 } do
526 unlisted_redirect_uri = "http://cross-site-request.com"
527 token = insert(:oauth_token, app: app)
528
529 conn =
530 conn
531 |> put_session(:oauth_token, token.token)
532 |> get(
533 "/oauth/authorize",
534 %{
535 "response_type" => "code",
536 "client_id" => app.client_id,
537 "redirect_uri" => unlisted_redirect_uri,
538 "state" => "specific_client_state",
539 "scope" => "read"
540 }
541 )
542
543 assert redirected_to(conn) == unlisted_redirect_uri
544 end
545
546 test "with existing authentication and OOB `redirect_uri`, redirects to app with `token` and `state` params",
547 %{
548 app: app,
549 conn: conn
550 } do
551 token = insert(:oauth_token, app: app)
552
553 conn =
554 conn
555 |> put_session(:oauth_token, token.token)
556 |> get(
557 "/oauth/authorize",
558 %{
559 "response_type" => "code",
560 "client_id" => app.client_id,
561 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
562 "scope" => "read"
563 }
564 )
565
566 assert html_response(conn, 200) =~ "Authorization exists"
567 end
568 end
569
570 describe "POST /oauth/authorize" do
571 test "redirects with oauth authorization, " <>
572 "granting requested app-supported scopes to both admin- and non-admin users" do
573 app_scopes = ["read", "write", "admin", "secret_scope"]
574 app = insert(:oauth_app, scopes: app_scopes)
575 redirect_uri = OAuthController.default_redirect_uri(app)
576
577 non_admin = insert(:user, is_admin: false)
578 admin = insert(:user, is_admin: true)
579 scopes_subset = ["read:subscope", "write", "admin"]
580
581 # In case scope param is missing, expecting _all_ app-supported scopes to be granted
582 for user <- [non_admin, admin],
583 {requested_scopes, expected_scopes} <-
584 %{scopes_subset => scopes_subset, nil => app_scopes} do
585 conn =
586 post(
587 build_conn(),
588 "/oauth/authorize",
589 %{
590 "authorization" => %{
591 "name" => user.nickname,
592 "password" => "test",
593 "client_id" => app.client_id,
594 "redirect_uri" => redirect_uri,
595 "scope" => requested_scopes,
596 "state" => "statepassed"
597 }
598 }
599 )
600
601 target = redirected_to(conn)
602 assert target =~ redirect_uri
603
604 query = URI.parse(target).query |> URI.query_decoder() |> Map.new()
605
606 assert %{"state" => "statepassed", "code" => code} = query
607 auth = Repo.get_by(Authorization, token: code)
608 assert auth
609 assert auth.scopes == expected_scopes
610 end
611 end
612
613 test "returns 401 for wrong credentials", %{conn: conn} do
614 user = insert(:user)
615 app = insert(:oauth_app)
616 redirect_uri = OAuthController.default_redirect_uri(app)
617
618 result =
619 conn
620 |> post("/oauth/authorize", %{
621 "authorization" => %{
622 "name" => user.nickname,
623 "password" => "wrong",
624 "client_id" => app.client_id,
625 "redirect_uri" => redirect_uri,
626 "state" => "statepassed",
627 "scope" => Enum.join(app.scopes, " ")
628 }
629 })
630 |> html_response(:unauthorized)
631
632 # Keep the details
633 assert result =~ app.client_id
634 assert result =~ redirect_uri
635
636 # Error message
637 assert result =~ "Invalid Username/Password"
638 end
639
640 test "returns 401 for missing scopes" do
641 user = insert(:user, is_admin: false)
642 app = insert(:oauth_app, scopes: ["read", "write", "admin"])
643 redirect_uri = OAuthController.default_redirect_uri(app)
644
645 result =
646 build_conn()
647 |> post("/oauth/authorize", %{
648 "authorization" => %{
649 "name" => user.nickname,
650 "password" => "test",
651 "client_id" => app.client_id,
652 "redirect_uri" => redirect_uri,
653 "state" => "statepassed",
654 "scope" => ""
655 }
656 })
657 |> html_response(:unauthorized)
658
659 # Keep the details
660 assert result =~ app.client_id
661 assert result =~ redirect_uri
662
663 # Error message
664 assert result =~ "This action is outside the authorized scopes"
665 end
666
667 test "returns 401 for scopes beyond app scopes hierarchy", %{conn: conn} do
668 user = insert(:user)
669 app = insert(:oauth_app, scopes: ["read", "write"])
670 redirect_uri = OAuthController.default_redirect_uri(app)
671
672 result =
673 conn
674 |> post("/oauth/authorize", %{
675 "authorization" => %{
676 "name" => user.nickname,
677 "password" => "test",
678 "client_id" => app.client_id,
679 "redirect_uri" => redirect_uri,
680 "state" => "statepassed",
681 "scope" => "read write follow"
682 }
683 })
684 |> html_response(:unauthorized)
685
686 # Keep the details
687 assert result =~ app.client_id
688 assert result =~ redirect_uri
689
690 # Error message
691 assert result =~ "This action is outside the authorized scopes"
692 end
693 end
694
695 describe "POST /oauth/token" do
696 test "issues a token for an all-body request" do
697 user = insert(:user)
698 app = insert(:oauth_app, scopes: ["read", "write"])
699
700 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
701
702 conn =
703 build_conn()
704 |> post("/oauth/token", %{
705 "grant_type" => "authorization_code",
706 "code" => auth.token,
707 "redirect_uri" => OAuthController.default_redirect_uri(app),
708 "client_id" => app.client_id,
709 "client_secret" => app.client_secret
710 })
711
712 assert %{"access_token" => token, "me" => ap_id} = json_response(conn, 200)
713
714 token = Repo.get_by(Token, token: token)
715 assert token
716 assert token.scopes == auth.scopes
717 assert user.ap_id == ap_id
718 end
719
720 test "issues a token for `password` grant_type with valid credentials, with full permissions by default" do
721 password = "testpassword"
722 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
723
724 app = insert(:oauth_app, scopes: ["read", "write"])
725
726 # Note: "scope" param is intentionally omitted
727 conn =
728 build_conn()
729 |> post("/oauth/token", %{
730 "grant_type" => "password",
731 "username" => user.nickname,
732 "password" => password,
733 "client_id" => app.client_id,
734 "client_secret" => app.client_secret
735 })
736
737 assert %{"access_token" => token} = json_response(conn, 200)
738
739 token = Repo.get_by(Token, token: token)
740 assert token
741 assert token.scopes == app.scopes
742 end
743
744 test "issues a token for request with HTTP basic auth client credentials" do
745 user = insert(:user)
746 app = insert(:oauth_app, scopes: ["scope1", "scope2", "scope3"])
747
748 {:ok, auth} = Authorization.create_authorization(app, user, ["scope1", "scope2"])
749 assert auth.scopes == ["scope1", "scope2"]
750
751 app_encoded =
752 (URI.encode_www_form(app.client_id) <> ":" <> URI.encode_www_form(app.client_secret))
753 |> Base.encode64()
754
755 conn =
756 build_conn()
757 |> put_req_header("authorization", "Basic " <> app_encoded)
758 |> post("/oauth/token", %{
759 "grant_type" => "authorization_code",
760 "code" => auth.token,
761 "redirect_uri" => OAuthController.default_redirect_uri(app)
762 })
763
764 assert %{"access_token" => token, "scope" => scope} = json_response(conn, 200)
765
766 assert scope == "scope1 scope2"
767
768 token = Repo.get_by(Token, token: token)
769 assert token
770 assert token.scopes == ["scope1", "scope2"]
771 end
772
773 test "issue a token for client_credentials grant type" do
774 app = insert(:oauth_app, scopes: ["read", "write"])
775
776 conn =
777 build_conn()
778 |> post("/oauth/token", %{
779 "grant_type" => "client_credentials",
780 "client_id" => app.client_id,
781 "client_secret" => app.client_secret
782 })
783
784 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
785 json_response(conn, 200)
786
787 assert token
788 token_from_db = Repo.get_by(Token, token: token)
789 assert token_from_db
790 assert refresh
791 assert scope == "read write"
792 end
793
794 test "rejects token exchange with invalid client credentials" do
795 user = insert(:user)
796 app = insert(:oauth_app)
797
798 {:ok, auth} = Authorization.create_authorization(app, user)
799
800 conn =
801 build_conn()
802 |> put_req_header("authorization", "Basic JTIxOiVGMCU5RiVBNCVCNwo=")
803 |> post("/oauth/token", %{
804 "grant_type" => "authorization_code",
805 "code" => auth.token,
806 "redirect_uri" => OAuthController.default_redirect_uri(app)
807 })
808
809 assert resp = json_response(conn, 400)
810 assert %{"error" => _} = resp
811 refute Map.has_key?(resp, "access_token")
812 end
813
814 test "rejects token exchange for valid credentials belonging to unconfirmed user and confirmation is required" do
815 Pleroma.Config.put([:instance, :account_activation_required], true)
816 password = "testpassword"
817
818 {:ok, user} =
819 insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
820 |> User.confirmation_changeset(need_confirmation: true)
821 |> User.update_and_set_cache()
822
823 refute Pleroma.User.account_status(user) == :active
824
825 app = insert(:oauth_app)
826
827 conn =
828 build_conn()
829 |> post("/oauth/token", %{
830 "grant_type" => "password",
831 "username" => user.nickname,
832 "password" => password,
833 "client_id" => app.client_id,
834 "client_secret" => app.client_secret
835 })
836
837 assert resp = json_response(conn, 403)
838 assert %{"error" => _} = resp
839 refute Map.has_key?(resp, "access_token")
840 end
841
842 test "rejects token exchange for valid credentials belonging to deactivated user" do
843 password = "testpassword"
844
845 user =
846 insert(:user,
847 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
848 deactivated: true
849 )
850
851 app = insert(:oauth_app)
852
853 resp =
854 build_conn()
855 |> post("/oauth/token", %{
856 "grant_type" => "password",
857 "username" => user.nickname,
858 "password" => password,
859 "client_id" => app.client_id,
860 "client_secret" => app.client_secret
861 })
862 |> json_response(403)
863
864 assert resp == %{
865 "error" => "Your account is currently disabled",
866 "identifier" => "account_is_disabled"
867 }
868 end
869
870 test "rejects token exchange for user with password_reset_pending set to true" do
871 password = "testpassword"
872
873 user =
874 insert(:user,
875 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
876 password_reset_pending: true
877 )
878
879 app = insert(:oauth_app, scopes: ["read", "write"])
880
881 resp =
882 build_conn()
883 |> post("/oauth/token", %{
884 "grant_type" => "password",
885 "username" => user.nickname,
886 "password" => password,
887 "client_id" => app.client_id,
888 "client_secret" => app.client_secret
889 })
890 |> json_response(403)
891
892 assert resp == %{
893 "error" => "Password reset is required",
894 "identifier" => "password_reset_required"
895 }
896 end
897
898 test "rejects token exchange for user with confirmation_pending set to true" do
899 Pleroma.Config.put([:instance, :account_activation_required], true)
900 password = "testpassword"
901
902 user =
903 insert(:user,
904 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
905 confirmation_pending: true
906 )
907
908 app = insert(:oauth_app, scopes: ["read", "write"])
909
910 resp =
911 build_conn()
912 |> post("/oauth/token", %{
913 "grant_type" => "password",
914 "username" => user.nickname,
915 "password" => password,
916 "client_id" => app.client_id,
917 "client_secret" => app.client_secret
918 })
919 |> json_response(403)
920
921 assert resp == %{
922 "error" => "Your login is missing a confirmed e-mail address",
923 "identifier" => "missing_confirmed_email"
924 }
925 end
926
927 test "rejects an invalid authorization code" do
928 app = insert(:oauth_app)
929
930 conn =
931 build_conn()
932 |> post("/oauth/token", %{
933 "grant_type" => "authorization_code",
934 "code" => "Imobviouslyinvalid",
935 "redirect_uri" => OAuthController.default_redirect_uri(app),
936 "client_id" => app.client_id,
937 "client_secret" => app.client_secret
938 })
939
940 assert resp = json_response(conn, 400)
941 assert %{"error" => _} = json_response(conn, 400)
942 refute Map.has_key?(resp, "access_token")
943 end
944 end
945
946 describe "POST /oauth/token - refresh token" do
947 clear_config([:oauth2, :issue_new_refresh_token])
948
949 test "issues a new access token with keep fresh token" do
950 Pleroma.Config.put([:oauth2, :issue_new_refresh_token], true)
951 user = insert(:user)
952 app = insert(:oauth_app, scopes: ["read", "write"])
953
954 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
955 {:ok, token} = Token.exchange_token(app, auth)
956
957 response =
958 build_conn()
959 |> post("/oauth/token", %{
960 "grant_type" => "refresh_token",
961 "refresh_token" => token.refresh_token,
962 "client_id" => app.client_id,
963 "client_secret" => app.client_secret
964 })
965 |> json_response(200)
966
967 ap_id = user.ap_id
968
969 assert match?(
970 %{
971 "scope" => "write",
972 "token_type" => "Bearer",
973 "expires_in" => 600,
974 "access_token" => _,
975 "refresh_token" => _,
976 "me" => ^ap_id
977 },
978 response
979 )
980
981 refute Repo.get_by(Token, token: token.token)
982 new_token = Repo.get_by(Token, token: response["access_token"])
983 assert new_token.refresh_token == token.refresh_token
984 assert new_token.scopes == auth.scopes
985 assert new_token.user_id == user.id
986 assert new_token.app_id == app.id
987 end
988
989 test "issues a new access token with new fresh token" do
990 Pleroma.Config.put([:oauth2, :issue_new_refresh_token], false)
991 user = insert(:user)
992 app = insert(:oauth_app, scopes: ["read", "write"])
993
994 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
995 {:ok, token} = Token.exchange_token(app, auth)
996
997 response =
998 build_conn()
999 |> post("/oauth/token", %{
1000 "grant_type" => "refresh_token",
1001 "refresh_token" => token.refresh_token,
1002 "client_id" => app.client_id,
1003 "client_secret" => app.client_secret
1004 })
1005 |> json_response(200)
1006
1007 ap_id = user.ap_id
1008
1009 assert match?(
1010 %{
1011 "scope" => "write",
1012 "token_type" => "Bearer",
1013 "expires_in" => 600,
1014 "access_token" => _,
1015 "refresh_token" => _,
1016 "me" => ^ap_id
1017 },
1018 response
1019 )
1020
1021 refute Repo.get_by(Token, token: token.token)
1022 new_token = Repo.get_by(Token, token: response["access_token"])
1023 refute new_token.refresh_token == token.refresh_token
1024 assert new_token.scopes == auth.scopes
1025 assert new_token.user_id == user.id
1026 assert new_token.app_id == app.id
1027 end
1028
1029 test "returns 400 if we try use access token" do
1030 user = insert(:user)
1031 app = insert(:oauth_app, scopes: ["read", "write"])
1032
1033 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1034 {:ok, token} = Token.exchange_token(app, auth)
1035
1036 response =
1037 build_conn()
1038 |> post("/oauth/token", %{
1039 "grant_type" => "refresh_token",
1040 "refresh_token" => token.token,
1041 "client_id" => app.client_id,
1042 "client_secret" => app.client_secret
1043 })
1044 |> json_response(400)
1045
1046 assert %{"error" => "Invalid credentials"} == response
1047 end
1048
1049 test "returns 400 if refresh_token invalid" do
1050 app = insert(:oauth_app, scopes: ["read", "write"])
1051
1052 response =
1053 build_conn()
1054 |> post("/oauth/token", %{
1055 "grant_type" => "refresh_token",
1056 "refresh_token" => "token.refresh_token",
1057 "client_id" => app.client_id,
1058 "client_secret" => app.client_secret
1059 })
1060 |> json_response(400)
1061
1062 assert %{"error" => "Invalid credentials"} == response
1063 end
1064
1065 test "issues a new token if token expired" do
1066 user = insert(:user)
1067 app = insert(:oauth_app, scopes: ["read", "write"])
1068
1069 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1070 {:ok, token} = Token.exchange_token(app, auth)
1071
1072 change =
1073 Ecto.Changeset.change(
1074 token,
1075 %{valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -86_400 * 30)}
1076 )
1077
1078 {:ok, access_token} = Repo.update(change)
1079
1080 response =
1081 build_conn()
1082 |> post("/oauth/token", %{
1083 "grant_type" => "refresh_token",
1084 "refresh_token" => access_token.refresh_token,
1085 "client_id" => app.client_id,
1086 "client_secret" => app.client_secret
1087 })
1088 |> json_response(200)
1089
1090 ap_id = user.ap_id
1091
1092 assert match?(
1093 %{
1094 "scope" => "write",
1095 "token_type" => "Bearer",
1096 "expires_in" => 600,
1097 "access_token" => _,
1098 "refresh_token" => _,
1099 "me" => ^ap_id
1100 },
1101 response
1102 )
1103
1104 refute Repo.get_by(Token, token: token.token)
1105 token = Repo.get_by(Token, token: response["access_token"])
1106 assert token
1107 assert token.scopes == auth.scopes
1108 assert token.user_id == user.id
1109 assert token.app_id == app.id
1110 end
1111 end
1112
1113 describe "POST /oauth/token - bad request" do
1114 test "returns 500" do
1115 response =
1116 build_conn()
1117 |> post("/oauth/token", %{})
1118 |> json_response(500)
1119
1120 assert %{"error" => "Bad request"} == response
1121 end
1122 end
1123
1124 describe "POST /oauth/revoke - bad request" do
1125 test "returns 500" do
1126 response =
1127 build_conn()
1128 |> post("/oauth/revoke", %{})
1129 |> json_response(500)
1130
1131 assert %{"error" => "Bad request"} == response
1132 end
1133 end
1134 end