Skip to content

Commit ffb3e39

Browse files
committed
add documentation and example
1 parent 1741502 commit ffb3e39

File tree

4 files changed

+127
-5
lines changed

4 files changed

+127
-5
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "smart-id-rust-client"
3-
version = "0.1.3"
3+
version = "0.1.4"
44
description = "Smart ID Rust Client"
55
homepage = "https://smart-id.com"
66
authors = ["Michallis Pashidis <[email protected]>"]

README.MD

+124-1
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,127 @@ cargo test
5858
```zsh
5959
cargo doc --no-deps --open
6060
```
61-
61+
62+
## Example Client
63+
64+
See the examples folder for a simple example client.
65+
To create an SmartID account, download the app:
66+
[SmartID App](https://www.smart-id.com/download/)
67+
68+
The example application goes through the following use cases:
69+
- Verify Certificate Existence
70+
- SmartID Authentication
71+
- SmartID Digital Signature
72+
73+
The example is using a MOCK ID to simulate the SmartID user.
74+
75+
```shell
76+
cargo run --example smart_id_client
77+
```
78+
79+
### Configuration
80+
SmartID configuration can be loaded form environment variables:
81+
```shell
82+
/// Get default Config (from environment variables)
83+
let cfg = get_config_from_env();
84+
```
85+
86+
Or using the builder pattern:
87+
```shell
88+
/// Config Builder
89+
let cfg = SmartIDConfigBuilder::new().url("https://sid.demo.sk.ee/smart-id-rp/v2").build().expect("Error building config");
90+
```
91+
92+
### Verify Certificate Existence
93+
94+
To check whether a user has been onboarded, use the `get_certificate_by_semantic_identifier`:
95+
```shell
96+
async fn uc_get_certificate_choice(cfg: &SmartIDConfig) -> Result<()> {
97+
98+
/// Create the semantic identifier
99+
let sem_id = SemanticsIdentifier::new_from_enum_mock(IdentityType::PNO, CountryCode::BE);
100+
101+
/// Verify if a certificate exists for given id
102+
let res = get_certificate_by_semantic_identifier(&cfg, sem_id).await;
103+
match res {
104+
Ok(r) => {
105+
let cert = validate_response_success(r).map(|res| res.cert.unwrap().value.unwrap())?;
106+
info!("Smart ID Certificate {:#?}", cert);
107+
Ok(())
108+
}
109+
Err(_) => Err(anyhow::anyhow!("Error getting certificate"))
110+
}
111+
}
112+
```
113+
114+
### SmartID Authentication
115+
116+
To authenticate a user, use the `authenticate_by_semantic_identifier`:
117+
```shell
118+
async fn uc_authenticate_by_semantic_identifier(cfg: &SmartIDConfig) -> Result<()> {
119+
/// Create the semantic identifier
120+
let sem_id = SemanticsIdentifier::new_from_enum_mock(IdentityType::PNO, CountryCode::BE);
121+
122+
/// Define interactions
123+
let interactions: Vec<Interaction> = vec![Interaction::diplay_text_and_pin("Authenticate to Application: ReadMyCards")];
124+
125+
/// Create hash
126+
let hash_type = HashType::SHA256;
127+
let hash = sha_digest("This is a test string".to_string().into_bytes(), &hash_type)?;
128+
let b64_hash = base64::engine::general_purpose::STANDARD.encode(hash.as_ref());
129+
let verification_code_for_user = generate_verification_number(hash.as_ref().to_vec())?;
130+
info!("Verification code for user: {}", verification_code_for_user);
131+
132+
/// Ask user for authentication
133+
let res = authenticate_by_semantic_identifier(&cfg, sem_id, interactions, b64_hash, hash_type).await;
134+
135+
match res {
136+
Ok(r) => {
137+
let session_result = validate_response_success(r).map(|res| res.result)?;
138+
info!("Smart ID Authentication result {:#?}", session_result);
139+
Ok(())
140+
}
141+
Err(_) => Err(anyhow::anyhow!("Error during authentication"))
142+
}
143+
}
144+
```
145+
146+
### SmartID Digital Signature
147+
148+
To sign a document as a user, use the `sign_by_semantic_identifier`:
149+
```shell
150+
async fn uc_sign_by_semantic_identifier(cfg: &SmartIDConfig) -> Result<()> {
151+
/// Create the semantic identifier
152+
let sem_id = SemanticsIdentifier::new_from_enum_mock(IdentityType::PNO, CountryCode::BE);
153+
154+
/// Define interactions
155+
let interactions: Vec<Interaction> = vec![Interaction::confirmation_message("Are you sure to sign document: something.pdf?"), Interaction::diplay_text_and_pin("Sign using ReadMyCards")];
156+
157+
/// Create hash
158+
let hash_type = HashType::SHA256;
159+
let hash = sha_digest("This is a test string".to_string().into_bytes(), &hash_type)?;
160+
let b64_hash = base64::engine::general_purpose::STANDARD.encode(hash.as_ref());
161+
162+
/// Create verification cod
163+
let verification_code_for_user = generate_verification_number(hash.as_ref().to_vec())?;
164+
info!("Verification code for user: {}", verification_code_for_user);
165+
166+
/// Ask user to sign
167+
let res = sign_by_semantic_identifier(&cfg, sem_id, interactions, b64_hash, hash_type).await;
168+
match res {
169+
Ok(r) => {
170+
match validate_response_success(r).map(|res| res.signature)? {
171+
None => {
172+
warn!("No signature");
173+
Ok(())
174+
}
175+
Some(signature) => {
176+
info!("Smart ID signature result {:#?}", signature);
177+
Ok(())
178+
}
179+
}
180+
}
181+
Err(_) => Err(anyhow::anyhow!("Error signing digest"))
182+
}
183+
}
184+
```

src/client/smart_id_connector.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::models::common::{SemanticsIdentifier};
22
use crate::models::session::SessionStatus;
33
use anyhow::Result;
4-
use tracing::debug;
5-
use tracing::log::info;
4+
use tracing::{debug, info};
65
use crate::client::reqwest_generic::{get, post};
76
use crate::models::requests::{AuthenticationSessionRequest, CertificateRequest, SignatureSessionRequest};
87
use crate::models::responses::{AuthenticationSessionResponse, CertificateChoiceResponse, SignatureSessionResponse};

0 commit comments

Comments
 (0)