1 defmodule Pleroma.Web.OAuth.OAuthController do
2 use Pleroma.Web, :controller
4 alias Pleroma.Web.OAuth.{Authorization, Token, App}
5 alias Pleroma.{Repo, User}
11 action_fallback(Pleroma.Web.OAuth.FallbackController)
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"]
23 def create_authorization(conn, %{
27 "password" => password,
28 "client_id" => client_id,
29 "redirect_uri" => redirect_uri
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", %{
41 connector = if String.contains?(redirect_uri, "?"), do: "&", else: "?"
42 url = "#{redirect_uri}#{connector}"
43 url_params = %{:code => auth.token}
47 Map.put(url_params, :state, params["state"])
52 url = "#{url}#{Plug.Conn.Query.encode(url_params)}"
54 redirect(conn, external: url)
60 # - proper scope handling
61 def token_exchange(conn, %{"grant_type" => "authorization_code"} = params) do
62 with %App{} = app <- get_app_from_request(conn, params),
63 fixed_token = fix_padding(params["code"]),
64 %Authorization{} = auth <-
65 Repo.get_by(Authorization, token: fixed_token, app_id: app.id),
66 {:ok, token} <- Token.exchange_token(app, auth),
67 {:ok, inserted_at} <- DateTime.from_naive(token.inserted_at, "Etc/UTC") do
70 access_token: token.token,
71 refresh_token: token.refresh_token,
72 created_at: DateTime.to_unix(inserted_at),
74 scope: "read write follow"
81 |> json(%{error: "Invalid credentials"})
86 # - investigate a way to verify the user wants to grant read/write/follow once scope handling is done
89 %{"grant_type" => "password", "username" => name, "password" => password} = params
91 with %App{} = app <- get_app_from_request(conn, params),
92 %User{} = user <- User.get_by_nickname_or_email(name),
93 true <- Pbkdf2.checkpw(password, user.password_hash),
94 {:ok, auth} <- Authorization.create_authorization(app, user),
95 {:ok, token} <- Token.exchange_token(app, auth) do
98 access_token: token.token,
99 refresh_token: token.refresh_token,
101 scope: "read write follow"
107 put_status(conn, 400)
108 |> json(%{error: "Invalid credentials"})
114 %{"grant_type" => "password", "name" => name, "password" => password} = params
118 |> Map.delete("name")
119 |> Map.put("username", name)
121 token_exchange(conn, params)
124 def token_revoke(conn, %{"token" => token} = params) do
125 with %App{} = app <- get_app_from_request(conn, params),
126 %Token{} = token <- Repo.get_by(Token, token: token, app_id: app.id),
127 {:ok, %Token{}} <- Repo.delete(token) do
131 # RFC 7009: invalid tokens [in the request] do not cause an error response
136 defp fix_padding(token) do
138 |> Base.url_decode64!(padding: false)
139 |> Base.url_encode64()
142 defp get_app_from_request(conn, params) do
143 # Per RFC 6749, HTTP Basic is preferred to body params
144 {client_id, client_secret} =
145 with ["Basic " <> encoded] <- get_req_header(conn, "authorization"),
146 {:ok, decoded} <- Base.decode64(encoded),
148 String.split(decoded, ":")
149 |> Enum.map(fn s -> URI.decode_www_form(s) end) do
152 _ -> {params["client_id"], params["client_secret"]}
155 if client_id && client_secret do
158 client_id: client_id,
159 client_secret: client_secret