Skip to content

Commit

Permalink
instead of shortening the period an existing valid token will be retu…
Browse files Browse the repository at this point in the history
…rned, just always generate a new one when requesting one
  • Loading branch information
tarzan committed Feb 19, 2024
1 parent d4ef4f5 commit 971f1f9
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 32 deletions.
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
25 changes: 0 additions & 25 deletions test/ex_oauth2_provider/access_tokens/access_tokens_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -116,31 +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

test "does not get the token when it expires within 5 minutes", %{application: application} do
{:ok, %{expires_in: expires_in} = access_token} =
AccessTokens.create_application_token(application, %{}, otp_app: :ex_oauth2_provider)

inserted_at = QueryHelpers.timestamp(OauthAccessToken, :inserted_at, seconds: -1 * expires_in + 299)
# the token will expire in 4min 59s
QueryHelpers.change!(access_token, inserted_at: inserted_at)

refute AccessTokens.get_application_token_for(application, 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 971f1f9

Please sign in to comment.