diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/plugs/oauth_plug.ex | 8 | ||||
-rw-r--r-- | lib/pleroma/web/endpoint.ex | 2 | ||||
-rw-r--r-- | lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 115 | ||||
-rw-r--r-- | lib/pleroma/web/mastodon_api/views/mastodon_view.ex | 5 | ||||
-rw-r--r-- | lib/pleroma/web/router.ex | 15 | ||||
-rw-r--r-- | lib/pleroma/web/templates/mastodon_api/mastodon/index.html.eex | 18 | ||||
-rw-r--r-- | lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex | 10 |
7 files changed, 168 insertions, 5 deletions
diff --git a/lib/pleroma/plugs/oauth_plug.ex b/lib/pleroma/plugs/oauth_plug.ex index fc2a907a2..8366e35af 100644 --- a/lib/pleroma/plugs/oauth_plug.ex +++ b/lib/pleroma/plugs/oauth_plug.ex @@ -10,8 +10,12 @@ defmodule Pleroma.Plugs.OAuthPlug do def call(%{assigns: %{user: %User{}}} = conn, _), do: conn def call(conn, opts) do - with ["Bearer " <> header] <- get_req_header(conn, "authorization"), - %Token{user_id: user_id} <- Repo.get_by(Token, token: header), + token = case get_req_header(conn, "authorization") do + ["Bearer " <> header] -> header + _ -> get_session(conn, :oauth_token) + end + with token when not is_nil(token) <- token, + %Token{user_id: user_id} <- Repo.get_by(Token, token: token), %User{} = user <- Repo.get(User, user_id) do conn |> assign(:user, user) diff --git a/lib/pleroma/web/endpoint.ex b/lib/pleroma/web/endpoint.ex index dc1ba2a05..b57cf3917 100644 --- a/lib/pleroma/web/endpoint.ex +++ b/lib/pleroma/web/endpoint.ex @@ -12,7 +12,7 @@ defmodule Pleroma.Web.Endpoint do at: "/media", from: "uploads", gzip: false plug Plug.Static, at: "/", from: :pleroma, - only: ~w(index.html static finmoji emoji) + only: ~w(index.html static finmoji emoji packs sounds sw.js) # Code reloading can be explicitly enabled under the # :code_reloader configuration of your endpoint. diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 9f50dc596..41fbe55e7 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -1,12 +1,13 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do use Pleroma.Web, :controller alias Pleroma.{Repo, Activity, User, Notification} - alias Pleroma.Web.OAuth.App alias Pleroma.Web - alias Pleroma.Web.MastodonAPI.{StatusView, AccountView} + alias Pleroma.Web.MastodonAPI.{StatusView, AccountView, MastodonView} alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.TwitterAPI.TwitterAPI alias Pleroma.Web.{CommonAPI, OStatus} + alias Pleroma.Web.OAuth.{Authorization, Token, App} + alias Comeonin.Pbkdf2 import Ecto.Query import Logger @@ -468,6 +469,116 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity}) end + def index(%{assigns: %{user: user}} = conn, _params) do + token = conn + |> get_session(:oauth_token) + + if user && token do + accounts = Map.put(%{}, user.id, AccountView.render("account.json", %{user: user})) + initial_state = %{ + meta: %{ + streaming_api_base_url: String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"), + access_token: token, + locale: "en", + domain: Pleroma.Web.Endpoint.host(), + admin: "1", + me: "#{user.id}", + unfollow_modal: false, + boost_modal: false, + delete_modal: true, + auto_play_gif: false, + reduce_motion: false + }, + compose: %{ + me: "#{user.id}", + default_privacy: "public", + default_sensitive: false + }, + media_attachments: %{ + accept_content_types: [ + ".jpg", + ".jpeg", + ".png", + ".gif", + ".webm", + ".mp4", + ".m4v", + "image\/jpeg", + "image\/png", + "image\/gif", + "video\/webm", + "video\/mp4" + ] + }, + settings: %{ + onboarded: true, + home: %{ + shows: %{ + reblog: true, + reply: true + } + }, + notifications: %{ + alerts: %{ + follow: true, + favourite: true, + reblog: true, + mention: true + }, + shows: %{ + follow: true, + favourite: true, + reblog: true, + mention: true + }, + sounds: %{ + follow: true, + favourite: true, + reblog: true, + mention: true + } + } + }, + push_subscription: nil, + accounts: accounts, + custom_emojis: %{} + } |> Poison.encode! + conn + |> put_layout(false) + |> render(MastodonView, "index.html", %{initial_state: initial_state}) + else + conn + |> redirect(to: "/web/login") + end + end + + def login(conn, params) do + conn + |> render(MastodonView, "login.html") + end + + defp get_or_make_app() do + with %App{} = app <- Repo.get_by(App, client_name: "Mastodon-Local") do + {:ok, app} + else + _e -> + cs = App.register_changeset(%App{}, %{client_name: "Mastodon-Local", redirect_uris: ".", scopes: "read,write,follow"}) + Repo.insert(cs) + end + end + + def login_post(conn, %{"authorization" => %{ "name" => name, "password" => password}}) do + with %User{} = user <- User.get_cached_by_nickname(name), + true <- Pbkdf2.checkpw(password, user.password_hash), + {:ok, app} <- get_or_make_app(), + {:ok, auth} <- Authorization.create_authorization(app, user), + {:ok, token} <- Token.exchange_token(app, auth) do + conn + |> put_session(:oauth_token, token.token) + |> redirect(to: "/web/timelines/public") + end + end + def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do Logger.debug("Unimplemented, returning unmodified relationship") with %User{} = target <- Repo.get(User, id) do diff --git a/lib/pleroma/web/mastodon_api/views/mastodon_view.ex b/lib/pleroma/web/mastodon_api/views/mastodon_view.ex new file mode 100644 index 000000000..370fad374 --- /dev/null +++ b/lib/pleroma/web/mastodon_api/views/mastodon_view.ex @@ -0,0 +1,5 @@ +defmodule Pleroma.Web.MastodonAPI.MastodonView do + use Pleroma.Web, :view + import Phoenix.HTML + import Phoenix.HTML.Form +end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 637c300c8..8fe1d8ec6 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -21,6 +21,13 @@ defmodule Pleroma.Web.Router do plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1} end + pipeline :mastodon_html do + plug :accepts, ["html"] + plug :fetch_session + plug Pleroma.Plugs.OAuthPlug + plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true} + end + pipeline :well_known do plug :accepts, ["xml", "xrd+xml"] end @@ -211,6 +218,14 @@ defmodule Pleroma.Web.Router do get "/webfinger", WebFinger.WebFingerController, :webfinger end + scope "/web", Pleroma.Web.MastodonAPI do + pipe_through :mastodon_html + + get "/login", MastodonAPIController, :login + post "/login", MastodonAPIController, :login_post + get "/*path", MastodonAPIController, :index + end + scope "/", Fallback do get "/*path", RedirectController, :redirector end diff --git a/lib/pleroma/web/templates/mastodon_api/mastodon/index.html.eex b/lib/pleroma/web/templates/mastodon_api/mastodon/index.html.eex new file mode 100644 index 000000000..334dc4f98 --- /dev/null +++ b/lib/pleroma/web/templates/mastodon_api/mastodon/index.html.eex @@ -0,0 +1,18 @@ +<!DOCTYPE html> +<html lang='en'> +<head> +<meta charset='utf-8'> +<meta content='width=device-width, initial-scale=1' name='viewport'> +<link rel="stylesheet" media="all" href="/packs/common.css" /> +<link rel="stylesheet" media="all" href="/packs/default.css" /> + +<script src="/packs/common.js"></script> +<script src="/packs/locale_en.js"></script> +<script id='initial-state' type='application/json'><%= raw @initial_state %></script> +<script src="/packs/application.js"></script> +</head> +<body class='app-body'> + <div class='app-holder' data-props='{"locale":"en"}' id='mastodon'> + </div> +</body> +</html> diff --git a/lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex b/lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex new file mode 100644 index 000000000..6db4b05dc --- /dev/null +++ b/lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex @@ -0,0 +1,10 @@ +<h2>Login in to Mastodon Frontend</h2> +<%= form_for @conn, mastodon_api_path(@conn, :login), [as: "authorization"], fn f -> %> +<%= label f, :name, "Name" %> +<%= text_input f, :name %> +<br> +<%= label f, :password, "Password" %> +<%= password_input f, :password %> +<br> +<%= submit "Authorize" %> +<% end %> |