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

Use :base_url instead of :site #140

Merged
merged 1 commit into from
Nov 19, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:
- run: mix deps.get
- run: mix compile --warnings-as-errors
- run: mix test
- run: mix credo
- run: mix credo --ignore design.tagtodo
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- `Req` supported by default as HTTP client
- Global application config support for HTTP and JWT adapters
- More expressive errors now including the whole HTTP response where applicable
- Deprecated `:site` config in favor of `:base_url` config

## v0.2.7 (2023-09-12)

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ defmodule TestProvider do
@impl true
def default_config(_config) do
[
# `:site` is the base URL used for any paths below
site: "http://localhost:4000/api/v1",
# Definining an absolute URI overrides the `:site` base URL
# `:base_url` will be used for any paths below
base_url: "http://localhost:4000/api/v1",
# Definining an absolute URI overrides the `:base_url`
authorize_url: "http://localhost:4000/oauth/authorize",
token_url: "/oauth/access_token",
user_url: "/user",
Expand Down
18 changes: 18 additions & 0 deletions lib/assent/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,22 @@ defmodule Assent.Config do
json_library
end
end

# TODO: Remove in next major version
def __base_url__(config) do
case fetch(config, :base_url) do
{:ok, base_url} ->
{:ok, base_url}

{:error, error} ->
case fetch(config, :site) do
{:ok, base_url} ->
IO.warn("The `:site` configuration key is deprecated, use `:base_url` instead")
{:ok, base_url}

{:error, _site_error} ->
{:error, error}
end
end
end
end
14 changes: 7 additions & 7 deletions lib/assent/strategies/apple.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ defmodule Assent.Strategy.Apple do

@impl true
def default_config(config) do
site = Config.get(config, :site, "https://appleid.apple.com")
base_url = Config.get(config, :base_url, "https://appleid.apple.com")

[
site: site,
base_url: base_url,
openid_configuration: %{
"issuer" => "https://appleid.apple.com",
"authorization_endpoint" => site <> "/auth/authorize",
"token_endpoint" => site <> "/auth/token",
"jwks_uri" => site <> "/auth/keys",
"authorization_endpoint" => base_url <> "/auth/authorize",
"token_endpoint" => base_url <> "/auth/token",
"jwks_uri" => base_url <> "/auth/keys",
"token_endpoint_auth_methods_supported" => ["client_secret_post"]
},
authorization_params: [scope: "email", response_mode: "form_post"],
Expand Down Expand Up @@ -89,14 +89,14 @@ defmodule Assent.Strategy.Apple do
|> default_config()
|> Keyword.merge(config)

with {:ok, site} <- Config.fetch(config, :site),
with {:ok, base_url} <- Config.fetch(config, :base_url),
{:ok, client_id} <- Config.fetch(config, :client_id),
{:ok, team_id} <- Config.fetch(config, :team_id),
:ok <- ensure_private_key_id(config),
{:ok, private_key} <- JWTAdapter.load_private_key(config) do

claims = %{
"aud" => site,
"aud" => base_url,
"iss" => team_id,
"sub" => client_id,
"iat" => timestamp,
Expand Down
2 changes: 1 addition & 1 deletion lib/assent/strategies/auth0.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule Assent.Strategy.Auth0 do

defp append_domain_config(config, default) do
case Config.fetch(config, :domain) do
{:ok, domain} -> Config.put(default, :site, prepend_scheme(domain))
{:ok, domain} -> Config.put(default, :base_url, prepend_scheme(domain))
_error -> default
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/assent/strategies/azure_ad.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule Assent.Strategy.AzureAD do
tenant_id = Config.get(config, :tenant_id, "common")

[
site: "https://login.microsoftonline.com/#{tenant_id}/v2.0",
base_url: "https://login.microsoftonline.com/#{tenant_id}/v2.0",
authorization_params: [scope: "email profile", response_mode: "form_post"],
client_authentication_method: "client_secret_post"
]
Expand Down
2 changes: 1 addition & 1 deletion lib/assent/strategies/basecamp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule Assent.Strategy.Basecamp do
@impl true
def default_config(_config) do
[
site: "https://launchpad.37signals.com",
base_url: "https://launchpad.37signals.com",
authorize_url: "/authorization/new",
token_url: "/authorization/token",
user_url: "/authorization.json",
Expand Down
2 changes: 1 addition & 1 deletion lib/assent/strategies/digital_ocean.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule Assent.Strategy.DigitalOcean do
@impl true
def default_config(config) do
[
site: "https://api.digitalocean.com",
base_url: "https://api.digitalocean.com",
authorize_url: "https://cloud.digitalocean.com/v1/oauth/authorize",
token_url: "https://cloud.digitalocean.com/v1/oauth/token",
user_url: "/v2/account",
Expand Down
2 changes: 1 addition & 1 deletion lib/assent/strategies/discord.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule Assent.Strategy.Discord do
@impl true
def default_config(_config) do
[
site: "https://discordapp.com/api",
base_url: "https://discordapp.com/api",
authorize_url: "/oauth2/authorize",
token_url: "/oauth2/token",
user_url: "/users/@me",
Expand Down
10 changes: 5 additions & 5 deletions lib/assent/strategies/facebook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ defmodule Assent.Strategy.Facebook do
@impl true
def default_config(_config) do
[
site: "https://graph.facebook.com/v#{@api_version}",
base_url: "https://graph.facebook.com/v#{@api_version}",
authorize_url: "https://www.facebook.com/v#{@api_version}/dialog/oauth",
token_url: "/oauth/access_token",
user_url: "/me",
Expand All @@ -85,22 +85,22 @@ defmodule Assent.Strategy.Facebook do

@impl true
def normalize(config, user) do
with {:ok, site} <- Config.fetch(config, :site) do
with {:ok, base_url} <- Config.fetch(config, :base_url) do
{:ok, %{
"sub" => user["id"],
"name" => user["name"],
"given_name" => user["first_name"],
"middle_name" => user["middle_name"],
"family_name" => user["last_name"],
"profile" => user["link"],
"picture" => picture_url(site, user),
"picture" => picture_url(base_url, user),
"email" => user["email"]
}}
end
end

defp picture_url(site, user) do
"#{site}/#{user["id"]}/picture"
defp picture_url(base_url, user) do
"#{base_url}/#{user["id"]}/picture"
end

@impl true
Expand Down
2 changes: 1 addition & 1 deletion lib/assent/strategies/github.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule Assent.Strategy.Github do
@impl true
def default_config(_config) do
[
site: "https://api.github.com",
base_url: "https://api.github.com",
authorize_url: "https://github.com/login/oauth/authorize",
token_url: "https://github.com/login/oauth/access_token",
user_url: "/user",
Expand Down
2 changes: 1 addition & 1 deletion lib/assent/strategies/gitlab.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule Assent.Strategy.Gitlab do
@impl true
def default_config(_config) do
[
site: "https://gitlab.com",
base_url: "https://gitlab.com",
authorize_url: "/oauth/authorize",
token_url: "/oauth/token",
user_url: "/api/v4/user",
Expand Down
2 changes: 1 addition & 1 deletion lib/assent/strategies/google.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule Assent.Strategy.Google do
@impl true
def default_config(_config) do
[
site: "https://www.googleapis.com",
base_url: "https://www.googleapis.com",
authorize_url: "https://accounts.google.com/o/oauth2/v2/auth",
token_url: "/oauth2/v4/token",
user_url: "/oauth2/v3/userinfo",
Expand Down
2 changes: 1 addition & 1 deletion lib/assent/strategies/instagram.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule Assent.Strategy.Instagram do
@impl true
def default_config(_config) do
[
site: "https://graph.instagram.com",
base_url: "https://graph.instagram.com",
authorize_url: "https://api.instagram.com/oauth/authorize",
token_url: "https://api.instagram.com/oauth/access_token",
user_url: "/me",
Expand Down
2 changes: 1 addition & 1 deletion lib/assent/strategies/line.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Assent.Strategy.LINE do
@impl true
def default_config(_config) do
[
site: "https://access.line.me",
base_url: "https://access.line.me",
authorization_params: [scope: "email profile", response_type: "code"],
id_token_signed_response_alg: "HS256"
]
Expand Down
2 changes: 1 addition & 1 deletion lib/assent/strategies/linkedin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule Assent.Strategy.Linkedin do
@impl true
def default_config(_config) do
[
site: "https://www.linkedin.com/oauth",
base_url: "https://www.linkedin.com/oauth",
authorization_params: [scope: "profile email"],
client_authentication_method: "client_secret_post"
]
Expand Down
34 changes: 17 additions & 17 deletions lib/assent/strategies/oauth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Assent.Strategy.OAuth do
## Configuration

- `:consumer_key` - The OAuth consumer key, required
- `:site` - The domain of the OAuth server, required
- `:base_url` - The base URL of the OAuth server, required
- `:signature_method` - The signature method, optional, defaults to
`:hmac_sha1`. The value may be one of the following:

Expand All @@ -28,7 +28,7 @@ defmodule Assent.Strategy.OAuth do
config = [
consumer_key: "REPLACE_WITH_CONSUMER_KEY",
consumer_secret: "REPLACE_WITH_CONSUMER_SECRET",
site: "https://auth.example.com",
base_url: "https://auth.example.com",
authorization_params: [scope: "user:read user:write"],
user_url: "https://example.com/api/user"
]
Expand Down Expand Up @@ -81,25 +81,25 @@ defmodule Assent.Strategy.OAuth do
end

defp get_request_token(config, oauth_params) do
with {:ok, site} <- Config.fetch(config, :site) do
with {:ok, base_url} <- Config.__base_url__(config) do
request_token_url = Config.get(config, :request_token_url, "/request_token")
url = process_url(site, request_token_url)
url = process_url(base_url, request_token_url)

config
|> do_request(:post, site, url, [], oauth_params)
|> do_request(:post, base_url, url, [], oauth_params)
|> process_token_response()
end
end

defp process_url(site, url) do
defp process_url(base_url, url) do
case String.downcase(url) do
<<"http://"::utf8, _::binary>> -> url
<<"https://"::utf8, _::binary>> -> url
_ -> site <> url
_ -> base_url <> url
end
end

defp do_request(config, method, site, url, params, oauth_params, headers \\ [], token_secret \\ nil) do
defp do_request(config, method, base_url, url, params, oauth_params, headers \\ [], token_secret \\ nil) do
params =
params
|> Enum.to_list()
Expand All @@ -112,7 +112,7 @@ defmodule Assent.Strategy.OAuth do
req_headers = request_headers(method, [signed_header] ++ headers)
req_body = request_body(method, params)
query_params = url_params(method, params)
url = Helpers.to_url(site, url, query_params)
url = Helpers.to_url(base_url, url, query_params)

Helpers.request(method, url, req_body, req_headers, config)
end
Expand Down Expand Up @@ -249,12 +249,12 @@ defmodule Assent.Strategy.OAuth do
defp process_response({:error, error}), do: {:error, error}

defp build_authorize_url({:ok, token}, config) do
with {:ok, site} <- Config.fetch(config, :site),
with {:ok, base_url} <- Config.__base_url__(config),
{:ok, oauth_token} <- fetch_from_token(token, "oauth_token"),
{:ok, oauth_token_secret} <- fetch_from_token(token, "oauth_token_secret") do
authorization_url = Config.get(config, :authorize_url, "/authorize")
params = authorization_params(config, oauth_token: oauth_token)
url = Helpers.to_url(site, authorization_url, params)
url = Helpers.to_url(base_url, authorization_url, params)

{:ok, url, oauth_token_secret}
end
Expand Down Expand Up @@ -304,13 +304,13 @@ defmodule Assent.Strategy.OAuth do
defp fetch_oauth_verifier(params), do: {:error, MissingParamError.exception(expected_key: "oauth_verifier", params: params)}

defp get_access_token(config, oauth_token, oauth_verifier) do
with {:ok, site} <- Config.fetch(config, :site) do
with {:ok, base_url} <- Config.__base_url__(config) do
access_token_url = Config.get(config, :access_token_url, "/access_token")
url = process_url(site, access_token_url)
url = process_url(base_url, access_token_url)
oauth_token_secret = Kernel.get_in(config, [:session_params, :oauth_token_secret])

config
|> do_request(:post, site, url, [], [{"oauth_token", oauth_token}, {"oauth_verifier", oauth_verifier}], [], oauth_token_secret)
|> do_request(:post, base_url, url, [], [{"oauth_token", oauth_token}, {"oauth_verifier", oauth_verifier}], [], oauth_token_secret)
|> process_token_response()
end
end
Expand All @@ -320,12 +320,12 @@ defmodule Assent.Strategy.OAuth do
"""
@spec request(Config.t(), map(), atom(), binary(), map() | Keyword.t(), [{binary(), binary()}]) :: {:ok, map()} | {:error, term()}
def request(config, token, method, url, params \\ [], headers \\ []) do
with {:ok, site} <- Config.fetch(config, :site),
with {:ok, base_url} <- Config.__base_url__(config),
{:ok, oauth_token} <- fetch_from_token(token, "oauth_token"),
{:ok, oauth_token_secret} <- fetch_from_token(token, "oauth_token_secret") do
url = process_url(site, url)
url = process_url(base_url, url)

do_request(config, method, site, url, params, [{"oauth_token", oauth_token}], headers, oauth_token_secret)
do_request(config, method, base_url, url, params, [{"oauth_token", oauth_token}], headers, oauth_token_secret)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/assent/strategies/oauth/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Assent.Strategy.OAuth.Base do
@impl true
def default_config(_config) do
[
site: "https://api.example.com",
base_url: "https://api.example.com",
authorize_url: "/authorization/new",
access_token_url: "/authorization/access_token"
request_token_url: "/authorization/request_token",
Expand Down
Loading
Loading