Formatting fixes.
[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, %{
24 "authorization" =>
25 %{
26 "name" => name,
27 "password" => password,
28 "client_id" => client_id,
29 "redirect_uri" => redirect_uri
30 } = params
31 }) do
32 with %User{} = user <- User.get_by_nickname_or_email(name),
33 true <- Pbkdf2.checkpw(password, user.password_hash),
34 %App{} = app <- Repo.get_by(App, client_id: client_id),
35 {:ok, auth} <- Authorization.create_authorization(app, user) do
36 if redirect_uri == "urn:ietf:wg:oauth:2.0:oob" do
37 render(conn, "results.html", %{
38 auth: auth
39 })
40 else
41 connector = if String.contains?(redirect_uri, "?"), do: "&", else: "?"
42 url = "#{redirect_uri}#{connector}code=#{auth.token}"
43
44 url =
45 if params["state"] do
46 url <> "&state=#{params["state"]}"
47 else
48 url
49 end
50
51 redirect(conn, external: url)
52 end
53 end
54 end
55
56 # TODO
57 # - proper scope handling
58 def token_exchange(conn, %{"grant_type" => "authorization_code"} = params) do
59 with %App{} = app <-
60 Repo.get_by(
61 App,
62 client_id: params["client_id"],
63 client_secret: params["client_secret"]
64 ),
65 fixed_token = fix_padding(params["code"]),
66 %Authorization{} = auth <-
67 Repo.get_by(Authorization, token: fixed_token, app_id: app.id),
68 {:ok, token} <- Token.exchange_token(app, auth) do
69 response = %{
70 token_type: "Bearer",
71 access_token: token.token,
72 refresh_token: token.refresh_token,
73 expires_in: 60 * 10,
74 scope: "read write follow"
75 }
76
77 json(conn, response)
78 else
79 _error -> json(conn, %{error: "Invalid credentials"})
80 end
81 end
82
83 # TODO
84 # - investigate a way to verify the user wants to grant read/write/follow once scope handling is done
85 def token_exchange(
86 conn,
87 %{"grant_type" => "password", "name" => name, "password" => password} = params
88 ) do
89 with %App{} = app <-
90 Repo.get_by(
91 App,
92 client_id: params["client_id"],
93 client_secret: params["client_secret"]
94 ),
95 %User{} = user <- User.get_cached_by_nickname(name),
96 true <- Pbkdf2.checkpw(password, user.password_hash),
97 {:ok, auth} <- Authorization.create_authorization(app, user),
98 {:ok, token} <- Token.exchange_token(app, auth) do
99 response = %{
100 token_type: "Bearer",
101 access_token: token.token,
102 refresh_token: token.refresh_token,
103 expires_in: 60 * 10,
104 scope: "read write follow"
105 }
106
107 json(conn, response)
108 else
109 _error -> json(conn, %{error: "Invalid credentials"})
110 end
111 end
112
113 defp fix_padding(token) do
114 token
115 |> Base.url_decode64!(padding: false)
116 |> Base.url_encode64()
117 end
118 end