Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for no auth method in access token request in OAuth2 strategy #24

Merged
merged 1 commit into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Removed unused `:resource` param in `Assent.Strategy.AzureAD`
* Added "email profile" to scope in `Assent.Strategy.AzureAD`
* Use `response_mode=form_post` for `Assent.Strategy.AzureAD`
* Updated `Assent.Strategy.OAuth2` to handle access token request correctly when `:auth_method` is `nil` per RFC specs

## v0.1.4 (2019-11-09)

Expand Down
16 changes: 13 additions & 3 deletions lib/assent/strategies/oauth2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ defmodule Assent.Strategy.OAuth2 do

- `:client_id` - The OAuth2 client id, required
- `:site` - The domain of the OAuth2 server, required
- `:auth_method` - The authentication strategy used, optional, defaults to
`:client_secret_basic`. The value may be one of the following:
- `:auth_method` - The authentication strategy used, optional. If not set,
no authentication will be used during the access token request. The value
may be one of the following:

- `:client_secret_basic` - Authenticate with basic authorization header
- `:client_secret_post` - Authenticate with post params
Expand Down Expand Up @@ -139,6 +140,15 @@ defmodule Assent.Strategy.OAuth2 do
do: {:error, %CallbackCSRFError{}}
defp do_check_state(_state, _params), do: :ok

defp authentication_params(nil, config) do
with {:ok, client_id} <- Config.fetch(config, :client_id) do

headers = []
body = [client_id: client_id]

{:ok, headers, body}
end
end
defp authentication_params(:client_secret_basic, config) do
with {:ok, client_id} <- Config.fetch(config, :client_id),
{:ok, client_secret} <- Config.fetch(config, :client_secret) do
Expand Down Expand Up @@ -207,7 +217,7 @@ defmodule Assent.Strategy.OAuth2 do
end

defp get_access_token(config, %{"code" => code}) do
auth_method = Config.get(config, :auth_method, :client_secret_basic)
auth_method = Config.get(config, :auth_method, nil)
token_url = Config.get(config, :token_url, "/oauth/token")

with {:ok, site} <- Config.fetch(config, :site),
Expand Down
16 changes: 16 additions & 0 deletions test/assent/strategies/oauth2_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,23 @@ defmodule Assent.Strategy.OAuth2Test do

@user_api_params %{name: "Dan Schultzer", email: "foo@example.com", uid: "1"}

test "with no auth method", %{config: config, callback_params: params, bypass: bypass} do
expect_oauth2_access_token_request(bypass, [], fn _conn, params ->
assert params["grant_type"] == "authorization_code"
assert params["code"] == "test"
assert params["redirect_uri"] == "http://localhost:4000/auth/callback"
assert params["client_id"] == @client_id
refute params["client_secret"]
end)

expect_oauth2_user_request(bypass, @user_api_params)

assert {:ok, _any} = OAuth2.callback(config, params)
end

test "with `:client_secret_basic` auth method", %{config: config, callback_params: params, bypass: bypass} do
config = Keyword.put(config, :auth_method, :client_secret_basic)

expect_oauth2_access_token_request(bypass, [], fn conn, params ->
assert [{"authorization", "Basic " <> token} | _rest] = conn.req_headers
assert Base.url_decode64(token, padding: false) == {:ok, "#{@client_id}:#{@client_secret}"}
Expand Down