1 defmodule Pleroma.Web.OAuth.OAuthController do
2 use Pleroma.Web, :controller
4 alias Pleroma.Web.OAuth.{Authorization, Token, App}
5 alias Pleroma.{Repo, User}
11 action_fallback Pleroma.Web.OAuth.FallbackController
13 def authorize(conn, params) do
14 render conn, "show.html", %{
15 response_type: params["response_type"],
16 client_id: params["client_id"],
17 scope: params["scope"],
18 redirect_uri: params["redirect_uri"],
19 state: params["state"]
23 def create_authorization(conn, %{"authorization" => %{"name" => name, "password" => password, "client_id" => client_id, "redirect_uri" => redirect_uri} = params}) do
24 with %User{} = user <- User.get_cached_by_nickname(name),
25 true <- Pbkdf2.checkpw(password, user.password_hash),
26 %App{} = app <- Repo.get_by(App, client_id: client_id),
27 {:ok, auth} <- Authorization.create_authorization(app, user) do
28 if redirect_uri == "urn:ietf:wg:oauth:2.0:oob" do
29 render conn, "results.html", %{
33 connector = if String.contains?(redirect_uri, "?"), do: "&", else: "?"
34 url = "#{redirect_uri}#{connector}code=#{auth.token}"
35 url = if params["state"] do
36 url <> "&state=#{params["state"]}"
40 redirect(conn, external: url)
46 # - proper scope handling
47 def token_exchange(conn, %{"grant_type" => "authorization_code"} = params) do
48 with %App{} = app <- Repo.get_by(App, client_id: params["client_id"], client_secret: params["client_secret"]),
49 fixed_token = fix_padding(params["code"]),
50 %Authorization{} = auth <- Repo.get_by(Authorization, token: fixed_token, app_id: app.id),
51 {:ok, token} <- Token.exchange_token(app, auth) do
54 access_token: token.token,
55 refresh_token: token.refresh_token,
57 scope: "read write follow"
61 _error -> json(conn, %{error: "Invalid credentials"})
65 defp fix_padding(token) do
67 |> Base.url_decode64!(padding: false)