Skip to content

Commit

Permalink
Add security macro in controllers (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rados13 authored Feb 22, 2024
1 parent a27e582 commit 01c9597
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 7 deletions.
3 changes: 1 addition & 2 deletions lib/jellyfish_web/api_spec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ defmodule JellyfishWeb.ApiSpec do
paths: Paths.from_router(JellyfishWeb.Router),
components: %Components{
securitySchemes: %{"authorization" => %SecurityScheme{type: "http", scheme: "bearer"}}
},
security: [%{"authorization" => []}]
}
}
|> OpenApiSpex.resolve_schema_modules()
end
Expand Down
2 changes: 2 additions & 0 deletions lib/jellyfish_web/controllers/component_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ defmodule JellyfishWeb.ComponentController do

tags [:room]

security(%{"authorization" => []})

operation :create,
operation_id: "add_component",
summary: "Creates the component and adds it to the room",
Expand Down
2 changes: 2 additions & 0 deletions lib/jellyfish_web/controllers/healthcheck_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ defmodule JellyfishWeb.HealthcheckController do

tags [:health]

security(%{"authorization" => []})

operation :show,
operation_id: "healthcheck",
summary: "Describes the health of Jellyfish",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule JellyfishWeb.HLSController do
defmodule JellyfishWeb.HLSContentController do
use JellyfishWeb, :controller
use OpenApiSpex.ControllerSpecs

Expand Down
2 changes: 2 additions & 0 deletions lib/jellyfish_web/controllers/peer_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ defmodule JellyfishWeb.PeerController do

tags [:room]

security(%{"authorization" => []})

operation :create,
operation_id: "add_peer",
summary: "Create peer",
Expand Down
53 changes: 53 additions & 0 deletions lib/jellyfish_web/controllers/recording_content_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
defmodule JellyfishWeb.RecordingContentController do
use JellyfishWeb, :controller
use OpenApiSpex.ControllerSpecs

require Logger

alias Jellyfish.Component.HLS.RequestHandler
alias JellyfishWeb.ApiSpec

alias Plug.Conn

action_fallback JellyfishWeb.FallbackController

@playlist_content_type "application/vnd.apple.mpegurl"
@recording_id_spec [in: :path, description: "Recording id", type: :string]

tags [:recording]

operation :index,
operation_id: "getRecordingContent",
summary: "Retrieve Recording (HLS) Content",
parameters: [
recording_id: @recording_id_spec,
filename: [in: :path, description: "Name of the file", type: :string]
],
required: [:recording_id, :filename],
responses: [
ok: ApiSpec.data("File was found", ApiSpec.HLS.Response),
not_found: ApiSpec.error("File not found"),
bad_request: ApiSpec.error("Invalid request")
]

def index(conn, %{"recording_id" => recording_id, "filename" => filename}) do
with {:ok, file} <-
RequestHandler.handle_recording_request(recording_id, filename) do
conn =
if String.ends_with?(filename, ".m3u8"),
do: put_resp_content_type(conn, @playlist_content_type, nil),
else: conn

Conn.send_resp(conn, 200, file)
else
{:error, :invalid_recording} ->
{:error, :bad_request, "Invalid recording, got: #{recording_id}"}

{:error, :invalid_path} ->
{:error, :bad_request, "Invalid filename, got: #{filename}"}

{:error, _reason} ->
{:error, :not_found, "File not found"}
end
end
end
2 changes: 2 additions & 0 deletions lib/jellyfish_web/controllers/recording_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ defmodule JellyfishWeb.RecordingController do

tags [:recording]

security(%{"authorization" => []})

operation :index,
operation_id: "getRecordingContent",
summary: "Retrieve Recording (HLS) Content",
Expand Down
2 changes: 2 additions & 0 deletions lib/jellyfish_web/controllers/room_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ defmodule JellyfishWeb.RoomController do

tags [:room]

security(%{"authorization" => []})

operation :index,
operation_id: "get_all_rooms",
summary: "Show information about all rooms",
Expand Down
2 changes: 2 additions & 0 deletions lib/jellyfish_web/controllers/sip_call_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ defmodule JellyfishWeb.SIPCallController do

tags [:sip]

security(%{"authorization" => []})

operation :create,
operation_id: "dial",
summary: "Make a call from the SIP component to the provided phone number",
Expand Down
2 changes: 2 additions & 0 deletions lib/jellyfish_web/controllers/subscription_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ defmodule JellyfishWeb.SubscriptionController do

tags [:hls]

security(%{"authorization" => []})

operation :create,
operation_id: "subscribe_hls_to",
summary: "Subscribe the HLS component to the tracks of peers or components",
Expand Down
4 changes: 2 additions & 2 deletions lib/jellyfish_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ defmodule JellyfishWeb.Router do

# Paths which DO NOT require auth
scope "/", JellyfishWeb do
get "/hls/:room_id/:filename", HLSController, :index
get "/recording/:recording_id/:filename", RecordingController, :index
get "/hls/:room_id/:filename", HLSContentController, :index
get "/recording/:recording_id/:filename", RecordingContentController, :index
end

# Enable LiveDashboard in development
Expand Down
31 changes: 29 additions & 2 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,8 @@ paths:
schema:
$ref: '#/components/schemas/Error'
description: Unauthorized
security:
- authorization: []
summary: Describes the health of Jellyfish
tags:
- health
Expand Down Expand Up @@ -810,6 +812,8 @@ paths:
schema:
$ref: '#/components/schemas/Error'
description: Room doesn't exist
security:
- authorization: []
summary: Subscribe the HLS component to the tracks of peers or components
tags:
- hls
Expand Down Expand Up @@ -900,6 +904,8 @@ paths:
schema:
$ref: '#/components/schemas/Error'
description: Unable to obtain recordings
security:
- authorization: []
summary: Lists all available recordings
tags:
- recording
Expand Down Expand Up @@ -935,6 +941,8 @@ paths:
schema:
$ref: '#/components/schemas/Error'
description: Recording doesn't exist
security:
- authorization: []
summary: Deletes the recording
tags:
- recording
Expand Down Expand Up @@ -995,6 +1003,8 @@ paths:
schema:
$ref: '#/components/schemas/Error'
description: Unauthorized
security:
- authorization: []
summary: Show information about all rooms
tags:
- room
Expand Down Expand Up @@ -1028,6 +1038,8 @@ paths:
schema:
$ref: '#/components/schemas/Error'
description: Unauthorized
security:
- authorization: []
summary: Creates a room
tags:
- room
Expand Down Expand Up @@ -1057,6 +1069,8 @@ paths:
schema:
$ref: '#/components/schemas/Error'
description: Room doesn't exist
security:
- authorization: []
summary: Delete the room
tags:
- room
Expand Down Expand Up @@ -1089,6 +1103,8 @@ paths:
schema:
$ref: '#/components/schemas/Error'
description: Room doesn't exist
security:
- authorization: []
summary: Shows information about the room
tags:
- room
Expand Down Expand Up @@ -1142,6 +1158,8 @@ paths:
schema:
$ref: '#/components/schemas/Error'
description: Room doesn't exist
security:
- authorization: []
summary: Creates the component and adds it to the room
tags:
- room
Expand Down Expand Up @@ -1177,6 +1195,8 @@ paths:
schema:
$ref: '#/components/schemas/Error'
description: Either component or the room doesn't exist
security:
- authorization: []
summary: Delete the component from the room
tags:
- room
Expand Down Expand Up @@ -1237,6 +1257,8 @@ paths:
schema:
$ref: '#/components/schemas/Error'
description: Peer limit has been reached
security:
- authorization: []
summary: Create peer
tags:
- room
Expand Down Expand Up @@ -1272,6 +1294,8 @@ paths:
schema:
$ref: '#/components/schemas/Error'
description: Room ID or Peer ID references a resource that doesn't exist
security:
- authorization: []
summary: Delete peer
tags:
- room
Expand Down Expand Up @@ -1313,6 +1337,8 @@ paths:
schema:
$ref: '#/components/schemas/Error'
description: Room doesn't exist
security:
- authorization: []
summary: Finish call made by SIP component
tags:
- sip
Expand Down Expand Up @@ -1360,10 +1386,11 @@ paths:
schema:
$ref: '#/components/schemas/Error'
description: Room doesn't exist
security:
- authorization: []
summary: Make a call from the SIP component to the provided phone number
tags:
- sip
security:
- authorization: []
security: []
servers: []
tags: []

0 comments on commit 01c9597

Please sign in to comment.