Making 2 plugs dependent on each other

Hi everyone…

I have these 2 plugs in my router file as

  pipeline :auth do
    plug EvercamMedia.AuthenticationPlug
  end

  pipeline :onvif do
    plug EvercamMedia.ONVIFAccessPlug
  end

and my current route situation is

  scope "/v1", EvercamMedia do
    pipe_through :api

       scope "/" do
          pipe_through :auth

          get "/users/:id", UserController, :get
       end
  end

and onvif plug is being used as

scope "/" do
  pipe_through :onvif

  get "/cameras/:id/ptz/status", ONVIFPTZController, :status
end

I want my onvif routes to go through auth plugin first and then go to onvif plug is that possible?

I have tried to do something

pipeline :onvif_auth do
    plug EvercamMedia.AuthenticationPlug
    plug EvercamMedia.ONVIFAccessPlug
end

But this dont work and only give me timeouts

pipe_through understands lists, so use pipe_through [:auth, :onvif]

1 Like

I did this… but nothing happened… I am not passing any api key and Id in headers… and its passing through the auth plug in without any error…

  def call(conn, _) do
    IO.inspect conn
    api_id = extract_credential(conn, %{header: "x-api-id", query: "api_id"})
    api_key = extract_credential(conn, %{header: "x-api-key", query: "api_key"})
    token =
      conn
      |> extract_credential(%{header: "authorization", query: "authorization"})
      |> String.downcase
      |> String.replace_leading("bearer ", "")

    IO.inspect api_id
    IO.inspect api_key
    IO.inspect token

    case EvercamMedia.Auth.validate(api_id, api_key, token) do
      :valid ->
        conn
      {:valid, user} ->
        conn
        |> assign(:current_user, user)
      :invalid ->
        conn
        |> put_resp_content_type("application/json")
        |> resp(401, Poison.encode!(%{message: "Invalid API keys"}))
        |> send_resp
        |> halt
    end
  end

Thanks issue was in my plug. You suggestion worked thanks

1 Like

welcome :slight_smile: