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

Add storage backed token source #63

Merged
merged 1 commit into from
Mar 14, 2024

Conversation

andrewhowdencom
Copy link
Owner

In order to have a client application persist through multiple invocations (essentially the requirement for a CLI application), the token needs to be stored outside the application in some persistence. This commit provides that persistence.

The general design of this work is to wrap a token source, and if underlying token source changes, then write that token back to storage. Then, whenever the application is reloaded, refresh the token source being used with whatever was in the cache.

This also means that there needs to be something that "seeds" the cache; for example, if the user doesn't actually have authentication just yet. To address this, a new primitive called a "Seed function" (essentially the actual authenticatino process rather than refresh) was created. This allows plugging in the auth to the token refresh cycle.

== Design Notes
=== Storage Implementations

The primary storage implementation is a file, intended to be used with a path supplied by the XDG library. This would allow storing tokens in ~/.local/share/x40/.token.json, which is a logical place for them.

There's also an implementation in memory (called "Test"), designed for ... well, testing. There's not really a limit to what other interfaces can be provided in future — I'd probably try and write a BoltDB one if this filesystem one proved buggy (so as to defer the hard™ parts to boltdb).

=== NewCacheSource

The signature for new cache source is complicated. The problem is testability — the underlying oauth library has a bunch of stuff private, which prevents assertions about its state or reuse within tests.

In order to have a client application persist through multiple
invocations (essentially the requirement for a CLI application), the
token needs to be stored outside the application in some persistence.
This commit provides that persistence.

The general design of this work is to wrap a token source, and if
underlying token source changes, then write that token back to storage.
Then, whenever the application is reloaded, refresh the token source
being used with whatever was in the cache.

This also means that there needs to be something that "seeds" the cache;
for example, if the user doesn't actually have authentication just yet.
To address this, a new primitive called a "Seed function" (essentially
the actual authenticatino process rather than refresh) was created. This
allows plugging in the auth to the token refresh cycle.

== Design Notes
=== Storage Implementations

The primary storage implementation is a file, intended to be used with a
path supplied by the XDG library. This would allow storing tokens in
~/.local/share/x40/.token.json, which is a logical place for them.

There's also an implementation in memory (called "Test"), designed for
... well, testing. There's not really a limit to what other interfaces
can be provided in future — I'd probably try and write a BoltDB one if
this filesystem one proved buggy (so as to defer the hard™ parts to
boltdb).

=== NewCacheSource

The signature for new cache source is complicated. The problem is
testability — the underlying oauth library has a bunch of stuff private,
which prevents assertions about its state or reuse within tests.
@andrewhowdencom andrewhowdencom force-pushed the add-filesystem-backed-token-refresher branch from fbac5d8 to ccc875f Compare January 26, 2024 17:25
@andrewhowdencom andrewhowdencom merged commit 8692103 into main Mar 14, 2024
1 check passed
@andrewhowdencom andrewhowdencom deleted the add-filesystem-backed-token-refresher branch March 14, 2024 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant