Merge branch 'hardening/stop-misusing-sharedinbox' into 'develop'
[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 @oauth_config_path [:oauth2, :issue_new_refresh_token]
15 @session_opts [
16 store: :cookie,
17 key: "_test",
18 signing_salt: "cooldude"
19 ]
20
21 describe "in OAuth consumer mode, " do
22 setup do
23 oauth_consumer_strategies_path = [:auth, :oauth_consumer_strategies]
24 oauth_consumer_strategies = Pleroma.Config.get(oauth_consumer_strategies_path)
25 Pleroma.Config.put(oauth_consumer_strategies_path, ~w(twitter facebook))
26
27 on_exit(fn ->
28 Pleroma.Config.put(oauth_consumer_strategies_path, oauth_consumer_strategies)
29 end)
30
31 [
32 app: insert(:oauth_app),
33 conn:
34 build_conn()
35 |> Plug.Session.call(Plug.Session.init(@session_opts))
36 |> fetch_session()
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_id: app.id)
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 "with existing authentication and non-OOB `redirect_uri`, redirects to app with `token` and `state` params",
473 %{
474 app: app,
475 conn: conn
476 } do
477 token = insert(:oauth_token, app_id: app.id)
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" => app.client_id,
487 "redirect_uri" => OAuthController.default_redirect_uri(app),
488 "state" => "specific_client_state",
489 "scope" => "read"
490 }
491 )
492
493 assert URI.decode(redirected_to(conn)) ==
494 "https://redirect.url?access_token=#{token.token}&state=specific_client_state"
495 end
496
497 test "with existing authentication and unlisted non-OOB `redirect_uri`, redirects without credentials",
498 %{
499 app: app,
500 conn: conn
501 } do
502 unlisted_redirect_uri = "http://cross-site-request.com"
503 token = insert(:oauth_token, app_id: app.id)
504
505 conn =
506 conn
507 |> put_session(:oauth_token, token.token)
508 |> get(
509 "/oauth/authorize",
510 %{
511 "response_type" => "code",
512 "client_id" => app.client_id,
513 "redirect_uri" => unlisted_redirect_uri,
514 "state" => "specific_client_state",
515 "scope" => "read"
516 }
517 )
518
519 assert redirected_to(conn) == unlisted_redirect_uri
520 end
521
522 test "with existing authentication and OOB `redirect_uri`, redirects to app with `token` and `state` params",
523 %{
524 app: app,
525 conn: conn
526 } do
527 token = insert(:oauth_token, app_id: app.id)
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" => "urn:ietf:wg:oauth:2.0:oob",
538 "scope" => "read"
539 }
540 )
541
542 assert html_response(conn, 200) =~ "Authorization exists"
543 end
544 end
545
546 describe "POST /oauth/authorize" do
547 test "redirects with oauth authorization" do
548 user = insert(:user)
549 app = insert(:oauth_app, scopes: ["read", "write", "follow"])
550 redirect_uri = OAuthController.default_redirect_uri(app)
551
552 conn =
553 build_conn()
554 |> post("/oauth/authorize", %{
555 "authorization" => %{
556 "name" => user.nickname,
557 "password" => "test",
558 "client_id" => app.client_id,
559 "redirect_uri" => redirect_uri,
560 "scope" => "read write",
561 "state" => "statepassed"
562 }
563 })
564
565 target = redirected_to(conn)
566 assert target =~ redirect_uri
567
568 query = URI.parse(target).query |> URI.query_decoder() |> Map.new()
569
570 assert %{"state" => "statepassed", "code" => code} = query
571 auth = Repo.get_by(Authorization, token: code)
572 assert auth
573 assert auth.scopes == ["read", "write"]
574 end
575
576 test "returns 401 for wrong credentials", %{conn: conn} do
577 user = insert(:user)
578 app = insert(:oauth_app)
579 redirect_uri = OAuthController.default_redirect_uri(app)
580
581 result =
582 conn
583 |> post("/oauth/authorize", %{
584 "authorization" => %{
585 "name" => user.nickname,
586 "password" => "wrong",
587 "client_id" => app.client_id,
588 "redirect_uri" => redirect_uri,
589 "state" => "statepassed",
590 "scope" => Enum.join(app.scopes, " ")
591 }
592 })
593 |> html_response(:unauthorized)
594
595 # Keep the details
596 assert result =~ app.client_id
597 assert result =~ redirect_uri
598
599 # Error message
600 assert result =~ "Invalid Username/Password"
601 end
602
603 test "returns 401 for missing scopes", %{conn: conn} do
604 user = insert(:user)
605 app = insert(:oauth_app)
606 redirect_uri = OAuthController.default_redirect_uri(app)
607
608 result =
609 conn
610 |> post("/oauth/authorize", %{
611 "authorization" => %{
612 "name" => user.nickname,
613 "password" => "test",
614 "client_id" => app.client_id,
615 "redirect_uri" => redirect_uri,
616 "state" => "statepassed",
617 "scope" => ""
618 }
619 })
620 |> html_response(:unauthorized)
621
622 # Keep the details
623 assert result =~ app.client_id
624 assert result =~ redirect_uri
625
626 # Error message
627 assert result =~ "This action is outside the authorized scopes"
628 end
629
630 test "returns 401 for scopes beyond app scopes", %{conn: conn} do
631 user = insert(:user)
632 app = insert(:oauth_app, scopes: ["read", "write"])
633 redirect_uri = OAuthController.default_redirect_uri(app)
634
635 result =
636 conn
637 |> post("/oauth/authorize", %{
638 "authorization" => %{
639 "name" => user.nickname,
640 "password" => "test",
641 "client_id" => app.client_id,
642 "redirect_uri" => redirect_uri,
643 "state" => "statepassed",
644 "scope" => "read write follow"
645 }
646 })
647 |> html_response(:unauthorized)
648
649 # Keep the details
650 assert result =~ app.client_id
651 assert result =~ redirect_uri
652
653 # Error message
654 assert result =~ "This action is outside the authorized scopes"
655 end
656 end
657
658 describe "POST /oauth/token" do
659 test "issues a token for an all-body request" do
660 user = insert(:user)
661 app = insert(:oauth_app, scopes: ["read", "write"])
662
663 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
664
665 conn =
666 build_conn()
667 |> post("/oauth/token", %{
668 "grant_type" => "authorization_code",
669 "code" => auth.token,
670 "redirect_uri" => OAuthController.default_redirect_uri(app),
671 "client_id" => app.client_id,
672 "client_secret" => app.client_secret
673 })
674
675 assert %{"access_token" => token, "me" => ap_id} = json_response(conn, 200)
676
677 token = Repo.get_by(Token, token: token)
678 assert token
679 assert token.scopes == auth.scopes
680 assert user.ap_id == ap_id
681 end
682
683 test "issues a token for `password` grant_type with valid credentials, with full permissions by default" do
684 password = "testpassword"
685 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
686
687 app = insert(:oauth_app, scopes: ["read", "write"])
688
689 # Note: "scope" param is intentionally omitted
690 conn =
691 build_conn()
692 |> post("/oauth/token", %{
693 "grant_type" => "password",
694 "username" => user.nickname,
695 "password" => password,
696 "client_id" => app.client_id,
697 "client_secret" => app.client_secret
698 })
699
700 assert %{"access_token" => token} = json_response(conn, 200)
701
702 token = Repo.get_by(Token, token: token)
703 assert token
704 assert token.scopes == app.scopes
705 end
706
707 test "issues a token for request with HTTP basic auth client credentials" do
708 user = insert(:user)
709 app = insert(:oauth_app, scopes: ["scope1", "scope2", "scope3"])
710
711 {:ok, auth} = Authorization.create_authorization(app, user, ["scope1", "scope2"])
712 assert auth.scopes == ["scope1", "scope2"]
713
714 app_encoded =
715 (URI.encode_www_form(app.client_id) <> ":" <> URI.encode_www_form(app.client_secret))
716 |> Base.encode64()
717
718 conn =
719 build_conn()
720 |> put_req_header("authorization", "Basic " <> app_encoded)
721 |> post("/oauth/token", %{
722 "grant_type" => "authorization_code",
723 "code" => auth.token,
724 "redirect_uri" => OAuthController.default_redirect_uri(app)
725 })
726
727 assert %{"access_token" => token, "scope" => scope} = json_response(conn, 200)
728
729 assert scope == "scope1 scope2"
730
731 token = Repo.get_by(Token, token: token)
732 assert token
733 assert token.scopes == ["scope1", "scope2"]
734 end
735
736 test "issue a token for client_credentials grant type" do
737 app = insert(:oauth_app, scopes: ["read", "write"])
738
739 conn =
740 build_conn()
741 |> post("/oauth/token", %{
742 "grant_type" => "client_credentials",
743 "client_id" => app.client_id,
744 "client_secret" => app.client_secret
745 })
746
747 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
748 json_response(conn, 200)
749
750 assert token
751 token_from_db = Repo.get_by(Token, token: token)
752 assert token_from_db
753 assert refresh
754 assert scope == "read write"
755 end
756
757 test "rejects token exchange with invalid client credentials" do
758 user = insert(:user)
759 app = insert(:oauth_app)
760
761 {:ok, auth} = Authorization.create_authorization(app, user)
762
763 conn =
764 build_conn()
765 |> put_req_header("authorization", "Basic JTIxOiVGMCU5RiVBNCVCNwo=")
766 |> post("/oauth/token", %{
767 "grant_type" => "authorization_code",
768 "code" => auth.token,
769 "redirect_uri" => OAuthController.default_redirect_uri(app)
770 })
771
772 assert resp = json_response(conn, 400)
773 assert %{"error" => _} = resp
774 refute Map.has_key?(resp, "access_token")
775 end
776
777 test "rejects token exchange for valid credentials belonging to unconfirmed user and confirmation is required" do
778 setting = Pleroma.Config.get([:instance, :account_activation_required])
779
780 unless setting do
781 Pleroma.Config.put([:instance, :account_activation_required], true)
782 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
783 end
784
785 password = "testpassword"
786 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
787 info_change = Pleroma.User.Info.confirmation_changeset(user.info, need_confirmation: true)
788
789 {:ok, user} =
790 user
791 |> Ecto.Changeset.change()
792 |> Ecto.Changeset.put_embed(:info, info_change)
793 |> Repo.update()
794
795 refute Pleroma.User.auth_active?(user)
796
797 app = insert(:oauth_app)
798
799 conn =
800 build_conn()
801 |> post("/oauth/token", %{
802 "grant_type" => "password",
803 "username" => user.nickname,
804 "password" => password,
805 "client_id" => app.client_id,
806 "client_secret" => app.client_secret
807 })
808
809 assert resp = json_response(conn, 403)
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 deactivated user" do
815 password = "testpassword"
816
817 user =
818 insert(:user,
819 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
820 info: %{deactivated: true}
821 )
822
823 app = insert(:oauth_app)
824
825 conn =
826 build_conn()
827 |> post("/oauth/token", %{
828 "grant_type" => "password",
829 "username" => user.nickname,
830 "password" => password,
831 "client_id" => app.client_id,
832 "client_secret" => app.client_secret
833 })
834
835 assert resp = json_response(conn, 403)
836 assert %{"error" => _} = resp
837 refute Map.has_key?(resp, "access_token")
838 end
839
840 test "rejects an invalid authorization code" do
841 app = insert(:oauth_app)
842
843 conn =
844 build_conn()
845 |> post("/oauth/token", %{
846 "grant_type" => "authorization_code",
847 "code" => "Imobviouslyinvalid",
848 "redirect_uri" => OAuthController.default_redirect_uri(app),
849 "client_id" => app.client_id,
850 "client_secret" => app.client_secret
851 })
852
853 assert resp = json_response(conn, 400)
854 assert %{"error" => _} = json_response(conn, 400)
855 refute Map.has_key?(resp, "access_token")
856 end
857 end
858
859 describe "POST /oauth/token - refresh token" do
860 setup do
861 oauth_token_config = Pleroma.Config.get(@oauth_config_path)
862
863 on_exit(fn ->
864 Pleroma.Config.get(@oauth_config_path, oauth_token_config)
865 end)
866 end
867
868 test "issues a new access token with keep fresh token" do
869 Pleroma.Config.put(@oauth_config_path, true)
870 user = insert(:user)
871 app = insert(:oauth_app, scopes: ["read", "write"])
872
873 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
874 {:ok, token} = Token.exchange_token(app, auth)
875
876 response =
877 build_conn()
878 |> post("/oauth/token", %{
879 "grant_type" => "refresh_token",
880 "refresh_token" => token.refresh_token,
881 "client_id" => app.client_id,
882 "client_secret" => app.client_secret
883 })
884 |> json_response(200)
885
886 ap_id = user.ap_id
887
888 assert match?(
889 %{
890 "scope" => "write",
891 "token_type" => "Bearer",
892 "expires_in" => 600,
893 "access_token" => _,
894 "refresh_token" => _,
895 "me" => ^ap_id
896 },
897 response
898 )
899
900 refute Repo.get_by(Token, token: token.token)
901 new_token = Repo.get_by(Token, token: response["access_token"])
902 assert new_token.refresh_token == token.refresh_token
903 assert new_token.scopes == auth.scopes
904 assert new_token.user_id == user.id
905 assert new_token.app_id == app.id
906 end
907
908 test "issues a new access token with new fresh token" do
909 Pleroma.Config.put(@oauth_config_path, false)
910 user = insert(:user)
911 app = insert(:oauth_app, scopes: ["read", "write"])
912
913 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
914 {:ok, token} = Token.exchange_token(app, auth)
915
916 response =
917 build_conn()
918 |> post("/oauth/token", %{
919 "grant_type" => "refresh_token",
920 "refresh_token" => token.refresh_token,
921 "client_id" => app.client_id,
922 "client_secret" => app.client_secret
923 })
924 |> json_response(200)
925
926 ap_id = user.ap_id
927
928 assert match?(
929 %{
930 "scope" => "write",
931 "token_type" => "Bearer",
932 "expires_in" => 600,
933 "access_token" => _,
934 "refresh_token" => _,
935 "me" => ^ap_id
936 },
937 response
938 )
939
940 refute Repo.get_by(Token, token: token.token)
941 new_token = Repo.get_by(Token, token: response["access_token"])
942 refute new_token.refresh_token == token.refresh_token
943 assert new_token.scopes == auth.scopes
944 assert new_token.user_id == user.id
945 assert new_token.app_id == app.id
946 end
947
948 test "returns 400 if we try use access token" do
949 user = insert(:user)
950 app = insert(:oauth_app, scopes: ["read", "write"])
951
952 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
953 {:ok, token} = Token.exchange_token(app, auth)
954
955 response =
956 build_conn()
957 |> post("/oauth/token", %{
958 "grant_type" => "refresh_token",
959 "refresh_token" => token.token,
960 "client_id" => app.client_id,
961 "client_secret" => app.client_secret
962 })
963 |> json_response(400)
964
965 assert %{"error" => "Invalid credentials"} == response
966 end
967
968 test "returns 400 if refresh_token invalid" do
969 app = insert(:oauth_app, scopes: ["read", "write"])
970
971 response =
972 build_conn()
973 |> post("/oauth/token", %{
974 "grant_type" => "refresh_token",
975 "refresh_token" => "token.refresh_token",
976 "client_id" => app.client_id,
977 "client_secret" => app.client_secret
978 })
979 |> json_response(400)
980
981 assert %{"error" => "Invalid credentials"} == response
982 end
983
984 test "issues a new token if token expired" do
985 user = insert(:user)
986 app = insert(:oauth_app, scopes: ["read", "write"])
987
988 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
989 {:ok, token} = Token.exchange_token(app, auth)
990
991 change =
992 Ecto.Changeset.change(
993 token,
994 %{valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -86_400 * 30)}
995 )
996
997 {:ok, access_token} = Repo.update(change)
998
999 response =
1000 build_conn()
1001 |> post("/oauth/token", %{
1002 "grant_type" => "refresh_token",
1003 "refresh_token" => access_token.refresh_token,
1004 "client_id" => app.client_id,
1005 "client_secret" => app.client_secret
1006 })
1007 |> json_response(200)
1008
1009 ap_id = user.ap_id
1010
1011 assert match?(
1012 %{
1013 "scope" => "write",
1014 "token_type" => "Bearer",
1015 "expires_in" => 600,
1016 "access_token" => _,
1017 "refresh_token" => _,
1018 "me" => ^ap_id
1019 },
1020 response
1021 )
1022
1023 refute Repo.get_by(Token, token: token.token)
1024 token = Repo.get_by(Token, token: response["access_token"])
1025 assert token
1026 assert token.scopes == auth.scopes
1027 assert token.user_id == user.id
1028 assert token.app_id == app.id
1029 end
1030 end
1031
1032 describe "POST /oauth/token - bad request" do
1033 test "returns 500" do
1034 response =
1035 build_conn()
1036 |> post("/oauth/token", %{})
1037 |> json_response(500)
1038
1039 assert %{"error" => "Bad request"} == response
1040 end
1041 end
1042
1043 describe "POST /oauth/revoke - bad request" do
1044 test "returns 500" do
1045 response =
1046 build_conn()
1047 |> post("/oauth/revoke", %{})
1048 |> json_response(500)
1049
1050 assert %{"error" => "Bad request"} == response
1051 end
1052 end
1053 end