1use std::{borrow::Cow, fmt::Debug};
2
3use instant_xml::ToXml;
4
5use crate::{
6 common::{NoExtension, ServiceExtension, EPP_XMLNS},
7 contact, domain, host,
8 request::{Command, Transaction, EPP_LANG, EPP_VERSION},
9};
10
11impl Transaction<NoExtension> for Login<'_> {}
12
13#[derive(Debug, Eq, PartialEq, ToXml)]
15#[xml(rename = "login", ns(EPP_XMLNS))]
16pub struct Login<'a> {
17 #[xml(rename = "clID")]
19 username: &'a str,
20 #[xml(rename = "pw")]
22 password: &'a str,
23 #[xml(rename = "newPW")]
25 new_password: Option<&'a str>,
26 options: Options<'a>,
28 #[xml(rename = "svcs")]
30 services: Services<'a>,
31}
32
33impl<'a> Login<'a> {
34 pub fn new(
35 username: &'a str,
36 password: &'a str,
37 new_password: Option<&'a str>,
38 ext_uris: Option<&'_ [&'a str]>,
39 ) -> Self {
40 Self {
41 username,
42 password,
43 new_password,
44 options: Options {
45 version: EPP_VERSION.into(),
46 lang: EPP_LANG.into(),
47 },
48 services: Services {
49 obj_uris: vec![
50 host::XMLNS.into(),
51 contact::XMLNS.into(),
52 domain::XMLNS.into(),
53 ],
54 svc_ext: ext_uris.and_then(|uris| {
55 (!uris.is_empty()).then(|| ServiceExtension {
56 ext_uris: uris.iter().map(|&u| u.into()).collect(),
57 })
58 }),
59 },
60 }
61 }
62
63 pub fn options(&mut self, options: Options<'a>) {
65 self.options = options;
66 }
67
68 pub fn services(&mut self, services: Services<'a>) {
70 self.services = services;
71 }
72}
73
74#[derive(Debug, Eq, PartialEq, ToXml)]
78#[xml(rename = "options", ns(EPP_XMLNS))]
79pub struct Options<'a> {
80 pub version: Cow<'a, str>,
82 pub lang: Cow<'a, str>,
84}
85
86impl<'a> Options<'a> {
87 pub fn build(version: &'a str, lang: &'a str) -> Self {
89 Self {
90 version: version.into(),
91 lang: lang.into(),
92 }
93 }
94}
95
96#[derive(Debug, Eq, PartialEq, ToXml)]
100#[xml(rename = "svcs", ns(EPP_XMLNS))]
101pub struct Services<'a> {
102 #[xml(rename = "objURI")]
104 pub obj_uris: Vec<Cow<'a, str>>,
105 #[xml(rename = "svcExtension")]
107 pub svc_ext: Option<ServiceExtension<'a>>,
108}
109
110impl Command for Login<'_> {
111 type Response = ();
112 const COMMAND: &'static str = "login";
113}
114
115#[cfg(test)]
116mod tests {
117 use super::Login;
118 use crate::response::ResultCode;
119 use crate::tests::{assert_serialized, response_from_file, CLTRID, SUCCESS_MSG, SVTRID};
120
121 #[test]
122 fn command() {
123 let ext_uris = Some(&["http://schema.ispapi.net/epp/xml/keyvalue-1.0"][..]);
124 let object = Login::new("username", "password", Some("new-password"), ext_uris);
125 assert_serialized("request/login.xml", &object);
126 }
127
128 #[test]
129 fn command_no_extension() {
130 let object = Login::new("username", "password", None, None);
131 assert_serialized("request/login_no_extension.xml", &object);
132 let object = Login::new("username", "password", None, Some(&[]));
133 assert_serialized("request/login_no_extension.xml", &object);
134 }
135
136 #[test]
137 fn response() {
138 let object = response_from_file::<Login>("response/login.xml");
139 assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
140 assert_eq!(object.result.message, SUCCESS_MSG);
141 assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
142 assert_eq!(object.tr_ids.server_tr_id, SVTRID);
143 }
144}