Use enum to define identity proof type
This commit is contained in:
parent
3f3518001d
commit
301ade36a3
4 changed files with 70 additions and 16 deletions
|
@ -12,13 +12,13 @@ use crate::identity::{
|
||||||
claims::create_identity_claim,
|
claims::create_identity_claim,
|
||||||
did::Did,
|
did::Did,
|
||||||
minisign::verify_minisign_identity_proof,
|
minisign::verify_minisign_identity_proof,
|
||||||
signatures::{PROOF_TYPE_ID_EIP191, PROOF_TYPE_ID_MINISIGN},
|
|
||||||
};
|
};
|
||||||
use crate::models::profiles::types::{
|
use crate::models::profiles::types::{
|
||||||
ExtraField,
|
ExtraField,
|
||||||
IdentityProof,
|
IdentityProof,
|
||||||
PaymentLink,
|
PaymentLink,
|
||||||
PaymentOption,
|
PaymentOption,
|
||||||
|
ProofType,
|
||||||
};
|
};
|
||||||
use super::types::ActorAttachment;
|
use super::types::ActorAttachment;
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ pub fn attach_identity_proof(
|
||||||
name: proof.issuer.to_string(),
|
name: proof.issuer.to_string(),
|
||||||
value: None,
|
value: None,
|
||||||
href: None,
|
href: None,
|
||||||
signature_algorithm: Some(proof.proof_type),
|
signature_algorithm: Some(proof.proof_type.to_string()),
|
||||||
signature_value: Some(proof.value),
|
signature_value: Some(proof.value),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,17 +43,19 @@ pub fn parse_identity_proof(
|
||||||
return Err(ValidationError("invalid attachment type"));
|
return Err(ValidationError("invalid attachment type"));
|
||||||
};
|
};
|
||||||
let proof_type = attachment.signature_algorithm.as_ref()
|
let proof_type = attachment.signature_algorithm.as_ref()
|
||||||
.ok_or(ValidationError("missing proof type"))?;
|
.ok_or(ValidationError("missing proof type"))?
|
||||||
|
.parse()
|
||||||
|
.map_err(|_| ValidationError("unsupported proof type"))?;
|
||||||
let did = attachment.name.parse::<Did>()
|
let did = attachment.name.parse::<Did>()
|
||||||
.map_err(|_| ValidationError("invalid did"))?;
|
.map_err(|_| ValidationError("invalid DID"))?;
|
||||||
let message = create_identity_claim(actor_id, &did)
|
let message = create_identity_claim(actor_id, &did)
|
||||||
.map_err(|_| ValidationError("invalid claim"))?;
|
.map_err(|_| ValidationError("invalid claim"))?;
|
||||||
let signature = attachment.signature_value.as_ref()
|
let signature = attachment.signature_value.as_ref()
|
||||||
.ok_or(ValidationError("missing signature"))?;
|
.ok_or(ValidationError("missing signature"))?;
|
||||||
match did {
|
match did {
|
||||||
Did::Key(ref did_key) => {
|
Did::Key(ref did_key) => {
|
||||||
if proof_type != PROOF_TYPE_ID_MINISIGN {
|
if !matches!(proof_type, ProofType::LegacyMinisignIdentityProof) {
|
||||||
return Err(ValidationError("unknown proof type"));
|
return Err(ValidationError("incorrect proof type"));
|
||||||
};
|
};
|
||||||
verify_minisign_identity_proof(
|
verify_minisign_identity_proof(
|
||||||
did_key,
|
did_key,
|
||||||
|
@ -62,8 +64,8 @@ pub fn parse_identity_proof(
|
||||||
).map_err(|_| ValidationError("invalid identity proof"))?;
|
).map_err(|_| ValidationError("invalid identity proof"))?;
|
||||||
},
|
},
|
||||||
Did::Pkh(ref did_pkh) => {
|
Did::Pkh(ref did_pkh) => {
|
||||||
if proof_type != PROOF_TYPE_ID_EIP191 {
|
if !matches!(proof_type, ProofType::LegacyEip191IdentityProof) {
|
||||||
return Err(ValidationError("unknown proof type"));
|
return Err(ValidationError("incorrect proof type"));
|
||||||
};
|
};
|
||||||
verify_eip191_identity_proof(
|
verify_eip191_identity_proof(
|
||||||
did_pkh,
|
did_pkh,
|
||||||
|
@ -74,7 +76,7 @@ pub fn parse_identity_proof(
|
||||||
};
|
};
|
||||||
let proof = IdentityProof {
|
let proof = IdentityProof {
|
||||||
issuer: did,
|
issuer: did,
|
||||||
proof_type: proof_type.to_string(),
|
proof_type: proof_type,
|
||||||
value: signature.to_string(),
|
value: signature.to_string(),
|
||||||
};
|
};
|
||||||
Ok(proof)
|
Ok(proof)
|
||||||
|
|
|
@ -30,7 +30,6 @@ use crate::identity::{
|
||||||
parse_minisign_signature,
|
parse_minisign_signature,
|
||||||
verify_minisign_identity_proof,
|
verify_minisign_identity_proof,
|
||||||
},
|
},
|
||||||
signatures::{PROOF_TYPE_ID_EIP191, PROOF_TYPE_ID_MINISIGN},
|
|
||||||
};
|
};
|
||||||
use crate::json_signatures::{
|
use crate::json_signatures::{
|
||||||
create::{add_integrity_proof, IntegrityProof},
|
create::{add_integrity_proof, IntegrityProof},
|
||||||
|
@ -53,6 +52,7 @@ use crate::models::profiles::queries::{
|
||||||
use crate::models::profiles::types::{
|
use crate::models::profiles::types::{
|
||||||
IdentityProof,
|
IdentityProof,
|
||||||
ProfileUpdateData,
|
ProfileUpdateData,
|
||||||
|
ProofType,
|
||||||
};
|
};
|
||||||
use crate::models::relationships::queries::{
|
use crate::models::relationships::queries::{
|
||||||
create_follow_request,
|
create_follow_request,
|
||||||
|
@ -389,7 +389,7 @@ async fn create_identity_proof(
|
||||||
&message,
|
&message,
|
||||||
&proof_data.signature,
|
&proof_data.signature,
|
||||||
).map_err(|_| ValidationError("invalid signature"))?;
|
).map_err(|_| ValidationError("invalid signature"))?;
|
||||||
PROOF_TYPE_ID_MINISIGN
|
ProofType::LegacyMinisignIdentityProof
|
||||||
},
|
},
|
||||||
Did::Pkh(ref did_pkh) => {
|
Did::Pkh(ref did_pkh) => {
|
||||||
if did_pkh.chain_id != ChainId::ethereum_mainnet() {
|
if did_pkh.chain_id != ChainId::ethereum_mainnet() {
|
||||||
|
@ -410,13 +410,13 @@ async fn create_identity_proof(
|
||||||
&message,
|
&message,
|
||||||
&proof_data.signature,
|
&proof_data.signature,
|
||||||
).map_err(|_| ValidationError("invalid signature"))?;
|
).map_err(|_| ValidationError("invalid signature"))?;
|
||||||
PROOF_TYPE_ID_EIP191
|
ProofType::LegacyEip191IdentityProof
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let proof = IdentityProof {
|
let proof = IdentityProof {
|
||||||
issuer: did,
|
issuer: did,
|
||||||
proof_type: proof_type.to_string(),
|
proof_type: proof_type,
|
||||||
value: proof_data.signature.clone(),
|
value: proof_data.signature.clone(),
|
||||||
};
|
};
|
||||||
let mut profile_data = ProfileUpdateData::from(¤t_user.profile);
|
let mut profile_data = ProfileUpdateData::from(¤t_user.profile);
|
||||||
|
|
|
@ -647,6 +647,7 @@ mod tests {
|
||||||
ExtraField,
|
ExtraField,
|
||||||
IdentityProof,
|
IdentityProof,
|
||||||
ProfileCreateData,
|
ProfileCreateData,
|
||||||
|
ProofType,
|
||||||
};
|
};
|
||||||
use crate::models::users::queries::create_user;
|
use crate::models::users::queries::create_user;
|
||||||
use crate::models::users::types::UserCreateData;
|
use crate::models::users::types::UserCreateData;
|
||||||
|
@ -794,7 +795,7 @@ mod tests {
|
||||||
let db_client = &mut create_test_database().await;
|
let db_client = &mut create_test_database().await;
|
||||||
let identity_proof = IdentityProof {
|
let identity_proof = IdentityProof {
|
||||||
issuer: Did::Pkh(DidPkh::from_address(ÐEREUM, "0x1234abcd")),
|
issuer: Did::Pkh(DidPkh::from_address(ÐEREUM, "0x1234abcd")),
|
||||||
proof_type: "ethereum".to_string(),
|
proof_type: ProofType::LegacyEip191IdentityProof,
|
||||||
value: "13590013185bdea963".to_string(),
|
value: "13590013185bdea963".to_string(),
|
||||||
};
|
};
|
||||||
let profile_data = ProfileCreateData {
|
let profile_data = ProfileCreateData {
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
use std::fmt;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
use chrono::{DateTime, Duration, Utc};
|
use chrono::{DateTime, Duration, Utc};
|
||||||
use postgres_types::FromSql;
|
use postgres_types::FromSql;
|
||||||
|
@ -14,7 +16,10 @@ use crate::activitypub::actors::types::{Actor, ActorAddress};
|
||||||
use crate::activitypub::identifiers::local_actor_id;
|
use crate::activitypub::identifiers::local_actor_id;
|
||||||
use crate::database::json_macro::{json_from_sql, json_to_sql};
|
use crate::database::json_macro::{json_from_sql, json_to_sql};
|
||||||
use crate::errors::{ConversionError, ValidationError};
|
use crate::errors::{ConversionError, ValidationError};
|
||||||
use crate::identity::did::Did;
|
use crate::identity::{
|
||||||
|
did::Did,
|
||||||
|
signatures::{PROOF_TYPE_ID_EIP191, PROOF_TYPE_ID_MINISIGN},
|
||||||
|
};
|
||||||
use crate::utils::caip2::ChainId;
|
use crate::utils::caip2::ChainId;
|
||||||
use super::validators::{
|
use super::validators::{
|
||||||
validate_username,
|
validate_username,
|
||||||
|
@ -23,10 +28,56 @@ use super::validators::{
|
||||||
clean_extra_fields,
|
clean_extra_fields,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub enum ProofType {
|
||||||
|
LegacyEip191IdentityProof,
|
||||||
|
LegacyMinisignIdentityProof,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromStr for ProofType {
|
||||||
|
type Err = ConversionError;
|
||||||
|
|
||||||
|
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
||||||
|
let proof_type = match value {
|
||||||
|
PROOF_TYPE_ID_EIP191 => Self::LegacyEip191IdentityProof,
|
||||||
|
PROOF_TYPE_ID_MINISIGN => Self::LegacyMinisignIdentityProof,
|
||||||
|
_ => return Err(ConversionError),
|
||||||
|
};
|
||||||
|
Ok(proof_type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for ProofType {
|
||||||
|
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
let proof_type_str = match self {
|
||||||
|
Self::LegacyEip191IdentityProof => PROOF_TYPE_ID_EIP191,
|
||||||
|
Self::LegacyMinisignIdentityProof => PROOF_TYPE_ID_MINISIGN,
|
||||||
|
};
|
||||||
|
write!(formatter, "{}", proof_type_str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> Deserialize<'de> for ProofType {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
where D: Deserializer<'de>
|
||||||
|
{
|
||||||
|
String::deserialize(deserializer)?
|
||||||
|
.parse().map_err(DeserializerError::custom)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Serialize for ProofType {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where S: Serializer
|
||||||
|
{
|
||||||
|
serializer.serialize_str(&self.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
pub struct IdentityProof {
|
pub struct IdentityProof {
|
||||||
pub issuer: Did,
|
pub issuer: Did,
|
||||||
pub proof_type: String,
|
pub proof_type: ProofType,
|
||||||
pub value: String,
|
pub value: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue