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], ~w(twitter facebook))
36 test "GET /oauth/authorize renders auth forms, including OAuth consumer form", %{
45 "response_type" => "code",
46 "client_id" => app.client_id,
47 "redirect_uri" => OAuthController.default_redirect_uri(app),
52 assert response = html_response(conn, 200)
53 assert response =~ "Sign in with Twitter"
54 assert response =~ o_auth_path(conn, :prepare_request)
57 test "GET /oauth/prepare_request encodes parameters as `state` and redirects", %{
64 "/oauth/prepare_request",
66 "provider" => "twitter",
68 "scope" => "read follow",
69 "client_id" => app.client_id,
70 "redirect_uri" => OAuthController.default_redirect_uri(app),
76 assert response = html_response(conn, 302)
78 redirect_query = URI.parse(redirected_to(conn)).query
79 assert %{"state" => state_param} = URI.decode_query(redirect_query)
80 assert {:ok, state_components} = Poison.decode(state_param)
82 expected_client_id = app.client_id
83 expected_redirect_uri = app.redirect_uris
86 "scope" => "read follow",
87 "client_id" => ^expected_client_id,
88 "redirect_uri" => ^expected_redirect_uri,
93 test "with user-bound registration, GET /oauth/<provider>/callback redirects to `redirect_uri` with `code`",
94 %{app: app, conn: conn} do
95 registration = insert(:registration)
96 redirect_uri = OAuthController.default_redirect_uri(app)
99 "scope" => Enum.join(app.scopes, " "),
100 "client_id" => app.client_id,
101 "redirect_uri" => redirect_uri,
107 |> assign(:ueberauth_auth, %{provider: registration.provider, uid: registration.uid})
109 "/oauth/twitter/callback",
111 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
112 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
113 "provider" => "twitter",
114 "state" => Poison.encode!(state_params)
118 assert response = html_response(conn, 302)
119 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
122 test "with user-unbound registration, GET /oauth/<provider>/callback renders registration_details page",
123 %{app: app, conn: conn} do
127 "scope" => "read write",
128 "client_id" => app.client_id,
129 "redirect_uri" => OAuthController.default_redirect_uri(app),
135 |> assign(:ueberauth_auth, %{
138 info: %{nickname: user.nickname, email: user.email, name: user.name, description: nil}
141 "/oauth/twitter/callback",
143 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
144 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
145 "provider" => "twitter",
146 "state" => Poison.encode!(state_params)
150 assert response = html_response(conn, 200)
151 assert response =~ ~r/name="op" type="submit" value="register"/
152 assert response =~ ~r/name="op" type="submit" value="connect"/
153 assert response =~ user.email
154 assert response =~ user.nickname
157 test "on authentication error, GET /oauth/<provider>/callback redirects to `redirect_uri`", %{
162 "scope" => Enum.join(app.scopes, " "),
163 "client_id" => app.client_id,
164 "redirect_uri" => OAuthController.default_redirect_uri(app),
170 |> assign(:ueberauth_failure, %{errors: [%{message: "(error description)"}]})
172 "/oauth/twitter/callback",
174 "oauth_token" => "G-5a3AAAAAAAwMH9AAABaektfSM",
175 "oauth_verifier" => "QZl8vUqNvXMTKpdmUnGejJxuHG75WWWs",
176 "provider" => "twitter",
177 "state" => Poison.encode!(state_params)
181 assert response = html_response(conn, 302)
182 assert redirected_to(conn) == app.redirect_uris
183 assert get_flash(conn, :error) == "Failed to authenticate: (error description)."
186 test "GET /oauth/registration_details renders registration details form", %{
193 "/oauth/registration_details",
195 "authorization" => %{
196 "scopes" => app.scopes,
197 "client_id" => app.client_id,
198 "redirect_uri" => OAuthController.default_redirect_uri(app),
199 "state" => "a_state",
201 "email" => "john@doe.com"
206 assert response = html_response(conn, 200)
207 assert response =~ ~r/name="op" type="submit" value="register"/
208 assert response =~ ~r/name="op" type="submit" value="connect"/
211 test "with valid params, POST /oauth/register?op=register redirects to `redirect_uri` with `code`",
216 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
217 redirect_uri = OAuthController.default_redirect_uri(app)
221 |> put_session(:registration_id, registration.id)
226 "authorization" => %{
227 "scopes" => app.scopes,
228 "client_id" => app.client_id,
229 "redirect_uri" => redirect_uri,
230 "state" => "a_state",
231 "nickname" => "availablenick",
232 "email" => "available@email.com"
237 assert response = html_response(conn, 302)
238 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
241 test "with unlisted `redirect_uri`, POST /oauth/register?op=register results in HTTP 401",
246 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
247 unlisted_redirect_uri = "http://cross-site-request.com"
251 |> put_session(:registration_id, registration.id)
256 "authorization" => %{
257 "scopes" => app.scopes,
258 "client_id" => app.client_id,
259 "redirect_uri" => unlisted_redirect_uri,
260 "state" => "a_state",
261 "nickname" => "availablenick",
262 "email" => "available@email.com"
267 assert response = html_response(conn, 401)
270 test "with invalid params, POST /oauth/register?op=register renders registration_details page",
275 another_user = insert(:user)
276 registration = insert(:registration, user: nil, info: %{"nickname" => nil, "email" => nil})
280 "authorization" => %{
281 "scopes" => app.scopes,
282 "client_id" => app.client_id,
283 "redirect_uri" => OAuthController.default_redirect_uri(app),
284 "state" => "a_state",
285 "nickname" => "availablenickname",
286 "email" => "available@email.com"
290 for {bad_param, bad_param_value} <-
291 [{"nickname", another_user.nickname}, {"email", another_user.email}] do
292 bad_registration_attrs = %{
293 "authorization" => Map.put(params["authorization"], bad_param, bad_param_value)
296 bad_params = Map.merge(params, bad_registration_attrs)
300 |> put_session(:registration_id, registration.id)
301 |> post("/oauth/register", bad_params)
303 assert html_response(conn, 403) =~ ~r/name="op" type="submit" value="register"/
304 assert get_flash(conn, :error) == "Error: #{bad_param} has already been taken."
308 test "with valid params, POST /oauth/register?op=connect redirects to `redirect_uri` with `code`",
313 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt("testpassword"))
314 registration = insert(:registration, user: nil)
315 redirect_uri = OAuthController.default_redirect_uri(app)
319 |> put_session(:registration_id, registration.id)
324 "authorization" => %{
325 "scopes" => app.scopes,
326 "client_id" => app.client_id,
327 "redirect_uri" => redirect_uri,
328 "state" => "a_state",
329 "name" => user.nickname,
330 "password" => "testpassword"
335 assert response = html_response(conn, 302)
336 assert redirected_to(conn) =~ ~r/#{redirect_uri}\?code=.+/
339 test "with unlisted `redirect_uri`, POST /oauth/register?op=connect results in HTTP 401`",
344 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt("testpassword"))
345 registration = insert(:registration, user: nil)
346 unlisted_redirect_uri = "http://cross-site-request.com"
350 |> put_session(:registration_id, registration.id)
355 "authorization" => %{
356 "scopes" => app.scopes,
357 "client_id" => app.client_id,
358 "redirect_uri" => unlisted_redirect_uri,
359 "state" => "a_state",
360 "name" => user.nickname,
361 "password" => "testpassword"
366 assert response = html_response(conn, 401)
369 test "with invalid params, POST /oauth/register?op=connect renders registration_details page",
375 registration = insert(:registration, user: nil)
379 "authorization" => %{
380 "scopes" => app.scopes,
381 "client_id" => app.client_id,
382 "redirect_uri" => OAuthController.default_redirect_uri(app),
383 "state" => "a_state",
384 "name" => user.nickname,
385 "password" => "wrong password"
391 |> put_session(:registration_id, registration.id)
392 |> post("/oauth/register", params)
394 assert html_response(conn, 401) =~ ~r/name="op" type="submit" value="connect"/
395 assert get_flash(conn, :error) == "Invalid Username/Password"
399 describe "GET /oauth/authorize" do
402 app: insert(:oauth_app, redirect_uris: "https://redirect.url"),
405 |> Plug.Session.call(Plug.Session.init(@session_opts))
410 test "renders authentication page", %{app: app, conn: conn} do
416 "response_type" => "code",
417 "client_id" => app.client_id,
418 "redirect_uri" => OAuthController.default_redirect_uri(app),
423 assert html_response(conn, 200) =~ ~s(type="submit")
426 test "properly handles internal calls with `authorization`-wrapped params", %{
435 "authorization" => %{
436 "response_type" => "code",
437 "client_id" => app.client_id,
438 "redirect_uri" => OAuthController.default_redirect_uri(app),
444 assert html_response(conn, 200) =~ ~s(type="submit")
447 test "renders authentication page if user is already authenticated but `force_login` is tru-ish",
448 %{app: app, conn: conn} do
449 token = insert(:oauth_token, app: app)
453 |> put_session(:oauth_token, token.token)
457 "response_type" => "code",
458 "client_id" => app.client_id,
459 "redirect_uri" => OAuthController.default_redirect_uri(app),
461 "force_login" => "true"
465 assert html_response(conn, 200) =~ ~s(type="submit")
468 test "renders authentication page if user is already authenticated but user request with another client",
473 token = insert(:oauth_token, app: app)
477 |> put_session(:oauth_token, token.token)
481 "response_type" => "code",
482 "client_id" => "another_client_id",
483 "redirect_uri" => OAuthController.default_redirect_uri(app),
488 assert html_response(conn, 200) =~ ~s(type="submit")
491 test "with existing authentication and non-OOB `redirect_uri`, redirects to app with `token` and `state` params",
496 token = insert(:oauth_token, app: app)
500 |> put_session(:oauth_token, token.token)
504 "response_type" => "code",
505 "client_id" => app.client_id,
506 "redirect_uri" => OAuthController.default_redirect_uri(app),
507 "state" => "specific_client_state",
512 assert URI.decode(redirected_to(conn)) ==
513 "https://redirect.url?access_token=#{token.token}&state=specific_client_state"
516 test "with existing authentication and unlisted non-OOB `redirect_uri`, redirects without credentials",
521 unlisted_redirect_uri = "http://cross-site-request.com"
522 token = insert(:oauth_token, app: app)
526 |> put_session(:oauth_token, token.token)
530 "response_type" => "code",
531 "client_id" => app.client_id,
532 "redirect_uri" => unlisted_redirect_uri,
533 "state" => "specific_client_state",
538 assert redirected_to(conn) == unlisted_redirect_uri
541 test "with existing authentication and OOB `redirect_uri`, redirects to app with `token` and `state` params",
546 token = insert(:oauth_token, app: app)
550 |> put_session(:oauth_token, token.token)
554 "response_type" => "code",
555 "client_id" => app.client_id,
556 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
561 assert html_response(conn, 200) =~ "Authorization exists"
565 describe "POST /oauth/authorize" do
566 test "redirects with oauth authorization, " <>
567 "granting requested app-supported scopes to both admin- and non-admin users" do
568 app_scopes = ["read", "write", "admin", "secret_scope"]
569 app = insert(:oauth_app, scopes: app_scopes)
570 redirect_uri = OAuthController.default_redirect_uri(app)
572 non_admin = insert(:user, is_admin: false)
573 admin = insert(:user, is_admin: true)
574 scopes_subset = ["read:subscope", "write", "admin"]
576 # In case scope param is missing, expecting _all_ app-supported scopes to be granted
577 for user <- [non_admin, admin],
578 {requested_scopes, expected_scopes} <-
579 %{scopes_subset => scopes_subset, nil => app_scopes} do
585 "authorization" => %{
586 "name" => user.nickname,
587 "password" => "test",
588 "client_id" => app.client_id,
589 "redirect_uri" => redirect_uri,
590 "scope" => requested_scopes,
591 "state" => "statepassed"
596 target = redirected_to(conn)
597 assert target =~ redirect_uri
599 query = URI.parse(target).query |> URI.query_decoder() |> Map.new()
601 assert %{"state" => "statepassed", "code" => code} = query
602 auth = Repo.get_by(Authorization, token: code)
604 assert auth.scopes == expected_scopes
608 test "returns 401 for wrong credentials", %{conn: conn} do
610 app = insert(:oauth_app)
611 redirect_uri = OAuthController.default_redirect_uri(app)
615 |> post("/oauth/authorize", %{
616 "authorization" => %{
617 "name" => user.nickname,
618 "password" => "wrong",
619 "client_id" => app.client_id,
620 "redirect_uri" => redirect_uri,
621 "state" => "statepassed",
622 "scope" => Enum.join(app.scopes, " ")
625 |> html_response(:unauthorized)
628 assert result =~ app.client_id
629 assert result =~ redirect_uri
632 assert result =~ "Invalid Username/Password"
635 test "returns 401 for missing scopes" do
636 user = insert(:user, is_admin: false)
637 app = insert(:oauth_app, scopes: ["read", "write", "admin"])
638 redirect_uri = OAuthController.default_redirect_uri(app)
642 |> post("/oauth/authorize", %{
643 "authorization" => %{
644 "name" => user.nickname,
645 "password" => "test",
646 "client_id" => app.client_id,
647 "redirect_uri" => redirect_uri,
648 "state" => "statepassed",
652 |> html_response(:unauthorized)
655 assert result =~ app.client_id
656 assert result =~ redirect_uri
659 assert result =~ "This action is outside the authorized scopes"
662 test "returns 401 for scopes beyond app scopes hierarchy", %{conn: conn} do
664 app = insert(:oauth_app, scopes: ["read", "write"])
665 redirect_uri = OAuthController.default_redirect_uri(app)
669 |> post("/oauth/authorize", %{
670 "authorization" => %{
671 "name" => user.nickname,
672 "password" => "test",
673 "client_id" => app.client_id,
674 "redirect_uri" => redirect_uri,
675 "state" => "statepassed",
676 "scope" => "read write follow"
679 |> html_response(:unauthorized)
682 assert result =~ app.client_id
683 assert result =~ redirect_uri
686 assert result =~ "This action is outside the authorized scopes"
690 describe "POST /oauth/token" do
691 test "issues a token for an all-body request" do
693 app = insert(:oauth_app, scopes: ["read", "write"])
695 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
699 |> post("/oauth/token", %{
700 "grant_type" => "authorization_code",
701 "code" => auth.token,
702 "redirect_uri" => OAuthController.default_redirect_uri(app),
703 "client_id" => app.client_id,
704 "client_secret" => app.client_secret
707 assert %{"access_token" => token, "me" => ap_id} = json_response(conn, 200)
709 token = Repo.get_by(Token, token: token)
711 assert token.scopes == auth.scopes
712 assert user.ap_id == ap_id
715 test "issues a token for `password` grant_type with valid credentials, with full permissions by default" do
716 password = "testpassword"
717 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
719 app = insert(:oauth_app, scopes: ["read", "write"])
721 # Note: "scope" param is intentionally omitted
724 |> post("/oauth/token", %{
725 "grant_type" => "password",
726 "username" => user.nickname,
727 "password" => password,
728 "client_id" => app.client_id,
729 "client_secret" => app.client_secret
732 assert %{"access_token" => token} = json_response(conn, 200)
734 token = Repo.get_by(Token, token: token)
736 assert token.scopes == app.scopes
739 test "issues a token for request with HTTP basic auth client credentials" do
741 app = insert(:oauth_app, scopes: ["scope1", "scope2", "scope3"])
743 {:ok, auth} = Authorization.create_authorization(app, user, ["scope1", "scope2"])
744 assert auth.scopes == ["scope1", "scope2"]
747 (URI.encode_www_form(app.client_id) <> ":" <> URI.encode_www_form(app.client_secret))
752 |> put_req_header("authorization", "Basic " <> app_encoded)
753 |> post("/oauth/token", %{
754 "grant_type" => "authorization_code",
755 "code" => auth.token,
756 "redirect_uri" => OAuthController.default_redirect_uri(app)
759 assert %{"access_token" => token, "scope" => scope} = json_response(conn, 200)
761 assert scope == "scope1 scope2"
763 token = Repo.get_by(Token, token: token)
765 assert token.scopes == ["scope1", "scope2"]
768 test "issue a token for client_credentials grant type" do
769 app = insert(:oauth_app, scopes: ["read", "write"])
773 |> post("/oauth/token", %{
774 "grant_type" => "client_credentials",
775 "client_id" => app.client_id,
776 "client_secret" => app.client_secret
779 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
780 json_response(conn, 200)
783 token_from_db = Repo.get_by(Token, token: token)
786 assert scope == "read write"
789 test "rejects token exchange with invalid client credentials" do
791 app = insert(:oauth_app)
793 {:ok, auth} = Authorization.create_authorization(app, user)
797 |> put_req_header("authorization", "Basic JTIxOiVGMCU5RiVBNCVCNwo=")
798 |> post("/oauth/token", %{
799 "grant_type" => "authorization_code",
800 "code" => auth.token,
801 "redirect_uri" => OAuthController.default_redirect_uri(app)
804 assert resp = json_response(conn, 400)
805 assert %{"error" => _} = resp
806 refute Map.has_key?(resp, "access_token")
809 test "rejects token exchange for valid credentials belonging to unconfirmed user and confirmation is required" do
810 Pleroma.Config.put([:instance, :account_activation_required], true)
811 password = "testpassword"
814 insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
815 |> User.confirmation_changeset(need_confirmation: true)
816 |> User.update_and_set_cache()
818 refute Pleroma.User.account_status(user) == :active
820 app = insert(:oauth_app)
824 |> post("/oauth/token", %{
825 "grant_type" => "password",
826 "username" => user.nickname,
827 "password" => password,
828 "client_id" => app.client_id,
829 "client_secret" => app.client_secret
832 assert resp = json_response(conn, 403)
833 assert %{"error" => _} = resp
834 refute Map.has_key?(resp, "access_token")
837 test "rejects token exchange for valid credentials belonging to deactivated user" do
838 password = "testpassword"
842 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
846 app = insert(:oauth_app)
850 |> post("/oauth/token", %{
851 "grant_type" => "password",
852 "username" => user.nickname,
853 "password" => password,
854 "client_id" => app.client_id,
855 "client_secret" => app.client_secret
857 |> json_response(403)
860 "error" => "Your account is currently disabled",
861 "identifier" => "account_is_disabled"
865 test "rejects token exchange for user with password_reset_pending set to true" do
866 password = "testpassword"
870 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
871 password_reset_pending: true
874 app = insert(:oauth_app, scopes: ["read", "write"])
878 |> post("/oauth/token", %{
879 "grant_type" => "password",
880 "username" => user.nickname,
881 "password" => password,
882 "client_id" => app.client_id,
883 "client_secret" => app.client_secret
885 |> json_response(403)
888 "error" => "Password reset is required",
889 "identifier" => "password_reset_required"
893 test "rejects token exchange for user with confirmation_pending set to true" do
894 Pleroma.Config.put([:instance, :account_activation_required], true)
895 password = "testpassword"
899 password_hash: Comeonin.Pbkdf2.hashpwsalt(password),
900 confirmation_pending: true
903 app = insert(:oauth_app, scopes: ["read", "write"])
907 |> post("/oauth/token", %{
908 "grant_type" => "password",
909 "username" => user.nickname,
910 "password" => password,
911 "client_id" => app.client_id,
912 "client_secret" => app.client_secret
914 |> json_response(403)
917 "error" => "Your login is missing a confirmed e-mail address",
918 "identifier" => "missing_confirmed_email"
922 test "rejects an invalid authorization code" do
923 app = insert(:oauth_app)
927 |> post("/oauth/token", %{
928 "grant_type" => "authorization_code",
929 "code" => "Imobviouslyinvalid",
930 "redirect_uri" => OAuthController.default_redirect_uri(app),
931 "client_id" => app.client_id,
932 "client_secret" => app.client_secret
935 assert resp = json_response(conn, 400)
936 assert %{"error" => _} = json_response(conn, 400)
937 refute Map.has_key?(resp, "access_token")
941 describe "POST /oauth/token - refresh token" do
942 clear_config([:oauth2, :issue_new_refresh_token])
944 test "issues a new access token with keep fresh token" do
945 Pleroma.Config.put([:oauth2, :issue_new_refresh_token], true)
947 app = insert(:oauth_app, scopes: ["read", "write"])
949 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
950 {:ok, token} = Token.exchange_token(app, auth)
954 |> post("/oauth/token", %{
955 "grant_type" => "refresh_token",
956 "refresh_token" => token.refresh_token,
957 "client_id" => app.client_id,
958 "client_secret" => app.client_secret
960 |> json_response(200)
967 "token_type" => "Bearer",
970 "refresh_token" => _,
976 refute Repo.get_by(Token, token: token.token)
977 new_token = Repo.get_by(Token, token: response["access_token"])
978 assert new_token.refresh_token == token.refresh_token
979 assert new_token.scopes == auth.scopes
980 assert new_token.user_id == user.id
981 assert new_token.app_id == app.id
984 test "issues a new access token with new fresh token" do
985 Pleroma.Config.put([:oauth2, :issue_new_refresh_token], false)
987 app = insert(:oauth_app, scopes: ["read", "write"])
989 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
990 {:ok, token} = Token.exchange_token(app, auth)
994 |> post("/oauth/token", %{
995 "grant_type" => "refresh_token",
996 "refresh_token" => token.refresh_token,
997 "client_id" => app.client_id,
998 "client_secret" => app.client_secret
1000 |> json_response(200)
1007 "token_type" => "Bearer",
1008 "expires_in" => 600,
1009 "access_token" => _,
1010 "refresh_token" => _,
1016 refute Repo.get_by(Token, token: token.token)
1017 new_token = Repo.get_by(Token, token: response["access_token"])
1018 refute new_token.refresh_token == token.refresh_token
1019 assert new_token.scopes == auth.scopes
1020 assert new_token.user_id == user.id
1021 assert new_token.app_id == app.id
1024 test "returns 400 if we try use access token" do
1025 user = insert(:user)
1026 app = insert(:oauth_app, scopes: ["read", "write"])
1028 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1029 {:ok, token} = Token.exchange_token(app, auth)
1033 |> post("/oauth/token", %{
1034 "grant_type" => "refresh_token",
1035 "refresh_token" => token.token,
1036 "client_id" => app.client_id,
1037 "client_secret" => app.client_secret
1039 |> json_response(400)
1041 assert %{"error" => "Invalid credentials"} == response
1044 test "returns 400 if refresh_token invalid" do
1045 app = insert(:oauth_app, scopes: ["read", "write"])
1049 |> post("/oauth/token", %{
1050 "grant_type" => "refresh_token",
1051 "refresh_token" => "token.refresh_token",
1052 "client_id" => app.client_id,
1053 "client_secret" => app.client_secret
1055 |> json_response(400)
1057 assert %{"error" => "Invalid credentials"} == response
1060 test "issues a new token if token expired" do
1061 user = insert(:user)
1062 app = insert(:oauth_app, scopes: ["read", "write"])
1064 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
1065 {:ok, token} = Token.exchange_token(app, auth)
1068 Ecto.Changeset.change(
1070 %{valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -86_400 * 30)}
1073 {:ok, access_token} = Repo.update(change)
1077 |> post("/oauth/token", %{
1078 "grant_type" => "refresh_token",
1079 "refresh_token" => access_token.refresh_token,
1080 "client_id" => app.client_id,
1081 "client_secret" => app.client_secret
1083 |> json_response(200)
1090 "token_type" => "Bearer",
1091 "expires_in" => 600,
1092 "access_token" => _,
1093 "refresh_token" => _,
1099 refute Repo.get_by(Token, token: token.token)
1100 token = Repo.get_by(Token, token: response["access_token"])
1102 assert token.scopes == auth.scopes
1103 assert token.user_id == user.id
1104 assert token.app_id == app.id
1108 describe "POST /oauth/token - bad request" do
1109 test "returns 500" do
1112 |> post("/oauth/token", %{})
1113 |> json_response(500)
1115 assert %{"error" => "Bad request"} == response
1119 describe "POST /oauth/revoke - bad request" do
1120 test "returns 500" do
1123 |> post("/oauth/revoke", %{})
1124 |> json_response(500)
1126 assert %{"error" => "Bad request"} == response