mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-11-19 03:19:17 +00:00
32 lines
759 B
Elixir
32 lines
759 B
Elixir
defmodule Pleroma.Plugs.OAuthPlug do
|
|
import Plug.Conn
|
|
alias Pleroma.User
|
|
alias Pleroma.Repo
|
|
alias Pleroma.Web.OAuth.Token
|
|
|
|
def init(options) do
|
|
options
|
|
end
|
|
|
|
def call(%{assigns: %{user: %User{}}} = conn, _), do: conn
|
|
|
|
def call(conn, _) do
|
|
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} = token <- Repo.get_by(Token, token: token),
|
|
%User{} = user <- Repo.get(User, user_id),
|
|
false <- !!user.info["deactivated"] do
|
|
conn
|
|
|> assign(:token, token)
|
|
|> assign(:user, user)
|
|
else
|
|
_ -> conn
|
|
end
|
|
end
|
|
end
|