1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.Web.OAuth
9 alias Pleroma.Web.OAuth.{Authorization, Token, App}
10 alias Pleroma.{Repo, User}
16 action_fallback(Pleroma.Web.OAuth.FallbackController)
18 def authorize(conn, params) do
19 render(conn, "show.html", %{
20 response_type: params["response_type"],
21 client_id: params["client_id"],
22 scopes: scopes(params) || [],
23 redirect_uri: params["redirect_uri"],
24 state: params["state"]
28 def create_authorization(conn, %{
32 "password" => password,
33 "client_id" => client_id,
34 "redirect_uri" => redirect_uri
37 with %User{} = user <- User.get_by_nickname_or_email(name),
38 true <- Pbkdf2.checkpw(password, user.password_hash),
39 {:auth_active, true} <- {:auth_active, User.auth_active?(user)},
40 %App{} = app <- Repo.get_by(App, client_id: client_id),
41 true <- redirect_uri in String.split(app.redirect_uris),
42 scopes <- scopes(params) || app.scopes,
43 [] <- scopes -- app.scopes,
44 true <- Enum.any?(scopes),
45 {:ok, auth} <- Authorization.create_authorization(app, user, scopes) do
46 # Special case: Local MastodonFE.
48 if redirect_uri == "." do
49 mastodon_api_url(conn, :login)
55 redirect_uri == "urn:ietf:wg:oauth:2.0:oob" ->
56 render(conn, "results.html", %{
61 connector = if String.contains?(redirect_uri, "?"), do: "&", else: "?"
62 url = "#{redirect_uri}#{connector}"
63 url_params = %{:code => auth.token}
67 Map.put(url_params, :state, params["state"])
72 url = "#{url}#{Plug.Conn.Query.encode(url_params)}"
74 redirect(conn, external: url)
77 {:auth_active, false} ->
79 |> put_flash(:error, "Account confirmation pending")
80 |> put_status(:forbidden)
88 def token_exchange(conn, %{"grant_type" => "authorization_code"} = params) do
89 with %App{} = app <- get_app_from_request(conn, params),
90 fixed_token = fix_padding(params["code"]),
91 %Authorization{} = auth <-
92 Repo.get_by(Authorization, token: fixed_token, app_id: app.id),
93 {:ok, token} <- Token.exchange_token(app, auth),
94 {:ok, inserted_at} <- DateTime.from_naive(token.inserted_at, "Etc/UTC") do
97 access_token: token.token,
98 refresh_token: token.refresh_token,
99 created_at: DateTime.to_unix(inserted_at),
101 scope: Enum.join(token.scopes)
107 put_status(conn, 400)
108 |> json(%{error: "Invalid credentials"})
114 %{"grant_type" => "password", "username" => name, "password" => password} = params
116 with %App{} = app <- get_app_from_request(conn, params),
117 %User{} = user <- User.get_by_nickname_or_email(name),
118 true <- Pbkdf2.checkpw(password, user.password_hash),
119 {:auth_active, true} <- {:auth_active, User.auth_active?(user)},
120 scopes <- scopes(params) || app.scopes,
121 {:ok, auth} <- Authorization.create_authorization(app, user, scopes),
122 {:ok, token} <- Token.exchange_token(app, auth) do
124 token_type: "Bearer",
125 access_token: token.token,
126 refresh_token: token.refresh_token,
128 scope: Enum.join(token.scopes, " ")
133 {:auth_active, false} ->
135 |> put_status(:forbidden)
136 |> json(%{error: "Account confirmation pending"})
139 put_status(conn, 400)
140 |> json(%{error: "Invalid credentials"})
146 %{"grant_type" => "password", "name" => name, "password" => _password} = params
150 |> Map.delete("name")
151 |> Map.put("username", name)
153 token_exchange(conn, params)
156 def token_revoke(conn, %{"token" => token} = params) do
157 with %App{} = app <- get_app_from_request(conn, params),
158 %Token{} = token <- Repo.get_by(Token, token: token, app_id: app.id),
159 {:ok, %Token{}} <- Repo.delete(token) do
163 # RFC 7009: invalid tokens [in the request] do not cause an error response
168 # XXX - for whatever reason our token arrives urlencoded, but Plug.Conn should be
169 # decoding it. Investigate sometime.
170 defp fix_padding(token) do
173 |> Base.url_decode64!(padding: false)
174 |> Base.url_encode64()
177 defp get_app_from_request(conn, params) do
178 # Per RFC 6749, HTTP Basic is preferred to body params
179 {client_id, client_secret} =
180 with ["Basic " <> encoded] <- get_req_header(conn, "authorization"),
181 {:ok, decoded} <- Base.decode64(encoded),
183 String.split(decoded, ":")
184 |> Enum.map(fn s -> URI.decode_www_form(s) end) do
187 _ -> {params["client_id"], params["client_secret"]}
190 if client_id && client_secret do
193 client_id: client_id,
194 client_secret: client_secret
202 do: OAuth.parse_scopes(params["scopes"] || params["scope"])