From e8e12ca521bd0c1c1888e5e7cb7abac2cc61f607 Mon Sep 17 00:00:00 2001 From: Matt Franczak Date: Mon, 25 Mar 2024 10:59:15 -0500 Subject: [PATCH] chore: rename from oauth2_token_manager to oauth2_token_agent to avoid conflict --- .gitignore | 2 +- README.md | 32 +++++++++---------- .../token_agent.ex | 4 +-- .../token_refresh_strategy.ex | 2 +- mix.exs | 8 ++--- .../token_agent_test.exs | 4 +-- .../token_refresh_strategy_test.exs | 4 +-- 7 files changed, 28 insertions(+), 28 deletions(-) rename lib/{o_auth2_token_manager => oauth2_token_agent}/token_agent.ex (98%) rename lib/{o_auth2_token_manager => oauth2_token_agent}/token_refresh_strategy.ex (95%) rename test/{o_auth2_token_manager => oauth2_token_agent}/token_agent_test.exs (98%) rename test/{o_auth2_token_manager => oauth2_token_agent}/token_refresh_strategy_test.exs (96%) diff --git a/.gitignore b/.gitignore index 2e7e911..22372d3 100644 --- a/.gitignore +++ b/.gitignore @@ -20,7 +20,7 @@ erl_crash.dump *.ez # Ignore package tarball (built via "mix hex.build"). -oauth2_token_manager-*.tar +oauth2_token_agent-*.tar # Temporary files, for example, from tests. /tmp/ diff --git a/README.md b/README.md index 3c30297..45cd125 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# OAuth2TokenManager +# OAuth2TokenAgent This package works with the `oauth2` package to manage the automatic renewal of tokens before they expire @@ -6,25 +6,25 @@ tokens before they expire ## Installation If [available in Hex](https://hex.pm/docs/publish), the package can be installed -by adding `oauth2_token_manager` to your list of dependencies in `mix.exs`: +by adding `oauth2_token_agent` to your list of dependencies in `mix.exs`: ```elixir def deps do [ - {:oauth2_token_manager, "~> 0.1.0"} + {:oauth2_token_agent, "~> 0.1.0"} ] end ``` Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) and published on [HexDocs](https://hexdocs.pm). Once published, the docs can -be found at . +be found at . ## Usage Create an `OAuth2.Client` instance to use to get the initial token and pass it to -`OAuth2TokenMananger.TokenAgent.start_link/1` along with the inline -`OAuth2TokenManager.TokenRefreshStrategy` and the name for the `Agent`. +`OAuth2TokenAgent.TokenAgent.start_link/1` along with the inline +`OAuth2TokenAgent.TokenRefreshStrategy` and the name for the `Agent`. The client can be configured as described at https://github.com/ueberauth/oauth2#configure-a-http-client. @@ -37,34 +37,34 @@ client = Client.new([ site: "https://example.com/" ]) -{:ok, agent} = OAuth2TokenManager.TokenAgent.start_link( +{:ok, agent} = OAuth2TokenAgent.TokenAgent.start_link( name: MyModule.TokenAgent, initial_client: client, - inline_refresh_strategy: %OAuth2TokenManager.TokenRefreshStrategy{seconds_before_expires: 30} + inline_refresh_strategy: %OAuth2TokenAgent.TokenRefreshStrategy{seconds_before_expires: 30} ) ``` The current version of the client can be retrieved using -`OAuth2TokenManager.TokenAgent.get_client/1` with the agent's PID or name. +`OAuth2TokenAgent.TokenAgent.get_client/1` with the agent's PID or name. This client will automatically use the access token as a Bearer token in the Authorization header when calling its request methods. The access token itself can be retrieved from the client struct if it is desirable to use separately -configured clients and `OAuth2TokenManager.TokenAgent.get_access_token/1` is +configured clients and `OAuth2TokenAgent.TokenAgent.get_access_token/1` is provided for convenience when doing so. ```Elixir -current_client = OAuth2TokenManager.TokenAgent.get_client(agent) -current_client = OAuth2TokenManager.TokenAgent.get_client(MyModule.TokenAgent) -access_token = OAuth2TokenManager.TokenAgent.get_access_token(MyModule.TokenAgent) +current_client = OAuth2TokenAgent.TokenAgent.get_client(agent) +current_client = OAuth2TokenAgent.TokenAgent.get_client(MyModule.TokenAgent) +access_token = OAuth2TokenAgent.TokenAgent.get_access_token(MyModule.TokenAgent) ``` -The token can be refreshed by calling `OAuth2TokenManager.TokenAgent.refresh_tokens/1`. +The token can be refreshed by calling `OAuth2TokenAgent.TokenAgent.refresh_tokens/1`. If a refresh token is available, it will be exchanged for a new set of tokens. If no refresh token is available or the attempt to use the refresh results in an error, the original client will be used again to attempt to obtain a new set of tokens. ```Elixir -:ok = OAuth2TokenManager.TokenAgent.refresh(MyModule.TokenAgent) +:ok = OAuth2TokenAgent.TokenAgent.refresh(MyModule.TokenAgent) ``` This can be used to handle complex token refresh logic, but it will generally be @@ -91,7 +91,7 @@ The inline refreshes only occur as part of request for data from the agent; this saves unnecessary renewal requests in low-volume systems but tokens may be allowed to expire if unused. If refresh tokens need to be kept active in a system where the time between requests exceeds the token duration and the initial client cannot be -reused, using `OAuth2TokenManager.TokenAgent.refresh` may be necessary. +reused, using `OAuth2TokenAgent.TokenAgent.refresh` may be necessary. The inline refreshes occur during message processing in the agent, which is not concurrent per agent. This guarantees that redundant refresh calls will not be diff --git a/lib/o_auth2_token_manager/token_agent.ex b/lib/oauth2_token_agent/token_agent.ex similarity index 98% rename from lib/o_auth2_token_manager/token_agent.ex rename to lib/oauth2_token_agent/token_agent.ex index d0dd7ec..17a9540 100644 --- a/lib/o_auth2_token_manager/token_agent.ex +++ b/lib/oauth2_token_agent/token_agent.ex @@ -1,4 +1,4 @@ -defmodule OAuth2TokenManager.TokenAgent do +defmodule OAuth2TokenAgent.TokenAgent do @moduledoc """ Defines the Agent used to manage the token and the struct it uses to store its state """ @@ -7,7 +7,7 @@ defmodule OAuth2TokenManager.TokenAgent do use TypedStruct alias __MODULE__ - alias OAuth2TokenManager.TokenRefreshStrategy + alias OAuth2TokenAgent.TokenRefreshStrategy alias OAuth2.{AccessToken, Client, Error, Response} require Logger diff --git a/lib/o_auth2_token_manager/token_refresh_strategy.ex b/lib/oauth2_token_agent/token_refresh_strategy.ex similarity index 95% rename from lib/o_auth2_token_manager/token_refresh_strategy.ex rename to lib/oauth2_token_agent/token_refresh_strategy.ex index 434cf58..a5583d2 100644 --- a/lib/o_auth2_token_manager/token_refresh_strategy.ex +++ b/lib/oauth2_token_agent/token_refresh_strategy.ex @@ -1,4 +1,4 @@ -defmodule OAuth2TokenManager.TokenRefreshStrategy do +defmodule OAuth2TokenAgent.TokenRefreshStrategy do @moduledoc """ Module defining a struct for representing strategies for refreshing tokens and the functions for applying them diff --git a/mix.exs b/mix.exs index f11e226..2867260 100644 --- a/mix.exs +++ b/mix.exs @@ -1,15 +1,15 @@ -defmodule OAuth2TokenManager.MixProject do +defmodule OAuth2TokenAgent.MixProject do use Mix.Project def project do [ - app: :oauth2_token_manager, + app: :oauth2_token_agent, version: "0.1.0", elixir: "~> 1.15", description: description(), package: package(), deps: deps(), - source_url: "https://github.com/Brale-xyz/oauth2-token-manager" + source_url: "https://github.com/Brale-xyz/oauth2-token-agent" ] end @@ -40,7 +40,7 @@ defmodule OAuth2TokenManager.MixProject do defp package do [ licenses: ["Apache-2.0"], - links: %{"GitHub" => "https://github.com/Brale-xyz/oauth2-token-manager"} + links: %{"GitHub" => "https://github.com/Brale-xyz/oauth2-token-agent"} ] end end diff --git a/test/o_auth2_token_manager/token_agent_test.exs b/test/oauth2_token_agent/token_agent_test.exs similarity index 98% rename from test/o_auth2_token_manager/token_agent_test.exs rename to test/oauth2_token_agent/token_agent_test.exs index 2dca9fa..80de7cd 100644 --- a/test/o_auth2_token_manager/token_agent_test.exs +++ b/test/oauth2_token_agent/token_agent_test.exs @@ -1,4 +1,4 @@ -defmodule OAuth2TokenManager.TokenAgentTest do +defmodule OAuth2TokenAgent.TokenAgentTest do @moduledoc """ Tests for TokenAgent, which is used to track token state """ @@ -7,7 +7,7 @@ defmodule OAuth2TokenManager.TokenAgentTest do import Mock alias OAuth2.{AccessToken, Client} - alias OAuth2TokenManager.{TokenAgent, TokenRefreshStrategy} + alias OAuth2TokenAgent.{TokenAgent, TokenRefreshStrategy} defp test_client do Client.new( diff --git a/test/o_auth2_token_manager/token_refresh_strategy_test.exs b/test/oauth2_token_agent/token_refresh_strategy_test.exs similarity index 96% rename from test/o_auth2_token_manager/token_refresh_strategy_test.exs rename to test/oauth2_token_agent/token_refresh_strategy_test.exs index 699f14a..8069648 100644 --- a/test/o_auth2_token_manager/token_refresh_strategy_test.exs +++ b/test/oauth2_token_agent/token_refresh_strategy_test.exs @@ -1,11 +1,11 @@ -defmodule OAuth2TokenManager.TokenRefreshStrategyTest do +defmodule OAuth2TokenAgent.TokenRefreshStrategyTest do @moduledoc """ Tests the refresh strategies configured with TokenRefreshStrategy """ use ExUnit.Case - alias OAuth2TokenManager.TokenRefreshStrategy + alias OAuth2TokenAgent.TokenRefreshStrategy defp seconds_from_now(seconds) do DateTime.utc_now() |> DateTime.add(seconds, :second, Calendar.UTCOnlyTimeZoneDatabase)