[#114] Account confirmation email, registration as unconfirmed (config-based), auth...
[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 true <- User.auth_active?(user),
35 %App{} = app <- Repo.get_by(App, client_id: client_id),
36 {:ok, auth} <- Authorization.create_authorization(app, user) do
37 # Special case: Local MastodonFE.
38 redirect_uri =
39 if redirect_uri == "." do
40 mastodon_api_url(conn, :login)
41 else
42 redirect_uri
43 end
44
45 cond do
46 redirect_uri == "urn:ietf:wg:oauth:2.0:oob" ->
47 render(conn, "results.html", %{
48 auth: auth
49 })
50
51 true ->
52 connector = if String.contains?(redirect_uri, "?"), do: "&", else: "?"
53 url = "#{redirect_uri}#{connector}"
54 url_params = %{:code => auth.token}
55
56 url_params =
57 if params["state"] do
58 Map.put(url_params, :state, params["state"])
59 else
60 url_params
61 end
62
63 url = "#{url}#{Plug.Conn.Query.encode(url_params)}"
64
65 redirect(conn, external: url)
66 end
67 end
68 end
69
70 # TODO
71 # - proper scope handling
72 def token_exchange(conn, %{"grant_type" => "authorization_code"} = params) do
73 with %App{} = app <- get_app_from_request(conn, params),
74 fixed_token = fix_padding(params["code"]),
75 %Authorization{} = auth <-
76 Repo.get_by(Authorization, token: fixed_token, app_id: app.id),
77 {:ok, token} <- Token.exchange_token(app, auth),
78 {:ok, inserted_at} <- DateTime.from_naive(token.inserted_at, "Etc/UTC") do
79 response = %{
80 token_type: "Bearer",
81 access_token: token.token,
82 refresh_token: token.refresh_token,
83 created_at: DateTime.to_unix(inserted_at),
84 expires_in: 60 * 10,
85 scope: "read write follow"
86 }
87
88 json(conn, response)
89 else
90 _error ->
91 put_status(conn, 400)
92 |> json(%{error: "Invalid credentials"})
93 end
94 end
95
96 # TODO
97 # - investigate a way to verify the user wants to grant read/write/follow once scope handling is done
98 def token_exchange(
99 conn,
100 %{"grant_type" => "password", "username" => name, "password" => password} = params
101 ) do
102 with %App{} = app <- get_app_from_request(conn, params),
103 %User{} = user <- User.get_by_nickname_or_email(name),
104 true <- Pbkdf2.checkpw(password, user.password_hash),
105 true <- User.auth_active?(user),
106 {:ok, auth} <- Authorization.create_authorization(app, user),
107 {:ok, token} <- Token.exchange_token(app, auth) do
108 response = %{
109 token_type: "Bearer",
110 access_token: token.token,
111 refresh_token: token.refresh_token,
112 expires_in: 60 * 10,
113 scope: "read write follow"
114 }
115
116 json(conn, response)
117 else
118 _error ->
119 put_status(conn, 400)
120 |> json(%{error: "Invalid credentials"})
121 end
122 end
123
124 def token_exchange(
125 conn,
126 %{"grant_type" => "password", "name" => name, "password" => _password} = params
127 ) do
128 params =
129 params
130 |> Map.delete("name")
131 |> Map.put("username", name)
132
133 token_exchange(conn, params)
134 end
135
136 def token_revoke(conn, %{"token" => token} = params) do
137 with %App{} = app <- get_app_from_request(conn, params),
138 %Token{} = token <- Repo.get_by(Token, token: token, app_id: app.id),
139 {:ok, %Token{}} <- Repo.delete(token) do
140 json(conn, %{})
141 else
142 _error ->
143 # RFC 7009: invalid tokens [in the request] do not cause an error response
144 json(conn, %{})
145 end
146 end
147
148 # XXX - for whatever reason our token arrives urlencoded, but Plug.Conn should be
149 # decoding it. Investigate sometime.
150 defp fix_padding(token) do
151 token
152 |> URI.decode()
153 |> Base.url_decode64!(padding: false)
154 |> Base.url_encode64()
155 end
156
157 defp get_app_from_request(conn, params) do
158 # Per RFC 6749, HTTP Basic is preferred to body params
159 {client_id, client_secret} =
160 with ["Basic " <> encoded] <- get_req_header(conn, "authorization"),
161 {:ok, decoded} <- Base.decode64(encoded),
162 [id, secret] <-
163 String.split(decoded, ":")
164 |> Enum.map(fn s -> URI.decode_www_form(s) end) do
165 {id, secret}
166 else
167 _ -> {params["client_id"], params["client_secret"]}
168 end
169
170 if client_id && client_secret do
171 Repo.get_by(
172 App,
173 client_id: client_id,
174 client_secret: client_secret
175 )
176 else
177 nil
178 end
179 end
180 end