From 02bea3146c9f1abb75fb812c263c93b3709b453a Mon Sep 17 00:00:00 2001 From: nazeh Date: Wed, 2 Oct 2024 17:17:01 +0300 Subject: [PATCH] examples(authn): add signup example --- Cargo.lock | 2 +- Cargo.toml | 2 +- examples/README.md | 6 +++ examples/authn/README.md | 13 +++++ examples/authn/signup.rs | 49 +++++++++++++++++++ .../src/main.rs => authenticator.rs} | 0 .../authenticator/Cargo.toml => cargo.toml} | 14 ++++-- 7 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 examples/README.md create mode 100644 examples/authn/README.md create mode 100644 examples/authn/signup.rs rename examples/authz/{authenticator/src/main.rs => authenticator.rs} (100%) rename examples/{authz/authenticator/Cargo.toml => cargo.toml} (55%) diff --git a/Cargo.lock b/Cargo.lock index d5af9d7..33dc3cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -136,7 +136,7 @@ dependencies = [ ] [[package]] -name = "authenticator" +name = "authn" version = "0.1.0" dependencies = [ "anyhow", diff --git a/Cargo.toml b/Cargo.toml index acd72a5..04d0af3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ members = [ "pubky", "pubky-*", - "examples/authz/authenticator" + "examples" ] # See: https://github.com/rust-lang/rust/issues/90148#issuecomment-949194352 diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..dad4ccc --- /dev/null +++ b/examples/README.md @@ -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. diff --git a/examples/authn/README.md b/examples/authn/README.md new file mode 100644 index 0000000..162c19e --- /dev/null +++ b/examples/authn/README.md @@ -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 +``` diff --git a/examples/authn/signup.rs b/examples/authn/signup.rs new file mode 100644 index 0000000..350f760 --- /dev/null +++ b/examples/authn/signup.rs @@ -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(()) +} diff --git a/examples/authz/authenticator/src/main.rs b/examples/authz/authenticator.rs similarity index 100% rename from examples/authz/authenticator/src/main.rs rename to examples/authz/authenticator.rs diff --git a/examples/authz/authenticator/Cargo.toml b/examples/cargo.toml similarity index 55% rename from examples/authz/authenticator/Cargo.toml rename to examples/cargo.toml index 932701b..a0e595f 100644 --- a/examples/authz/authenticator/Cargo.toml +++ b/examples/cargo.toml @@ -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"