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 | 21 | ||||
-rw-r--r-- | lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex | 10 |
7 files changed, 171 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 c28e20ed1..83003b917 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 @@ -405,6 +406,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 0a0aea966..5c94ba392 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 @@ -207,6 +214,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..a05680205 --- /dev/null +++ b/lib/pleroma/web/templates/mastodon_api/mastodon/index.html.eex @@ -0,0 +1,21 @@ +<!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 id='initial-state' type='application/json'>{"meta":{"streaming_api_base_url":"wss://pleroma.soykaf.com","access_token":"n6CFGTLKHjyxf98AFwQNXWiVkeCcntWUd2YUT3yUNAg=","locale":"en","domain":"pleroma.soykaf.com","admin":"1","me":"6","unfollow_modal":false,"boost_modal":false,"delete_modal":true,"auto_play_gif":false,"reduce_motion":false},"compose":{"me":"6","default_privacy":"public","default_sensitive":false},"accounts":{"6": {"username":"lain","url":"https://pleroma.soykaf.com/users/lain","statuses_count":26717,"source":{"sensitive":"false","privacy":"public","note":""},"note":"pleroma dev. let's be friends\r\n\r\nxmpp: lain@xmpp.soykaf.com \r\ntox: D816CA3999814F0CC67F31EC3FED566734377D1C50925E8F9F0CF384C088AF6DA8BC1F8BA9D7 \r\nmatrix: @lambadalambda:matrix.heldscal.la \r\n10grans: 0xF90F5f5cFfc3E5469c428424c19Ebc1310d59e82","locked":false,"id":6,"header_static":"https://pleroma.soykaf.com/media/1354ed08-bf5f-4d25-9af5-e60944c0bcec/319EE96456BF3FB505DAE8158A47D5EC0B19FA03FC41E48785FFE1CAF06114F8.gif","header":"https://pleroma.soykaf.com/media/1354ed08-bf5f-4d25-9af5-e60944c0bcec/319EE96456BF3FB505DAE8158A47D5EC0B19FA03FC41E48785FFE1CAF06114F8.gif","following_count":510,"followers_count":708,"display_name":"⑨ lain ⑨","created_at":"2017-04-16T09:06:38.000Z","avatar_static":"https://pleroma.soykaf.com/media/6519c8f9-b2ad-49b0-96e8-e2885e163f54/4DA3506EA10AE74A6CDE61277818A4E88FEC665A984E86789C440C8BBDDC4B59.gif","avatar":"https://pleroma.soykaf.com/media/6519c8f9-b2ad-49b0-96e8-e2885e163f54/4DA3506EA10AE74A6CDE61277818A4E88FEC665A984E86789C440C8BBDDC4B59.gif","acct":"lain"}, + "264":{"id":"264","username":"lambadalambda","acct":"lambadalambda","display_name":"Critical Value","locked":false,"created_at":"2016-09-04T16:55:41.021Z","note":"\u003cp\u003e\u003c/p\u003e","url":"https://mastodon.social/@lambadalambda","avatar":"https://files.mastodon.social/accounts/avatars/000/000/264/original/1429214160519.gif","avatar_static":"https://files.mastodon.social/accounts/avatars/000/000/264/static/1429214160519.png","header":"https://files.mastodon.social/accounts/headers/000/000/264/original/28b26104f83747d2.gif","header_static":"https://files.mastodon.social/accounts/headers/000/000/264/static/28b26104f83747d2.png","followers_count":61,"following_count":10,"statuses_count":243},"1":{"id":"1","username":"Gargron","acct":"Gargron","display_name":"Eugen","locked":false,"created_at":"2016-03-16T14:34:26.392Z","note":"\u003cp\u003eDeveloper of Mastodon\u003c/p\u003e\u003cp\u003e\u003ca href=\"https://github.com/tootsuite/mastodon\" rel=\"nofollow noopener\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003egithub.com/tootsuite/mastodon\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e \u003ca href=\"https://www.patreon.com/user?u=619786\" rel=\"nofollow noopener\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://www.\u003c/span\u003e\u003cspan class=\"\"\u003epatreon.com/user?u=619786\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e\u003c/p\u003e\u003cp\u003eAvatar by \u003cspan class=\"h-card\"\u003e\u003ca href=\"https://mastodon.art/@DearMsDear\" class=\"u-url mention\"\u003e@\u003cspan\u003eDearMsDear\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e\u003c/p\u003e","url":"https://mastodon.social/@Gargron","avatar":"https://files.mastodon.social/accounts/avatars/000/000/001/original/8e3dd118a025f184.png","avatar_static":"https://files.mastodon.social/accounts/avatars/000/000/001/original/8e3dd118a025f184.png","header":"https://files.mastodon.social/accounts/headers/000/000/001/original/media.jpeg","header_static":"https://files.mastodon.social/accounts/headers/000/000/001/original/media.jpeg","followers_count":24950,"following_count":667,"statuses_count":24810}},"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":null,"custom_emojis":[{"shortcode":"sickmeme","url":"https://files.mastodon.social/custom_emojis/images/000/000/778/original/sickmeme.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/000/778/static/sickmeme.png"},{"shortcode":"breathe","url":"https://files.mastodon.social/custom_emojis/images/000/000/782/original/breathe.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/000/782/static/breathe.png"},{"shortcode":"hotboi","url":"https://files.mastodon.social/custom_emojis/images/000/000/783/original/hotboi.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/000/783/static/hotboi.png"},{"shortcode":"nigmaGrin","url":"https://files.mastodon.social/custom_emojis/images/000/000/121/original/294208830299176960.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/000/121/static/294208830299176960.png"},{"shortcode":"unarist","url":"https://files.mastodon.social/custom_emojis/images/000/001/091/original/b6816ca3542e9fc0.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/001/091/static/b6816ca3542e9fc0.png"},{"shortcode":"coolcat","url":"https://files.mastodon.social/custom_emojis/images/000/000/005/original/354315741937270794.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/000/005/static/354315741937270794.png"},{"shortcode":"angery","url":"https://files.mastodon.social/custom_emojis/images/000/000/006/original/angery.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/000/006/static/angery.png"},{"shortcode":"thinkhappy","url":"https://files.mastodon.social/custom_emojis/images/000/000/011/original/328081997266288640.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/000/011/static/328081997266288640.png"},{"shortcode":"thaenkin","url":"https://files.mastodon.social/custom_emojis/images/000/000/012/original/334845559435296768.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/000/012/static/334845559435296768.png"},{"shortcode":"maple","url":"https://files.mastodon.social/custom_emojis/images/000/000/953/original/1b22dacadcf0e224.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/000/953/static/1b22dacadcf0e224.png"},{"shortcode":"computerfairies","url":"https://files.mastodon.social/custom_emojis/images/000/000/954/original/abd0669604e01d4d.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/000/954/static/abd0669604e01d4d.png"},{"shortcode":"noelle","url":"https://files.mastodon.social/custom_emojis/images/000/000/956/original/peridot-santa-transparent.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/000/956/static/peridot-santa-transparent.png"},{"shortcode":"wyd","url":"https://files.mastodon.social/custom_emojis/images/000/001/069/original/wyd.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/001/069/static/wyd.png"},{"shortcode":"weirdfish","url":"https://files.mastodon.social/custom_emojis/images/000/001/489/original/8a2fdcf42b344cd9.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/001/489/static/8a2fdcf42b344cd9.png"},{"shortcode":"screwattack","url":"https://files.mastodon.social/custom_emojis/images/000/001/889/original/6f11873e8ac5dd20.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/001/889/static/6f11873e8ac5dd20.png"},{"shortcode":"dnd","url":"https://files.mastodon.social/custom_emojis/images/000/004/590/original/b12ce74fe2b86ab8.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/004/590/static/b12ce74fe2b86ab8.png"},{"shortcode":"stardewvalley","url":"https://files.mastodon.social/custom_emojis/images/000/004/591/original/f9a94b8af8dd1c72.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/004/591/static/f9a94b8af8dd1c72.png"},{"shortcode":"splatoon","url":"https://files.mastodon.social/custom_emojis/images/000/004/592/original/632e04f8f0f4ca62.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/004/592/static/632e04f8f0f4ca62.png"},{"shortcode":"warcraft","url":"https://files.mastodon.social/custom_emojis/images/000/004/593/original/7ca494c09fae0384.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/004/593/static/7ca494c09fae0384.png"},{"shortcode":"overwatch","url":"https://files.mastodon.social/custom_emojis/images/000/004/594/original/6d181fa79a38e644.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/004/594/static/6d181fa79a38e644.png"},{"shortcode":"kerbal","url":"https://files.mastodon.social/custom_emojis/images/000/003/317/original/9614b39f11d19bcf.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/003/317/static/9614b39f11d19bcf.png"},{"shortcode":"gargamel","url":"https://files.mastodon.social/custom_emojis/images/000/003/677/original/d3af6f81b96e082d.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/003/677/static/d3af6f81b96e082d.png"},{"shortcode":"mastodon","url":"https://files.mastodon.social/custom_emojis/images/000/003/675/original/089aaae26a2abcc1.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/003/675/static/089aaae26a2abcc1.png"},{"shortcode":"blobpats","url":"https://files.mastodon.social/custom_emojis/images/000/003/679/original/80d1ba80bf06950e.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/003/679/static/80d1ba80bf06950e.png"},{"shortcode":"sabakan","url":"https://files.mastodon.social/custom_emojis/images/000/003/676/original/77c4094eacccac9e.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/003/676/static/77c4094eacccac9e.png"},{"shortcode":"wily_ufo","url":"https://files.mastodon.social/custom_emojis/images/000/002/321/original/dc7da5987f1e07b0.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/002/321/static/dc7da5987f1e07b0.png"},{"shortcode":"batman","url":"https://files.mastodon.social/custom_emojis/images/000/005/163/original/8iGbkB7aT.png","static_url":"https://files.mastodon.social/custom_emojis/images/000/005/163/static/8iGbkB7aT.png"}]}</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 %> |