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 test "redirects with oauth authorization" do
15 app = insert(:oauth_app, scopes: ["read", "write", "follow"])
19 |> post("/oauth/authorize", %{
21 "name" => user.nickname,
23 "client_id" => app.client_id,
24 "redirect_uri" => app.redirect_uris,
25 "scope" => "read write",
26 "state" => "statepassed"
30 target = redirected_to(conn)
31 assert target =~ app.redirect_uris
33 query = URI.parse(target).query |> URI.query_decoder() |> Map.new()
35 assert %{"state" => "statepassed", "code" => code} = query
36 auth = Repo.get_by(Authorization, token: code)
38 assert auth.scopes == ["read", "write"]
41 test "returns 401 for wrong credentials", %{conn: conn} do
43 app = insert(:oauth_app)
47 |> post("/oauth/authorize", %{
49 "name" => user.nickname,
50 "password" => "wrong",
51 "client_id" => app.client_id,
52 "redirect_uri" => app.redirect_uris,
53 "state" => "statepassed",
54 "scope" => Enum.join(app.scopes, " ")
57 |> html_response(:unauthorized)
60 assert result =~ app.client_id
61 assert result =~ app.redirect_uris
64 assert result =~ "Invalid Username/Password"
67 test "returns 401 for missing scopes", %{conn: conn} do
69 app = insert(:oauth_app)
73 |> post("/oauth/authorize", %{
75 "name" => user.nickname,
77 "client_id" => app.client_id,
78 "redirect_uri" => app.redirect_uris,
79 "state" => "statepassed",
83 |> html_response(:unauthorized)
86 assert result =~ app.client_id
87 assert result =~ app.redirect_uris
90 assert result =~ "This action is outside the authorized scopes"
93 test "returns 401 for scopes beyond app scopes", %{conn: conn} do
95 app = insert(:oauth_app, scopes: ["read", "write"])
99 |> post("/oauth/authorize", %{
100 "authorization" => %{
101 "name" => user.nickname,
102 "password" => "test",
103 "client_id" => app.client_id,
104 "redirect_uri" => app.redirect_uris,
105 "state" => "statepassed",
106 "scope" => "read write follow"
109 |> html_response(:unauthorized)
112 assert result =~ app.client_id
113 assert result =~ app.redirect_uris
116 assert result =~ "This action is outside the authorized scopes"
119 test "issues a token for an all-body request" do
121 app = insert(:oauth_app, scopes: ["read", "write"])
123 {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
127 |> post("/oauth/token", %{
128 "grant_type" => "authorization_code",
129 "code" => auth.token,
130 "redirect_uri" => app.redirect_uris,
131 "client_id" => app.client_id,
132 "client_secret" => app.client_secret
135 assert %{"access_token" => token, "me" => ap_id} = json_response(conn, 200)
137 token = Repo.get_by(Token, token: token)
139 assert token.scopes == auth.scopes
140 assert user.ap_id == ap_id
143 test "issues a token for `password` grant_type with valid credentials, with full permissions by default" do
144 password = "testpassword"
145 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
147 app = insert(:oauth_app, scopes: ["read", "write"])
149 # Note: "scope" param is intentionally omitted
152 |> post("/oauth/token", %{
153 "grant_type" => "password",
154 "username" => user.nickname,
155 "password" => password,
156 "client_id" => app.client_id,
157 "client_secret" => app.client_secret
160 assert %{"access_token" => token} = json_response(conn, 200)
162 token = Repo.get_by(Token, token: token)
164 assert token.scopes == app.scopes
167 test "issues a token for request with HTTP basic auth client credentials" do
169 app = insert(:oauth_app, scopes: ["scope1", "scope2", "scope3"])
171 {:ok, auth} = Authorization.create_authorization(app, user, ["scope1", "scope2"])
172 assert auth.scopes == ["scope1", "scope2"]
175 (URI.encode_www_form(app.client_id) <> ":" <> URI.encode_www_form(app.client_secret))
180 |> put_req_header("authorization", "Basic " <> app_encoded)
181 |> post("/oauth/token", %{
182 "grant_type" => "authorization_code",
183 "code" => auth.token,
184 "redirect_uri" => app.redirect_uris
187 assert %{"access_token" => token, "scope" => scope} = json_response(conn, 200)
189 assert scope == "scope1 scope2"
191 token = Repo.get_by(Token, token: token)
193 assert token.scopes == ["scope1", "scope2"]
196 test "rejects token exchange with invalid client credentials" do
198 app = insert(:oauth_app)
200 {:ok, auth} = Authorization.create_authorization(app, user)
204 |> put_req_header("authorization", "Basic JTIxOiVGMCU5RiVBNCVCNwo=")
205 |> post("/oauth/token", %{
206 "grant_type" => "authorization_code",
207 "code" => auth.token,
208 "redirect_uri" => app.redirect_uris
211 assert resp = json_response(conn, 400)
212 assert %{"error" => _} = resp
213 refute Map.has_key?(resp, "access_token")
216 test "rejects token exchange for valid credentials belonging to unconfirmed user and confirmation is required" do
217 setting = Pleroma.Config.get([:instance, :account_activation_required])
220 Pleroma.Config.put([:instance, :account_activation_required], true)
221 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
224 password = "testpassword"
225 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
226 info_change = Pleroma.User.Info.confirmation_changeset(user.info, :unconfirmed)
230 |> Ecto.Changeset.change()
231 |> Ecto.Changeset.put_embed(:info, info_change)
234 refute Pleroma.User.auth_active?(user)
236 app = insert(:oauth_app)
240 |> post("/oauth/token", %{
241 "grant_type" => "password",
242 "username" => user.nickname,
243 "password" => password,
244 "client_id" => app.client_id,
245 "client_secret" => app.client_secret
248 assert resp = json_response(conn, 403)
249 assert %{"error" => _} = resp
250 refute Map.has_key?(resp, "access_token")
253 test "rejects an invalid authorization code" do
254 app = insert(:oauth_app)
258 |> post("/oauth/token", %{
259 "grant_type" => "authorization_code",
260 "code" => "Imobviouslyinvalid",
261 "redirect_uri" => app.redirect_uris,
262 "client_id" => app.client_id,
263 "client_secret" => app.client_secret
266 assert resp = json_response(conn, 400)
267 assert %{"error" => _} = json_response(conn, 400)
268 refute Map.has_key?(resp, "access_token")