Add very basic oauth and mastodon api support.
[akkoma] / lib / pleroma / web / oauth / oauth_controller.ex
1 defmodule Pleroma.Web.OAuth.OAuthController do
2 use Pleroma.Web, :controller
3
4 alias Pleroma.Web.OAuth.{Authorization, Token}
5 alias Pleroma.{Repo, User, App}
6 alias Comeonin.Pbkdf2
7
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"]
14 }
15 end
16
17 def create_authorization(conn, %{"authorization" => %{"name" => name, "password" => password, "client_id" => client_id}} = params) do
18 with %User{} = user <- User.get_cached_by_nickname(name),
19 true <- Pbkdf2.checkpw(password, user.password_hash),
20 %App{} = app <- Pleroma.Repo.get_by(Pleroma.App, client_id: client_id),
21 {:ok, auth} <- Authorization.create_authorization(app, user) do
22 render conn, "results.html", %{
23 auth: auth
24 }
25 end
26 end
27
28 # TODO CRITICAL
29 # - Check validity of auth token
30 def token_exchange(conn, %{"grant_type" => "authorization_code"} = params) do
31 with %App{} = app <- Repo.get_by(App, client_id: params["client_id"], client_secret: params["client_secret"]),
32 %Authorization{} = auth <- Repo.get_by(Authorization, token: params["code"], app_id: app.id),
33 {:ok, token} <- Token.create_token(app, Repo.get(User, auth.user_id)) do
34 response = %{
35 token_type: "Bearer",
36 access_token: token.token,
37 refresh_token: token.refresh_token,
38 expires_in: 60 * 10,
39 scope: "read write follow"
40 }
41 json(conn, response)
42 end
43 end
44 end