examples(authn): add signup example

This commit is contained in:
nazeh
2024-10-02 17:17:01 +03:00
parent 4730927226
commit 02bea3146c
7 changed files with 81 additions and 5 deletions

2
Cargo.lock generated
View File

@@ -136,7 +136,7 @@ dependencies = [
]
[[package]]
name = "authenticator"
name = "authn"
version = "0.1.0"
dependencies = [
"anyhow",

View File

@@ -3,7 +3,7 @@ members = [
"pubky",
"pubky-*",
"examples/authz/authenticator"
"examples"
]
# See: https://github.com/rust-lang/rust/issues/90148#issuecomment-949194352

6
examples/README.md Normal file
View File

@@ -0,0 +1,6 @@
# Pubky examples
Minimal examples for different flows and functions you might need to implement using Pubky.
- [authentication](./authn/README.md): shows how to signup, signin or signout to and from a homeserver.
- [authorization flow](./authz/README.md): shows how to setup Pubky authz for a 3rd party application and how to implement an authenticator to sign in such app.

13
examples/authn/README.md Normal file
View File

@@ -0,0 +1,13 @@
# Authentication examples
You can use these examples to test Signup or Signin to a provided homeserver using a keypair,
as opposed to using a the 3rd party [authorization flow](../authz).
## Usage
### Signup
```bash
cargo run --bin signup <homeserver pubky> </path/to/recovery file>
```

49
examples/authn/signup.rs Normal file
View File

@@ -0,0 +1,49 @@
use anyhow::Result;
use clap::Parser;
use pubky::PubkyClient;
use std::path::PathBuf;
use pubky_common::{crypto::PublicKey};
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Cli {
/// Homeserver Pkarr Domain (for example `5jsjx1o6fzu6aeeo697r3i5rx15zq41kikcye8wtwdqm4nb4tryo`)
homeserver: String,
/// Path to a recovery_file of the Pubky you want to sign in with
recovery_file: PathBuf,
}
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse();
let recovery_file = std::fs::read(&cli.recovery_file)?;
println!("\nSuccessfully opened recovery file");
let homeserver = cli.homeserver;
let client = PubkyClient::builder().build() ;
println!("Enter your recovery_file's passphrase to signup:");
let passphrase = rpassword::read_password()?;
let keypair = pubky_common::recovery_file::decrypt_recovery_file(&recovery_file, &passphrase)?;
println!("Successfully decrypted the recovery file, signing up to the homeserver:");
client
.signup(&keypair, &PublicKey::try_from(homeserver).unwrap())
.await?;
println!("Successfully signed up. Checking session:");
let session = client.session(&keypair.public_key()).await?;
println!("Successfully resolved current session at the homeserver.");
println!("{:?}", session);
Ok(())
}

View File

@@ -1,14 +1,22 @@
[package]
name = "authenticator"
name = "authn"
version = "0.1.0"
edition = "2021"
[[bin]]
name = "signup"
path = "./authn/signup.rs"
[[bin]]
name = "authenticator"
path = "./authz/authenticator.rs"
[dependencies]
anyhow = "1.0.86"
base64 = "0.22.1"
clap = { version = "4.5.16", features = ["derive"] }
pubky = { version = "0.1.0", path = "../../../pubky" }
pubky-common = { version = "0.1.0", path = "../../../pubky-common" }
pubky = { version = "0.1.0", path = "../pubky" }
pubky-common = { version = "0.1.0", path = "../pubky-common" }
rpassword = "7.3.1"
tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] }
url = "2.5.2"