Skip to content

Commit

Permalink
Merge pull request #2 from DefactoSoftware/maarten/refresh-token-when…
Browse files Browse the repository at this point in the history
…-expires-within-5-minutes

Always regenerate a new token on valid client credentials
  • Loading branch information
tarzan authored Feb 19, 2024
2 parents 561876a + ca6e5d0 commit 2673725
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 41 deletions.
20 changes: 0 additions & 20 deletions lib/ex_oauth2_provider/access_tokens/access_tokens.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,6 @@ defmodule ExOauth2Provider.AccessTokens do
|> load_matching_token_for(application, scopes, config)
end

@doc """
Gets the most recent, acccessible, matching access token for an application.
## Examples
iex> get_application_token_for(application, "read write", otp_app: :my_app)
%OauthAccessToken{}
iex> get_application_token_for(application, "read invalid", otp_app: :my_app)
nil
"""
@spec get_application_token_for(Application.t(), binary(), keyword()) :: AccessToken.t() | nil
def get_application_token_for(application, scopes, config \\ []) do
config
|> Config.access_token()
|> scope_belongs_to(:resource_owner_id, nil)
|> scope_belongs_to(:application_id, application)
|> load_matching_token_for(application, scopes, config)
end

defp load_matching_token_for(queryable, application, scopes, config) do
now =
config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,7 @@ defmodule ExOauth2Provider.Token.ClientCredentials do
scopes: scopes
}

application
|> AccessTokens.get_application_token_for(scopes, config)
|> case do
nil -> AccessTokens.create_application_token(application, token_params, config)
access_token -> {:ok, access_token}
end
|> case do
case AccessTokens.create_application_token(application, token_params, config) do
{:ok, access_token} -> {:ok, Map.merge(params, %{access_token: access_token})}
{:error, error} -> Error.add_error({:ok, params}, error)
end
Expand Down
14 changes: 0 additions & 14 deletions test/ex_oauth2_provider/access_tokens/access_tokens_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,6 @@ defmodule ExOauth2Provider.AccessTokensTest do
end
end

describe "get_application_token_for/3" do
test "fetches", %{application: application} do
{:ok, access_token1} = AccessTokens.create_application_token(application, %{}, otp_app: :ex_oauth2_provider)
inserted_at = QueryHelpers.timestamp(OauthAccessToken, :inserted_at, seconds: -1)
QueryHelpers.change!(access_token1, inserted_at: inserted_at)
{:ok, access_token2} = AccessTokens.create_application_token(application, %{}, otp_app: :ex_oauth2_provider)

assert %OauthAccessToken{id: id} = AccessTokens.get_application_token_for(application, nil, otp_app: :ex_oauth2_provider)
assert id == access_token2.id

refute AccessTokens.get_application_token_for(Fixtures.application(uid: "application-2"), nil, otp_app: :ex_oauth2_provider)
end
end

test "get_authorized_tokens_for/2", %{user: user, application: application} do
{:ok, access_token} = AccessTokens.create_token(user, %{application: application}, otp_app: :ex_oauth2_provider)

Expand Down

0 comments on commit 2673725

Please sign in to comment.