Struct rings_core::session::AuthorizedInfo
source · pub struct AuthorizedInfo {
pub authorizer: Authorizer,
pub signer: Signer,
pub did: Did,
pub ttl_ms: Ttl,
pub ts_ms: u128,
}Expand description
AuthorizedInfo need authorizer and signer, and set ttl, use to verify in session.
Fields§
§signer: Signer§did: Did§ttl_ms: Ttl§ts_ms: u128Implementations§
source§impl AuthorizedInfo
impl AuthorizedInfo
sourcepub fn to_string(&self) -> Result<String>
pub fn to_string(&self) -> Result<String>
Examples found in repository?
src/session.rs (line 117)
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
pub fn verify(&self) -> bool {
if self.is_expired() {
return false;
}
if let Ok(auth_str) = self.auth.to_string() {
match self.auth.signer {
Signer::DEFAULT => {
signers::default::verify(&auth_str, &self.auth.authorizer.did.into(), &self.sig)
}
Signer::EIP191 => {
signers::eip191::verify(&auth_str, &self.auth.authorizer.did.into(), &self.sig)
}
Signer::EdDSA => match self.authorizer_pubkey() {
Ok(p) => signers::ed25519::verify(
&auth_str,
&self.auth.authorizer.did.into(),
&self.sig,
p,
),
Err(_) => false,
},
}
} else {
false
}
}
pub fn did(&self) -> Result<Did> {
if !self.verify() {
Err(Error::VerifySignatureFailed)
} else {
Ok(self.auth.did)
}
}
pub fn authorizer_pubkey(&self) -> Result<PublicKey> {
let auth = self.auth.to_string()?;
match self.auth.signer {
Signer::DEFAULT => signers::default::recover(&auth, &self.sig),
Signer::EIP191 => signers::eip191::recover(&auth, &self.sig),
Signer::EdDSA => self
.auth
.authorizer
.pubkey
.ok_or(Error::EdDSAPublicKeyNotFound),
}
}
}
impl SessionManager {
/// gen unsign info with a given ed25519 pubkey
pub fn gen_unsign_info_with_ed25519_pubkey(
ttl: Option<Ttl>,
pubkey: PublicKey,
) -> Result<(AuthorizedInfo, SecretKey)> {
Self::gen_unsign_info_with_pubkey(ttl, Some(Signer::EdDSA), pubkey)
}
pub fn gen_unsign_info_with_pubkey(
ttl: Option<Ttl>,
signer: Option<Signer>,
pubkey: PublicKey,
) -> Result<(AuthorizedInfo, SecretKey)> {
let key = SecretKey::random();
let signer = signer.unwrap_or(Signer::DEFAULT);
let authorizer = Authorizer {
did: pubkey.address().into(),
pubkey: Some(pubkey),
};
let info = AuthorizedInfo {
signer,
authorizer,
did: key.address().into(),
ttl_ms: ttl.unwrap_or(Ttl::Some(DEFAULT_SESSION_TTL_MS)),
ts_ms: utils::get_epoch_ms(),
};
Ok((info, key))
}
pub fn gen_unsign_info(
did: Did,
ttl: Option<Ttl>,
signer: Option<Signer>,
) -> (AuthorizedInfo, SecretKey) {
let key = SecretKey::random();
let signer = signer.unwrap_or(Signer::DEFAULT);
let authorizer = Authorizer { did, pubkey: None };
let info = AuthorizedInfo {
signer,
authorizer,
did: key.address().into(),
ttl_ms: ttl.unwrap_or(Ttl::Some(DEFAULT_SESSION_TTL_MS)),
ts_ms: utils::get_epoch_ms(),
};
(info, key)
}
/// sig: Signature of AuthorizedInfo
/// auth_info: generated from `gen_unsign_info`
/// session_key: temp key from gen_unsign_info
pub fn new(sig: &[u8], auth_info: &AuthorizedInfo, session_key: &SecretKey) -> Self {
let inner = SessionWithKey {
session: Session::new(sig, auth_info),
session_key: *session_key,
};
Self {
inner: Arc::new(RwLock::new(inner)),
}
}
/// generate Session with private key
/// only use it for unittest
pub fn new_with_seckey(key: &SecretKey, ttl: Option<Ttl>) -> Result<Self> {
let (auth, s_key) = Self::gen_unsign_info(key.address().into(), ttl, None);
let sig = key.sign(&auth.to_string()?).to_vec();
Ok(Self::new(&sig, &auth, &s_key))
}Trait Implementations§
source§impl Clone for AuthorizedInfo
impl Clone for AuthorizedInfo
source§fn clone(&self) -> AuthorizedInfo
fn clone(&self) -> AuthorizedInfo
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moresource§impl Debug for AuthorizedInfo
impl Debug for AuthorizedInfo
source§impl<'de> Deserialize<'de> for AuthorizedInfo
impl<'de> Deserialize<'de> for AuthorizedInfo
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl PartialEq<AuthorizedInfo> for AuthorizedInfo
impl PartialEq<AuthorizedInfo> for AuthorizedInfo
source§fn eq(&self, other: &AuthorizedInfo) -> bool
fn eq(&self, other: &AuthorizedInfo) -> bool
This method tests for
self and other values to be equal, and is used
by ==.source§impl Serialize for AuthorizedInfo
impl Serialize for AuthorizedInfo
impl Eq for AuthorizedInfo
impl StructuralEq for AuthorizedInfo
impl StructuralPartialEq for AuthorizedInfo
Auto Trait Implementations§
impl RefUnwindSafe for AuthorizedInfo
impl Send for AuthorizedInfo
impl Sync for AuthorizedInfo
impl Unpin for AuthorizedInfo
impl UnwindSafe for AuthorizedInfo
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T> AsTaggedExplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedExplicit<'a> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T> AsTaggedImplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedImplicit<'a> for Twhere
T: 'a,
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.