jsonprooftoken/jwp/
header.rs

1// Copyright 2023 Fondazione LINKS
2
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6
7//     http://www.apache.org/licenses/LICENSE-2.0
8
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use serde::{Deserialize, Serialize};
16
17use crate::{
18    jpa::algs::{PresentationProofAlgorithm, ProofAlgorithm},
19    jpt::claims::Claims,
20};
21
22#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
23pub struct IssuerProtectedHeader {
24    /// JWP type (JPT)
25    #[serde(skip_serializing_if = "Option::is_none")]
26    typ: Option<String>,
27    /// Algorithm used for the JWP
28    alg: ProofAlgorithm,
29    /// ID for the key used for the JWP.
30    #[serde(skip_serializing_if = "Option::is_none")]
31    kid: Option<String>,
32    /// cid can be used similar to a cid in order to ensure externally resolve of claims. FOr now this is not handled!
33    #[serde(skip_serializing_if = "Option::is_none")]
34    cid: Option<String>,
35    /// if you want you can put the claims directly into the header
36    #[serde(skip_serializing_if = "Option::is_none")]
37    claims: Option<Claims>,
38}
39
40impl IssuerProtectedHeader {
41    pub fn new(alg: ProofAlgorithm) -> Self {
42        Self {
43            typ: Some("JPT".to_owned()),
44            alg,
45            kid: None,
46            cid: None,
47            claims: None,
48        }
49    }
50
51    // Getter for alg
52    pub fn alg(&self) -> ProofAlgorithm {
53        self.alg
54    }
55
56    // Getter for typ
57    pub fn typ(&self) -> Option<&String> {
58        self.typ.as_ref()
59    }
60
61    // Setter for typ
62    pub fn set_typ(&mut self, value: Option<String>) {
63        self.typ = value;
64    }
65
66    // Getter for kid
67    pub fn kid(&self) -> Option<&String> {
68        self.kid.as_ref()
69    }
70
71    // Setter for kid
72    pub fn set_kid(&mut self, value: Option<String>) {
73        self.kid = value;
74    }
75
76    // Getter for cid
77    pub fn cid(&self) -> Option<&String> {
78        self.cid.as_ref()
79    }
80
81    // Setter for cid
82    pub fn set_cid(&mut self, value: Option<String>) {
83        self.cid = value;
84    }
85
86    // Getter for claims
87    pub fn claims(&self) -> Option<&Claims> {
88        self.claims.as_ref()
89    }
90
91    // Setter for claims
92    pub(crate) fn set_claims(&mut self, value: Option<Claims>) {
93        self.claims = value;
94    }
95}
96
97#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
98pub struct PresentationProtectedHeader {
99    alg: PresentationProofAlgorithm,
100    /// ID for the key used for the JWP.
101    #[serde(skip_serializing_if = "Option::is_none")]
102    kid: Option<String>,
103    /// Who have to receive the JPT.
104    #[serde(skip_serializing_if = "Option::is_none")]
105    aud: Option<String>,
106    /// For replay attacks
107    #[serde(skip_serializing_if = "Option::is_none")]
108    nonce: Option<String>,
109}
110
111impl PresentationProtectedHeader {
112    pub fn new(alg: PresentationProofAlgorithm) -> Self {
113        Self {
114            alg,
115            kid: None,
116            aud: None,
117            nonce: None,
118        }
119    }
120
121    // Getter for alg
122    pub fn alg(&self) -> PresentationProofAlgorithm {
123        self.alg
124    }
125
126    // Getter for kid
127    pub fn kid(&self) -> Option<&String> {
128        self.kid.as_ref()
129    }
130
131    // Setter for kid
132    pub fn set_kid(&mut self, value: Option<String>) {
133        self.kid = value;
134    }
135
136    // Getter for aud
137    pub fn aud(&self) -> Option<&String> {
138        self.aud.as_ref()
139    }
140
141    // Setter for aud
142    pub fn set_aud(&mut self, value: Option<String>) {
143        self.aud = value;
144    }
145
146    // Getter for nonce
147    pub fn nonce(&self) -> Option<&String> {
148        self.nonce.as_ref()
149    }
150
151    // Setter for nonce
152    pub fn set_nonce(&mut self, value: Option<String>) {
153        self.nonce = value;
154    }
155}