Refactor Login into login.rs

This commit is contained in:
Nicholas Rempel 2021-11-29 14:25:35 -08:00 committed by masalachai
parent 76f2ca5b16
commit 2745e2b1ca
7 changed files with 93 additions and 84 deletions

View File

@ -53,10 +53,9 @@ use crate::config::EppClientConfig;
use crate::connection::registry::{epp_connect, EppConnection}; use crate::connection::registry::{epp_connect, EppConnection};
use crate::error; use crate::error;
use crate::hello::{EppGreeting, EppHello}; use crate::hello::{EppGreeting, EppHello};
use crate::request::{generate_client_tr_id, EppLogin, EppLogout}; use crate::login::{EppLogin, EppLoginResponse};
use crate::response::{ use crate::request::{generate_client_tr_id, EppLogout};
EppCommandResponse, EppCommandResponseError, EppLoginResponse, EppLogoutResponse, use crate::response::{EppCommandResponse, EppCommandResponseError, EppLogoutResponse};
};
use crate::xml::EppXml; use crate::xml::EppXml;
/// Instances of the EppClient type are used to transact with the registry. /// Instances of the EppClient type are used to transact with the registry.
/// Once initialized, the EppClient instance can serialize EPP requests to XML and send them /// Once initialized, the EppClient instance can serialize EPP requests to XML and send them

View File

@ -108,6 +108,7 @@ pub mod domain;
pub mod error; pub mod error;
pub mod hello; pub mod hello;
pub mod host; pub mod host;
pub mod login;
pub mod message; pub mod message;
pub mod request; pub mod request;
pub mod response; pub mod response;

83
epp-client/src/login.rs Normal file
View File

@ -0,0 +1,83 @@
use std::fmt::Debug;
use epp_client_macros::ElementName;
use serde::{Deserialize, Serialize};
use crate::{
common::{ElementName, EppObject, Options, ServiceExtension, Services, StringValue},
contact::EPP_CONTACT_XMLNS,
domain::EPP_DOMAIN_XMLNS,
host::EPP_HOST_XMLNS,
request::{Command, EPP_LANG, EPP_VERSION},
response::EppCommandResponse,
};
/// The EPP Login Request
pub type EppLogin = EppObject<Command<Login>>;
impl EppLogin {
/// Creates a new EPP Login request
pub fn new(
username: &str,
password: &str,
ext_uris: &Option<Vec<String>>,
client_tr_id: &str,
) -> EppLogin {
let ext_uris = ext_uris
.as_ref()
.map(|uris| uris.iter().map(|u| u.as_str().into()).collect());
let login = Login {
username: username.into(),
password: password.into(),
options: Options {
version: EPP_VERSION.into(),
lang: EPP_LANG.into(),
},
services: Services {
obj_uris: vec![
EPP_HOST_XMLNS.into(),
EPP_CONTACT_XMLNS.into(),
EPP_DOMAIN_XMLNS.into(),
],
svc_ext: Some(ServiceExtension { ext_uris }),
},
};
EppObject::build(Command::<Login> {
command: login,
extension: None,
client_tr_id: client_tr_id.into(),
})
}
/// Sets the <options> tag data
pub fn options(&mut self, options: Options) {
self.data.command.options = options;
}
/// Sets the <svcs> tag data
pub fn services(&mut self, services: Services) {
self.data.command.services = services;
}
}
/// An alias of `EppCommandResponse` received in response to a successful login request
pub type EppLoginResponse = EppCommandResponse;
#[derive(Serialize, Deserialize, Debug, PartialEq, ElementName)]
#[element_name(name = "login")]
/// Type corresponding to the &lt;login&gt; tag in an EPP XML login request
pub struct Login {
/// The username to use for the login
#[serde(rename(serialize = "clID", deserialize = "clID"))]
username: StringValue,
/// The password to use for the login
#[serde(rename = "pw", default)]
password: StringValue,
/// Data under the <options> tag
options: Options,
/// Data under the <svcs> tag
#[serde(rename = "svcs")]
services: Services,
}

View File

@ -4,16 +4,8 @@ use serde::{ser::SerializeStruct, ser::Serializer, Deserialize, Serialize};
use std::error::Error; use std::error::Error;
use std::time::SystemTime; use std::time::SystemTime;
use crate::{ use crate::common::{ElementName, EmptyTag, EppObject, Extension, StringValue};
common::{ use epp_client_macros::ElementName;
ElementName, EmptyTag, EppObject, Extension, Options, ServiceExtension, Services,
StringValue,
},
contact::EPP_CONTACT_XMLNS,
domain::EPP_DOMAIN_XMLNS,
host::EPP_HOST_XMLNS,
};
use epp_client_macros::*;
pub const EPP_VERSION: &str = "1.0"; pub const EPP_VERSION: &str = "1.0";
pub const EPP_LANG: &str = "en"; pub const EPP_LANG: &str = "en";
@ -22,8 +14,6 @@ pub const EPP_LANG: &str = "en";
/// without an &lt;extension&gt; tag /// without an &lt;extension&gt; tag
pub type Command<T> = CommandWithExtension<T, EmptyTag>; pub type Command<T> = CommandWithExtension<T, EmptyTag>;
/// The EPP Login Request
pub type EppLogin = EppObject<Command<Login>>;
/// The EPP Logout request /// The EPP Logout request
pub type EppLogout = EppObject<Command<Logout>>; pub type EppLogout = EppObject<Command<Logout>>;
@ -84,70 +74,6 @@ pub fn generate_client_tr_id(username: &str) -> Result<String, Box<dyn Error>> {
Ok(format!("{}:{}", username, timestamp.as_secs())) Ok(format!("{}:{}", username, timestamp.as_secs()))
} }
#[derive(Serialize, Deserialize, Debug, PartialEq, ElementName)]
#[element_name(name = "login")]
/// Type corresponding to the &lt;login&gt; tag in an EPP XML login request
pub struct Login {
/// The username to use for the login
#[serde(rename(serialize = "clID", deserialize = "clID"))]
username: StringValue,
/// The password to use for the login
#[serde(rename = "pw", default)]
password: StringValue,
/// Data under the <options> tag
options: Options,
/// Data under the <svcs> tag
#[serde(rename = "svcs")]
services: Services,
}
impl EppLogin {
/// Creates a new EPP Login request
pub fn new(
username: &str,
password: &str,
ext_uris: &Option<Vec<String>>,
client_tr_id: &str,
) -> EppLogin {
let ext_uris = ext_uris
.as_ref()
.map(|uris| uris.iter().map(|u| u.as_str().into()).collect());
let login = Login {
username: username.into(),
password: password.into(),
options: Options {
version: EPP_VERSION.into(),
lang: EPP_LANG.into(),
},
services: Services {
obj_uris: vec![
EPP_HOST_XMLNS.into(),
EPP_CONTACT_XMLNS.into(),
EPP_DOMAIN_XMLNS.into(),
],
svc_ext: Some(ServiceExtension { ext_uris }),
},
};
EppObject::build(Command::<Login> {
command: login,
extension: None,
client_tr_id: client_tr_id.into(),
})
}
/// Sets the <options> tag data
pub fn options(&mut self, options: Options) {
self.data.command.options = options;
}
/// Sets the <svcs> tag data
pub fn services(&mut self, services: Services) {
self.data.command.services = services;
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, ElementName)] #[derive(Serialize, Deserialize, Debug, PartialEq, ElementName)]
#[element_name(name = "logout")] #[element_name(name = "logout")]
/// Type corresponding to the &lt;logout&gt; tag in an EPP XML logout request /// Type corresponding to the &lt;logout&gt; tag in an EPP XML logout request

View File

@ -13,8 +13,6 @@ pub type CommandResponse<T> = CommandResponseWithExtension<T, EmptyTag>;
pub type EppCommandResponse = EppObject<CommandResponseStatus>; pub type EppCommandResponse = EppObject<CommandResponseStatus>;
/// An alias of `EppCommandResponse` indicating an EPP Error /// An alias of `EppCommandResponse` indicating an EPP Error
pub type EppCommandResponseError = EppCommandResponse; pub type EppCommandResponseError = EppCommandResponse;
/// An alias of `EppCommandResponse` received in response to a successful login request
pub type EppLoginResponse = EppCommandResponse;
/// An alias of `EppCommandResponse` received in response to a successful logout request /// An alias of `EppCommandResponse` received in response to a successful logout request
pub type EppLogoutResponse = EppCommandResponse; pub type EppLogoutResponse = EppCommandResponse;

View File

@ -28,9 +28,10 @@ mod response {
use crate::host::delete::EppHostDeleteResponse; use crate::host::delete::EppHostDeleteResponse;
use crate::host::info::EppHostInfoResponse; use crate::host::info::EppHostInfoResponse;
use crate::host::update::EppHostUpdateResponse; use crate::host::update::EppHostUpdateResponse;
use crate::login::EppLoginResponse;
use crate::message::ack::EppMessageAckResponse; use crate::message::ack::EppMessageAckResponse;
use crate::message::poll::EppMessagePollResponse; use crate::message::poll::EppMessagePollResponse;
use crate::response::{EppCommandResponseError, EppLoginResponse, EppLogoutResponse}; use crate::response::{EppCommandResponseError, EppLogoutResponse};
use crate::xml::EppXml; use crate::xml::EppXml;
const SVTRID: &str = "RO-6879-1627224678242975"; const SVTRID: &str = "RO-6879-1627224678242975";

View File

@ -35,9 +35,10 @@ mod request {
use crate::host::update::EppHostUpdate; use crate::host::update::EppHostUpdate;
use crate::host::update::HostAddRemove; use crate::host::update::HostAddRemove;
use crate::host::update::HostChangeInfo; use crate::host::update::HostChangeInfo;
use crate::login::EppLogin;
use crate::message::ack::EppMessageAck; use crate::message::ack::EppMessageAck;
use crate::message::poll::EppMessagePoll; use crate::message::poll::EppMessagePoll;
use crate::request::{EppLogin, EppLogout}; use crate::request::EppLogout;
use crate::xml::EppXml; use crate::xml::EppXml;
use chrono::{DateTime, NaiveDate}; use chrono::{DateTime, NaiveDate};
use std::str::FromStr; use std::str::FromStr;