danschultzer / phoenix_oauth2_provider

Get an OAuth 2 provider running in your phoenix with controllers, views and models in just two minutes
MIT License
84 stars 41 forks source link

** (RuntimeError) No `:repo` found in ExOauth2Provider configuration. #24

Closed kitplummer closed 5 years ago

kitplummer commented 5 years ago

Sorry to bug. Am following the post: https://dreamconception.com/tech/phoenix-full-fledged-api-in-five-minutes/ - or trying to. New to all things Phoenix and Elixir.

I get all the way through the authorization step and can get a token. But when I run curl http://localhost:4000/api/v1/accounts -H "Authorization: Bearer 0d840c6ae3aea3fcdcd2dbd2b9d884f4811e4a8bdb16168f33334624dfbb04c2" I get this:

[debug] Processing with LeiApiWeb.API.V1.UserController.index/2
  Parameters: %{}
  Pipelines: [:api, :api_protected]
[info] Sent 500 in 37ms
[error] #PID<0.509.0> running LeiApiWeb.Endpoint (connection #PID<0.508.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /api/v1/accounts
** (exit) an exception was raised:
    ** (RuntimeError) No `:repo` found in ExOauth2Provider configuration.

Please set up the repo in your configuration:

config :ex_oauth_provider, ExOauthProvider,
  repo: MyApp.Repo

        (ex_oauth2_provider) lib/ex_oauth2_provider/config.ex:6: ExOauth2Provider.Config.repo/1
        (ex_oauth2_provider) lib/ex_oauth2_provider/access_tokens/access_tokens.ex:34: ExOauth2Provider.AccessTokens.get_by_token/2
        (ex_oauth2_provider) lib/ex_oauth2_provider.ex:64: ExOauth2Provider.load_access_token/2
        (ex_oauth2_provider) lib/ex_oauth2_provider.ex:57: ExOauth2Provider.authenticate_token/2
        (ex_oauth2_provider) lib/ex_oauth2_provider/plug/verify_header.ex:78: ExOauth2Provider.Plug.VerifyHeader.verify_token/4
        (lei_api) LeiApiWeb.Router.api_protected/2
        (lei_api) lib/lei_api_web/router.ex:1: LeiApiWeb.Router.__pipe_through0__/1
        (phoenix) lib/phoenix/router.ex:283: Phoenix.Router.__call__/2
        (lei_api) lib/lei_api_web/endpoint.ex:1: LeiApiWeb.Endpoint.plug_builder_call/2
        (lei_api) lib/plug/debugger.ex:122: LeiApiWeb.Endpoint."call (overridable 3)"/2
        (lei_api) lib/lei_api_web/endpoint.ex:1: LeiApiWeb.Endpoint.call/2
        (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:40: Phoenix.Endpoint.Cowboy2Handler.init/2
        (cowboy) /Users/cplummer8/Code/lowendinsight-api/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
        (cowboy) /Users/cplummer8/Code/lowendinsight-api/deps/cowboy/src/cowboy_stream_h.erl:296: :cowboy_stream_h.execute/3
        (cowboy) /Users/cplummer8/Code/lowendinsight-api/deps/cowboy/src/cowboy_stream_h.erl:274: :cowboy_stream_h.request_process/3
        (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

It is quite possible I've done something completely stupid. I am a bit confused on the diff between ExOauthProvider, ExOauth2Provider and PhoenixOauth2Provider.

My config.exs does contain the repo config:

  user: LeiApi.Users.User,
  repo: LeiApi.Repo

config :lei_api, ExOauth2Provider,
  repo: LeiApi.Repo,
  resource_owner: LeiApi.Users.User

Any pointers? Thanks in advance.

Schultzer commented 5 years ago

You need to change config :lei_api, ExOauth2Provider to config :ex_oauth_provider, ExOauthProvider

kitplummer commented 5 years ago

Thanks for the quick response! I did try that. :) But got this on startup:

Compiling 25 files (.ex)
Generated lei_api app
[info] Running LeiApiWeb.Endpoint with cowboy 2.6.3 at 0.0.0.0:4000 (http)
[info] Access LeiApiWeb.Endpoint at http://localhost:4000
You have configured application :ex_oauth_provider in your configuration file,
but the application is not available.

This usually means one of:

  1. You have not added the application as a dependency in a mix.exs file.

  2. You are configuring an application that does not really exist.

Please ensure :ex_oauth_provider exists or remove the configuration.

And then the same error as before:

[info] GET /api/v1/accounts
[debug] Processing with LeiApiWeb.API.V1.UserController.index/2
  Parameters: %{}
  Pipelines: [:api, :api_protected]
[info] Sent 500 in 46ms
[error] #PID<0.599.0> running LeiApiWeb.Endpoint (connection #PID<0.598.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /api/v1/accounts
** (exit) an exception was raised:
    ** (RuntimeError) No `:repo` found in ExOauth2Provider configuration.

Please set up the repo in your configuration:

config :ex_oauth_provider, ExOauthProvider,
  repo: MyApp.Repo
danschultzer commented 5 years ago

How does your router.ex look? The :otp_app should be set there, for the app config to be loaded:

defmodule LeiApiWeb.Router do
  use Phoenix.Router
  use PhoenixOauth2Provider.Router, otp_app: :lei_api

  # ...
end
kitplummer commented 5 years ago

Think I've set it up with the right parts:

defmodule LeiApiWeb.Router do
  use LeiApiWeb, :router
  use Pow.Phoenix.Router
  use PhoenixOauth2Provider.Router, otp_app: :lei_api

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  # BEGIN added for Pow
  pipeline :protected do
    plug Pow.Plug.RequireAuthenticated,
      error_handler: Pow.Phoenix.PlugErrorHandler
  end

  pipeline :api_protected do
    plug ExOauth2Provider.Plug.VerifyHeader, otp_app: :my_app, realm: "Bearer"
    plug ExOauth2Provider.Plug.EnsureAuthenticated
  end

  scope "/api/v1", LeiApiWeb.API.V1 do
    pipe_through [:api, :api_protected]

    resources "/accounts", UserController
  end

  scope "/" do
    pipe_through :browser
    pow_routes()
  end

  scope "/" do
    pipe_through :api

    oauth_api_routes()
  end

  scope "/" do
    pipe_through [:browser, :protected]

    oauth_routes()
  end

  scope "/", LeiApiWeb do
    pipe_through [:browser, :protected]
    resources "/queries", QueryController
  end

  scope "/", LeiApiWeb do
    pipe_through :browser

    get "/", PageController, :index
  end
....
end
danschultzer commented 5 years ago

You have to change:

plug ExOauth2Provider.Plug.VerifyHeader, otp_app: :my_app, realm: "Bearer"

To

plug ExOauth2Provider.Plug.VerifyHeader, otp_app: :lei_api, realm: "Bearer"
danschultzer commented 5 years ago

I've improved the error message. There is no ExOauthProvider module, it was a typo, and the error message will now output the :otp_app setting, so you would instead see this:

No `:repo` found in ExOauth2Provider configuration.

Please set up the repo in your configuration:

config :my_app, ExOauth2Provider,
  repo: MyApp.Repo

Thanks for letting me know 😄