2017-09-06 11:06:25 -06:00
|
|
|
defmodule Pleroma.Web.OAuth.OAuthController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
2017-09-07 00:58:10 -06:00
|
|
|
alias Pleroma.Web.OAuth.{Authorization, Token, App}
|
|
|
|
alias Pleroma.{Repo, User}
|
2017-09-06 11:06:25 -06:00
|
|
|
alias Comeonin.Pbkdf2
|
|
|
|
|
2018-02-08 09:57:30 -07:00
|
|
|
plug :fetch_session
|
|
|
|
plug :fetch_flash
|
|
|
|
|
|
|
|
action_fallback Pleroma.Web.OAuth.FallbackController
|
|
|
|
|
2017-09-06 11:06:25 -06:00
|
|
|
def authorize(conn, params) do
|
|
|
|
render conn, "show.html", %{
|
|
|
|
response_type: params["response_type"],
|
|
|
|
client_id: params["client_id"],
|
|
|
|
scope: params["scope"],
|
2017-09-14 01:29:51 -06:00
|
|
|
redirect_uri: params["redirect_uri"],
|
|
|
|
state: params["state"]
|
2017-09-06 11:06:25 -06:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-09-16 03:37:55 -06:00
|
|
|
def create_authorization(conn, %{"authorization" => %{"name" => name, "password" => password, "client_id" => client_id, "redirect_uri" => redirect_uri} = params}) do
|
2017-09-06 11:06:25 -06:00
|
|
|
with %User{} = user <- User.get_cached_by_nickname(name),
|
|
|
|
true <- Pbkdf2.checkpw(password, user.password_hash),
|
2017-09-07 00:58:10 -06:00
|
|
|
%App{} = app <- Repo.get_by(App, client_id: client_id),
|
2017-09-06 11:06:25 -06:00
|
|
|
{:ok, auth} <- Authorization.create_authorization(app, user) do
|
2017-09-09 11:03:57 -06:00
|
|
|
if redirect_uri == "urn:ietf:wg:oauth:2.0:oob" do
|
|
|
|
render conn, "results.html", %{
|
|
|
|
auth: auth
|
|
|
|
}
|
|
|
|
else
|
2017-11-10 10:24:50 -07:00
|
|
|
connector = if String.contains?(redirect_uri, "?"), do: "&", else: "?"
|
|
|
|
url = "#{redirect_uri}#{connector}code=#{auth.token}"
|
2017-09-14 01:29:51 -06:00
|
|
|
url = if params["state"] do
|
|
|
|
url <> "&state=#{params["state"]}"
|
|
|
|
else
|
|
|
|
url
|
|
|
|
end
|
2017-09-09 11:03:57 -06:00
|
|
|
redirect(conn, external: url)
|
|
|
|
end
|
2017-09-06 11:06:25 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-09 04:10:46 -06:00
|
|
|
# TODO
|
|
|
|
# - proper scope handling
|
2017-09-06 11:06:25 -06:00
|
|
|
def token_exchange(conn, %{"grant_type" => "authorization_code"} = params) do
|
|
|
|
with %App{} = app <- Repo.get_by(App, client_id: params["client_id"], client_secret: params["client_secret"]),
|
2017-11-06 12:51:31 -07:00
|
|
|
fixed_token = fix_padding(params["code"]),
|
|
|
|
%Authorization{} = auth <- Repo.get_by(Authorization, token: fixed_token, app_id: app.id),
|
2017-09-09 04:10:46 -06:00
|
|
|
{:ok, token} <- Token.exchange_token(app, auth) do
|
2017-09-06 11:06:25 -06:00
|
|
|
response = %{
|
|
|
|
token_type: "Bearer",
|
|
|
|
access_token: token.token,
|
|
|
|
refresh_token: token.refresh_token,
|
|
|
|
expires_in: 60 * 10,
|
|
|
|
scope: "read write follow"
|
|
|
|
}
|
|
|
|
json(conn, response)
|
2017-11-06 12:51:31 -07:00
|
|
|
else
|
|
|
|
_error -> json(conn, %{error: "Invalid credentials"})
|
2017-09-06 11:06:25 -06:00
|
|
|
end
|
|
|
|
end
|
2017-11-06 12:51:31 -07:00
|
|
|
|
|
|
|
defp fix_padding(token) do
|
|
|
|
token
|
|
|
|
|> Base.url_decode64!(padding: false)
|
|
|
|
|> Base.url_encode64
|
|
|
|
end
|
2017-09-06 11:06:25 -06:00
|
|
|
end
|