Skip to content

Commit 3b3f862

Browse files
committed
Improve Agent API
1 parent 66ae8bd commit 3b3f862

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

lib/src/agents.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ impl<T: Into<String>> From<T> for ForAgent {
4444
}
4545
}
4646

47+
/// An Agent can be thought of as a User. Agents are used for authentication and authorization.
48+
/// The private key of the Agent is used to sign [crate::Commit]s.
4749
#[derive(Clone, Debug)]
4850
pub struct Agent {
4951
/// Private key for signing commits
5052
pub private_key: Option<String>,
51-
/// Private key for signing commits
53+
/// Used for validating commit signatures and for the username.
5254
pub public_key: String,
5355
/// URL of the Agent
5456
pub subject: String,
@@ -86,6 +88,8 @@ impl Agent {
8688
Ok(Agent::new_from_private_key(name, store, &keypair.private))
8789
}
8890

91+
/// Creates a new Agent on this server, using the server's Server URL.
92+
/// Derives the public key.
8993
pub fn new_from_private_key(
9094
name: Option<&str>,
9195
store: &impl Storelike,
@@ -102,6 +106,8 @@ impl Agent {
102106
}
103107
}
104108

109+
/// Creates a new Agent on this server, using the server's Server URL.
110+
/// This will not be able to write, because there is no private key.
105111
pub fn new_from_public_key(store: &impl Storelike, public_key: &str) -> AtomicResult<Agent> {
106112
verify_public_key(public_key)?;
107113

@@ -128,6 +134,18 @@ impl Agent {
128134
};
129135
Ok(agent)
130136
}
137+
138+
pub fn from_private_key_and_subject(private_key: &str, subject: &str) -> AtomicResult<Agent> {
139+
let keypair = generate_public_key(private_key);
140+
141+
Ok(Agent {
142+
private_key: Some(keypair.private),
143+
public_key: keypair.public.clone(),
144+
subject: subject.into(),
145+
name: None,
146+
created_at: crate::utils::now(),
147+
})
148+
}
131149
}
132150

133151
/// keypair, serialized using base64
@@ -142,10 +160,10 @@ fn generate_keypair() -> AtomicResult<Pair> {
142160
let rng = ring::rand::SystemRandom::new();
143161
const SEED_LEN: usize = 32;
144162
let seed: [u8; SEED_LEN] = ring::rand::generate(&rng)
145-
.map_err(|_| "Error generating random seed: {}")?
163+
.map_err(|e| format!("Error generating random seed: {}", e))?
146164
.expose();
147165
let key_pair = ring::signature::Ed25519KeyPair::from_seed_unchecked(&seed)
148-
.map_err(|e| format!("Error generating keypair {}", e))
166+
.map_err(|e| format!("Error generating keypair: {}", e))
149167
.unwrap();
150168
Ok(Pair {
151169
private: encode_base64(&seed),
@@ -158,7 +176,7 @@ pub fn generate_public_key(private_key: &str) -> Pair {
158176
use ring::signature::KeyPair;
159177
let private_key_bytes = decode_base64(private_key).unwrap();
160178
let key_pair = ring::signature::Ed25519KeyPair::from_seed_unchecked(private_key_bytes.as_ref())
161-
.map_err(|_| "Error generating keypair")
179+
.map_err(|e| format!("Error generating keypair: {e}"))
162180
.unwrap();
163181
Pair {
164182
private: encode_base64(&private_key_bytes),

0 commit comments

Comments
 (0)