Skip to main content

idkollen_client/models/
mitid.rs

1use chrono::NaiveDate;
2use serde::{Deserialize, Serialize};
3
4use super::common::ApiErrorCode;
5use super::email::Email;
6use super::ssn::Cpr;
7use super::url::Url;
8
9/// Request body for starting a MitID authentication session.
10#[must_use]
11#[derive(Debug, Clone, Default, Serialize)]
12#[serde(rename_all = "camelCase")]
13pub struct MitIdAuthRequest {
14    /// URL to redirect the user to after completing the flow.
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub redirect_url: Option<Url>,
17    /// Text shown to the user during authentication. Must not contain `%` or `<` (max 130 chars).
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub reference_text: Option<String>,
20    /// Request the user's phone number.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub request_phone: Option<bool>,
23    /// Request the user's email address.
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub request_email: Option<bool>,
26    /// Request the user's registered address.
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub request_address: Option<bool>,
29    /// Reference ID returned verbatim in the result and callback.
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub ref_id: Option<String>,
32}
33
34impl MitIdAuthRequest {
35    #[inline]
36    pub fn new() -> Self {
37        Self::default()
38    }
39
40    #[inline]
41    pub fn redirect_url(mut self, url: Url) -> Self {
42        self.redirect_url = Some(url);
43        self
44    }
45
46    #[inline]
47    pub fn reference_text(mut self, v: impl Into<String>) -> Self {
48        self.reference_text = Some(v.into());
49        self
50    }
51
52    #[inline]
53    pub fn request_phone(mut self, v: bool) -> Self {
54        self.request_phone = Some(v);
55        self
56    }
57
58    #[inline]
59    pub fn request_email(mut self, v: bool) -> Self {
60        self.request_email = Some(v);
61        self
62    }
63
64    #[inline]
65    pub fn request_address(mut self, v: bool) -> Self {
66        self.request_address = Some(v);
67        self
68    }
69
70    #[inline]
71    pub fn ref_id(mut self, ref_id: impl Into<String>) -> Self {
72        self.ref_id = Some(ref_id.into());
73        self
74    }
75}
76
77/// Request body for starting a MitID backchannel authentication session.
78#[must_use]
79#[derive(Debug, Clone, Serialize)]
80#[serde(rename_all = "camelCase")]
81pub struct MitIdBackchannelAuthRequest {
82    /// Danish CPR number.
83    pub ssn: Cpr,
84    /// Message displayed in the MitID app to bind the session.
85    pub binding_message: String,
86    /// URL to receive the result callback on success or failure.
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub callback_url: Option<Url>,
89    /// Reference ID returned verbatim in the result and callback.
90    #[serde(skip_serializing_if = "Option::is_none")]
91    pub ref_id: Option<String>,
92}
93
94impl MitIdBackchannelAuthRequest {
95    #[inline]
96    pub fn new(ssn: Cpr, binding_message: impl Into<String>) -> Self {
97        Self {
98            ssn,
99            binding_message: binding_message.into(),
100            callback_url: None,
101            ref_id: None,
102        }
103    }
104
105    #[inline]
106    pub fn callback_url(mut self, url: Url) -> Self {
107        self.callback_url = Some(url);
108        self
109    }
110
111    #[inline]
112    pub fn ref_id(mut self, ref_id: impl Into<String>) -> Self {
113        self.ref_id = Some(ref_id.into());
114        self
115    }
116}
117
118/// Request body for starting a MitID signing session.
119#[must_use]
120#[derive(Debug, Clone, Serialize)]
121#[serde(rename_all = "camelCase")]
122pub struct MitIdSignRequest {
123    /// Text to sign, displayed in MitID (max 600 chars).
124    pub text: String,
125    /// URL to redirect the user to after completing the flow.
126    #[serde(skip_serializing_if = "Option::is_none")]
127    pub redirect_url: Option<Url>,
128    /// Reference ID returned verbatim in the result and callback.
129    #[serde(skip_serializing_if = "Option::is_none")]
130    pub ref_id: Option<String>,
131}
132
133impl MitIdSignRequest {
134    #[inline]
135    pub fn new(text: impl Into<String>) -> Self {
136        Self {
137            text: text.into(),
138            redirect_url: None,
139            ref_id: None,
140        }
141    }
142
143    #[inline]
144    pub fn redirect_url(mut self, url: Url) -> Self {
145        self.redirect_url = Some(url);
146        self
147    }
148
149    #[inline]
150    pub fn ref_id(mut self, ref_id: impl Into<String>) -> Self {
151        self.ref_id = Some(ref_id.into());
152        self
153    }
154}
155
156/// MitID session status.
157#[non_exhaustive]
158#[derive(Debug, Clone, Deserialize)]
159#[serde(tag = "status")]
160pub enum MitIdStatus {
161    #[serde(rename = "PENDING")]
162    Pending(MitIdPending),
163    #[serde(rename = "COMPLETED")]
164    Completed(MitIdCompleted),
165    #[serde(rename = "FAILED")]
166    Failed(MitIdFailed),
167}
168
169/// Returned while the user has not yet acted.
170#[derive(Debug, Clone, Deserialize)]
171#[serde(rename_all = "camelCase")]
172pub struct MitIdPending {
173    pub id: String,
174    pub ref_id: Option<String>,
175    pub url: Option<String>,
176    /// Present in the backchannel flow — display this to the user.
177    pub binding_message: Option<String>,
178}
179
180/// Returned when the MitID session has completed successfully.
181#[derive(Debug, Clone, Deserialize)]
182#[serde(rename_all = "camelCase")]
183pub struct MitIdCompleted {
184    pub id: String,
185    pub ref_id: Option<String>,
186    /// Danish CPR number.
187    pub ssn: Cpr,
188    pub name: String,
189    pub given_name: String,
190    pub surname: String,
191    pub phone: Option<String>,
192    pub email: Option<Email>,
193    pub address: Option<String>,
194    pub birth_date: Option<NaiveDate>,
195    pub pid: Option<String>,
196    pub bank_id: Option<String>,
197    /// Present when the session was a signing session.
198    pub sign_result: Option<MitIdSignResult>,
199}
200
201/// Returned when the MitID session has failed.
202#[derive(Debug, Clone, Deserialize)]
203#[serde(rename_all = "camelCase")]
204pub struct MitIdFailed {
205    pub id: String,
206    pub ref_id: Option<String>,
207    pub error: ApiErrorCode,
208}
209
210/// Signing result returned in a completed MitID sign session.
211#[derive(Debug, Clone, Deserialize)]
212#[serde(rename_all = "camelCase")]
213pub struct MitIdSignResult {
214    pub checksum: String,
215}