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.OAuthController do
6 use Pleroma.Web, :controller
8 alias Pleroma.Helpers.UriHelper
9 alias Pleroma.Plugs.RateLimiter
10 alias Pleroma.Registration
13 alias Pleroma.Web.Auth.Authenticator
14 alias Pleroma.Web.ControllerHelper
15 alias Pleroma.Web.OAuth.App
16 alias Pleroma.Web.OAuth.Authorization
17 alias Pleroma.Web.OAuth.Scopes
18 alias Pleroma.Web.OAuth.Token
19 alias Pleroma.Web.OAuth.Token.Strategy.RefreshToken
20 alias Pleroma.Web.OAuth.Token.Strategy.Revoke, as: RevokeToken
24 if Pleroma.Config.oauth_consumer_enabled?(), do: plug(Ueberauth)
29 plug(:skip_plug, [Pleroma.Plugs.OAuthScopesPlug, Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug])
31 plug(RateLimiter, [name: :authentication] when action == :create_authorization)
33 action_fallback(Pleroma.Web.OAuth.FallbackController)
35 @oob_token_redirect_uri "urn:ietf:wg:oauth:2.0:oob"
37 # Note: this definition is only called from error-handling methods with `conn.params` as 2nd arg
38 def authorize(%Plug.Conn{} = conn, %{"authorization" => _} = params) do
39 {auth_attrs, params} = Map.pop(params, "authorization")
40 authorize(conn, Map.merge(params, auth_attrs))
43 def authorize(%Plug.Conn{assigns: %{token: %Token{}}} = conn, %{"force_login" => _} = params) do
44 if ControllerHelper.truthy_param?(params["force_login"]) do
45 do_authorize(conn, params)
47 handle_existing_authorization(conn, params)
51 # Note: the token is set in oauth_plug, but the token and client do not always go together.
52 # For example, MastodonFE's token is set if user requests with another client,
53 # after user already authorized to MastodonFE.
54 # So we have to check client and token.
56 %Plug.Conn{assigns: %{token: %Token{} = token}} = conn,
57 %{"client_id" => client_id} = params
59 with %Token{} = t <- Repo.get_by(Token, token: token.token) |> Repo.preload(:app),
60 ^client_id <- t.app.client_id do
61 handle_existing_authorization(conn, params)
63 _ -> do_authorize(conn, params)
67 def authorize(%Plug.Conn{} = conn, params), do: do_authorize(conn, params)
69 defp do_authorize(%Plug.Conn{} = conn, params) do
70 app = Repo.get_by(App, client_id: params["client_id"])
71 available_scopes = (app && app.scopes) || []
72 scopes = Scopes.fetch_scopes(params, available_scopes)
74 # Note: `params` might differ from `conn.params`; use `@params` not `@conn.params` in template
75 render(conn, Authenticator.auth_template(), %{
76 response_type: params["response_type"],
77 client_id: params["client_id"],
78 available_scopes: available_scopes,
80 redirect_uri: params["redirect_uri"],
81 state: params["state"],
86 defp handle_existing_authorization(
87 %Plug.Conn{assigns: %{token: %Token{} = token}} = conn,
88 %{"redirect_uri" => @oob_token_redirect_uri}
90 render(conn, "oob_token_exists.html", %{token: token})
93 defp handle_existing_authorization(
94 %Plug.Conn{assigns: %{token: %Token{} = token}} = conn,
97 app = Repo.preload(token, :app).app
100 if is_binary(params["redirect_uri"]) do
101 params["redirect_uri"]
103 default_redirect_uri(app)
106 if redirect_uri in String.split(app.redirect_uris) do
107 redirect_uri = redirect_uri(conn, redirect_uri)
108 url_params = %{access_token: token.token}
109 url_params = UriHelper.append_param_if_present(url_params, :state, params["state"])
110 url = UriHelper.append_uri_params(redirect_uri, url_params)
111 redirect(conn, external: url)
114 |> put_flash(:error, dgettext("errors", "Unlisted redirect_uri."))
115 |> redirect(external: redirect_uri(conn, redirect_uri))
119 def create_authorization(
121 %{"authorization" => _} = params,
124 with {:ok, auth} <- do_create_authorization(conn, params, opts[:user]) do
125 after_create_authorization(conn, auth, params)
128 handle_create_authorization_error(conn, error, params)
132 def after_create_authorization(%Plug.Conn{} = conn, %Authorization{} = auth, %{
133 "authorization" => %{"redirect_uri" => @oob_token_redirect_uri}
135 render(conn, "oob_authorization_created.html", %{auth: auth})
138 def after_create_authorization(%Plug.Conn{} = conn, %Authorization{} = auth, %{
139 "authorization" => %{"redirect_uri" => redirect_uri} = auth_attrs
141 app = Repo.preload(auth, :app).app
143 # An extra safety measure before we redirect (also done in `do_create_authorization/2`)
144 if redirect_uri in String.split(app.redirect_uris) do
145 redirect_uri = redirect_uri(conn, redirect_uri)
146 url_params = %{code: auth.token}
147 url_params = UriHelper.append_param_if_present(url_params, :state, auth_attrs["state"])
148 url = UriHelper.append_uri_params(redirect_uri, url_params)
149 redirect(conn, external: url)
152 |> put_flash(:error, dgettext("errors", "Unlisted redirect_uri."))
153 |> redirect(external: redirect_uri(conn, redirect_uri))
157 defp handle_create_authorization_error(
159 {:error, scopes_issue},
160 %{"authorization" => _} = params
162 when scopes_issue in [:unsupported_scopes, :missing_scopes] do
163 # Per https://github.com/tootsuite/mastodon/blob/
164 # 51e154f5e87968d6bb115e053689767ab33e80cd/app/controllers/api/base_controller.rb#L39
166 |> put_flash(:error, dgettext("errors", "This action is outside the authorized scopes"))
167 |> put_status(:unauthorized)
171 defp handle_create_authorization_error(
173 {:account_status, :confirmation_pending},
174 %{"authorization" => _} = params
177 |> put_flash(:error, dgettext("errors", "Your login is missing a confirmed e-mail address"))
178 |> put_status(:forbidden)
182 defp handle_create_authorization_error(
184 {:account_status, :password_reset_pending},
185 %{"authorization" => _} = params
188 |> put_flash(:error, dgettext("errors", "Password reset is required"))
189 |> put_status(:forbidden)
193 defp handle_create_authorization_error(
195 {:account_status, :deactivated},
196 %{"authorization" => _} = params
199 |> put_flash(:error, dgettext("errors", "Your account is currently disabled"))
200 |> put_status(:forbidden)
204 defp handle_create_authorization_error(%Plug.Conn{} = conn, error, %{"authorization" => _}) do
205 Authenticator.handle_error(conn, error)
208 @doc "Renew access_token with refresh_token"
211 %{"grant_type" => "refresh_token", "refresh_token" => token} = _params
213 with {:ok, app} <- Token.Utils.fetch_app(conn),
214 {:ok, %{user: user} = token} <- Token.get_by_refresh_token(app, token),
215 {:ok, token} <- RefreshToken.grant(token) do
216 response_attrs = %{created_at: Token.Utils.format_created_at(token)}
218 json(conn, Token.Response.build(user, token, response_attrs))
220 _error -> render_invalid_credentials_error(conn)
224 def token_exchange(%Plug.Conn{} = conn, %{"grant_type" => "authorization_code"} = params) do
225 with {:ok, app} <- Token.Utils.fetch_app(conn),
226 fixed_token = Token.Utils.fix_padding(params["code"]),
227 {:ok, auth} <- Authorization.get_by_token(app, fixed_token),
228 %User{} = user <- User.get_cached_by_id(auth.user_id),
229 {:ok, token} <- Token.exchange_token(app, auth) do
230 response_attrs = %{created_at: Token.Utils.format_created_at(token)}
232 json(conn, Token.Response.build(user, token, response_attrs))
234 _error -> render_invalid_credentials_error(conn)
240 %{"grant_type" => "password"} = params
242 with {:ok, %User{} = user} <- Authenticator.get_user(conn),
243 {:ok, app} <- Token.Utils.fetch_app(conn),
244 {:account_status, :active} <- {:account_status, User.account_status(user)},
245 {:ok, scopes} <- validate_scopes(app, params),
246 {:ok, auth} <- Authorization.create_authorization(app, user, scopes),
247 {:ok, token} <- Token.exchange_token(app, auth) do
248 json(conn, Token.Response.build(user, token))
251 handle_token_exchange_error(conn, error)
257 %{"grant_type" => "password", "name" => name, "password" => _password} = params
261 |> Map.delete("name")
262 |> Map.put("username", name)
264 token_exchange(conn, params)
267 def token_exchange(%Plug.Conn{} = conn, %{"grant_type" => "client_credentials"} = _params) do
268 with {:ok, app} <- Token.Utils.fetch_app(conn),
269 {:ok, auth} <- Authorization.create_authorization(app, %User{}),
270 {:ok, token} <- Token.exchange_token(app, auth) do
271 json(conn, Token.Response.build_for_client_credentials(token))
273 _error -> render_invalid_credentials_error(conn)
278 def token_exchange(%Plug.Conn{} = conn, params), do: bad_request(conn, params)
280 defp handle_token_exchange_error(%Plug.Conn{} = conn, {:account_status, :deactivated}) do
284 "Your account is currently disabled",
286 "account_is_disabled"
290 defp handle_token_exchange_error(
292 {:account_status, :password_reset_pending}
297 "Password reset is required",
299 "password_reset_required"
303 defp handle_token_exchange_error(%Plug.Conn{} = conn, {:account_status, :confirmation_pending}) do
307 "Your login is missing a confirmed e-mail address",
309 "missing_confirmed_email"
313 defp handle_token_exchange_error(%Plug.Conn{} = conn, _error) do
314 render_invalid_credentials_error(conn)
317 def token_revoke(%Plug.Conn{} = conn, %{"token" => _token} = params) do
318 with {:ok, app} <- Token.Utils.fetch_app(conn),
319 {:ok, _token} <- RevokeToken.revoke(app, params) do
323 # RFC 7009: invalid tokens [in the request] do not cause an error response
328 def token_revoke(%Plug.Conn{} = conn, params), do: bad_request(conn, params)
330 # Response for bad request
331 defp bad_request(%Plug.Conn{} = conn, _) do
332 render_error(conn, :internal_server_error, "Bad request")
335 @doc "Prepares OAuth request to provider for Ueberauth"
336 def prepare_request(%Plug.Conn{} = conn, %{
337 "provider" => provider,
338 "authorization" => auth_attrs
342 |> Scopes.fetch_scopes([])
343 |> Scopes.to_string()
347 |> Map.delete("scopes")
348 |> Map.put("scope", scope)
353 |> Map.drop(~w(scope scopes client_id redirect_uri))
354 |> Map.put("state", state)
356 # Handing the request to Ueberauth
357 redirect(conn, to: o_auth_path(conn, :request, provider, params))
360 def request(%Plug.Conn{} = conn, params) do
362 if params["provider"] do
363 dgettext("errors", "Unsupported OAuth provider: %{provider}.",
364 provider: params["provider"]
367 dgettext("errors", "Bad OAuth request.")
371 |> put_flash(:error, message)
375 def callback(%Plug.Conn{assigns: %{ueberauth_failure: failure}} = conn, params) do
376 params = callback_params(params)
377 messages = for e <- Map.get(failure, :errors, []), do: e.message
378 message = Enum.join(messages, "; ")
383 dgettext("errors", "Failed to authenticate: %{message}.", message: message)
385 |> redirect(external: redirect_uri(conn, params["redirect_uri"]))
388 def callback(%Plug.Conn{} = conn, params) do
389 params = callback_params(params)
391 with {:ok, registration} <- Authenticator.get_registration(conn) do
392 auth_attrs = Map.take(params, ~w(client_id redirect_uri scope scopes state))
394 case Repo.get_assoc(registration, :user) do
396 create_authorization(conn, %{"authorization" => auth_attrs}, user: user)
399 registration_params =
400 Map.merge(auth_attrs, %{
401 "nickname" => Registration.nickname(registration),
402 "email" => Registration.email(registration)
406 |> put_session_registration_id(registration.id)
407 |> registration_details(%{"authorization" => registration_params})
411 Logger.debug(inspect(["OAUTH_ERROR", error, conn.assigns]))
414 |> put_flash(:error, dgettext("errors", "Failed to set up user account."))
415 |> redirect(external: redirect_uri(conn, params["redirect_uri"]))
419 defp callback_params(%{"state" => state} = params) do
420 Map.merge(params, Jason.decode!(state))
423 def registration_details(%Plug.Conn{} = conn, %{"authorization" => auth_attrs}) do
424 render(conn, "register.html", %{
425 client_id: auth_attrs["client_id"],
426 redirect_uri: auth_attrs["redirect_uri"],
427 state: auth_attrs["state"],
428 scopes: Scopes.fetch_scopes(auth_attrs, []),
429 nickname: auth_attrs["nickname"],
430 email: auth_attrs["email"]
434 def register(%Plug.Conn{} = conn, %{"authorization" => _, "op" => "connect"} = params) do
435 with registration_id when not is_nil(registration_id) <- get_session_registration_id(conn),
436 %Registration{} = registration <- Repo.get(Registration, registration_id),
437 {_, {:ok, auth}} <- {:create_authorization, do_create_authorization(conn, params)},
438 %User{} = user <- Repo.preload(auth, :user).user,
439 {:ok, _updated_registration} <- Registration.bind_to_user(registration, user) do
441 |> put_session_registration_id(nil)
442 |> after_create_authorization(auth, params)
444 {:create_authorization, error} ->
445 {:register, handle_create_authorization_error(conn, error, params)}
448 {:register, :generic_error}
452 def register(%Plug.Conn{} = conn, %{"authorization" => _, "op" => "register"} = params) do
453 with registration_id when not is_nil(registration_id) <- get_session_registration_id(conn),
454 %Registration{} = registration <- Repo.get(Registration, registration_id),
455 {:ok, user} <- Authenticator.create_from_registration(conn, registration) do
457 |> put_session_registration_id(nil)
458 |> create_authorization(
463 {:error, changeset} ->
465 Enum.map(changeset.errors, fn {field, {error, _}} ->
473 "ap_id has already been taken",
474 "nickname has already been taken"
478 |> put_status(:forbidden)
479 |> put_flash(:error, "Error: #{message}.")
480 |> registration_details(params)
483 {:register, :generic_error}
487 defp do_create_authorization(
492 "client_id" => client_id,
493 "redirect_uri" => redirect_uri
498 with {_, {:ok, %User{} = user}} <-
499 {:get_user, (user && {:ok, user}) || Authenticator.get_user(conn)},
500 %App{} = app <- Repo.get_by(App, client_id: client_id),
501 true <- redirect_uri in String.split(app.redirect_uris),
502 {:ok, scopes} <- validate_scopes(app, auth_attrs),
503 {:account_status, :active} <- {:account_status, User.account_status(user)} do
504 Authorization.create_authorization(app, user, scopes)
508 # Special case: Local MastodonFE
509 defp redirect_uri(%Plug.Conn{} = conn, "."), do: auth_url(conn, :login)
511 defp redirect_uri(%Plug.Conn{}, redirect_uri), do: redirect_uri
513 defp get_session_registration_id(%Plug.Conn{} = conn), do: get_session(conn, :registration_id)
515 defp put_session_registration_id(%Plug.Conn{} = conn, registration_id),
516 do: put_session(conn, :registration_id, registration_id)
518 @spec validate_scopes(App.t(), map()) ::
519 {:ok, list()} | {:error, :missing_scopes | :unsupported_scopes}
520 defp validate_scopes(%App{} = app, params) do
522 |> Scopes.fetch_scopes(app.scopes)
523 |> Scopes.validate(app.scopes)
526 def default_redirect_uri(%App{} = app) do
532 defp render_invalid_credentials_error(conn) do
533 render_error(conn, :bad_request, "Invalid credentials")