1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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
10 alias Pleroma.Web.OAuth.{Authorization, Token}
12 test "redirects with oauth authorization" do
14 app = insert(:oauth_app)
18 |> post("/oauth/authorize", %{
20 "name" => user.nickname,
22 "client_id" => app.client_id,
23 "redirect_uri" => app.redirect_uris,
24 "state" => "statepassed"
28 target = redirected_to(conn)
29 assert target =~ app.redirect_uris
31 query = URI.parse(target).query |> URI.query_decoder() |> Map.new()
33 assert %{"state" => "statepassed", "code" => code} = query
34 assert Repo.get_by(Authorization, token: code)
37 test "correctly handles wrong credentials", %{conn: conn} do
39 app = insert(:oauth_app)
43 |> post("/oauth/authorize", %{
45 "name" => user.nickname,
46 "password" => "wrong",
47 "client_id" => app.client_id,
48 "redirect_uri" => app.redirect_uris,
49 "state" => "statepassed"
52 |> html_response(:unauthorized)
55 assert result =~ app.client_id
56 assert result =~ app.redirect_uris
59 assert result =~ "Invalid"
62 test "issues a token for an all-body request" do
64 app = insert(:oauth_app)
66 {:ok, auth} = Authorization.create_authorization(app, user)
70 |> post("/oauth/token", %{
71 "grant_type" => "authorization_code",
73 "redirect_uri" => app.redirect_uris,
74 "client_id" => app.client_id,
75 "client_secret" => app.client_secret
78 assert %{"access_token" => token} = json_response(conn, 200)
79 assert Repo.get_by(Token, token: token)
82 test "issues a token for `password` grant_type with valid credentials" do
83 password = "testpassword"
84 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
86 app = insert(:oauth_app)
90 |> post("/oauth/token", %{
91 "grant_type" => "password",
92 "username" => user.nickname,
93 "password" => password,
94 "client_id" => app.client_id,
95 "client_secret" => app.client_secret
98 assert %{"access_token" => token} = json_response(conn, 200)
99 assert Repo.get_by(Token, token: token)
102 test "issues a token for request with HTTP basic auth client credentials" do
104 app = insert(:oauth_app)
106 {:ok, auth} = Authorization.create_authorization(app, user)
109 (URI.encode_www_form(app.client_id) <> ":" <> URI.encode_www_form(app.client_secret))
114 |> put_req_header("authorization", "Basic " <> app_encoded)
115 |> post("/oauth/token", %{
116 "grant_type" => "authorization_code",
117 "code" => auth.token,
118 "redirect_uri" => app.redirect_uris
121 assert %{"access_token" => token} = json_response(conn, 200)
122 assert Repo.get_by(Token, token: token)
125 test "rejects token exchange with invalid client credentials" do
127 app = insert(:oauth_app)
129 {:ok, auth} = Authorization.create_authorization(app, user)
133 |> put_req_header("authorization", "Basic JTIxOiVGMCU5RiVBNCVCNwo=")
134 |> post("/oauth/token", %{
135 "grant_type" => "authorization_code",
136 "code" => auth.token,
137 "redirect_uri" => app.redirect_uris
140 assert resp = json_response(conn, 400)
141 assert %{"error" => _} = resp
142 refute Map.has_key?(resp, "access_token")
145 test "rejects token exchange for valid credentials belonging to unconfirmed user and confirmation is required" do
146 setting = Pleroma.Config.get([:instance, :account_activation_required])
149 Pleroma.Config.put([:instance, :account_activation_required], true)
150 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
153 password = "testpassword"
154 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
155 info_change = Pleroma.User.Info.confirmation_changeset(user.info, :unconfirmed)
159 |> Ecto.Changeset.change()
160 |> Ecto.Changeset.put_embed(:info, info_change)
163 refute Pleroma.User.auth_active?(user)
165 app = insert(:oauth_app)
169 |> post("/oauth/token", %{
170 "grant_type" => "password",
171 "username" => user.nickname,
172 "password" => password,
173 "client_id" => app.client_id,
174 "client_secret" => app.client_secret
177 assert resp = json_response(conn, 403)
178 assert %{"error" => _} = resp
179 refute Map.has_key?(resp, "access_token")
182 test "rejects an invalid authorization code" do
183 app = insert(:oauth_app)
187 |> post("/oauth/token", %{
188 "grant_type" => "authorization_code",
189 "code" => "Imobviouslyinvalid",
190 "redirect_uri" => app.redirect_uris,
191 "client_id" => app.client_id,
192 "client_secret" => app.client_secret
195 assert resp = json_response(conn, 400)
196 assert %{"error" => _} = json_response(conn, 400)
197 refute Map.has_key?(resp, "access_token")