Merge branch 'fix/sign-in-with-toot' into 'develop'
[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),
64 {:ok, inserted_at} <- DateTime.from_naive(token.inserted_at, "Etc/UTC") do
65 response = %{
66 token_type: "Bearer",
67 access_token: token.token,
68 refresh_token: token.refresh_token,
69 created_at: DateTime.to_unix(inserted_at),
70 expires_in: 60 * 10,
71 scope: "read write follow"
72 }
73
74 json(conn, response)
75 else
76 _error ->
77 put_status(conn, 400)
78 |> json(%{error: "Invalid credentials"})
79 end
80 end
81
82 # TODO
83 # - investigate a way to verify the user wants to grant read/write/follow once scope handling is done
84 def token_exchange(
85 conn,
86 %{"grant_type" => "password", "username" => name, "password" => password} = params
87 ) do
88 with %App{} = app <- get_app_from_request(conn, params),
89 %User{} = user <- User.get_by_nickname_or_email(name),
90 true <- Pbkdf2.checkpw(password, user.password_hash),
91 {:ok, auth} <- Authorization.create_authorization(app, user),
92 {:ok, token} <- Token.exchange_token(app, auth) do
93 response = %{
94 token_type: "Bearer",
95 access_token: token.token,
96 refresh_token: token.refresh_token,
97 expires_in: 60 * 10,
98 scope: "read write follow"
99 }
100
101 json(conn, response)
102 else
103 _error ->
104 put_status(conn, 400)
105 |> json(%{error: "Invalid credentials"})
106 end
107 end
108
109 def token_exchange(
110 conn,
111 %{"grant_type" => "password", "name" => name, "password" => password} = params
112 ) do
113 params =
114 params
115 |> Map.delete("name")
116 |> Map.put("username", name)
117
118 token_exchange(conn, params)
119 end
120
121 def token_revoke(conn, %{"token" => token} = params) do
122 with %App{} = app <- get_app_from_request(conn, params),
123 %Token{} = token <- Repo.get_by(Token, token: token, app_id: app.id),
124 {:ok, %Token{}} <- Repo.delete(token) do
125 json(conn, %{})
126 else
127 _error ->
128 # RFC 7009: invalid tokens [in the request] do not cause an error response
129 json(conn, %{})
130 end
131 end
132
133 defp fix_padding(token) do
134 token
135 |> Base.url_decode64!(padding: false)
136 |> Base.url_encode64()
137 end
138
139 defp get_app_from_request(conn, params) do
140 # Per RFC 6749, HTTP Basic is preferred to body params
141 {client_id, client_secret} =
142 with ["Basic " <> encoded] <- get_req_header(conn, "authorization"),
143 {:ok, decoded} <- Base.decode64(encoded),
144 [id, secret] <-
145 String.split(decoded, ":")
146 |> Enum.map(fn s -> URI.decode_www_form(s) end) do
147 {id, secret}
148 else
149 _ -> {params["client_id"], params["client_secret"]}
150 end
151
152 if client_id && client_secret do
153 Repo.get_by(
154 App,
155 client_id: client_id,
156 client_secret: client_secret
157 )
158 else
159 nil
160 end
161 end
162 end