97 Commits

Author SHA1 Message Date
Lewin Bormann
194f6a3b1d Remove misleading old examples 2023-04-01 07:54:37 +02:00
Joe Neeman
9e31f1912f Expand example. 2022-11-23 14:46:36 -06:00
Joe Neeman
923a149e99 Add support for generating impersonated ids.
The previous service account impersonation feature only allowed requesting
impersonated access tokens. This one adds id tokens.
2022-11-23 14:43:40 -06:00
Lewin Bormann
78b79cf92c Remove very outdated service_account example 2022-10-27 20:49:28 +02:00
Lewin Bormann
7c9c92163c Remove very outdated drive_example 2022-10-27 20:47:17 +02:00
Joe Neeman
e907226c3c Implement service account impersonation. 2022-10-17 16:23:19 -05:00
Brandon Ogle
db8dd7d68d Fix tests and examples to use token() method on AccessToken 2022-10-10 12:46:45 -07:00
Brandon Ogle
2d805cf19e Merge branch 'nagisa/id_token' of github.com:nagisa/yup-oauth2 into nagisa-nagisa/id_token 2022-09-21 18:51:59 -07:00
Kyle Gentle
c76ae18224 feat(Authenticator client): Accept custom connectors
Update Authenticator to accept clients with custom connectors, rather
than depending on the sealed hyper::client::connect::Connect trait, as recommended by hyper: https://docs.rs/hyper/0.13.8/src/hyper/client/connect/mod.rs.html#256-258

Closes #177.
2022-05-22 16:29:02 -04:00
Dirkjan Ochtman
f9c59bb743 Upgrade rustls and related dependencies 2022-04-06 11:22:05 +02:00
Lukas Winkler
7e90b28cf2 Add myself as adc sample author 2021-11-25 19:54:12 +01:00
Lukas Winkler
169e5ff1c0 Allow overriding metadata url used during testing 2021-11-25 19:37:11 +01:00
Antti Peltonen
921f1c7190 builder pattern for adc struct 2021-11-21 09:57:07 +01:00
Antti Peltonen
7638946508 work started on adc implementation 2021-11-21 09:57:06 +01:00
Simonas Kazlauskas
4e54fbaeaf Implement support for ID tokens
For google stuff these are relevant when trying to invoke e.g. Cloud
Run services. I'm not at all knowledgeable enough with OAuth to be able
to tell if what I'm doing here is correct.

This is a breaking change. `AccessToken` got renamed to just `Token`
(since it now encompasses more than just `access_token` and there are
some changes to the `TokenInfo` type too.

Sponsored by: standard.ai
2021-07-19 16:23:37 +03:00
Simonas Kazlauskas
dd004fed3c Implement an ability to work without default client 2021-06-29 13:30:40 +03:00
Lewin Bormann
e63aa4b843 Merge pull request #146 from djrodgerspryor/custom_storage_options
Custom token storage
2021-04-01 22:53:36 +02:00
Daniel Rodgers-Pryor
6e4503f55e Simplify with_storage interface
By only allowing a custom storage. To use one of the built-in storage mechanism, there is already a special-purpose `persist_tokens_to_disk` method available.
2021-03-25 21:20:21 +11:00
Daniel Rodgers-Pryor
98ee92f8b2 Don't publish the ScopeSet struct
Just pass `&[&str]` into custom storage providers. The scopeset struct has a range of unnecessary internal features.

It's now also part of the interface for custom storage providers that the given scopes will be both unique and sorted.

The only slightly awkward thing is that there's no conventient way to expose a `scopes_covered_by` helper method (which almost all custom storage engines will need), but it's still included in the example code.
2021-03-25 21:08:12 +11:00
Lewin Bormann
e3b2509753 Make drive_example compile and add warning 2021-03-04 11:40:36 +01:00
Lewin Bormann
88e36b19f9 Make service_account example compile and add README with warning 2021-03-04 11:38:05 +01:00
Daniel Rodgers-Pryor
384963e091 Revert mutable authenticator interface change
Instead, suggest using interior mutability (and RwLock in the example) to manage storage of token states. This makes it easier to share authenticators between threads.
2021-02-06 22:59:00 +11:00
Daniel Rodgers-Pryor
5ef498f801 Custom token storage
Allow users to build their own token storage system by implementing the `TokenStorage` trait. This allows use of more secure storage mechanisms like OS keychains, encrypted files, or secret-management tools.

Custom storage providers are Box-ed to avoid adding more generics to the API — the indirection cost will only apply if using a custom store.

I've added `anyhow` to allow easy handling of a wide range of errors from custom storage providers.
2021-02-06 22:59:00 +11:00
Lewin Bormann
864f918496 Merge pull request #138 from maximebedard/bump-tokio-0-3
Bump to tokio 1.0 and hyper 0.14
2020-12-27 09:05:14 +01:00
Maxime Bedard
c5bc3913d7 remove unecessary rt feature flag 2020-12-24 09:39:10 -05:00
Maxime Bedard
ec28689080 bump hyper=0.14, tokio=1.0, hyper-rustls, httptest 2020-12-24 09:31:18 -05:00
Michael Kefeder
adc99a10ec example showing howto implement a custom flow, here opening the
default webbrowser with the oauth2 URL for the installed flow.
2020-11-16 09:52:08 +01:00
Lewin Bormann
be4faf944b drive_example: Make example compile again. Fixes #134 2020-10-27 15:25:16 +01:00
Lewin Bormann
55a8984c81 Another slight update to README 2020-10-18 22:42:14 +02:00
Glenn Griffin
1d5c3a4512 Switch from mockito to httptest 2019-12-18 09:07:45 -08:00
Glenn Griffin
9238153723 Move to hyper 0.13.1!!!! 2019-12-18 09:07:45 -08:00
Glenn Griffin
8e38d3976b Make helpers that read from disk async 2019-12-18 09:07:45 -08:00
Glenn Griffin
e72b4c2335 Rename service_account_key_from_file to read_service_account_key
This makes the name consistent with the other helper
read_application_secret.
2019-12-18 09:07:45 -08:00
Glenn Griffin
5256f642d7 Tie ServiceAccount's into Authenticator.
Prior to this change DeviceFlow and InstalledFlow were used within
Authenticator, while ServiceAccountAccess was used on it's own. AFAICT
this was the case because ServiceAccountAccess never used refresh tokens
and Authenticator assumed all tokens contained refresh tokens.
Authenticator was recently modified to handle the case where a token
does not contain a refresh token so I don't see any reason to keep the
service account access separate anymore. Folding it into the
authenticator provides a nice consistent interface, and the service
account implementation no longer needs to provide it's own caching since
it is now handled by Authenticator.
2019-12-18 09:07:45 -08:00
Glenn Griffin
3aadc6b0ef Major refactor of the public API.
1) Remove the GetToken trait. The trait seemed to be organically
designed. It appeared to be mostly tailored for simplifying the
implementation since there was no way for users to provide their own
implementation to Authenticator. It sadly seemed to get in the way of
implementations more than it helped. An enum representing the known
implementations is a more straightforward way to accomplish the goal and
also has the benefit of not requiring Boxing when returning features
(which admittedly is a minor concern for this use case).

2) Reduce the number of type parameters by using trait object for
delegates. This simplifies the code considerably and the performance
impact of virtual dispatch for the delegate calls is a non-factor.

3) With the above two simplifications it became easier to unify the
public interface for building an authenticator. See the examples for how
InstalledFlow, DeviceFlow, and ServiceAccount authenticators are now created.
2019-12-18 08:57:24 -08:00
Glenn Griffin
88a8f74406 Refactor token storage.
The current code uses standard blocking i/o operations (std::fs::*) this
is problematic as it would block the entire futures executor waiting for
i/o.

This change is a major refactoring to make the token storage mechansim
async i/o friendly. The first major decision was to abandon the GetToken
trait. The trait is only implemented internally and there was no
mechanism for users to provide their own, but async fn's are not
currently supported in trait impls so keeping the trait would have
required Boxing futures. This probably would have been fine, but seemed
unnecessary. Instead of a trait the storage mechanism is just an enum
with a choice between Memory and Disk storage.

The DiskStorage works primarily as it did before, rewriting the entire
contents of the file on every set() invocation. The only difference is
that we now defer the actual writing to a separate task so that it does
not block the return of the Token to the user. If disk i/o is too slow
to keep up with the rate of incoming writes it will push back and
will eventually block the return of tokens, this is to prevent a buildup
of in-flight requests. One major drawback to this approach is that any
errors that happen on write are simply logged and no delegate function
is invoked on error because the delegate no longer has the ability to
say to sleep, retry, etc.
2019-12-18 08:57:24 -08:00
Glenn Griffin
060eb92bf7 Refactor JWT handling in ServiceAccountAccess.
Avoid reading and parsing the private key file on every invocation of
token() in favor or reading it once when the ServiceAccountAccess is
built. Also avoid unnecessary allocations when signing JWT tokens and
renamed sub to subject to avoid any confusion with the std::ops::Sub
trait.
2019-12-18 08:53:22 -08:00
Glenn Griffin
0e9cf512ba Remove the HTTPRedirectEphemeral variant.
In favor of making it the default and removing the option to specify a
port to listen on. If needed a variant can be added to specify a port
explicitly, but most users should want an ephemeral port chosen so
making it the default makes sense while other breaking changes are in
flight.
2019-12-18 08:53:22 -08:00
Glenn Griffin
4bd81c3263 cargo fmt 2019-12-18 08:53:22 -08:00
Glenn Griffin
696577aa01 Accept scopes as a slice of anything that can produce a &str.
Along with the public facing change the implementation has been modified
to no longer clone the scopes instead using the pointer to the scopes
the user provided. This greatly reduces the number of allocations on
each token() call.

Note that this also changes the hashing method used for token storage in
an incompatible way with the previous implementation. The previous
implementation pre-sorted the vector and hashed the contents to make the
result independent of the ordering of the scopes. Instead we now combine
the hash values of each scope together with XOR, thus producing a hash
value that does not depend on order without needing to allocate another
vector and sort.
2019-12-18 08:53:22 -08:00
Glenn Griffin
93cbd91341 Move to std::futures to support async/await. 2019-12-18 08:53:22 -08:00
Aaron Hill
9597a05dc8 chore(dependencies): Update ring and hyper-rustls
This allows downstream crates to use newer versions of ring.
2019-09-28 16:57:29 -04:00
Glenn Griffin
ccc6601ff3 Use the builder pattern to create authenticators.
Beyond simply moving to the builder pattern for intialization this has a
few other effects.

The DeviceFlow and InstalledFlow can no longer be used without an
associated Authenticator. This is becaus they no longer have any
publicly accessible constructor. All initialization goes through the
Authenticator. This also means that the flows are always initialized
with a clone of the hyper client used by the Authenticator.

The authenticator uses the builder pattern which allows omitting
optional fields. This means that if users simply want a default hyper
client, they don't need to create one explicitly. One will be created
automatically. If users want to specify a hyper client (maybe to allow
sharing a single client between different libraries) they can still do so
by using the hyper_client method on the builder. Additionally for both
AuthenticatorDelegate's and FlowDelegate's if the user does not specify
an override the default ones will be used.

The builders are now exposed publicly with the names of Authenicator,
InstalledFlow, and DeviceFlow. The structs that actually implement those
behaviors are now hidden and only expose the GetToken trait. This means
some methods that were previously publicly accessible are no longer
available, but the methods appeared to be implementation details that
probably shouldn't have been exposed anyway.
2019-08-29 11:47:15 -07:00
Glenn Griffin
eb2a82f685 Make listening on an ephemeral port an option rather than the default. 2019-08-29 09:59:10 -07:00
Glenn Griffin
e83ec7e25e Have the installed flow http server always listen on an ephemeral port.
Specifying a port of zero has the server listen on an ephemeral port.
Many users may not be aware of that unless they have a background in
networking where that's common practice. I'm also not able to think of
any use cases where listening on a hardcoded port would be beneficial,
so with this change I've opted to remove the ability entirely rather
than simply documenting that almost everybody should specify zero.
2019-08-09 13:59:03 -07:00
Lewin Bormann
58af9fc36b refactor(deps): Remove dependency on openssl. 2019-08-01 21:32:22 +02:00
Lewin Bormann
c321f6d2e6 fix(ServiceAccount): Make cache behavior more intuitive.
Now the cache is only checked for a token when the future is polled, not
at future creation time.

This also allows for reverting c2b41c3.
2019-06-21 18:41:17 +02:00
Lewin Bormann
c2b41c3da2 fix(test-svc-acct): Non-lazy cache lookup resulted in two requests. 2019-06-21 11:12:59 +02:00
Lewin Bormann
0eb1268567 doc(tokio): Set keep_alive to false on hyper clients.
This prevents hanging event loops.
2019-06-13 18:52:04 +02:00
Lewin Bormann
bdb0bd92e7 fix(examples): Update examples to use Authenticator. 2019-06-13 15:32:48 +02:00