Skip to content

Commit a5b8f1e

Browse files
author
Jeremy T
committed
Merge branch 'tim/2fa' into 'master'
2FA See merge request TankerHQ/sdk-rust!28
2 parents 747b186 + f6e7222 commit a5b8f1e

File tree

8 files changed

+308
-36
lines changed

8 files changed

+308
-36
lines changed

src/core.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,16 @@ impl Core {
8484
///
8585
/// # Arguments
8686
/// * `verification` - The verification to use for identity registration
87-
pub async fn register_identity(&self, verification: &Verification) -> Result<(), Error> {
87+
pub async fn register_identity(
88+
&self,
89+
verification: &Verification,
90+
options: &VerificationOptions,
91+
) -> Result<Option<String>, Error> {
8892
let verif_wrapper = verification.to_cverification_wrapper();
89-
unsafe { ctanker::register_identity(self.ctanker, verif_wrapper.as_cverification()).await }
93+
unsafe {
94+
ctanker::register_identity(self.ctanker, verif_wrapper.as_cverification(), options)
95+
.await
96+
}
9097
}
9198

9299
/// Verifies the user's identity with which [start()](Self::start) has been called, and starts the session.
@@ -95,9 +102,15 @@ impl Core {
95102
///
96103
/// # Arguments
97104
/// * `verification` - The verification to use
98-
pub async fn verify_identity(&self, verification: &Verification) -> Result<(), Error> {
105+
pub async fn verify_identity(
106+
&self,
107+
verification: &Verification,
108+
options: &VerificationOptions,
109+
) -> Result<Option<String>, Error> {
99110
let verif_wrapper = verification.to_cverification_wrapper();
100-
unsafe { ctanker::verify_identity(self.ctanker, verif_wrapper.as_cverification()).await }
111+
unsafe {
112+
ctanker::verify_identity(self.ctanker, verif_wrapper.as_cverification(), options).await
113+
}
101114
}
102115

103116
/// Attaches a provisional identity to the user.
@@ -129,10 +142,19 @@ impl Core {
129142
///
130143
/// # Arguments
131144
/// * `verification` - The verification to set
132-
pub async fn set_verification_method(&self, verification: &Verification) -> Result<(), Error> {
145+
pub async fn set_verification_method(
146+
&self,
147+
verification: &Verification,
148+
options: &VerificationOptions,
149+
) -> Result<Option<String>, Error> {
133150
let verif_wrapper = verification.to_cverification_wrapper();
134151
unsafe {
135-
ctanker::set_verification_method(self.ctanker, verif_wrapper.as_cverification()).await
152+
ctanker::set_verification_method(
153+
self.ctanker,
154+
verif_wrapper.as_cverification(),
155+
options,
156+
)
157+
.await
136158
}
137159
}
138160

src/ctanker.rs

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
mod cfuture;
2+
23
pub use cfuture::*;
34
use std::marker::PhantomData;
45

56
mod cstream;
7+
68
pub use cstream::*;
79

810
pub use self::bindings::tanker_future;
@@ -18,12 +20,13 @@ pub type LogHandlerCallback = Box<dyn Fn(LogRecord) + Send>;
1820

1921
use crate::{
2022
AttachResult, Device, EncryptionOptions, Error, ErrorCode, LogRecord, LogRecordLevel, Options,
21-
SharingOptions, Status, VerificationMethod,
23+
SharingOptions, Status, VerificationMethod, VerificationOptions,
2224
};
2325
use lazy_static::lazy_static;
2426
use std::convert::TryFrom;
2527
use std::ffi::{c_void, CStr, CString};
2628
use std::os::raw::c_char;
29+
use std::ptr::NonNull;
2730
use std::sync::{Mutex, Once};
2831

2932
static RUST_SDK_VERSION: &str = env!("CARGO_PKG_VERSION");
@@ -152,17 +155,47 @@ pub async unsafe fn generate_verification_key(ctanker: CTankerPtr) -> Result<Str
152155
pub async unsafe fn register_identity(
153156
ctanker: CTankerPtr,
154157
verification: *const CVerification,
155-
) -> Result<(), Error> {
156-
let fut = unsafe { CFuture::<c_void>::new(tanker_register_identity(ctanker, verification)) };
157-
fut.await.map(|_| ())
158+
options: &VerificationOptions,
159+
) -> Result<Option<String>, Error> {
160+
let c_options = tanker_verification_options {
161+
version: 1,
162+
with_session_token: options.with_session_token,
163+
};
164+
let fut = unsafe {
165+
CFuture::<c_void>::new(tanker_register_identity(ctanker, verification, &c_options))
166+
};
167+
let token_str_ptr = fut.await? as *mut i8;
168+
Ok(NonNull::new(token_str_ptr).map(|str_ptr| {
169+
let str = CStr::from_ptr(str_ptr.as_ptr())
170+
.to_str()
171+
.unwrap()
172+
.to_owned();
173+
free_buffer(str_ptr.as_ptr() as *mut c_void);
174+
str
175+
}))
158176
}
159177

160178
pub async unsafe fn verify_identity(
161179
ctanker: CTankerPtr,
162180
verification: *const CVerification,
163-
) -> Result<(), Error> {
164-
let fut = unsafe { CFuture::<c_void>::new(tanker_verify_identity(ctanker, verification)) };
165-
fut.await.map(|_| ())
181+
options: &VerificationOptions,
182+
) -> Result<Option<String>, Error> {
183+
let c_options = tanker_verification_options {
184+
version: 1,
185+
with_session_token: options.with_session_token,
186+
};
187+
let fut = unsafe {
188+
CFuture::<c_void>::new(tanker_verify_identity(ctanker, verification, &c_options))
189+
};
190+
let token_str_ptr = fut.await? as *mut i8;
191+
Ok(NonNull::new(token_str_ptr).map(|str_ptr| {
192+
let str = CStr::from_ptr(str_ptr.as_ptr())
193+
.to_str()
194+
.unwrap()
195+
.to_owned();
196+
free_buffer(str_ptr.as_ptr() as *mut c_void);
197+
str
198+
}))
166199
}
167200

168201
pub async unsafe fn verify_provisional_identity(
@@ -176,10 +209,28 @@ pub async unsafe fn verify_provisional_identity(
176209
pub async unsafe fn set_verification_method(
177210
ctanker: CTankerPtr,
178211
verification: *const CVerification,
179-
) -> Result<(), Error> {
180-
let fut =
181-
unsafe { CFuture::<c_void>::new(tanker_set_verification_method(ctanker, verification)) };
182-
fut.await.map(|_| ())
212+
options: &VerificationOptions,
213+
) -> Result<Option<String>, Error> {
214+
let c_options = tanker_verification_options {
215+
version: 1,
216+
with_session_token: options.with_session_token,
217+
};
218+
let fut = unsafe {
219+
CFuture::<c_void>::new(tanker_set_verification_method(
220+
ctanker,
221+
verification,
222+
&c_options,
223+
))
224+
};
225+
let token_str_ptr = fut.await? as *mut i8;
226+
Ok(NonNull::new(token_str_ptr).map(|str_ptr| {
227+
let str = CStr::from_ptr(str_ptr.as_ptr())
228+
.to_str()
229+
.unwrap()
230+
.to_owned();
231+
free_buffer(str_ptr.as_ptr() as *mut c_void);
232+
str
233+
}))
183234
}
184235

185236
pub async unsafe fn get_verification_methods(

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ pub use types::*;
1818
mod sharing_options;
1919
pub use sharing_options::{EncryptionOptions, SharingOptions};
2020

21+
mod verification_options;
22+
pub use verification_options::VerificationOptions;
23+
2124
mod verification_methods;
2225
pub use verification_methods::*;
2326

src/verification_options.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// Extra options used during identity verification
2+
#[derive(Debug, Clone)]
3+
#[non_exhaustive]
4+
pub struct VerificationOptions {
5+
pub(crate) with_session_token: bool,
6+
}
7+
8+
impl VerificationOptions {
9+
pub fn new() -> Self {
10+
Default::default()
11+
}
12+
13+
/// Requests to create a Session Token on verification
14+
pub fn with_session_token(mut self) -> Self {
15+
self.with_session_token = true;
16+
self
17+
}
18+
}
19+
20+
impl Default for VerificationOptions {
21+
fn default() -> Self {
22+
Self {
23+
with_session_token: false,
24+
}
25+
}
26+
}

tests/identity/admin.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ impl Admin {
8787
id: &str,
8888
oidc_client_id: Option<&str>,
8989
oidc_provider: Option<&str>,
90+
with_session_token: Option<bool>,
9091
) -> Result<(), Error> {
9192
let url = self.make_url(id);
9293
let mut json = serde_json::Map::<_, _>::new();
@@ -96,6 +97,12 @@ impl Admin {
9697
if let Some(oidc_provider) = oidc_provider {
9798
json.insert("oidc_provider".to_owned(), oidc_provider.into());
9899
}
100+
if let Some(with_session_token) = with_session_token {
101+
json.insert(
102+
"session_certificates_enabled".to_owned(),
103+
with_session_token.into(),
104+
);
105+
}
99106
let json: Value = json.into();
100107

101108
admin_rest_request(self.client.patch(&url).json(&json)).await?;

tests/identity/test_app.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::identity::{create_identity, create_provisional_identity, get_public_i
99
use futures::executor::block_on;
1010
use rand::distributions::Alphanumeric;
1111
use rand::Rng;
12-
use tankersdk::{Core, Error, LogRecordLevel, Options, Status, Verification};
12+
use tankersdk::{Core, Error, LogRecordLevel, Options, Status, Verification, VerificationOptions};
1313

1414
pub struct TestApp {
1515
config: Config,
@@ -53,6 +53,18 @@ impl TestApp {
5353
&self.app.id
5454
}
5555

56+
pub fn url(&self) -> &str {
57+
&self.config.api_url
58+
}
59+
60+
pub fn trustchaind_url(&self) -> &str {
61+
&self.config.trustchain_url
62+
}
63+
64+
pub fn auth_token(&self) -> &str {
65+
&self.app.auth_token
66+
}
67+
5668
pub async fn get_verification_code(&self, email: &str) -> Result<String, Error> {
5769
self.app.get_verification_code(email).await
5870
}
@@ -61,9 +73,15 @@ impl TestApp {
6173
&self,
6274
oidc_client_id: Option<&str>,
6375
oidc_provider: Option<&str>,
76+
with_session_token: Option<bool>,
6477
) -> Result<(), Error> {
6578
self.admin
66-
.app_update(&self.app.id, oidc_client_id, oidc_provider)
79+
.app_update(
80+
&self.app.id,
81+
oidc_client_id,
82+
oidc_provider,
83+
with_session_token,
84+
)
6785
.await
6886
}
6987

@@ -93,7 +111,9 @@ impl TestApp {
93111

94112
let key = tanker.generate_verification_key().await?;
95113
let verif = Verification::VerificationKey(key);
96-
tanker.register_identity(&verif).await?;
114+
tanker
115+
.register_identity(&verif, &VerificationOptions::new())
116+
.await?;
97117
assert_eq!(tanker.status(), Status::Ready);
98118

99119
Ok(tanker)

tests/tanker_tests.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ async fn start_stop_session() -> Result<(), Error> {
4141
assert_eq!(status, Status::IdentityRegistrationNeeded);
4242

4343
let passphrase = Verification::Passphrase("pass".into());
44-
tanker.register_identity(&passphrase).await?;
44+
tanker
45+
.register_identity(&passphrase, &VerificationOptions::new())
46+
.await?;
4547
assert_eq!(tanker.status(), Status::Ready);
4648

4749
tanker.stop().await?;
@@ -210,7 +212,8 @@ async fn attach_provisional_with_single_verif() -> Result<(), Error> {
210212
email: bob_email.clone(),
211213
verification_code: app.get_verification_code(&bob_email).await?,
212214
};
213-
bob.register_identity(&verif).await?;
215+
bob.register_identity(&verif, &VerificationOptions::new())
216+
.await?;
214217

215218
let attach_result = bob.attach_provisional_identity(&bob_provisional).await?;
216219
assert_eq!(attach_result.status, Status::Ready);

0 commit comments

Comments
 (0)