Skip to content

Commit 1c736a1

Browse files
authored
Merge pull request #23 from danschultzer/ex-oauth2-provider-0-5-1
Update to use ExOauth2Provider 0.5.1
2 parents b402745 + 95cff56 commit 1c736a1

File tree

18 files changed

+87
-83
lines changed

18 files changed

+87
-83
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v0.5.1 (TBA)
4+
5+
* Require ExOauth2Provider v0.5.1
6+
* Pass configuration to all ExOauth2Provider methods
7+
38
## v0.5.0 (2019-05-08)
49

510
This is a full rewrite of the library, and are several breaking changes. You're encouraged to test your app well if you upgrade from 0.4.x.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Set up routes:
3535
```elixir
3636
defmodule MyAppWeb.Router do
3737
use MyAppWeb, :router
38-
use PhoenixOauth2Provider.Router
38+
use PhoenixOauth2Provider.Router, otp_app: :my_app
3939

4040
# ...
4141

lib/mix/phoenix_oauth2_provider/template.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
defmodule Mix.PhoenixOauth2Provider.Template do
22
@moduledoc false
33

4-
alias ExOauth2Provider.Config
5-
alias Mix.{Generator, Phoenix}
4+
alias Mix.{ExOauth2Provider, Generator, Phoenix}
65

76
@views ["application", "authorization", "authorized_application"]
87
@view_template """
@@ -54,7 +53,7 @@ defmodule Mix.PhoenixOauth2Provider.Template do
5453
defp web_path(context_app), do: Phoenix.web_path(context_app)
5554

5655
defp web_app(ctx_app) do
57-
this_app = Config.otp_app()
56+
this_app = ExOauth2Provider.otp_app()
5857

5958
if ctx_app == this_app do
6059
"#{ctx_app}_web"

lib/mix/tasks/phoenix_oauth2_provider.gen.templates.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ defmodule Mix.Tasks.PhoenixOauth2Provider.Gen.Templates do
1212
"""
1313
use Mix.Task
1414

15-
alias ExOauth2Provider.Config
1615
alias Mix.ExOauth2Provider
1716
alias Mix.PhoenixOauth2Provider.Template
1817

@@ -35,7 +34,7 @@ defmodule Mix.Tasks.PhoenixOauth2Provider.Gen.Templates do
3534
defp create_template_files(config) do
3635
config
3736
|> Map.get(:context_app)
38-
|> Kernel.||(Config.otp_app())
37+
|> Kernel.||(ExOauth2Provider.otp_app())
3938
|> Template.create_view_and_template_files()
4039
end
4140
end

lib/phoenix_oauth2_provider/config.ex

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
defmodule PhoenixOauth2Provider.Config do
22
@moduledoc false
33

4-
alias ExOauth2Provider.Config
5-
64
@spec current_resource_owner(keyword()) :: atom()
75
def current_resource_owner(config), do: get(config, :current_resource_owner, :current_user)
86

97
@spec web_module(keyword()) :: atom()
108
def web_module(config), do: get(config, :web_module)
119

1210
defp get(config, key, value \\ nil) do
11+
otp_app = Keyword.get(config, :otp_app)
12+
1313
config
1414
|> get_from_config(key)
15-
|> get_from_app_env(key)
15+
|> get_from_app_env(otp_app, key)
1616
|> get_from_global_env(key)
1717
|> case do
1818
:not_found -> value
@@ -22,14 +22,13 @@ defmodule PhoenixOauth2Provider.Config do
2222

2323
defp get_from_config(config, key), do: Keyword.get(config, key, :not_found)
2424

25-
defp get_from_app_env(:not_found, key) do
26-
app = Config.otp_app()
27-
28-
app
25+
defp get_from_app_env(:not_found, nil, _key), do: :not_found
26+
defp get_from_app_env(:not_found, otp_app, key) do
27+
otp_app
2928
|> Application.get_env(PhoenixOauth2Provider, [])
3029
|> Keyword.get(key, :not_found)
3130
end
32-
defp get_from_app_env(value, _key), do: value
31+
defp get_from_app_env(value, _otp_app, _key), do: value
3332

3433
defp get_from_global_env(:not_found, key) do
3534
:phoenix_oauth2_provider

lib/phoenix_oauth2_provider/controller.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ defmodule PhoenixOauth2Provider.Controller do
1616
defdelegate put_web_module_view(conn, type), to: unquote(__MODULE__), as: :__put_web_module_view__
1717

1818
def action(conn, _opts) do
19-
params = unquote(__MODULE__).__action_params__(conn, unquote(type))
19+
config = conn.private[:phoenix_oauth2_provider_config]
20+
params = unquote(__MODULE__).__action_params__(conn, config, unquote(type))
2021

2122
apply(__MODULE__, action_name(conn), params)
2223
end
@@ -74,8 +75,8 @@ defmodule PhoenixOauth2Provider.Controller do
7475
defp load_config(conn), do: Map.get(conn.private, :phoenix_oauth2_provider_config, [])
7576

7677
@doc false
77-
def __action_params__(conn, :api), do: [conn, conn.params]
78-
def __action_params__(conn, _any), do: [conn, conn.params, current_resource_owner(conn)]
78+
def __action_params__(conn, config, :api), do: [conn, conn.params, config]
79+
def __action_params__(conn, config, _any), do: [conn, conn.params, current_resource_owner(conn), config]
7980

8081
defp current_resource_owner(conn) do
8182
resource_owner_key = Config.current_resource_owner([])

lib/phoenix_oauth2_provider/controllers/application_controller.ex

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,27 @@ defmodule PhoenixOauth2Provider.ApplicationController do
55
alias ExOauth2Provider.Applications
66
alias Plug.Conn
77

8-
@spec index(Conn.t(), map(), map()) :: Conn.t()
9-
def index(conn, _params, resource_owner) do
10-
applications = Applications.get_applications_for(resource_owner)
8+
@spec index(Conn.t(), map(), map(), keyword()) :: Conn.t()
9+
def index(conn, _params, resource_owner, config) do
10+
applications = Applications.get_applications_for(resource_owner, config)
1111

1212
render(conn, "index.html", applications: applications)
1313
end
1414

15-
@spec new(Conn.t(), map(), map()) :: Conn.t()
16-
def new(conn, _params, _resource_owner) do
15+
@spec new(Conn.t(), map(), map(), keyword()) :: Conn.t()
16+
def new(conn, _params, _resource_owner, config) do
1717
changeset =
18-
ExOauth2Provider.Config.application([])
18+
ExOauth2Provider.Config.application(config)
1919
|> struct()
20-
|> Applications.change_application()
20+
|> Applications.change_application(%{}, config)
2121

2222
render(conn, "new.html", changeset: changeset)
2323
end
2424

25-
@spec create(Conn.t(), map(), map()) :: Conn.t()
26-
def create(conn, %{"oauth_application" => application_params}, resource_owner) do
25+
@spec create(Conn.t(), map(), map(), keyword()) :: Conn.t()
26+
def create(conn, %{"oauth_application" => application_params}, resource_owner, config) do
2727
resource_owner
28-
|> Applications.create_application(application_params)
28+
|> Applications.create_application(application_params, config)
2929
|> case do
3030
{:ok, application} ->
3131
conn
@@ -37,26 +37,26 @@ defmodule PhoenixOauth2Provider.ApplicationController do
3737
end
3838
end
3939

40-
@spec show(Conn.t(), map(), map()) :: Conn.t()
41-
def show(conn, %{"uid" => uid}, resource_owner) do
42-
application = get_application_for!(resource_owner, uid)
40+
@spec show(Conn.t(), map(), map(), keyword()) :: Conn.t()
41+
def show(conn, %{"uid" => uid}, resource_owner, config) do
42+
application = get_application_for!(resource_owner, uid, config)
4343

4444
render(conn, "show.html", application: application)
4545
end
4646

47-
@spec edit(Conn.t(), map(), map()) :: Conn.t()
48-
def edit(conn, %{"uid" => uid}, resource_owner) do
49-
application = get_application_for!(resource_owner, uid)
50-
changeset = Applications.change_application(application)
47+
@spec edit(Conn.t(), map(), map(), keyword()) :: Conn.t()
48+
def edit(conn, %{"uid" => uid}, resource_owner, config) do
49+
application = get_application_for!(resource_owner, uid, config)
50+
changeset = Applications.change_application(application, %{}, config)
5151

5252
render(conn, "edit.html", application: application, changeset: changeset)
5353
end
5454

55-
@spec update(Conn.t(), map(), map()) :: Conn.t()
56-
def update(conn, %{"uid" => uid, "oauth_application" => application_params}, resource_owner) do
57-
application = get_application_for!(resource_owner, uid)
55+
@spec update(Conn.t(), map(), map(), keyword()) :: Conn.t()
56+
def update(conn, %{"uid" => uid, "oauth_application" => application_params}, resource_owner, config) do
57+
application = get_application_for!(resource_owner, uid, config)
5858

59-
case Applications.update_application(application, application_params) do
59+
case Applications.update_application(application, application_params, config) do
6060
{:ok, application} ->
6161
conn
6262
|> put_flash(:info, "Application updated successfully.")
@@ -67,19 +67,19 @@ defmodule PhoenixOauth2Provider.ApplicationController do
6767
end
6868
end
6969

70-
@spec delete(Conn.t(), map(), map()) :: Conn.t()
71-
def delete(conn, %{"uid" => uid}, resource_owner) do
70+
@spec delete(Conn.t(), map(), map(), keyword()) :: Conn.t()
71+
def delete(conn, %{"uid" => uid}, resource_owner, config) do
7272
{:ok, _application} =
7373
resource_owner
74-
|> get_application_for!(uid)
75-
|> Applications.delete_application()
74+
|> get_application_for!(uid, config)
75+
|> Applications.delete_application(config)
7676

7777
conn
7878
|> put_flash(:info, "Application deleted successfully.")
7979
|> redirect(to: Routes.oauth_application_path(conn, :index))
8080
end
8181

82-
defp get_application_for!(resource_owner, uid) do
83-
Applications.get_application_for!(resource_owner, uid)
82+
defp get_application_for!(resource_owner, uid, config) do
83+
Applications.get_application_for!(resource_owner, uid, config)
8484
end
8585
end

lib/phoenix_oauth2_provider/controllers/authorization_controller.ex

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ defmodule PhoenixOauth2Provider.AuthorizationController do
55
alias ExOauth2Provider.Authorization
66
alias Plug.Conn
77

8-
@spec new(Conn.t(), map(), map()) :: Conn.t()
9-
def new(conn, params, resource_owner) do
8+
@spec new(Conn.t(), map(), map(), keyword()) :: Conn.t()
9+
def new(conn, params, resource_owner, config) do
1010
resource_owner
11-
|> Authorization.preauthorize(params)
11+
|> Authorization.preauthorize(params, config)
1212
|> case do
1313
{:ok, client, scopes} ->
1414
render(conn, "new.html", params: params, client: client, scopes: scopes)
@@ -26,22 +26,22 @@ defmodule PhoenixOauth2Provider.AuthorizationController do
2626
end
2727
end
2828

29-
@spec create(Conn.t(), map(), map()) :: Conn.t()
30-
def create(conn, params, resource_owner) do
29+
@spec create(Conn.t(), map(), map(), keyword()) :: Conn.t()
30+
def create(conn, params, resource_owner, config) do
3131
resource_owner
32-
|> Authorization.authorize(params)
32+
|> Authorization.authorize(params, config)
3333
|> redirect_or_render(conn)
3434
end
3535

36-
@spec delete(Conn.t(), map(), map()) :: Conn.t()
37-
def delete(conn, params, resource_owner) do
36+
@spec delete(Conn.t(), map(), map(), keyword()) :: Conn.t()
37+
def delete(conn, params, resource_owner, config) do
3838
resource_owner
39-
|> Authorization.deny(params)
39+
|> Authorization.deny(params, config)
4040
|> redirect_or_render(conn)
4141
end
4242

43-
@spec show(Conn.t(), map(), map()) :: Conn.t()
44-
def show(conn, %{"code" => code}, _resource_owner) do
43+
@spec show(Conn.t(), map(), map(), keyword()) :: Conn.t()
44+
def show(conn, %{"code" => code}, _resource_owner, _config) do
4545
render(conn, "show.html", code: code)
4646
end
4747

lib/phoenix_oauth2_provider/controllers/authorized_application_controller.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ defmodule PhoenixOauth2Provider.AuthorizedApplicationController do
33
use PhoenixOauth2Provider.Controller
44
alias ExOauth2Provider.Applications
55

6-
@spec index(Conn.t(), map(), map()) :: Conn.t()
7-
def index(conn, _params, resource_owner) do
8-
applications = Applications.get_authorized_applications_for(resource_owner)
6+
@spec index(Conn.t(), map(), map(), keyword()) :: Conn.t()
7+
def index(conn, _params, resource_owner, config) do
8+
applications = Applications.get_authorized_applications_for(resource_owner, config)
99

1010
render(conn, "index.html", applications: applications)
1111
end
1212

13-
@spec delete(Conn.t(), map(), map()) :: Conn.t()
14-
def delete(conn, %{"uid" => uid}, resource_owner) do
13+
@spec delete(Conn.t(), map(), map(), keyword()) :: Conn.t()
14+
def delete(conn, %{"uid" => uid}, resource_owner, config) do
1515
{:ok, _application} =
1616
uid
17-
|> Applications.get_application!()
18-
|> Applications.revoke_all_access_tokens_for(resource_owner)
17+
|> Applications.get_application!(config)
18+
|> Applications.revoke_all_access_tokens_for(resource_owner, config)
1919

2020
conn
2121
|> put_flash(:info, "Application revoked.")

lib/phoenix_oauth2_provider/controllers/token_controller.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ defmodule PhoenixOauth2Provider.TokenController do
44

55
alias ExOauth2Provider.Token
66

7-
@spec create(Conn.t(), map()) :: Conn.t()
8-
def create(conn, params) do
7+
@spec create(Conn.t(), map(), keyword()) :: Conn.t()
8+
def create(conn, params, config) do
99
params
10-
|> Token.grant()
10+
|> Token.grant(config)
1111
|> case do
1212
{:ok, access_token} ->
1313
json(conn, access_token)
@@ -19,10 +19,10 @@ defmodule PhoenixOauth2Provider.TokenController do
1919
end
2020
end
2121

22-
@spec revoke(Conn.t(), map()) :: Conn.t()
23-
def revoke(conn, params) do
22+
@spec revoke(Conn.t(), map(), keyword()) :: Conn.t()
23+
def revoke(conn, params, config) do
2424
params
25-
|> Token.revoke()
25+
|> Token.revoke(config)
2626
|> case do
2727
{:ok, response} ->
2828
json(conn, response)

0 commit comments

Comments
 (0)