Try host-meta call over https.
[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, App}
5 alias Pleroma.{Repo, User}
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, "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", %{
24 auth: auth
25 }
26 else
27 url = "#{redirect_uri}?code=#{auth.token}"
28 redirect(conn, external: url)
29 end
30 end
31 end
32
33 # TODO
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
39 response = %{
40 token_type: "Bearer",
41 access_token: token.token,
42 refresh_token: token.refresh_token,
43 expires_in: 60 * 10,
44 scope: "read write follow"
45 }
46 json(conn, response)
47 end
48 end
49 end