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
11 alias Pleroma.Web.OAuth.Token
13 describe "GET /oauth/authorize" do
18 signing_salt: "cooldude"
22 app: insert(:oauth_app, redirect_uris: "https://redirect.url"),
25 |> Plug.Session.call(Plug.Session.init(session_opts))
30 test "renders authentication page", %{app: app, conn: conn} do
36 "response_type" => "code",
37 "client_id" => app.client_id,
38 "redirect_uri" => app.redirect_uris,
43 assert html_response(conn, 200) =~ ~s(type="submit")
46 test "renders authentication page if user is already authenticated but `force_login` is tru-ish",
47 %{app: app, conn: conn} do
48 token = insert(:oauth_token, app_id: app.id)
52 |> put_session(:oauth_token, token.token)
56 "response_type" => "code",
57 "client_id" => app.client_id,
58 "redirect_uri" => app.redirect_uris,
60 "force_login" => "true"
64 assert html_response(conn, 200) =~ ~s(type="submit")
67 test "redirects to app if user is already authenticated", %{app: app, conn: conn} do
68 token = insert(:oauth_token, app_id: app.id)
72 |> put_session(:oauth_token, token.token)
76 "response_type" => "code",
77 "client_id" => app.client_id,
78 "redirect_uri" => app.redirect_uris,
83 assert redirected_to(conn) == "https://redirect.url"
87 describe "POST /oauth/authorize" do
88 test "redirects with oauth authorization" do
90 app = insert(:oauth_app, scopes: ["read", "write", "follow"])
94 |> post("/oauth/authorize", %{
96 "name" => user.nickname,
98 "client_id" => app.client_id,
99 "redirect_uri" => app.redirect_uris,
100 "scope" => "read write",
101 "state" => "statepassed"
105 target = redirected_to(conn)
106 assert target =~ app.redirect_uris
108 query = URI.parse(target).query |> URI.query_decoder() |> Map.new()
110 assert %{"state" => "statepassed", "code" => code} = query
111 auth = Repo.get_by(Authorization, token: code)
113 assert auth.scopes == ["read", "write"]
116 test "returns 401 for wrong credentials", %{conn: conn} do
118 app = insert(:oauth_app)
122 |> post("/oauth/authorize", %{
123 "authorization" => %{
124 "name" => user.nickname,
125 "password" => "wrong",
126 "client_id" => app.client_id,
127 "redirect_uri" => app.redirect_uris,
128 "state" => "statepassed",
129 "scope" => Enum.join(app.scopes, " ")
132 |> html_response(:unauthorized)
135 assert result =~ app.client_id
136 assert result =~ app.redirect_uris
139 assert result =~ "Invalid Username/Password"
142 test "returns 401 for missing scopes", %{conn: conn} do
144 app = insert(:oauth_app)
148 |> post("/oauth/authorize", %{
149 "authorization" => %{
150 "name" => user.nickname,
151 "password" => "test",
152 "client_id" => app.client_id,
153 "redirect_uri" => app.redirect_uris,
154 "state" => "statepassed",
158 |> html_response(:unauthorized)
161 assert result =~ app.client_id
162 assert result =~ app.redirect_uris
165 assert result =~ "This action is outside the authorized scopes"
168 test "returns 401 for scopes beyond app scopes", %{conn: conn} do
170 app = insert(:oauth_app, scopes: ["read", "write"])
174 |> post("/oauth/authorize", %{
175 "authorization" => %{
176 "name" => user.nickname,
177 "password" => "test",
178 "client_id" => app.client_id,
179 "redirect_uri" => app.redirect_uris,
180 "state" => "statepassed",
181 "scope" => "read write follow"
184 |> html_response(:unauthorized)
187 assert result =~ app.client_id
188 assert result =~ app.redirect_uris
191 assert result =~ "This action is outside the authorized scopes"
195 describe "POST /oauth/token" do
196 test "issues a token for an all-body request" do
198 app = insert(:oauth_app, scopes: ["read", "write"])
200 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
204 |> post("/oauth/token", %{
205 "grant_type" => "authorization_code",
206 "code" => auth.token,
207 "redirect_uri" => app.redirect_uris,
208 "client_id" => app.client_id,
209 "client_secret" => app.client_secret
212 assert %{"access_token" => token, "me" => ap_id} = json_response(conn, 200)
214 token = Repo.get_by(Token, token: token)
216 assert token.scopes == auth.scopes
217 assert user.ap_id == ap_id
220 test "issues a token for `password` grant_type with valid credentials, with full permissions by default" do
221 password = "testpassword"
222 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
224 app = insert(:oauth_app, scopes: ["read", "write"])
226 # Note: "scope" param is intentionally omitted
229 |> post("/oauth/token", %{
230 "grant_type" => "password",
231 "username" => user.nickname,
232 "password" => password,
233 "client_id" => app.client_id,
234 "client_secret" => app.client_secret
237 assert %{"access_token" => token} = json_response(conn, 200)
239 token = Repo.get_by(Token, token: token)
241 assert token.scopes == app.scopes
244 test "issues a token for request with HTTP basic auth client credentials" do
246 app = insert(:oauth_app, scopes: ["scope1", "scope2", "scope3"])
248 {:ok, auth} = Authorization.create_authorization(app, user, ["scope1", "scope2"])
249 assert auth.scopes == ["scope1", "scope2"]
252 (URI.encode_www_form(app.client_id) <> ":" <> URI.encode_www_form(app.client_secret))
257 |> put_req_header("authorization", "Basic " <> app_encoded)
258 |> post("/oauth/token", %{
259 "grant_type" => "authorization_code",
260 "code" => auth.token,
261 "redirect_uri" => app.redirect_uris
264 assert %{"access_token" => token, "scope" => scope} = json_response(conn, 200)
266 assert scope == "scope1 scope2"
268 token = Repo.get_by(Token, token: token)
270 assert token.scopes == ["scope1", "scope2"]
273 test "rejects token exchange with invalid client credentials" do
275 app = insert(:oauth_app)
277 {:ok, auth} = Authorization.create_authorization(app, user)
281 |> put_req_header("authorization", "Basic JTIxOiVGMCU5RiVBNCVCNwo=")
282 |> post("/oauth/token", %{
283 "grant_type" => "authorization_code",
284 "code" => auth.token,
285 "redirect_uri" => app.redirect_uris
288 assert resp = json_response(conn, 400)
289 assert %{"error" => _} = resp
290 refute Map.has_key?(resp, "access_token")
293 test "rejects token exchange for valid credentials belonging to unconfirmed user and confirmation is required" do
294 setting = Pleroma.Config.get([:instance, :account_activation_required])
297 Pleroma.Config.put([:instance, :account_activation_required], true)
298 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
301 password = "testpassword"
302 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
303 info_change = Pleroma.User.Info.confirmation_changeset(user.info, :unconfirmed)
307 |> Ecto.Changeset.change()
308 |> Ecto.Changeset.put_embed(:info, info_change)
311 refute Pleroma.User.auth_active?(user)
313 app = insert(:oauth_app)
317 |> post("/oauth/token", %{
318 "grant_type" => "password",
319 "username" => user.nickname,
320 "password" => password,
321 "client_id" => app.client_id,
322 "client_secret" => app.client_secret
325 assert resp = json_response(conn, 403)
326 assert %{"error" => _} = resp
327 refute Map.has_key?(resp, "access_token")
330 test "rejects an invalid authorization code" do
331 app = insert(:oauth_app)
335 |> post("/oauth/token", %{
336 "grant_type" => "authorization_code",
337 "code" => "Imobviouslyinvalid",
338 "redirect_uri" => app.redirect_uris,
339 "client_id" => app.client_id,
340 "client_secret" => app.client_secret
343 assert resp = json_response(conn, 400)
344 assert %{"error" => _} = json_response(conn, 400)
345 refute Map.has_key?(resp, "access_token")