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