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}
8 def authorize(conn, params) do
9 render conn, "show.html", %{
10 response_type: params["response_type"],
11 client_id: params["client_id"],
12 scope: params["scope"],
13 redirect_uri: params["redirect_uri"]
17 def create_authorization(conn, %{"authorization" => %{"name" => name, "password" => password, "client_id" => client_id, "redirect_uri" => redirect_uri}} = params) do
18 with %User{} = user <- User.get_cached_by_nickname(name),
19 true <- Pbkdf2.checkpw(password, user.password_hash),
20 %App{} = app <- Repo.get_by(App, client_id: client_id),
21 {:ok, auth} <- Authorization.create_authorization(app, user) do
22 if redirect_uri == "urn:ietf:wg:oauth:2.0:oob" do
23 render conn, "results.html", %{
27 url = "#{redirect_uri}?code=#{auth.token}"
28 redirect(conn, external: url)
34 # - proper scope handling
35 def token_exchange(conn, %{"grant_type" => "authorization_code"} = params) do
36 with %App{} = app <- Repo.get_by(App, client_id: params["client_id"], client_secret: params["client_secret"]),
37 %Authorization{} = auth <- Repo.get_by(Authorization, token: params["code"], app_id: app.id),
38 {:ok, token} <- Token.exchange_token(app, auth) do
41 access_token: token.token,
42 refresh_token: token.refresh_token,
44 scope: "read write follow"