oauth: implement grant_type=password for single-page apps
[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 plug :fetch_session
9 plug :fetch_flash
10
11 action_fallback Pleroma.Web.OAuth.FallbackController
12
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"]
20 }
21 end
22
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", %{
30 auth: auth
31 }
32 else
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"]}"
37 else
38 url
39 end
40 redirect(conn, external: url)
41 end
42 end
43 end
44
45 # TODO
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
52 response = %{
53 token_type: "Bearer",
54 access_token: token.token,
55 refresh_token: token.refresh_token,
56 expires_in: 60 * 10,
57 scope: "read write follow"
58 }
59 json(conn, response)
60 else
61 _error -> json(conn, %{error: "Invalid credentials"})
62 end
63 end
64
65 # TODO
66 # - investigate a way to verify the user wants to grant read/write/follow once scope handling is done
67 def token_exchange(conn, %{"grant_type" => "password", "name" => name, "password" => password} = params) do
68 with %App{} = app <- Repo.get_by(App, client_id: params["client_id"], client_secret: params["client_secret"]),
69 %User{} = user <- User.get_cached_by_nickname(name),
70 true <- Pbkdf2.checkpw(password, user.password_hash),
71 {:ok, auth} <- Authorization.create_authorization(app, user),
72 {:ok, token} <- Token.exchange_token(app, auth) do
73 response = %{
74 token_type: "Bearer",
75 access_token: token.token,
76 refresh_token: token.refresh_token,
77 expires_in: 60 * 10,
78 scope: "read write follow"
79 }
80 json(conn, response)
81 else
82 _error -> json(conn, %{error: "Invalid credentials"})
83 end
84 end
85
86 defp fix_padding(token) do
87 token
88 |> Base.url_decode64!(padding: false)
89 |> Base.url_encode64
90 end
91 end