honkoma/lib/pleroma/plugs/oauth_plug.ex

78 lines
2.1 KiB
Elixir
Raw Normal View History

# Pleroma: A lightweight social networking server
2018-12-31 15:41:47 +00:00
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Plugs.OAuthPlug do
import Plug.Conn
2018-12-05 14:29:49 +00:00
import Ecto.Query
2019-02-09 15:16:26 +00:00
alias Pleroma.User
alias Pleroma.Repo
alias Pleroma.Web.OAuth.Token
2018-12-05 14:29:49 +00:00
@realm_reg Regex.compile!("Bearer\:?\s+(.*)$", "i")
def init(options), do: options
def call(%{assigns: %{user: %User{}}} = conn, _), do: conn
2018-03-30 13:01:53 +00:00
2017-11-19 01:22:07 +00:00
def call(conn, _) do
with {:ok, token_str} <- fetch_token_str(conn),
{:ok, user, token_record} <- fetch_user_and_token(token_str) do
conn
|> assign(:token, token_record)
|> assign(:user, user)
else
_ -> conn
end
end
2018-12-05 14:29:49 +00:00
# Gets user by token
#
@spec fetch_user_and_token(String.t()) :: {:ok, User.t(), Token.t()} | nil
defp fetch_user_and_token(token) do
2019-01-26 14:55:53 +00:00
query =
from(t in Token,
where: t.token == ^token,
join: user in assoc(t, :user),
preload: [user: user]
)
2018-12-05 14:29:49 +00:00
with %Token{user: %{info: %{deactivated: false} = _} = user} = token_record <- Repo.one(query) do
{:ok, user, token_record}
2018-12-05 14:29:49 +00:00
end
end
# Gets token from session by :oauth_token key
#
@spec fetch_token_from_session(Plug.Conn.t()) :: :no_token_found | {:ok, String.t()}
defp fetch_token_from_session(conn) do
case get_session(conn, :oauth_token) do
nil -> :no_token_found
token -> {:ok, token}
end
end
# Gets token from headers
#
@spec fetch_token_str(Plug.Conn.t()) :: :no_token_found | {:ok, String.t()}
defp fetch_token_str(%Plug.Conn{} = conn) do
2018-12-05 14:29:49 +00:00
headers = get_req_header(conn, "authorization")
with :no_token_found <- fetch_token_str(headers),
2018-12-05 14:29:49 +00:00
do: fetch_token_from_session(conn)
end
@spec fetch_token_str(Keyword.t()) :: :no_token_found | {:ok, String.t()}
defp fetch_token_str([]), do: :no_token_found
2018-12-05 14:29:49 +00:00
defp fetch_token_str([token | tail]) do
2018-12-05 14:29:49 +00:00
trimmed_token = String.trim(token)
case Regex.run(@realm_reg, trimmed_token) do
[_, match] -> {:ok, String.trim(match)}
_ -> fetch_token_str(tail)
2018-12-05 14:29:49 +00:00
end
end
end