Skip to content

Commit

Permalink
fix: getting/revoking tokens (#621)
Browse files Browse the repository at this point in the history
Description
---
Added more information exported by the `get_all_jwt` function. Fix the
revoke function (takes id, instead of the token).

How Has This Been Tested?
---
Manually.

What process can a PR reviewer use to test or verify this change?
---
The UI changes are in separate PR by @NovaT82 .


Breaking Changes
---

- [x] None
- [ ] Requires data directory to be deleted
- [ ] Other - Please specify
  • Loading branch information
Cifko authored Jul 20, 2023
1 parent 5fca558 commit b3bb9b0
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 28 deletions.
8 changes: 4 additions & 4 deletions applications/tari_dan_wallet_cli/src/command/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub struct DenyArgs {

#[derive(Debug, Args, Clone)]
pub struct RevokeArgs {
permission_token: String,
permission_token_id: i32,
}

impl AuthSubcommand {
Expand Down Expand Up @@ -108,15 +108,15 @@ impl AuthSubcommand {
Revoke(args) => {
client
.auth_revoke(AuthRevokeTokenRequest {
permission_token: args.permission_token,
permission_token_id: args.permission_token_id,
})
.await?;
println!("Token revoked!");
},
List => {
let tokens = client.auth_get_all_jwt(AuthGetAllJwtRequest {}).await?;
for (id, name) in &tokens.jwt {
println!("Id {id} name {name}");
for claims in &tokens.jwt {
println!("Id {} name {}", claims.id, claims.name);
}
},
}
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_dan_wallet_daemon/src/handlers/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub async fn handle_revoke(
) -> Result<AuthRevokeTokenResponse, anyhow::Error> {
let jwt = context.wallet_sdk().jwt_api();
jwt.check_auth(token, &[JrpcPermission::Admin])?;
jwt.revoke(revoke_request.permission_token.as_str())?;
jwt.revoke(revoke_request.permission_token_id)?;
Ok(AuthRevokeTokenResponse {})
}

Expand Down
6 changes: 3 additions & 3 deletions clients/wallet_daemon_client/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use serde::{Deserialize, Serialize};
use tari_common_types::types::PublicKey;
use tari_dan_common_types::ShardId;
use tari_dan_wallet_sdk::{
apis::jwt::JrpcPermissions,
apis::jwt::{Claims, JrpcPermissions},
models::{Account, ConfidentialProofId, TransactionStatus},
};
use tari_engine_types::{
Expand Down Expand Up @@ -491,7 +491,7 @@ pub struct AuthLoginDenyResponse {}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AuthRevokeTokenRequest {
pub permission_token: String,
pub permission_token_id: i32,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
Expand Down Expand Up @@ -544,5 +544,5 @@ pub struct AuthGetAllJwtRequest {}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AuthGetAllJwtResponse {
pub jwt: Vec<(i32, String)>,
pub jwt: Vec<Claims>,
}
28 changes: 12 additions & 16 deletions dan_layer/wallet/sdk/src/apis/jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ impl JrpcPermissions {
}
}

#[derive(Debug, Serialize, Deserialize)]
struct Claims {
id: u64,
name: String,
permissions: JrpcPermissions,
exp: usize,
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Claims {
pub id: u64,
pub name: String,
pub permissions: JrpcPermissions,
pub exp: usize,
}

// This is used when you request permission.
Expand Down Expand Up @@ -179,10 +179,6 @@ impl<'a, TStore: WalletStore> JwtApi<'a, TStore> {
self.get_token_claims(token).map(|claims| claims.permissions)
}

fn get_name(&self, token: &str) -> Result<String, JwtApiError> {
self.get_token_claims(token).map(|claims| claims.name)
}

pub fn grant(&self, name: String, auth_token: String) -> Result<String, JwtApiError> {
let auth_claims = self.check_auth_token(auth_token.as_ref())?;
let my_claims = Claims {
Expand Down Expand Up @@ -230,20 +226,20 @@ impl<'a, TStore: WalletStore> JwtApi<'a, TStore> {
Ok(())
}

pub fn revoke(&self, token: &str) -> Result<(), JwtApiError> {
pub fn revoke(&self, token_id: i32) -> Result<(), JwtApiError> {
let mut tx = self.store.create_write_tx()?;
tx.jwt_revoke(token)?;
tx.jwt_revoke(token_id)?;
tx.commit()?;
Ok(())
}

pub fn get_tokens(&self) -> Result<Vec<(i32, String)>, JwtApiError> {
pub fn get_tokens(&self) -> Result<Vec<Claims>, JwtApiError> {
let mut tx = self.store.create_read_tx()?;
let tokens = tx.jwt_get_all()?;
let mut res = Vec::new();
for (id, token) in tokens.iter().filter(|(_, token)| token.is_some()) {
if let Ok(name) = self.get_name(token.as_ref().unwrap().as_str()) {
res.push((*id, name));
for (_, token) in tokens.iter().filter(|(_, token)| token.is_some()) {
if let Ok(claims) = self.get_token_claims(token.as_ref().unwrap().as_str()) {
res.push(claims);
}
}
Ok(res)
Expand Down
2 changes: 1 addition & 1 deletion dan_layer/wallet/sdk/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub trait WalletStoreWriter {
fn jwt_add_empty_token(&mut self) -> Result<u64, WalletStorageError>;
fn jwt_store_decision(&mut self, id: u64, permissions_token: Option<String>) -> Result<(), WalletStorageError>;
fn jwt_is_revoked(&mut self, token: &str) -> Result<bool, WalletStorageError>;
fn jwt_revoke(&mut self, token: &str) -> Result<(), WalletStorageError>;
fn jwt_revoke(&mut self, token_id: i32) -> Result<(), WalletStorageError>;

// Key manager
fn key_manager_insert(&mut self, branch: &str, index: u64) -> Result<(), WalletStorageError>;
Expand Down
1 change: 1 addition & 0 deletions dan_layer/wallet/storage_sqlite/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ impl WalletStoreReader for ReadTransaction<'_> {
let res = auth_status::table
.select((auth_status::id, auth_status::token))
.filter(auth_status::granted.eq(true))
.filter(auth_status::revoked.eq(false))
.get_results::<(i32, Option<String>)>(self.connection())
.map_err(|e| WalletStorageError::general("jwt_get_all", e))?;
Ok(res)
Expand Down
6 changes: 3 additions & 3 deletions dan_layer/wallet/storage_sqlite/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,16 @@ impl WalletStoreWriter for WriteTransaction<'_> {
}
}

fn jwt_revoke(&mut self, token: &str) -> Result<(), WalletStorageError> {
fn jwt_revoke(&mut self, token_id: i32) -> Result<(), WalletStorageError> {
if diesel::update(auth_status::table)
.set(auth_status::revoked.eq(true))
.filter(auth_status::token.eq(token))
.filter(auth_status::id.eq(token_id))
.execute(self.connection())
.map_err(|e| WalletStorageError::general("jwt_revoke", e))? ==
0
{
diesel::insert_into(auth_status::table)
.values((auth_status::revoked.eq(true), auth_status::token.eq(token)))
.values((auth_status::revoked.eq(true), auth_status::id.eq(token_id)))
.execute(self.connection())
.map_err(|e| WalletStorageError::general("jwt_revoke", e))?;
}
Expand Down

0 comments on commit b3bb9b0

Please sign in to comment.