Make OAuth token endpoint work with HTTP Basic 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 %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 <- get_app_from_request(conn, params),
60 fixed_token = fix_padding(params["code"]),
61 %Authorization{} = auth <-
62 Repo.get_by(Authorization, token: fixed_token, app_id: app.id),
63 {:ok, token} <- Token.exchange_token(app, auth) do
64 response = %{
65 token_type: "Bearer",
66 access_token: token.token,
67 refresh_token: token.refresh_token,
68 expires_in: 60 * 10,
69 scope: "read write follow"
70 }
71
72 json(conn, response)
73 else
74 _error -> json(conn, %{error: "Invalid credentials"})
75 end
76 end
77
78 # TODO
79 # - investigate a way to verify the user wants to grant read/write/follow once scope handling is done
80 def token_exchange(
81 conn,
82 %{"grant_type" => "password", "name" => name, "password" => password} = params
83 ) do
84 with %App{} = app <- get_app_from_request(conn, params),
85 %User{} = user <- User.get_cached_by_nickname(name),
86 true <- Pbkdf2.checkpw(password, user.password_hash),
87 {:ok, auth} <- Authorization.create_authorization(app, user),
88 {:ok, token} <- Token.exchange_token(app, auth) do
89 response = %{
90 token_type: "Bearer",
91 access_token: token.token,
92 refresh_token: token.refresh_token,
93 expires_in: 60 * 10,
94 scope: "read write follow"
95 }
96
97 json(conn, response)
98 else
99 _error -> json(conn, %{error: "Invalid credentials"})
100 end
101 end
102
103 defp fix_padding(token) do
104 token
105 |> Base.url_decode64!(padding: false)
106 |> Base.url_encode64()
107 end
108
109 defp get_app_from_request(conn, params) do
110 # Per RFC 6749, HTTP Basic is preferred to body params
111 {client_id, client_secret} =
112 with ["Basic " <> encoded] <- get_req_header(conn, "authorization"),
113 {:ok, decoded} <- Base.decode64(encoded),
114 [id, secret] <-
115 String.split(decoded, ":")
116 |> Enum.map(fn s -> URI.decode_www_form(s) end) do
117 {id, secret}
118 else
119 _ -> {params["client_id"], params["client_secret"]}
120 end
121
122 if client_id && client_secret do
123 Repo.get_by(
124 App,
125 client_id: client_id,
126 client_secret: client_secret
127 )
128 else
129 nil
130 end
131 end
132 end