Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to use separate library for interactions with irma server. #6

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ edition = "2018"
id-contact-sentry = { git = "https://github.com/id-contact/id-contact-sentry.git" }
id-contact-jwt = { git = "https://github.com/id-contact/id-contact-jwt.git" }
id-contact-proto = { git = "https://github.com/id-contact/id-contact-proto.git" }
irma = { git = "https://github.com/tweedegolf/irmars" }
log = "0.4.14"
rocket = { version = "0.5.0-rc.1", features = ["json"] }
serde = "1.0.123"
serde_json = "1.0.61"
serde_yaml = "0.8.16"
strum = "0.20.0"
strum_macros = "0.20.0"
reqwest = { version = "0.11.0", features = ["json"] }
reqwest = { version = "0.11.6", features = ["json"] }
urlencoding = "1.1.1"
base64 = "0.13.0"
josekit = "0.7.1"
Expand Down
51 changes: 35 additions & 16 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type AttributeMapping = HashMap<String, Vec<String>>;

#[derive(Debug)]
pub enum Error {
Irma(irma::Error),
UnknownAttribute(String),
NotMatching(&'static str),
InvalidResponse(&'static str),
Expand All @@ -34,6 +35,12 @@ impl From<id_contact_jwt::Error> for Error {
}
}

impl From<irma::Error> for Error {
fn from(e: irma::Error) -> Error {
Error::Irma(e)
}
}

impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand All @@ -45,6 +52,7 @@ impl Display for Error {
}
Error::Json(e) => e.fmt(f),
Error::Jwt(e) => e.fmt(f),
Error::Irma(e) => e.fmt(f),
}
}
}
Expand All @@ -55,6 +63,7 @@ impl StdError for Error {
Error::Yaml(e) => Some(e),
Error::Json(e) => Some(e),
Error::Jwt(e) => Some(e),
Error::Irma(e) => Some(e),
_ => None,
}
}
Expand All @@ -66,12 +75,16 @@ struct IrmaserverConfig {
auth_token: Option<String>,
}

impl From<IrmaserverConfig> for super::irma::IrmaServer {
fn from(config: IrmaserverConfig) -> Self {
match config.auth_token {
Some(token) => Self::new_with_auth(&config.url, &token),
None => Self::new(&config.url),
}
impl TryFrom<IrmaserverConfig> for irma::IrmaClient {
type Error = Error;

fn try_from(config: IrmaserverConfig) -> Result<Self, Error> {
Ok(match config.auth_token {
Some(token) => irma::IrmaClientBuilder::new(&config.url)?
.token_authentication(token)
.build(),
None => irma::IrmaClient::new(&config.url)?,
})
}
}

Expand All @@ -95,7 +108,7 @@ pub struct Config {
sentry_dsn: Option<String>,
ui_irma_url: String,
attributes: AttributeMapping,
irma_server: super::irma::IrmaServer,
irma_server: irma::IrmaClient,
encrypter: Box<dyn JweEncrypter>,
signer: Box<dyn JwsSigner>,
}
Expand All @@ -110,24 +123,24 @@ impl TryFrom<RawConfig> for Config {
sentry_dsn: config.sentry_dsn,
ui_irma_url: config.ui_irma_url,
attributes: config.attributes,
irma_server: super::irma::IrmaServer::from(config.irma_server),
irma_server: irma::IrmaClient::try_from(config.irma_server)?,
encrypter: Box::<dyn JweEncrypter>::try_from(config.encryption_pubkey)?,
signer: Box::<dyn JwsSigner>::try_from(config.signing_privkey)?,
})
}
}

impl Config {
pub fn map_attributes(&self, attributes: &[String]) -> Result<crate::irma::ConDisCon, Error> {
let mut result: super::irma::ConDisCon = vec![];
pub fn map_attributes(&self, attributes: &[String]) -> Result<irma::ConDisCon, Error> {
let mut result: irma::ConDisCon = vec![];
for attribute in attributes {
let mut dis: Vec<Vec<super::irma::Attribute>> = vec![];
let mut dis: Vec<Vec<irma::AttributeRequest>> = vec![];
for request_attribute in self
.attributes
.get(attribute)
.ok_or_else(|| Error::UnknownAttribute(attribute.clone()))?
{
dis.push(vec![super::irma::Attribute::Simple(
dis.push(vec![irma::AttributeRequest::Simple(
request_attribute.clone(),
)]);
}
Expand All @@ -139,7 +152,7 @@ impl Config {
pub fn map_response(
&self,
attributes: &[String],
response: crate::irma::IrmaResult,
response: irma::SessionResult,
) -> Result<HashMap<String, String>, Error> {
if attributes.len() != response.disclosed.len() {
return Err(Error::NotMatching("mismatch between request and response"));
Expand All @@ -157,18 +170,24 @@ impl Config {
.attributes
.get(attribute)
.ok_or_else(|| Error::UnknownAttribute(attribute.clone()))?;
if !allowed_irma_attributes.contains(&response.disclosed[i][0].id) {
if !allowed_irma_attributes.contains(&response.disclosed[i][0].identifier) {
return Err(Error::InvalidResponse(
"Incorrect attribute in inner conjunction",
));
}
result.insert(attribute.clone(), response.disclosed[i][0].rawvalue.clone());
result.insert(
attribute.clone(),
response.disclosed[i][0]
.raw_value
.clone()
.unwrap_or_else(|| "".into()),
);
}

Ok(result)
}

pub fn irma_server(&self) -> &super::irma::IrmaServer {
pub fn irma_server(&self) -> &irma::IrmaClient {
&self.irma_server
}

Expand Down
Loading