-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
## Type of change <!-- (mark with an `X`) --> ``` - [ ] Bug fix - [x] 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--> This PR adds the ability to manage basic state via the SDK. It also updates the `bws` crate to use this SDK change, implementing basic state management for auth tokens. The core benefit is that reauthentication per command will no longer be needed, so that server auth requests will be mitigated. ## 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--> _(to be added)_ - **file.ext:** Description of what was changed and why ## Before you submit - Please add **unit tests** where it makes sense to do so (encouraged but not required) --------- Co-authored-by: Oscar Hinton <Hinton@users.noreply.github.com> Co-authored-by: Daniel García <dani-garcia@users.noreply.github.com>
- Loading branch information
1 parent
65df2ed
commit 9d4ec76
Showing
14 changed files
with
195 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod projects; | ||
pub mod secrets; | ||
pub mod state; | ||
|
||
mod client_projects; | ||
mod client_secrets; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{ | ||
client::AccessToken, | ||
crypto::{EncString, KeyDecryptable, KeyEncryptable}, | ||
error::{Error, Result}, | ||
}; | ||
use std::{fmt::Debug, path::Path}; | ||
|
||
const STATE_VERSION: u32 = 1; | ||
|
||
#[cfg(feature = "secrets")] | ||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct ClientState { | ||
pub(crate) version: u32, | ||
pub(crate) token: String, | ||
pub(crate) encryption_key: String, | ||
} | ||
|
||
impl ClientState { | ||
pub fn new(token: String, encryption_key: String) -> Self { | ||
Self { | ||
version: STATE_VERSION, | ||
token, | ||
encryption_key, | ||
} | ||
} | ||
} | ||
|
||
pub fn get(state_file: &Path, access_token: &AccessToken) -> Result<ClientState> { | ||
let file_content = std::fs::read_to_string(state_file)?; | ||
|
||
let encrypted_state: EncString = file_content.parse()?; | ||
let decrypted_state: String = encrypted_state.decrypt_with_key(&access_token.encryption_key)?; | ||
let client_state: ClientState = serde_json::from_str(&decrypted_state)?; | ||
|
||
if client_state.version != STATE_VERSION { | ||
return Err(Error::InvalidStateFileVersion); | ||
} | ||
|
||
Ok(client_state) | ||
} | ||
|
||
pub fn set(state_file: &Path, access_token: &AccessToken, state: ClientState) -> Result<()> { | ||
let serialized_state: String = serde_json::to_string(&state)?; | ||
let encrypted_state: EncString = | ||
serialized_state.encrypt_with_key(&access_token.encryption_key)?; | ||
let state_string: String = encrypted_state.to_string(); | ||
|
||
std::fs::write(state_file, state_string) | ||
.map_err(|_| "Failure writing to the state file.".into()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.