1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.OAuth.OAuthControllerTest do
6 use Pleroma.Web.ConnCase
11 alias Pleroma.Web.OAuth.Authorization
12 alias Pleroma.Web.OAuth.OAuthController
13 alias Pleroma.Web.OAuth.Token
18 signing_salt: "cooldude"
21 clear_config([:instance, :account_activation_required])
23 describe "in OAuth consumer mode, " do
26 app: insert(:oauth_app),
29 |> Plug.Session.call(Plug.Session.init(@session_opts))
34 clear_config([:auth, :oauth_consumer_strategies]) do
36 [:auth, :oauth_consumer_strategies],
41 test "GET /oauth/authorize renders auth forms, including OAuth consumer form", %{
50 "response_type" => "code",
51 "client_id" => app.client_id,
52 "redirect_uri" => OAuthController.default_redirect_uri(app),
57 assert response = html_response(conn, 200)
58 assert response =~ "Sign in with Twitter"
59 assert response =~ o_auth_path(conn, :prepare_request)
62 test "GET /oauth/prepare_request encodes parameters as `state` and redirects", %{
69 "/oauth/prepare_request",
71 "provider" => "twitter",
73 "scope" => "read follow",
74 "client_id" => app.client_id,
75 "redirect_uri" => OAuthController.default_redirect_uri(app),
81 assert response = html_response(conn, 302)
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)
87 expected_client_id = app.client_id
88 expected_redirect_uri = app.redirect_uris
91 "scope" => "read follow",
92 "client_id" => ^expected_client_id,
93 "redirect_uri" => ^expected_redirect_uri,
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)
104 "scope" => Enum.join(app.scopes, " "),
105 "client_id" => app.client_id,
106 "redirect_uri" => redirect_uri,
112 |> assign(:ueberauth_auth, %{provider: registration.provider, uid: registration.uid})
114 "/oauth/twitter/callback",
116 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
117 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
118 "provider" => "twitter",
119 "state" => Poison.encode!(state_params)
123 assert response = html_response(conn, 302)
124 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
127 test "with user-unbound registration, GET /oauth/<provider>/callback renders registration_details page",
128 %{app: app, conn: conn} do
132 "scope" => "read write",
133 "client_id" => app.client_id,
134 "redirect_uri" => OAuthController.default_redirect_uri(app),
140 |> assign(:ueberauth_auth, %{
143 info: %{nickname: user.nickname, email: user.email, name: user.name, description: nil}
146 "/oauth/twitter/callback",
148 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
149 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
150 "provider" => "twitter",
151 "state" => Poison.encode!(state_params)
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
162 test "on authentication error, GET /oauth/<provider>/callback redirects to `redirect_uri`", %{
167 "scope" => Enum.join(app.scopes, " "),
168 "client_id" => app.client_id,
169 "redirect_uri" => OAuthController.default_redirect_uri(app),
175 |> assign(:ueberauth_failure, %{errors: [%{message: "(error description)"}]})
177 "/oauth/twitter/callback",
179 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
180 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
181 "provider" => "twitter",
182 "state" => Poison.encode!(state_params)
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)."
191 test "GET /oauth/registration_details renders registration details form", %{
198 "/oauth/registration_details",
200 "authorization" => %{
201 "scopes" => app.scopes,
202 "client_id" => app.client_id,
203 "redirect_uri" => OAuthController.default_redirect_uri(app),
204 "state" => "a_state",
206 "email" => "john@doe.com"
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"/
216 test "with valid params, POST /oauth/register?op=register redirects to `redirect_uri` with `code`",
221 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
222 redirect_uri = OAuthController.default_redirect_uri(app)
226 |> put_session(:registration_id, registration.id)
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"
242 assert response = html_response(conn, 302)
243 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
246 test "with unlisted `redirect_uri`, POST /oauth/register?op=register results in HTTP 401",
251 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
252 unlisted_redirect_uri = "http://cross-site-request.com"
256 |> put_session(:registration_id, registration.id)
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"
272 assert response = html_response(conn, 401)
275 test "with invalid params, POST /oauth/register?op=register renders registration_details page",
280 another_user = insert(:user)
281 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
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"
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)
301 bad_params = Map.merge(params, bad_registration_attrs)
305 |> put_session(:registration_id, registration.id)
306 |> post("/oauth/register", bad_params)
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."
313 test "with valid params, POST /oauth/register?op=connect redirects to `redirect_uri` with `code`",
318 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt("testpassword"))
319 registration = insert(:registration, user: nil)
320 redirect_uri = OAuthController.default_redirect_uri(app)
324 |> put_session(:registration_id, registration.id)
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"
340 assert response = html_response(conn, 302)
341 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
344 test "with unlisted `redirect_uri`, POST /oauth/register?op=connect results in HTTP 401`",
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"
355 |> put_session(:registration_id, registration.id)
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"
371 assert response = html_response(conn, 401)
374 test "with invalid params, POST /oauth/register?op=connect renders registration_details page",
380 registration = insert(:registration, user: nil)
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"
396 |> put_session(:registration_id, registration.id)
397 |> post("/oauth/register", params)
399 assert html_response(conn, 401) =~ ~r/name="op" type="submit" value="connect"/
400 assert get_flash(conn, :error) == "Invalid Username/Password"
404 describe "GET /oauth/authorize" do
407 app: insert(:oauth_app, redirect_uris: "https://redirect.url"),
410 |> Plug.Session.call(Plug.Session.init(@session_opts))
415 test "renders authentication page", %{app: app, conn: conn} do
421 "response_type" => "code",
422 "client_id" => app.client_id,
423 "redirect_uri" => OAuthController.default_redirect_uri(app),
428 assert html_response(conn, 200) =~ ~s(type="submit")
431 test "properly handles internal calls with `authorization`-wrapped params", %{
440 "authorization" => %{
441 "response_type" => "code",
442 "client_id" => app.client_id,
443 "redirect_uri" => OAuthController.default_redirect_uri(app),
449 assert html_response(conn, 200) =~ ~s(type="submit")
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)
458 |> put_session(:oauth_token, token.token)
462 "response_type" => "code",
463 "client_id" => app.client_id,
464 "redirect_uri" => OAuthController.default_redirect_uri(app),
466 "force_login" => "true"
470 assert html_response(conn, 200) =~ ~s(type="submit")
473 test "renders authentication page if user is already authenticated but user request with another client",
478 token = insert(:oauth_token, app: app)
482 |> put_session(:oauth_token, token.token)
486 "response_type" => "code",
487 "client_id" => "another_client_id",
488 "redirect_uri" => OAuthController.default_redirect_uri(app),
493 assert html_response(conn, 200) =~ ~s(type="submit")
496 test "with existing authentication and non-OOB `redirect_uri`, redirects to app with `token` and `state` params",
501 token = insert(:oauth_token, app: app)
505 |> put_session(:oauth_token, token.token)
509 "response_type" => "code",
510 "client_id" => app.client_id,
511 "redirect_uri" => OAuthController.default_redirect_uri(app),
512 "state" => "specific_client_state",
517 assert URI.decode(redirected_to(conn)) ==
518 "https://redirect.url?access_token=#{token.token}&state=specific_client_state"
521 test "with existing authentication and unlisted non-OOB `redirect_uri`, redirects without credentials",
526 unlisted_redirect_uri = "http://cross-site-request.com"
527 token = insert(:oauth_token, app: app)
531 |> put_session(:oauth_token, token.token)
535 "response_type" => "code",
536 "client_id" => app.client_id,
537 "redirect_uri" => unlisted_redirect_uri,
538 "state" => "specific_client_state",
543 assert redirected_to(conn) == unlisted_redirect_uri
546 test "with existing authentication and OOB `redirect_uri`, redirects to app with `token` and `state` params",
551 token = insert(:oauth_token, app: app)
555 |> put_session(:oauth_token, token.token)
559 "response_type" => "code",
560 "client_id" => app.client_id,
561 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
566 assert html_response(conn, 200) =~ "Authorization exists"
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)
577 non_admin = insert(:user, is_admin: false)
578 admin = insert(:user, is_admin: true)
579 scopes_subset = ["read:subscope", "write", "admin"]
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
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"
601 target = redirected_to(conn)
602 assert target =~ redirect_uri
604 query = URI.parse(target).query |> URI.query_decoder() |> Map.new()
606 assert %{"state" => "statepassed", "code" => code} = query
607 auth = Repo.get_by(Authorization, token: code)
609 assert auth.scopes == expected_scopes
613 test "returns 401 for wrong credentials", %{conn: conn} do
615 app = insert(:oauth_app)
616 redirect_uri = OAuthController.default_redirect_uri(app)
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, " ")
630 |> html_response(:unauthorized)
633 assert result =~ app.client_id
634 assert result =~ redirect_uri
637 assert result =~ "Invalid Username/Password"
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)
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",
657 |> html_response(:unauthorized)
660 assert result =~ app.client_id
661 assert result =~ redirect_uri
664 assert result =~ "This action is outside the authorized scopes"
667 test "returns 401 for scopes beyond app scopes hierarchy", %{conn: conn} do
669 app = insert(:oauth_app, scopes: ["read", "write"])
670 redirect_uri = OAuthController.default_redirect_uri(app)
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"
684 |> html_response(:unauthorized)
687 assert result =~ app.client_id
688 assert result =~ redirect_uri
691 assert result =~ "This action is outside the authorized scopes"
695 describe "POST /oauth/token" do
696 test "issues a token for an all-body request" do
698 app = insert(:oauth_app, scopes: ["read", "write"])
700 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
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
712 assert %{"access_token" => token, "me" => ap_id} = json_response(conn, 200)
714 token = Repo.get_by(Token, token: token)
716 assert token.scopes == auth.scopes
717 assert user.ap_id == ap_id
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))
724 app = insert(:oauth_app, scopes: ["read", "write"])
726 # Note: "scope" param is intentionally omitted
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
737 assert %{"access_token" => token} = json_response(conn, 200)
739 token = Repo.get_by(Token, token: token)
741 assert token.scopes == app.scopes
744 test "issues a token for request with HTTP basic auth client credentials" do
746 app = insert(:oauth_app, scopes: ["scope1", "scope2", "scope3"])
748 {:ok, auth} = Authorization.create_authorization(app, user, ["scope1", "scope2"])
749 assert auth.scopes == ["scope1", "scope2"]
752 (URI.encode_www_form(app.client_id) <> ":" <> URI.encode_www_form(app.client_secret))
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)
764 assert %{"access_token" => token, "scope" => scope} = json_response(conn, 200)
766 assert scope == "scope1 scope2"
768 token = Repo.get_by(Token, token: token)
770 assert token.scopes == ["scope1", "scope2"]
773 test "issue a token for client_credentials grant type" do
774 app = insert(:oauth_app, scopes: ["read", "write"])
778 |> post("/oauth/token", %{
779 "grant_type" => "client_credentials",
780 "client_id" => app.client_id,
781 "client_secret" => app.client_secret
784 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
785 json_response(conn, 200)
788 token_from_db = Repo.get_by(Token, token: token)
791 assert scope == "read write"
794 test "rejects token exchange with invalid client credentials" do
796 app = insert(:oauth_app)
798 {:ok, auth} = Authorization.create_authorization(app, user)
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)
809 assert resp = json_response(conn, 400)
810 assert %{"error" => _} = resp
811 refute Map.has_key?(resp, "access_token")
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"
819 insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
820 |> User.confirmation_changeset(need_confirmation: true)
821 |> User.update_and_set_cache()
823 refute Pleroma.User.account_status(user) == :active
825 app = insert(:oauth_app)
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
837 assert resp = json_response(conn, 403)
838 assert %{"error" => _} = resp
839 refute Map.has_key?(resp, "access_token")
842 test "rejects token exchange for valid credentials belonging to deactivated user" do
843 password = "testpassword"
847 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
851 app = insert(:oauth_app)
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
862 |> json_response(403)
865 "error" => "Your account is currently disabled",
866 "identifier" => "account_is_disabled"
870 test "rejects token exchange for user with password_reset_pending set to true" do
871 password = "testpassword"
875 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
876 password_reset_pending: true
879 app = insert(:oauth_app, scopes: ["read", "write"])
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
890 |> json_response(403)
893 "error" => "Password reset is required",
894 "identifier" => "password_reset_required"
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"
904 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
905 confirmation_pending: true
908 app = insert(:oauth_app, scopes: ["read", "write"])
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
919 |> json_response(403)
922 "error" => "Your login is missing a confirmed e-mail address",
923 "identifier" => "missing_confirmed_email"
927 test "rejects an invalid authorization code" do
928 app = insert(:oauth_app)
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
940 assert resp = json_response(conn, 400)
941 assert %{"error" => _} = json_response(conn, 400)
942 refute Map.has_key?(resp, "access_token")
946 describe "POST /oauth/token - refresh token" do
947 clear_config([:oauth2, :issue_new_refresh_token])
949 test "issues a new access token with keep fresh token" do
950 Pleroma.Config.put([:oauth2, :issue_new_refresh_token], true)
952 app = insert(:oauth_app, scopes: ["read", "write"])
954 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
955 {:ok, token} = Token.exchange_token(app, auth)
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
965 |> json_response(200)
972 "token_type" => "Bearer",
975 "refresh_token" => _,
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
989 test "issues a new access token with new fresh token" do
990 Pleroma.Config.put([:oauth2, :issue_new_refresh_token], false)
992 app = insert(:oauth_app, scopes: ["read", "write"])
994 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
995 {:ok, token} = Token.exchange_token(app, auth)
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
1005 |> json_response(200)
1012 "token_type" => "Bearer",
1013 "expires_in" => 600,
1014 "access_token" => _,
1015 "refresh_token" => _,
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
1029 test "returns 400 if we try use access token" do
1030 user = insert(:user)
1031 app = insert(:oauth_app, scopes: ["read", "write"])
1033 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1034 {:ok, token} = Token.exchange_token(app, auth)
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
1044 |> json_response(400)
1046 assert %{"error" => "Invalid credentials"} == response
1049 test "returns 400 if refresh_token invalid" do
1050 app = insert(:oauth_app, scopes: ["read", "write"])
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
1060 |> json_response(400)
1062 assert %{"error" => "Invalid credentials"} == response
1065 test "issues a new token if token expired" do
1066 user = insert(:user)
1067 app = insert(:oauth_app, scopes: ["read", "write"])
1069 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1070 {:ok, token} = Token.exchange_token(app, auth)
1073 Ecto.Changeset.change(
1075 %{valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -86_400 * 30)}
1078 {:ok, access_token} = Repo.update(change)
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
1088 |> json_response(200)
1095 "token_type" => "Bearer",
1096 "expires_in" => 600,
1097 "access_token" => _,
1098 "refresh_token" => _,
1104 refute Repo.get_by(Token, token: token.token)
1105 token = Repo.get_by(Token, token: response["access_token"])
1107 assert token.scopes == auth.scopes
1108 assert token.user_id == user.id
1109 assert token.app_id == app.id
1113 describe "POST /oauth/token - bad request" do
1114 test "returns 500" do
1117 |> post("/oauth/token", %{})
1118 |> json_response(500)
1120 assert %{"error" => "Bad request"} == response
1124 describe "POST /oauth/revoke - bad request" do
1125 test "returns 500" do
1128 |> post("/oauth/revoke", %{})
1129 |> json_response(500)
1131 assert %{"error" => "Bad request"} == response