cratestack_cose/envelope/
trait_impl.rs1#![cfg(not(target_arch = "wasm32"))]
8
9use std::future::Future;
10
11use bytes::Bytes;
12use cratestack_core::{
13 Binding, BodyShape, CratestackCodec, CratestackContext, CratestackEnvelope, CratestackError,
14 StreamOpener, StreamSealer, VerifiedSigner,
15};
16use serde::Serialize;
17
18use super::{CoseEnvelope, CoseRole};
19use crate::seal::PAYLOAD_CAPACITY_HINT;
20
21impl CratestackEnvelope for CoseEnvelope {
22 fn media_type(&self, shape: BodyShape) -> Option<&'static str> {
27 match shape {
28 BodyShape::Unary => Some(self.inner.mode.media_type()),
29 BodyShape::Stream => None,
30 }
31 }
32
33 fn seal<'a>(
37 &'a self,
38 payload: Bytes,
39 bind: &'a Binding<'a>,
40 ) -> impl Future<Output = Result<Bytes, CratestackError>> + Send + 'a {
41 let request = self.inner.role == CoseRole::Client;
42 async move {
43 crate::seal::seal(&self.inner, bind, request, payload.len(), |out| {
44 out.extend_from_slice(&payload);
45 Ok(())
46 })
47 .await
48 }
49 }
50
51 fn seal_value<'a, C, T>(
57 &'a self,
58 codec: &'a C,
59 value: &'a T,
60 bind: &'a Binding<'a>,
61 ) -> impl Future<Output = Result<Bytes, CratestackError>> + Send + 'a
62 where
63 C: CratestackCodec,
64 T: Serialize + ?Sized + Sync,
65 {
66 let request = self.inner.role == CoseRole::Client;
67 async move {
68 crate::seal::seal(&self.inner, bind, request, PAYLOAD_CAPACITY_HINT, |out| {
69 codec.encode_into(value, out)
70 })
71 .await
72 }
73 }
74
75 fn open<'a>(
82 &'a self,
83 body: Bytes,
84 bind: &'a Binding<'a>,
85 ctx: &'a mut CratestackContext,
86 ) -> impl Future<Output = Result<Bytes, CratestackError>> + Send + 'a {
87 let request = self.inner.role == CoseRole::Server;
88 async move {
89 let opened = crate::open::open(&self.inner, body, bind, request).await?;
90 ctx.record_verified_signer(VerifiedSigner::new(
91 Bytes::copy_from_slice(&opened.kid),
92 opened.thumbprint,
93 opened.alg.id(),
94 ));
95 Ok(opened.payload)
96 }
97 }
98
99 fn stream_sealer(&self, _bind: Binding<'static>) -> Option<Box<dyn StreamSealer>> {
100 None
101 }
102
103 fn stream_opener(&self, _bind: Binding<'static>) -> Option<Box<dyn StreamOpener>> {
104 None
105 }
106}