Skip to content

Commit

Permalink
feat: Add act to unregister identity (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
Elyniss authored Mar 31, 2023
1 parent 2edaecc commit 8bf0e36
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 0 additions & 32 deletions src/handlers/identity/mod.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,3 @@
use {
crate::auth::jwt::{JwtClaims, JwtVerifierByIssuer},
serde::{Deserialize, Serialize},
};

pub mod register;
pub mod resolve;
pub mod unregister;

#[derive(Debug, Serialize, Deserialize)]
pub struct IdentityKeyClaims {
aud: String, // keys server url used for registering
exp: usize, // timestamp when jwt must expire TODO: Should be 1 hour
iat: usize, // timestamp when jwt was issued
iss: String, // public identity key in form of did:key, also used to verify jwt signature
pkh: String, // corresponding blockchain account (did:pkh)
}

impl JwtClaims for IdentityKeyClaims {
fn is_valid(&self) -> bool {
true
// TODO: Add validation:
// aud must be equal this dns?
// exp must be in future
// iat must be in past
// iss must be valid did:key
// pkh must be valid did:pkh
}
}

impl JwtVerifierByIssuer for IdentityKeyClaims {
fn get_iss(&self) -> &str {
&self.iss
}
}
42 changes: 34 additions & 8 deletions src/handlers/identity/unregister.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use {
super::{
super::{validate_caip10_account, validate_identity_key, Response},
IdentityKeyClaims,
},
super::super::{validate_caip10_account, validate_identity_key, Response},
crate::{
auth::{
did::{extract_did_data, DID_METHOD_KEY, DID_METHOD_PKH},
jwt::Jwt,
jwt::{Jwt, JwtClaims, JwtVerifierByIssuer},
},
error,
state::AppState,
},
axum::{extract::State, Json},
serde::Deserialize,
serde::{Deserialize, Serialize},
std::sync::Arc,
validator::Validate,
};
Expand All @@ -31,14 +28,43 @@ pub struct UnregisterIdentityParams {
identity_key: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct UnregisterIdentityKeyClaims {
aud: String, // keys server url used for registering
exp: usize, // timestamp when jwt must expire TODO: Should be 1 hour
iat: usize, // timestamp when jwt was issued
iss: String, // public identity key in form of did:key, also used to verify jwt signature
pkh: String, // corresponding blockchain account (did:pkh)
act: String, // description of action intent. Must be equal to "unregister_identity"
}

impl JwtClaims for UnregisterIdentityKeyClaims {
fn is_valid(&self) -> bool {
// TODO: Add validation:
// aud must be equal this dns?
// exp must be in future
// iat must be in past
// iss must be valid did:key
// pkh must be valid did:pkh
println!("act: {}", self.act);
self.act == "unregister_identity"
}
}

impl JwtVerifierByIssuer for UnregisterIdentityKeyClaims {
fn get_iss(&self) -> &str {
&self.iss
}
}

pub async fn handler(
State(state): State<Arc<AppState>>,
Json(payload): Json<UnregisterIdentityPayload>,
) -> error::Result<Response> {
let jwt = Jwt::<IdentityKeyClaims>::new(&payload.id_auth)?;
let jwt = Jwt::<UnregisterIdentityKeyClaims>::new(&payload.id_auth)?;
jwt.verify()?;

let claims: IdentityKeyClaims = jwt.claims;
let claims: UnregisterIdentityKeyClaims = jwt.claims;
let account = extract_did_data(&claims.pkh, DID_METHOD_PKH)?;
let identity_key = extract_did_data(&claims.iss, DID_METHOD_KEY)?;

Expand Down

0 comments on commit 8bf0e36

Please sign in to comment.