diff --git a/.dialyzer_ignore.exs b/.dialyzer_ignore.exs new file mode 100644 index 0000000..833c446 --- /dev/null +++ b/.dialyzer_ignore.exs @@ -0,0 +1,3 @@ +[ + { "lib/phoenix/router.ex", :pattern_match, 402 } + ] \ No newline at end of file diff --git a/apps/rest_api/lib/routers/admin.ex b/apps/rest_api/lib/routers/admin.ex new file mode 100644 index 0000000..7d79312 --- /dev/null +++ b/apps/rest_api/lib/routers/admin.ex @@ -0,0 +1,22 @@ +defmodule RestAPI.Routers.Admin do + @moduledoc false + + use RestAPI.Router + + alias RestAPI.Plugs.{Authentication, Authorization} + + pipeline :authenticated do + plug Authentication + end + + pipeline :authorized_by_admin do + plug Authorization, type: "admin" + end + + scope "/v1", RestAPI.Controller.Admin do + pipe_through :authenticated + pipe_through :authorized_by_admin + + resources("/users", User, except: [:new]) + end +end diff --git a/apps/rest_api/lib/routers/default.ex b/apps/rest_api/lib/routers/default.ex index 46178fa..854a3f5 100644 --- a/apps/rest_api/lib/routers/default.ex +++ b/apps/rest_api/lib/routers/default.ex @@ -3,75 +3,9 @@ defmodule RestAPI.Routers.Default do use RestAPI.Router - alias PhoenixSwagger.Plug.SwaggerUI + alias RestAPI.Routers.{Admin, Documentation, Public} - alias RestAPI.Controllers.Public - alias RestAPI.Plugs.{Authentication, Authorization} - - pipeline :rest_api do - plug :accepts, ["json"] - end - - pipeline :authenticated do - plug Authentication - end - - pipeline :authorized_by_admin do - plug Authorization, type: "admin" - end - - # This should be used only for documentation purposes - # When running in production it should be disabled - scope "/api/v1/swagger" do - forward "/", SwaggerUI, otp_app: :rest_api, swagger_file: "swagger.json" - end - - scope "/api/v1", Public do - pipe_through :rest_api - - scope "/auth/protocol/openid-connect" do - post "/token", Auth, :sign_in - - scope "/" do - pipe_through :authenticated - - post "/logout", Auth, :sign_out - post "/logout-all-sessions", Auth, :sign_out_all_sessions - end - end - end - - scope "/admin/v1", RestAPI.Controller.Admin do - pipe_through :authenticated - pipe_through :authorized_by_admin - - resources("/users", User, except: [:new]) - end - - def swagger_info do - %{ - schemes: ["https", "http"], - info: %{ - version: "1.0", - title: "WatcherEx", - description: "An Oauth2 and Resource server interelly in elixir.", - termsOfService: "Open for public", - contact: %{ - name: "Luiz Carlos", - email: "lcpojr@gmail.com" - } - }, - securityDefinitions: %{ - Bearer: %{ - type: "apiKey", - name: "Authorization", - description: "API Token must be provided via `Authorization: Bearer ` header", - in: "header" - } - }, - consumes: ["application/json"], - produces: ["application/json"], - tags: [] - } - end + forward "/admin", Admin + forward "/api", Public + forward "/", Documentation end diff --git a/apps/rest_api/lib/routers/documentation.ex b/apps/rest_api/lib/routers/documentation.ex new file mode 100644 index 0000000..75dab9d --- /dev/null +++ b/apps/rest_api/lib/routers/documentation.ex @@ -0,0 +1,40 @@ +defmodule RestAPI.Routers.Documentation do + @moduledoc false + + use RestAPI.Router + + alias PhoenixSwagger.Plug.SwaggerUI + + # This should be used only for documentation purposes + # When running in production it should be disabled + scope "/api/v1/swagger" do + forward "/", SwaggerUI, otp_app: :rest_api, swagger_file: "swagger.json" + end + + def swagger_info do + %{ + schemes: ["https", "http"], + info: %{ + version: "1.0", + title: "WatcherEx", + description: "An Oauth2 and Resource server interelly in elixir.", + termsOfService: "Open for public", + contact: %{ + name: "Luiz Carlos", + email: "lcpojr@gmail.com" + } + }, + securityDefinitions: %{ + Bearer: %{ + type: "apiKey", + name: "Authorization", + description: "API Token must be provided via `Authorization: Bearer ` header", + in: "header" + } + }, + consumes: ["application/json"], + produces: ["application/json"], + tags: [] + } + end +end diff --git a/apps/rest_api/lib/routers/public.ex b/apps/rest_api/lib/routers/public.ex new file mode 100644 index 0000000..37c03ae --- /dev/null +++ b/apps/rest_api/lib/routers/public.ex @@ -0,0 +1,39 @@ +defmodule RestAPI.Routers.Public do + @moduledoc false + + use RestAPI.Router + + alias PhoenixSwagger.Plug.SwaggerUI + + alias RestAPI.Controllers.Public + alias RestAPI.Plugs.Authentication + + pipeline :rest_api do + plug :accepts, ["json"] + end + + pipeline :authenticated do + plug Authentication + end + + # This should be used only for documentation purposes + # When running in production it should be disabled + scope "/v1/swagger" do + forward "/", SwaggerUI, otp_app: :rest_api, swagger_file: "swagger.json" + end + + scope "/v1", Public do + pipe_through :rest_api + + scope "/auth/protocol/openid-connect" do + post "/token", Auth, :sign_in + + scope "/" do + pipe_through :authenticated + + post "/logout", Auth, :sign_out + post "/logout-all-sessions", Auth, :sign_out_all_sessions + end + end + end +end