uniauth/
lib.rs

1//! Easy to use abstraction over authentication
2//! Both servers and clients must use `make_challenge`.
3//! Clients must communicate with the authentication daemon using `daemon::Daemon`.
4//! Servers use `AnyPubkey::verify` on signature sent by a connecting client
5
6#[cfg(feature = "any")]
7pub mod any;
8#[cfg(feature = "client")]
9pub mod daemon;
10#[cfg(feature = "error")]
11pub mod error;
12pub mod requests;
13pub mod status;
14pub mod util;
15
16use std::io::Write;
17#[cfg(feature = "daemon")]
18use std::{
19	env,
20	path::PathBuf
21};
22
23#[cfg(feature = "daemon")]
24/// Get the default path to the daemon's unix socket
25pub fn daemon_path() -> PathBuf {
26	let runtime_dir = env::var("XDG_RUNTIME_DIR")
27		.expect("Missing XDG_RUNTIME_DIR environment variable");
28	let mut path = PathBuf::from(runtime_dir);
29	path.push("uniauth2.sock");
30	path
31}
32
33/// Create challenge text that the authenticator will sign.
34/// It should not be parsed as any field can contain / and "break" the format
35pub fn make_challenge(service: &str, name: &str, action: &str, nonce: &[u8]) -> Vec<u8> {
36	// 7 = UNIAUTH
37	// 4 = '/' * 4
38	let len = 11 + service.len() + name.len() + action.len() + nonce.len();
39	util::make_packet(len, |p| {
40		p.write_all(b"UNIAUTH")?;
41		p.push(b'/');
42		p.write_all(service.as_bytes())?;
43		p.push(b'/');
44		p.write_all(name.as_bytes())?;
45		p.push(b'/');
46		p.write_all(action.as_bytes())?;
47		p.push(b'/');
48		p.write_all(nonce)
49	}).unwrap()
50}