Struct rings_core::session::Session

source ·
pub struct Session {
    pub sig: Vec<u8>,
    pub auth: AuthorizedInfo,
}
Expand description

Session contain signature which sign with Signer, so need AuthorizedInfo as well.

Fields§

§sig: Vec<u8>§auth: AuthorizedInfo

Implementations§

Examples found in repository?
src/session.rs (line 215)
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
    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))
    }

    pub fn renew(&self, sig: &[u8], auth_info: &AuthorizedInfo, key: &SecretKey) -> Result<&Self> {
        let new_inner = SessionWithKey {
            session: Session::new(sig, auth_info),
            session_key: *key,
        };
        let mut inner = self
            .inner
            .try_write()
            .map_err(|_| Error::SessionTryLockFailed)?;
        *inner = new_inner;
        Ok(self)
    }
Examples found in repository?
src/session.rs (line 114)
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
    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
        }
    }
Examples found in repository?
src/session.rs (line 141)
140
141
142
143
144
145
146
    pub fn did(&self) -> Result<Did> {
        if !self.verify() {
            Err(Error::VerifySignatureFailed)
        } else {
            Ok(self.auth.did)
        }
    }
More examples
Hide additional examples
src/message/protocols/verify.rs (line 23)
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    pub fn verify<T>(&self, data: &T) -> bool
    where T: Serialize {
        if !self.session.verify() {
            tracing::warn!("session is expired");
            return false;
        }

        if let (Ok(did), Ok(msg)) = (self.session.did(), self.msg(data)) {
            signers::default::verify(&msg, &did, &self.sig)
        } else {
            tracing::warn!("failed to verify message");
            false
        }
    }
Examples found in repository?
src/message/protocols/verify.rs (line 28)
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    pub fn verify<T>(&self, data: &T) -> bool
    where T: Serialize {
        if !self.session.verify() {
            tracing::warn!("session is expired");
            return false;
        }

        if let (Ok(did), Ok(msg)) = (self.session.did(), self.msg(data)) {
            signers::default::verify(&msg, &did, &self.sig)
        } else {
            tracing::warn!("failed to verify message");
            false
        }
    }
Examples found in repository?
src/session.rs (line 125)
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
    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
        }
    }
More examples
Hide additional examples
src/transports/default/transport.rs (line 503)
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
    async fn register_remote_info(&self, data: Encoded) -> Result<Did> {
        let data: MessagePayload<TricklePayload> = data.decode()?;
        tracing::trace!("register remote info: {:?}", data);
        match data.verify() {
            true => {
                let sdp = serde_json::from_str::<RTCSessionDescription>(&data.data.sdp)
                    .map_err(Error::Deserialize)?;
                tracing::trace!("setting remote sdp: {:?}", sdp);
                self.set_remote_description(sdp).await?;
                tracing::trace!("setting remote candidate");
                for c in &data.data.candidates {
                    tracing::trace!("add candidates: {:?}", c);
                    if self.add_ice_candidate(c.clone()).await.is_err() {
                        tracing::warn!("failed on add add candidates: {:?}", c.clone());
                    };
                }
                if let Ok(public_key) = data.origin_verification.session.authorizer_pubkey() {
                    let mut pk = self.public_key.write().await;
                    *pk = Some(public_key);
                };
                {
                    let mut services = self.services.write().await;
                    *services = data.data.services.into_iter().collect();
                }
                Ok(data.addr)
            }
            _ => {
                tracing::error!("cannot verify message sig");
                return Err(Error::VerifySignatureFailed);
            }
        }
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Deserialize this value from the given Serde deserializer. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more