Skip to content

Commit

Permalink
[SM-1189] Fix renew for service account access token logins (#702)
Browse files Browse the repository at this point in the history
## Type of change

<!-- (mark with an `X`) -->

```
- [X] Bug fix
- [ ] New feature development
- [ ] Tech debt (refactoring, code cleanup, dependency upgrades, etc)
- [ ] Build/deploy pipeline (DevOps)
- [ ] Other
```

## Objective

<!--Describe what the purpose of this PR is. For example: what bug
you're fixing or what new feature you're adding-->
Fix `renew_token` to properly `client.set_tokens(r.access_token,
r.refresh_token, r.expires_in)` when logged in via service account
access token.

When the client's OAuth token expired, a call would be made to the
identity server, but the OAuth token was never replaced on the client.

The response would map to nothing
https://github.com/bitwarden/sdk/blob/a5692418b5836acd3662ce425258d660614bf7f3/crates/bitwarden/src/auth/renew.rs#L87-L90

Then silently error here 

https://github.com/bitwarden/sdk/blob/4a339a911e6db1583465f69b4c9ff981104ea0ef/crates/bitwarden/src/client/client.rs#L170-L175

This occurs when a client successfully
`client.auth().login_access_token` via state, but then shortly after the
OAuth token would expire.
Subsequent calls would produce HTTP 401. 

An easier way to produce the error is to log in without state
`thread::sleep(Duration::from_secs(4000));` then attempt to make any
client call.

## Code changes

<!--Explain the changes you've made to each file or major component.
This should help the reviewer understand your changes-->
<!--Also refer to any related changes or PRs in other repositories-->

- **crates/bitwarden/src/auth/renew.rs:**
Match to the `IdentityTokenResponse:Payload` returned by the access
token request to the identity server.

## Before you submit

- Please add **unit tests** where it makes sense to do so
  • Loading branch information
Thomas-Avery authored Apr 22, 2024
1 parent a863e89 commit 435cecd
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions crates/bitwarden/src/auth/renew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,8 @@ pub(crate) async fn renew_token(client: &mut Client) -> Result<()> {
.send(&client.__api_configurations)
.await?;

if let (
IdentityTokenResponse::Authenticated(r),
Some(state_file),
Ok(enc_settings),
) = (&result, state_file, client.get_encryption_settings())
if let (IdentityTokenResponse::Payload(r), Some(state_file), Ok(enc_settings)) =
(&result, state_file, client.get_encryption_settings())
{
if let Some(enc_key) = enc_settings.get_key(&None) {
let state =
Expand All @@ -83,6 +80,10 @@ pub(crate) async fn renew_token(client: &mut Client) -> Result<()> {
client.set_tokens(r.access_token, r.refresh_token, r.expires_in);
return Ok(());
}
IdentityTokenResponse::Payload(r) => {
client.set_tokens(r.access_token, r.refresh_token, r.expires_in);
return Ok(());
}
_ => {
// We should never get here
return Err(Error::InvalidResponse);
Expand Down

0 comments on commit 435cecd

Please sign in to comment.