Commit ee1ad9c4 authored by Julian Sparber's avatar Julian Sparber

login: store identity server

* save identity server used by a user at login
* store identity server in the same place as the homeserver
parent 2a72fc0e
......@@ -38,6 +38,7 @@ impl AppOp {
self.sync();
self.init_protocols();
}
......@@ -95,12 +96,17 @@ impl AppOp {
let server_entry: gtk::Entry = self.ui.builder
.get_object("login_server")
.expect("Can't find login_server in ui file.");
let idp_entry: gtk::Entry = self.ui.builder
.get_object("login_idp")
.expect("Can't find login_idp in ui file.");
let login_error: gtk::Label = self.ui.builder
.get_object("login_error_msg")
.expect("Can't find login_error_msg in ui file.");
let username = user_entry.get_text();
let password = pass_entry.get_text();
let server = server_entry.get_text();
let identity = idp_entry.get_text();
if username.clone().unwrap_or_default().is_empty() ||
password.clone().unwrap_or_default().is_empty() {
......@@ -112,12 +118,14 @@ impl AppOp {
login_error.hide();
}
/* FIXME: validate server and identity same as username and passwod */
self.set_state(AppState::Loading);
self.since = None;
self.connect(username, password, server_entry.get_text());
self.connect(username, password, server, identity);
}
pub fn set_login_pass(&self, username: &str, password: &str, server: &str) {
pub fn set_login_pass(&self, username: &str, password: &str, server: &str, identity: &str) {
let user_entry: gtk::Entry = self.ui.builder
.get_object("login_username")
.expect("Can't find login_username in ui file.");
......@@ -127,10 +135,14 @@ impl AppOp {
let server_entry: gtk::Entry = self.ui.builder
.get_object("login_server")
.expect("Can't find login_server in ui file.");
let idp_entry: gtk::Entry = self.ui.builder
.get_object("login_idp")
.expect("Can't find login_idp in ui file.");
user_entry.set_text(username);
pass_entry.set_text(password);
server_entry.set_text(server);
idp_entry.set_text(identity);
}
#[allow(dead_code)]
......@@ -147,6 +159,9 @@ impl AppOp {
let server_entry: gtk::Entry = self.ui.builder
.get_object("register_server")
.expect("Can't find register_server in ui file.");
let _idp_entry: gtk::Entry = self.ui.builder
.get_object("login_idp")
.expect("Can't find login_idp in ui file.");
let username = match user_entry.get_text() {
Some(s) => s,
......@@ -170,6 +185,7 @@ impl AppOp {
Some(s) => s,
None => String::from("https://matrix.org"),
};
/* FIXME ask also for the identity server */
//self.store_pass(username.clone(), password.clone(), server_url.clone())
// .unwrap_or_else(|_| {
......@@ -183,13 +199,18 @@ impl AppOp {
self.backend.send(BKCommand::Register(uname, pass, ser)).unwrap();
}
pub fn connect(&mut self, username: Option<String>, password: Option<String>, server: Option<String>) -> Option<()> {
pub fn connect(&mut self, username: Option<String>, password: Option<String>, server: Option<String>, identity: Option<String>) -> Option<()> {
self.server_url = match server {
Some(s) => s,
None => String::from("https://matrix.org"),
};
self.store_pass(username.clone()?, password.clone()?, self.server_url.clone())
self.identity_url = match identity {
Some(u) => u,
None => String::from("https://vector.im"),
};
self.store_pass(username.clone()?, password.clone()?, self.server_url.clone(), self.identity_url.clone())
.unwrap_or_else(|_| {
// TODO: show an error
println!("Error: Can't store the password using libsecret");
......
......@@ -64,6 +64,7 @@ pub struct AppOp {
pub uid: Option<String>,
pub avatar: Option<String>,
pub server_url: String,
pub identity_url: String,
pub autoscroll: bool,
pub active_room: Option<String>,
......@@ -115,6 +116,7 @@ impl AppOp {
uid: None,
avatar: None,
server_url: String::from("https://matrix.org"),
identity_url: String::from("https://vector.im"),
syncing: false,
tmp_msgs: vec![],
shown_messages: 0,
......@@ -157,8 +159,8 @@ impl AppOp {
if let Ok((token, uid)) = self.get_token() {
self.set_token(Some(token), Some(uid), Some(pass.2));
} else {
self.set_login_pass(&pass.0, &pass.1, &pass.2);
self.connect(Some(pass.0), Some(pass.1), Some(pass.2));
self.set_login_pass(&pass.0, &pass.1, &pass.2, &pass.3);
self.connect(Some(pass.0), Some(pass.1), Some(pass.2), Some(pass.3));
}
} else {
self.set_state(AppState::Login);
......
......@@ -46,14 +46,14 @@ pub trait PasswordStorage {
}
}
fn store_pass(&self, username: String, password: String, server: String) -> Result<(), Error> {
fn store_pass(&self, username: String, password: String, server: String, identity: String) -> Result<(), Error> {
match pwd_conf() {
PWDConf::PlainText => plain_text::store_pass(username, password, server),
_ => ss_storage::store_pass(username, password, server),
PWDConf::PlainText => plain_text::store_pass(username, password, server, identity),
_ => ss_storage::store_pass(username, password, server, identity),
}
}
fn get_pass(&self) -> Result<(String, String, String), Error> {
fn get_pass(&self) -> Result<(String, String, String, String), Error> {
match pwd_conf() {
PWDConf::PlainText => plain_text::get_pass(),
_ => ss_storage::get_pass(),
......@@ -149,7 +149,7 @@ mod ss_storage {
Ok((token, uid))
}
pub fn store_pass(username: String, password: String, server: String) -> Result<(), Error> {
pub fn store_pass(username: String, password: String, server: String, identity: String) -> Result<(), Error> {
let ss = SecretService::new(EncryptionType::Dh)?;
let collection = ss.get_default_collection()?;
let key = "fractal";
......@@ -161,7 +161,7 @@ mod ss_storage {
collection.unlock()?;
collection.create_item(
key, // label
vec![("username", &username), ("server", &server)], // properties
vec![("username", &username), ("server", &server), ("identity", &identity)], // properties
password.as_bytes(), //secret
true, // replace item with same attributes
"text/plain", // secret content type
......@@ -205,13 +205,16 @@ mod ss_storage {
for p in passwd {
p.delete()?;
}
/* It wasn't possibile to have a different identity server therefore set it always to
* vector.im */
let identity = String::from("https://vector.im");
store_pass(username, pwd, server)?;
store_pass(username, pwd, server, identity)?;
Ok(())
}
pub fn get_pass() -> Result<(String, String, String), Error> {
pub fn get_pass() -> Result<(String, String, String, String), Error> {
migrate_old_passwd()?;
let ss = SecretService::new(EncryptionType::Dh)?;
......@@ -232,18 +235,30 @@ mod ss_storage {
let attrs = p.get_attributes()?;
let secret = p.get_secret()?;
let mut attr = attrs
let attr = attrs
.iter()
.find(|&ref x| x.0 == "username")
.ok_or(Error::SecretServiceError)?;
let username = attr.1.clone();
attr = attrs
let attr = attrs
.iter()
.find(|&ref x| x.0 == "server")
.ok_or(Error::SecretServiceError)?;
let server = attr.1.clone();
let tup = (username, String::from_utf8(secret).unwrap(), server);
let attr = attrs
.iter()
.find(|&ref x| x.0 == "identity");
/* Fallback to the vector identity server when there is none */
let identity = match attr {
Some(a) => a.1.clone(),
None => {
String::from("https://vector.im")
},
};
let tup = (username, String::from_utf8(secret).unwrap(), server, identity);
Ok(tup)
}
......@@ -263,6 +278,7 @@ mod plain_text {
pub struct UserData {
pub username: String,
pub server: String,
pub identity: String,
pub password: Option<String>,
pub token: Option<String>,
}
......@@ -326,17 +342,18 @@ mod plain_text {
Ok((data.token.unwrap_or_default(), data.username))
}
pub fn store_pass(username: String, password: String, server: String) -> Result<(), Error> {
pub fn store_pass(username: String, password: String, server: String, identity: String) -> Result<(), Error> {
let mut data = load().unwrap_or_default();
data.username = username;
data.password = Some(password);
data.server = server;
data.identity = identity;
store(&data)?;
Ok(())
}
pub fn get_pass() -> Result<(String, String, String), Error> {
pub fn get_pass() -> Result<(String, String, String, String), Error> {
let data = load().unwrap_or_default();
Ok((data.username, data.password.unwrap_or_default(), data.server))
Ok((data.username, data.password.unwrap_or_default(), data.server, data.identity))
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment