Merge pull request #4 from Brale-xyz/matt/bra-8627
chore: rename from oauth2_token_manager to oauth2_token_agent to avoid conflict
This commit is contained in:
commit
5a5535a4a7
2
.gitignore
vendored
2
.gitignore
vendored
@ -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/
|
||||
|
||||
32
README.md
32
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 <https://hexdocs.pm/oauth2_token_manager>.
|
||||
be found at <https://hexdocs.pm/oauth2_token_agent>.
|
||||
|
||||
## 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
|
||||
|
||||
@ -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
|
||||
@ -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
|
||||
8
mix.exs
8
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
|
||||
|
||||
@ -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(
|
||||
@ -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)
|
||||
Loading…
Reference in New Issue
Block a user