Skip to main content

mls_rs_core/identity/
basic.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// Copyright by contributors to this project.
3// SPDX-License-Identifier: (Apache-2.0 OR MIT)
4
5use core::{
6    convert::Infallible,
7    fmt::{self, Debug},
8};
9
10use alloc::vec::Vec;
11use mls_rs_codec::{MlsDecode, MlsEncode, MlsSize};
12
13use super::{Credential, CredentialType, MlsCredential};
14
15#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, MlsSize, MlsEncode, MlsDecode)]
16#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18/// Bare assertion of an identity without any additional information.
19///
20/// The format of the encoded identity is defined by the application.
21///
22///
23/// # Warning
24///
25/// Basic credentials are inherently insecure since they can not be
26/// properly validated. It is not recommended to use [`BasicCredential`]
27/// in production applications.
28pub struct BasicCredential {
29    /// Underlying identifier as raw bytes.
30    #[mls_codec(with = "mls_rs_codec::byte_vec")]
31    #[cfg_attr(feature = "serde", serde(with = "crate::vec_serde"))]
32    pub identifier: Vec<u8>,
33}
34
35impl Debug for BasicCredential {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        crate::debug::pretty_bytes(&self.identifier)
38            .named("BasicCredential")
39            .fmt(f)
40    }
41}
42
43impl BasicCredential {
44    /// Create a new basic credential with raw bytes.
45    pub fn new(identifier: Vec<u8>) -> BasicCredential {
46        BasicCredential { identifier }
47    }
48
49    /// Underlying identifier as raw bytes.
50    pub fn identifier(&self) -> &[u8] {
51        &self.identifier
52    }
53}
54
55impl BasicCredential {
56    pub fn credential_type() -> CredentialType {
57        CredentialType::BASIC
58    }
59
60    pub fn into_credential(self) -> Credential {
61        Credential::Basic(self)
62    }
63}
64
65impl MlsCredential for BasicCredential {
66    type Error = Infallible;
67
68    fn credential_type() -> CredentialType {
69        Self::credential_type()
70    }
71
72    fn into_credential(self) -> Result<Credential, Self::Error> {
73        Ok(self.into_credential())
74    }
75}