mirror of
https://git.verdigado.com/NB-Public/simple-wkd.git
synced 2024-10-30 03:05:51 +01:00
Improve getting user email from cert
This commit is contained in:
parent
6a090414fe
commit
6d65c5ffc3
3 changed files with 20 additions and 7 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
/target
|
/target
|
||||||
/data
|
/data
|
||||||
|
/logs
|
||||||
/config.toml
|
/config.toml
|
|
@ -3,6 +3,8 @@ use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Error, Debug, Clone, Copy)]
|
#[derive(Error, Debug, Clone, Copy)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
#[error("EC1: Cert is invalid")]
|
||||||
|
InvalidCert,
|
||||||
#[error("EP1: Error while parsing cert")]
|
#[error("EP1: Error while parsing cert")]
|
||||||
ParseCert,
|
ParseCert,
|
||||||
#[error("EP2: Error while parsing an E-Mail address")]
|
#[error("EP2: Error while parsing an E-Mail address")]
|
||||||
|
|
24
src/utils.rs
24
src/utils.rs
|
@ -4,7 +4,7 @@ use crate::settings::SETTINGS;
|
||||||
use flexi_logger::{style, DeferredNow, FileSpec, FlexiLoggerError, Logger, LoggerHandle, Record};
|
use flexi_logger::{style, DeferredNow, FileSpec, FlexiLoggerError, Logger, LoggerHandle, Record};
|
||||||
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
||||||
use sequoia_net::wkd::Url;
|
use sequoia_net::wkd::Url;
|
||||||
use sequoia_openpgp::{parse::Parse, Cert};
|
use sequoia_openpgp::{parse::Parse, Cert, policy::NullPolicy};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
|
@ -15,10 +15,15 @@ macro_rules! pending_path {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_pem(pemfile: &str) -> Result<Cert, Error> {
|
pub fn parse_pem(pemfile: &str) -> Result<Cert, Error> {
|
||||||
match sequoia_openpgp::Cert::from_bytes(pemfile.as_bytes()) {
|
let cert = match sequoia_openpgp::Cert::from_bytes(pemfile.as_bytes()) {
|
||||||
Ok(cert) => Ok(cert),
|
Ok(cert) => cert,
|
||||||
Err(_) => Err(Error::ParseCert),
|
Err(_) => return Err(Error::ParseCert),
|
||||||
|
};
|
||||||
|
let policy = NullPolicy::new();
|
||||||
|
if cert.with_policy(&policy, None).is_err() {
|
||||||
|
return Err(Error::InvalidCert)
|
||||||
}
|
}
|
||||||
|
Ok(cert)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn gen_random_token() -> String {
|
pub fn gen_random_token() -> String {
|
||||||
|
@ -27,9 +32,14 @@ pub fn gen_random_token() -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_email_from_cert(cert: &Cert) -> Result<String, Error> {
|
pub fn get_email_from_cert(cert: &Cert) -> Result<String, Error> {
|
||||||
let userid_opt = match cert.userids().next() {
|
let policy = NullPolicy::new();
|
||||||
Some(userid_opt) => userid_opt,
|
let validcert = match cert.with_policy(&policy, None) {
|
||||||
None => return Err(Error::ParseCert),
|
Ok(validcert) => validcert,
|
||||||
|
Err(_) => return Err(Error::InvalidCert)
|
||||||
|
};
|
||||||
|
let userid_opt = match validcert.primary_userid() {
|
||||||
|
Ok(userid_opt) => userid_opt,
|
||||||
|
Err(_) => return Err(Error::ParseCert),
|
||||||
};
|
};
|
||||||
let email_opt = match userid_opt.email() {
|
let email_opt = match userid_opt.email() {
|
||||||
Ok(email_opt) => email_opt,
|
Ok(email_opt) => email_opt,
|
||||||
|
|
Loading…
Reference in a new issue