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

Production only - key :app not found #19

Closed chasers closed 5 years ago

chasers commented 5 years ago

Just got everything working on dev and deployed this to prod. I'm getting an interal server error when I try to create an application. Seems as though the create went through to the db though. Here is the log message:

17:10:44.098 [info] POST /oauth/applications/d75d1313bff21c7975a1e9456a42b1e993c3770a20955ecb2deb4438ae4756c6
17:10:44.105 [info] Converted error KeyError to 500 response
17:10:44.105 [info] Sent 500 in 6ms
17:10:44.106 [error] #PID<0.2413.0> running LogflareWeb.Endpoint (connection #PID<0.2412.0>, stream id 1) terminated
Server: logflare.app:80 (http)
Request: POST /oauth/applications/d75d1313bff21c7975a1e9456a42b1e993c3770a20955ecb2deb4438ae4756c6
** (exit) an exception was raised:
    ** (KeyError) key :app not found in: [aliases: [], build_embedded: false, build_per_environment: true, build_scm: Mix.SCM.Path, config_path: "config/config.exs", consolidate_protocols: true, default_task: "run", deps: [], deps_path: "deps", elixirc_p$
        (elixir) lib/keyword.ex:389: Keyword.fetch!/2
        (phoenix) lib/mix/phoenix.ex:131: Mix.Phoenix.base/0
        (phoenix_oauth2_provider) lib/phoenix_oauth2_provider.ex:52: PhoenixOauth2Provider.web_module/0
        (phoenix_oauth2_provider) lib/phoenix_oauth2_provider.ex:47: PhoenixOauth2Provider.router_module/0
        (phoenix_oauth2_provider) lib/phoenix_oauth2_provider.ex:41: PhoenixOauth2Provider.router_helpers/0
        (phoenix_oauth2_provider) lib/phoenix_oauth2_provider/web/controllers/application_controller.ex:78: PhoenixOauth2Provider.ApplicationController.delete/2
        (phoenix_oauth2_provider) lib/phoenix_oauth2_provider/web/controllers/application_controller.ex:1: PhoenixOauth2Provider.ApplicationController.action/2
        (phoenix_oauth2_provider) lib/phoenix_oauth2_provider/web/controllers/application_controller.ex:1: PhoenixOauth2Provider.ApplicationController.phoenix_controller_pipeline/2

Code is on master here: https://github.com/Logflare/logflare

Any help is very much appreciated!

chasers commented 5 years ago

Note: all my other db ops are working.

danschultzer commented 5 years ago

Seems like the :app key is not included in your release. I assume something has changed in elixir/phoenix since I released this library, and using Mix.Phoenix.base/0 is no longer recommended.

I'll update the phoenix-1-4 branch with a fix for this. I've already written Pow with a much more reliable way to fetch web_module.

danschultzer commented 5 years ago

Ok, pull the PhoenixOauth2Provider from git with the most recent commit, and update the config with :module:

config :phoenix_oauth2_provider, PhoenixOauth2Provider,
  current_resource_owner: :user,
  repo: Logflare.Repo,
  resource_owner: Logflare.User,
  grant_flows: ~w(authorization_code),
  use_refresh_token: true,
  default_scopes: ~w(public),
  optional_scopes: ~w(read write),
  revoke_refresh_token_on_use: true,
  module: Logflare

I'm in the process of rewritten both of these libraries completely. It was build with Phoenix 1.2/1.3 in mind, and with some bad practices as I was still pretty green. It'll take a bit of time to get it all up to date, but by then this bug will be removed. Let's keep this issue open until I've applied a fix for this.

chasers commented 5 years ago

Dude you're awesome! Working now on prod too!

Please keep this updated :) I definitely found your libraries to be more approachable plus having the routes and templates built in was one less thing to figure out.

Going to need an actual login / registration form soon so I'll probably go with Pow.

danschultzer commented 5 years ago

v0.5.0 has been released at last!

chasers commented 5 years ago

Hey turns out I have this problem in production again. Config is now as you said...

config :logflare, ExOauth2Provider,
  repo: Logflare.Repo,
  resource_owner: Logflare.User,
  grant_flows: ~w(authorization_code),
  use_refresh_token: true,
  default_scopes: ~w(public),
  optional_scopes: ~w(read write),
  revoke_refresh_token_on_use: true

config :logflare, PhoenixOauth2Provider,
  current_resource_owner: :user,
  web_module: LogflareWeb

Any ideas?

danschultzer commented 5 years ago

Is the error stacktrace the same as the first one you experienced?

chasers commented 5 years ago

Yeah

00:17:43.612 [error] #PID<0.756.57> running LogflareWeb.Endpoint (connection #PID<0.967.57>, stream id 1) terminated
Server: logflare.app:80 (http)
Request: GET /oauth/authorize?client_id=d8a8af24ada66bfa29477d746d74ae7a8833d80019c6fa285f07fe6159491a5f&redirect_uri=https%3A%2F%2Fwww.cloudflare.com%2Fapps%2Foauth%2F&response_type=code&scope=read+write
** (exit) an exception was raised:
    ** (KeyError) key :app not found in: [aliases: [], build_embedded: false, build_per_environment: true, build_scm: Mix.SCM.Path, config_path: "config/config.exs", consolidate_protocols: true, default_task: "run", deps: [], deps_path: "deps", elixirc_paths: ["lib"], erlc_paths: ["src"], erlc_include_path: "include", erlc_options: [:debug_info], lockfile: "mix.lock", preferred_cli_env: [], start_permanent: false]
        (elixir) lib/keyword.ex:389: Keyword.fetch!/2
        (phoenix_oauth2_provider) lib/phoenix_oauth2_provider/config.ex:26: PhoenixOauth2Provider.Config.get_from_app_env/2
        (phoenix_oauth2_provider) lib/phoenix_oauth2_provider/config.ex:15: PhoenixOauth2Provider.Config.get/3
        (phoenix_oauth2_provider) lib/phoenix_oauth2_provider/controller.ex:32: PhoenixOauth2Provider.Controller.__put_web_module_view__/2
        (phoenix_oauth2_provider) lib/phoenix_oauth2_provider/controllers/authorization_controller.ex:1: PhoenixOauth2Provider.AuthorizationController.phoenix_controller_pipeline/2
        (logflare) lib/logflare_web/endpoint.ex:1: LogflareWeb.Endpoint.instrument/4
        (phoenix) lib/phoenix/router.ex:275: Phoenix.Router.__call__/1
        (logflare) lib/logflare_web/endpoint.ex:1: LogflareWeb.Endpoint.plug_builder_call/2
danschultzer commented 5 years ago

Ok, I think I found the issue, let me just do some tests here with releases to make sure I got a fix.

danschultzer commented 5 years ago

The issue is that :app key is not available in releases 😞 Working on a 5.0.1 release that mitigates the issue.

danschultzer commented 5 years ago

Ok, v0.5.1 has been released, so now :otp_app has to be passed explicitly. I've opened a PR at logflare.