Skip to content

Commit c1e5205

Browse files
committed
Rewrite to functional configuration, separate mix tasks, and built in templates
1 parent 9287c3c commit c1e5205

File tree

82 files changed

+1222
-1904
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+1222
-1904
lines changed

.gitignore

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
/.fetch
66
erl_crash.dump
77
*.ez
8-
9-
/priv/test/migrations/*
10-
!/priv/test/migrations/20170320052040_create_user.exs
11-
/priv/test/tmp
8+
/tmp
9+
.DS_Store
10+
/.elixir_ls

.travis.yml

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,13 @@ language: elixir
22
sudo: false
33
matrix:
44
include:
5-
- elixir: 1.4
6-
otp_release: 19.0
5+
- elixir: 1.8
6+
otp_release: 21.0
77
env: MIX_ENV=test
8-
- elixir: 1.5
9-
otp_release: 19.0
10-
env: MIX_ENV=test
11-
- elixir: 1.6
12-
otp_release: 20.0
13-
env: MIX_ENV=test
14-
- elixir: 1.7
15-
otp_release: 20.0
16-
env: MIX_ENV=test UUID=resource_owners
17-
- elixir: 1.7
18-
otp_release: 20.0
19-
env: MIX_ENV=test UUID=all
8+
- elixir: 1.8
9+
otp_release: 21.0
10+
env: MIX_ENV=test UUID=true
2011
script:
21-
- mix deps.compile
2212
- mix test
2313
after_script:
2414
- mix credo

CHANGELOG.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Changelog
2+
3+
## v0.5.0 (TBA)
4+
5+
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.
6+
7+
### 1. ExOauth2Provider
8+
9+
Read the [ExOauth2Provider CHANGELOG.md](https://github.com/danschultzer/ex_oauth2_provider) for upgrade instructions.
10+
11+
### 2. Routes
12+
13+
Routes are now separated into api and non api routes. Update your routes like so:
14+
15+
```elixir
16+
defmodule MyAppWeb.Router do
17+
use MyAppWeb, :router
18+
use PhoenixOauth2Provider.Router
19+
20+
# ...
21+
22+
pipeline :protected do
23+
# Require user authentication
24+
end
25+
26+
scope "/" do
27+
pipe_through :api
28+
29+
oauth_api_routes()
30+
end
31+
32+
scope "/" do
33+
pipe_through [:browser, :protected]
34+
35+
oauth_routes()
36+
end
37+
38+
# ...
39+
end
40+
```
41+
42+
### 3. Templates and views
43+
44+
Update `:module` to `:web_module` in your configuration. Templates and views are no longer required to be generated so you can remove them entirely if the default ones work for you.

README.md

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
[![Build Status](https://travis-ci.org/danschultzer/phoenix_oauth2_provider.svg?branch=master)](https://travis-ci.org/danschultzer/phoenix_oauth2_provider) [![hex.pm](http://img.shields.io/hexpm/v/phoenix_oauth2_provider.svg?style=flat)](https://hex.pm/packages/phoenix_oauth2_provider) [![hex.pm downloads](https://img.shields.io/hexpm/dt/phoenix_oauth2_provider.svg?style=flat)](https://hex.pm/packages/phoenix_oauth2_provider)
44

5-
Get an OAuth 2 provider running in your Phoenix app with controllers, views and models in just two minutes.
6-
7-
> This version requires Phoenix 1.3 or higher. If you use a previous Phoenix version, please use v0.2.0 instead.
5+
Get an OAuth 2.0 provider running in your Phoenix app with schema modules and templates in just two minutes.
86

97
## Installation
108

@@ -22,12 +20,16 @@ end
2220

2321
Run `mix deps.get` to install it.
2422

25-
Add migrations and set up `config/config.exs`:
23+
## Getting started
24+
25+
Install ExOauthProvider first:
2626

2727
```bash
28-
mix phoenix_oauth2_provider.install
28+
mix ex_oauth2_provider.install
2929
```
3030

31+
Follow the instructions to update `config/config.exs`.
32+
3133
Set up routes:
3234

3335
```elixir
@@ -41,18 +43,16 @@ defmodule MyAppWeb.Router do
4143
# Require user authentication
4244
end
4345

44-
pipeline :oauth_public do
45-
plug :put_secure_browser_headers
46-
end
46+
scope "/" do
47+
pipe_through :api
4748

48-
scope "/", MyAppWeb do
49-
pipe_through :oauth_public
50-
oauth_routes :public
49+
oauth_api_routes()
5150
end
5251

53-
scope "/", MyAppWeb do
54-
pipe_through :protected
55-
oauth_routes :protected
52+
scope "/" do
53+
pipe_through [:browser, :protected]
54+
55+
oauth_routes()
5656
end
5757

5858
# ...
@@ -61,7 +61,7 @@ end
6161

6262
That's it! The following OAuth 2.0 routes will now be available in your app:
6363

64-
```
64+
```text
6565
oauth_authorize_path GET /oauth/authorize AuthorizationController :new
6666
oauth_authorize_path POST /oauth/authorize AuthorizationController :create
6767
oauth_authorize_path GET /oauth/authorize/:code AuthorizationController :show
@@ -70,33 +70,34 @@ oauth_token_path POST /oauth/token TokenController :create
7070
oauth_token_path POST /oauth/revoke TokenController :revoke
7171
```
7272

73-
Please read the [ex_oauth2_provider](https://github.com/danschultzer/ex_oauth2_provider) documentation for further customization.
73+
Please read the [ExOauth2Provider](https://github.com/danschultzer/ex_oauth2_provider) documentation for further customization.
7474

7575
## Configuration
7676

77-
### Resource owner schema
77+
### Templates
7878

79-
By default `MyApp.User` is used as the `resource_owner`, you can change that in the following way:
79+
To generate views and templates run:
80+
81+
```bash
82+
mix phoenix_oauth2_provider.gen.templates
83+
```
84+
85+
Set up the PhoenixOauth2Provider configuration with `:web_module`:
8086

8187
```elixir
82-
config :phoenix_oauth2_provider, PhoenixOauth2Provider,
83-
repo: MyApp.Repo,
84-
resource_owner: MyApp.CustomUser
88+
config :my_app, PhoenixOauth2Provider,
89+
web_module: MyAppWeb
8590
```
8691

87-
### Resource owner
92+
### Current resource owner
8893

89-
Set up what `assigns` in the plug that PhoenixOauth2Provider should gather the authorized user from.
94+
Set up what key in the plug conn `assigns` that PhoenixOauth2Provider should use to fetch the current resource owner.
9095

9196
```elixir
92-
config :phoenix_oauth2_provider, PhoenixOauth2Provider,
97+
config :my_app, PhoenixOauth2Provider,
9398
current_resource_owner: :current_user
9499
```
95100

96-
## Acknowledgement
97-
98-
This library was made thanks to [coherence](https://github.com/smpallen99/coherence) that gave the conceptual building blocks.
99-
100101
## LICENSE
101102

102103
(The MIT License)

config/test.exs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
11
use Mix.Config
22

3-
config :phoenix_oauth2_provider, PhoenixOauth2Provider,
4-
module: PhoenixOauth2Provider.Test,
5-
current_resource_owner: :current_test_user,
6-
repo: PhoenixOauth2Provider.Test.Repo,
7-
resource_owner: PhoenixOauth2Provider.Test.User,
8-
scopes: ~w(read write),
9-
router: PhoenixOauth2Provider.Test.Router,
10-
use_refresh_token: true
3+
config :phoenix, :json_library, Jason
4+
5+
config :phoenix_oauth2_provider, namespace: Dummy
6+
7+
config :phoenix_oauth2_provider, DummyWeb.Endpoint,
8+
secret_key_base: "1lJGFCaor+gPGc21GCvn+NE0WDOA5ujAMeZoy7oC5un7NPUXDir8LAE+Iba5bpGH",
9+
render_errors: [view: DummyWeb.ErrorView, accepts: ~w(html json)]
1110

12-
config :phoenix_oauth2_provider, ecto_repos: [PhoenixOauth2Provider.Test.Repo]
13-
config :phoenix_oauth2_provider, PhoenixOauth2Provider.Test.Repo,
14-
adapter: Ecto.Adapters.Postgres,
11+
config :phoenix_oauth2_provider, Dummy.Repo,
1512
database: "phoenix_oauth2_provider_test",
1613
pool: Ecto.Adapters.SQL.Sandbox,
17-
priv: "priv/test"
18-
config :phoenix_oauth2_provider, PhoenixOauth2Provider.Test.Endpoint,
19-
secret_key_base: "1lJGFCaor+gPGc21GCvn+NE0WDOA5ujAMeZoy7oC5un7NPUXDir8LAE+Iba5bpGH",
20-
render_errors: [view: PhoenixOauth2Provider.Test.ErrorView, accepts: ~w(html json)]
14+
priv: "test/support/priv"
2115

22-
unless is_nil(System.get_env("UUID")) do
23-
config :phoenix_oauth2_provider, PhoenixOauth2Provider,
24-
resource_owner: {PhoenixOauth2Provider.Test.User, :binary_id}
25-
end
16+
config :phoenix_oauth2_provider, ExOauth2Provider,
17+
repo: Dummy.Repo,
18+
resource_owner: Dummy.Users.User,
19+
scopes: ~w(read write),
20+
use_refresh_token: true
2621

27-
if System.get_env("UUID") == "all" do
2822
config :phoenix_oauth2_provider, PhoenixOauth2Provider,
29-
app_schema: ExOauth2Provider.Schema.UUID
30-
end
23+
current_resource_owner: :current_test_user
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
defmodule Mix.PhoenixOauth2Provider.Template do
2+
@moduledoc false
3+
4+
alias ExOauth2Provider.Config
5+
alias Mix.{Generator, Phoenix}
6+
7+
@views ["application", "authorization", "authorized_application"]
8+
@view_template """
9+
defmodule <%= inspect view_module %> do
10+
use <%= inspect web_module %>, :view
11+
end
12+
"""
13+
14+
@spec create_view_and_template_files(binary()) :: map()
15+
def create_view_and_template_files(context_app) do
16+
web_module =
17+
context_app
18+
|> web_app()
19+
|> Macro.camelize()
20+
|> List.wrap()
21+
|> Module.concat()
22+
web_path = web_path(context_app)
23+
24+
for name <- @views do
25+
create_view_file(web_module, web_path, name)
26+
create_template_files(web_path, name)
27+
end
28+
end
29+
30+
defp create_view_file(web_module, web_path, name) do
31+
view_name = "#{name}_view"
32+
dir = Path.join([web_path, "views", "phoenix_oauth2_provider"])
33+
path = Path.join(dir, "#{view_name}.ex")
34+
view_module = Module.concat([web_module, PhoenixOauth2Provider, Macro.camelize(view_name)])
35+
content = EEx.eval_string(@view_template, view_module: view_module, web_module: web_module)
36+
37+
Generator.create_directory(dir)
38+
Generator.create_file(path, content)
39+
end
40+
41+
defp create_template_files(web_path, name) do
42+
view_module = Module.concat([PhoenixOauth2Provider, Macro.camelize("#{name}_view")])
43+
44+
for template <- view_module.templates() do
45+
content = view_module.html(template)
46+
dir = Path.join([web_path, "templates", "phoenix_oauth2_provider", name])
47+
path = Path.join(dir, "#{template}.eex")
48+
49+
Generator.create_directory(dir)
50+
Generator.create_file(path, content)
51+
end
52+
end
53+
54+
defp web_path(context_app), do: Phoenix.web_path(context_app)
55+
56+
defp web_app(ctx_app) do
57+
this_app = Config.otp_app()
58+
59+
if ctx_app == this_app do
60+
"#{ctx_app}_web"
61+
else
62+
ctx_app
63+
end
64+
end
65+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
defmodule Mix.Tasks.PhoenixOauth2Provider.Gen.Templates do
2+
@shortdoc "Generates PhoenixOauth2Provider templates"
3+
4+
@moduledoc """
5+
Generates views and templates.
6+
7+
mix phoenix_oauth2_provider.gen.templates
8+
9+
## Arguments
10+
11+
* `--context-app` - context app to use for path and module names
12+
"""
13+
use Mix.Task
14+
15+
alias ExOauth2Provider.Config
16+
alias Mix.ExOauth2Provider
17+
alias Mix.PhoenixOauth2Provider.Template
18+
19+
@switches [context_app: :string]
20+
@default_opts []
21+
@mix_task "phoenix_oauth2_provider.gen.templates"
22+
23+
@impl true
24+
def run(args) do
25+
ExOauth2Provider.no_umbrella!(@mix_task)
26+
27+
args
28+
|> ExOauth2Provider.parse_options(@switches, @default_opts)
29+
|> parse()
30+
|> create_template_files()
31+
end
32+
33+
defp parse({config, _parsed, _invalid}), do: config
34+
35+
defp create_template_files(config) do
36+
config
37+
|> Map.get(:context_app)
38+
|> Kernel.||(Config.otp_app())
39+
|> Template.create_view_and_template_files()
40+
end
41+
end

0 commit comments

Comments
 (0)