Changeset View
Changeset View
Standalone View
Standalone View
lib/kolab_wopi/api.ex
defmodule KolabWopi.API do | defmodule KolabWopi.API do | ||||
@moduledoc """ | @moduledoc """ | ||||
An implementation of the Web Application Open Platform Interface (WOPI) API. | An implementation of the Web Application Open Platform Interface (WOPI) API. | ||||
See https://wopi.readthedocs.io/en/latest/ | See https://wopi.readthedocs.io/en/latest/ | ||||
""" | """ | ||||
use Plug.Router | use Plug.Router | ||||
use Plug.Debugger | use Plug.Debugger | ||||
require Logger | require Logger | ||||
plug Plug.Logger | plug Plug.Logger | ||||
plug :wopi_headers | |||||
plug :access_token_handler | plug :access_token_handler | ||||
plug :match | plug :match | ||||
plug :dispatch | plug :dispatch | ||||
def init(options) do | def init(options) do | ||||
options | options | ||||
end | end | ||||
def start_link do | def start_link do | ||||
{:ok, _} = Plug.Adapters.Cowboy.http __MODULE__, [] | {:ok, _} = Plug.Adapters.Cowboy.http __MODULE__, [] | ||||
end | end | ||||
forward "/wopi/files", to: __MODULE__.Files | forward "/wopi/files", to: __MODULE__.Files | ||||
forward "/wopi/containers", to: __MODULE__.Containers | forward "/wopi/containers", to: __MODULE__.Containers | ||||
forward "/wopi/ecosystem", to: __MODULE__.Ecosystem | forward "/wopi/ecosystem", to: __MODULE__.Ecosystem | ||||
forward "/wopibootstrapper", to: __MODULE__.Bootstrap | forward "/wopibootstrapper", to: __MODULE__.Bootstrap | ||||
match _ do | match _ do | ||||
send_status_resp(conn, 404) | send_status_resp(conn, 404) | ||||
end | end | ||||
@doc """ | @doc """ | ||||
Plug that adds the standard WOPI response headers | |||||
@see https://wopirest.readthedocs.io/en/latest/common_headers.html | |||||
""" | |||||
def wopi_headers(conn, _opts) do | |||||
host_info = Application.get_env(:kolab_wopi, :host_info) | |||||
conn | |||||
|> put_resp_header("X-WOPI-HostEndpoint", Keyword.get(host_info, :endpoint_desc)) | |||||
|> put_resp_header("X-WOPI-MachineName", Keyword.get(host_info, :machine_name)) | |||||
|> put_resp_header("X-WOPI-ServerVersion", Keyword.get(host_info, :version)) | |||||
end | |||||
@doc """ | |||||
Plug that fetches query parameters and validates the access_token. | Plug that fetches query parameters and validates the access_token. | ||||
The access_token parameter is required in every WOPI request. | The access_token parameter is required in every WOPI request. | ||||
If it's missing "401 Unauthorized" response will be send and | If it's missing "401 Unauthorized" response will be send and | ||||
request processing aborted. | request processing aborted. | ||||
""" | """ | ||||
def access_token_handler(conn, _opts) do | def access_token_handler(conn, _opts) do | ||||
conn = Plug.Conn.fetch_query_params(conn) | conn = Plug.Conn.fetch_query_params(conn) | ||||
Show All 13 Lines | defp check_token(conn, _) do | ||||
|> halt() | |> halt() | ||||
end | end | ||||
@doc """ | @doc """ | ||||
Sends the response with setting status code. | Sends the response with setting status code. | ||||
It supports Chwala status codes. | It supports Chwala status codes. | ||||
""" | """ | ||||
def send_status_resp(conn, code, reason \\ nil) | def send_status_resp(conn, code, reason \\ "Unknown error") | ||||
def send_status_resp(conn, 403, reason) do | def send_status_resp(conn, 403, reason) do | ||||
# Chwala uses only 200, 403, 500, 501, 503 | # Chwala uses only 200, 403, 500, 501, 503 | ||||
send_status_resp(conn, 401, reason) | send_status_resp(conn, 401, reason) | ||||
end | end | ||||
def send_status_resp(conn, code, reason) when code >= 500 and code < 600 do | |||||
# TODO: log status reason? | |||||
conn | |||||
|> put_resp_header("X-WOPI-ServerError", reason) | |||||
|> send_resp(code, "") | |||||
end | |||||
def send_status_resp(conn, code, _reason) do | def send_status_resp(conn, code, _reason) do | ||||
# TODO: log status reason? | # TODO: log status reason? | ||||
send_resp(conn, code, "") | send_resp(conn, code, "") | ||||
end | end | ||||
@doc """ | @doc """ | ||||
Return X-WOPI-Override header value. | Return X-WOPI-Override header value. | ||||
Show All 12 Lines |