Skip to content

Commit

Permalink
feat: remove AuthToken
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 8, 2023
1 parent 108495e commit 96d9378
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 212 deletions.
24 changes: 6 additions & 18 deletions src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::sync::Arc;
use std::time::{Duration, Instant};

use crate::blobs::Collection;
use crate::protocol::{write_lp, AnyGetRequest, AuthToken, Handshake, RangeSpecSeq};
use crate::protocol::{write_lp, AnyGetRequest, Handshake, RangeSpecSeq};
use crate::provider::Ticket;
use crate::subnet::{same_subnet_v4, same_subnet_v6};
use crate::tls::{self, Keypair, PeerId};
Expand Down Expand Up @@ -123,7 +123,7 @@ pub async fn run_ticket(
max_concurrent: u8,
) -> Result<get_response_machine::AtInitial> {
let connection = dial_ticket(ticket, keylog, max_concurrent.into()).await?;
Ok(run_connection(connection, request, ticket.token()))
Ok(run_connection(connection, request))
}

async fn dial_ticket(
Expand Down Expand Up @@ -245,24 +245,17 @@ pub mod get_response_machine {
pub struct AtInitial {
connection: quinn::Connection,
request: AnyGetRequest,
auth_token: AuthToken,
}

impl AtInitial {
/// Create a new get response
///
/// `connection` is an existing connection
/// `request` is the request to be sent
/// `auth_token` is the auth token for the request
pub fn new(
connection: quinn::Connection,
request: AnyGetRequest,
auth_token: AuthToken,
) -> Self {
pub fn new(connection: quinn::Connection, request: AnyGetRequest) -> Self {
Self {
connection,
request,
auth_token,
}
}

Expand All @@ -277,7 +270,6 @@ pub mod get_response_machine {
reader,
writer,
request: self.request,
auth_token: self.auth_token,
})
}
}
Expand All @@ -289,7 +281,6 @@ pub mod get_response_machine {
reader: TrackingReader<quinn::RecvStream>,
writer: TrackingWriter<quinn::SendStream>,
request: AnyGetRequest,
auth_token: AuthToken,
}

/// Possible next states after the handshake has been sent
Expand All @@ -316,14 +307,13 @@ pub mod get_response_machine {
mut reader,
mut writer,
request,
auth_token,
} = self;
let mut out_buffer = BytesMut::zeroed(Handshake::POSTCARD_MAX_SIZE);

// 1. Send Handshake
{
debug!("sending handshake");
let handshake = Handshake::new(auth_token);
let handshake = Handshake::new();
let used = postcard::to_slice(&handshake, &mut out_buffer)?;
write_lp(&mut writer, used).await?;
}
Expand Down Expand Up @@ -734,20 +724,18 @@ pub mod get_response_machine {
/// Dial a peer and run a get request
pub async fn run(
request: AnyGetRequest,
auth_token: AuthToken,
opts: Options,
) -> anyhow::Result<get_response_machine::AtInitial> {
let connection = dial_peer(opts).await?;
Ok(run_connection(connection, request, auth_token))
Ok(run_connection(connection, request))
}

/// Do a get request and return a stream of responses
pub fn run_connection(
connection: quinn::Connection,
request: AnyGetRequest,
auth_token: AuthToken,
) -> get_response_machine::AtInitial {
get_response_machine::AtInitial::new(connection, request, auth_token)
get_response_machine::AtInitial::new(connection, request)
}

/// Error when processing a response
Expand Down
26 changes: 4 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod tests {
blobs::Collection,
get::{dial_peer, get_response_machine},
get::{get_response_machine::ConnectedNext, Stats},
protocol::{AnyGetRequest, AuthToken, GetRequest},
protocol::{AnyGetRequest, GetRequest},
provider::{create_collection, CustomGetHandler, DataSource, Database, Event, Provider},
tls::PeerId,
util::Hash,
Expand Down Expand Up @@ -143,7 +143,6 @@ mod tests {

async fn run_client(
hash: Hash,
token: AuthToken,
file_hash: Hash,
name: String,
addr: SocketAddr,
Expand All @@ -157,7 +156,7 @@ mod tests {
};
let expected_data = &content;
let expected_name = &name;
let response = get::run(GetRequest::all(hash).into(), token, opts).await?;
let response = get::run(GetRequest::all(hash).into(), opts).await?;
let (collection, children, _stats) = aggregate_get_response(response).await?;
assert_eq!(expected_name, &collection.blobs()[0].name);
assert_eq!(&file_hash, &collection.blobs()[0].hash);
Expand All @@ -170,7 +169,6 @@ mod tests {
for _i in 0..3 {
tasks.push(tokio::task::spawn(run_client(
hash,
provider.auth_token(),
expect_hash.into(),
expect_name.clone(),
provider.local_address(),
Expand Down Expand Up @@ -267,12 +265,7 @@ mod tests {
keylog: true,
};

let response = get::run(
GetRequest::all(collection_hash).into(),
provider.auth_token(),
opts,
)
.await?;
let response = get::run(GetRequest::all(collection_hash).into(), opts).await?;
let (collection, children, _stats) = aggregate_get_response(response).await?;
assert_eq!(num_blobs, collection.blobs().len());
for (i, (name, path, hash)) in expects.into_iter().enumerate() {
Expand Down Expand Up @@ -343,7 +336,6 @@ mod tests {
.bind_addr("127.0.0.1:0".parse().unwrap())
.spawn()
.unwrap();
let auth_token = provider.auth_token();
let provider_addr = provider.local_address();

// This tasks closes the connection on the provider side as soon as the transfer
Expand Down Expand Up @@ -374,7 +366,6 @@ mod tests {

let response = get::run(
GetRequest::all(hash).into(),
auth_token,
get::Options {
addr: provider_addr,
peer_id: None,
Expand Down Expand Up @@ -411,13 +402,11 @@ mod tests {
let provider = Provider::builder(db)
.bind_addr("127.0.0.1:0".parse().unwrap())
.spawn()?;
let auth_token = provider.auth_token();
let provider_addr = provider.local_address();

let timeout = tokio::time::timeout(std::time::Duration::from_secs(10), async move {
let request = get::run(
GetRequest::all(hash).into(),
auth_token,
get::Options {
addr: provider_addr,
peer_id: None,
Expand Down Expand Up @@ -456,13 +445,11 @@ mod tests {
return;
}
};
let auth_token = provider.auth_token();
let addr = provider.local_address();
let peer_id = Some(provider.peer_id());
tokio::time::timeout(Duration::from_secs(10), async move {
let request = get::run(
GetRequest::all(hash).into(),
auth_token,
get::Options {
addr,
peer_id,
Expand Down Expand Up @@ -561,7 +548,6 @@ mod tests {
return;
}
};
let auth_token = provider.auth_token();
let addr = provider.local_address();
let peer_id = Some(provider.peer_id());
tokio::time::timeout(Duration::from_secs(10), async move {
Expand All @@ -572,7 +558,7 @@ mod tests {
})
.await?;
let request = GetRequest::all(hash).into();
let stream = get::run_connection(connection, request, auth_token);
let stream = get::run_connection(connection, request);
let (collection, children, _) = aggregate_get_response(stream).await?;
validate_children(collection, children)?;
anyhow::Ok(())
Expand Down Expand Up @@ -641,14 +627,12 @@ mod tests {
.custom_get_handler(BlobCustomHandler)
.spawn()
.unwrap();
let auth_token = provider.auth_token();
let addr = provider.local_address();
let peer_id = Some(provider.peer_id());
tokio::time::timeout(Duration::from_secs(10), async move {
let request: AnyGetRequest = Bytes::from(&b"hello"[..]).into();
let response = get::run(
request,
auth_token,
get::Options {
addr,
peer_id,
Expand Down Expand Up @@ -677,14 +661,12 @@ mod tests {
.custom_get_handler(CollectionCustomHandler)
.spawn()
.unwrap();
let auth_token = provider.auth_token();
let addr = provider.local_address();
let peer_id = Some(provider.peer_id());
tokio::time::timeout(Duration::from_secs(10), async move {
let request: AnyGetRequest = Bytes::from(&b"hello"[..]).into();
let response = get::run(
request,
auth_token,
get::Options {
addr,
peer_id,
Expand Down
37 changes: 5 additions & 32 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use indicatif::{
use iroh::blobs::{Blob, Collection};
use iroh::get::get_response_machine::{ConnectedNext, EndBlobNext};
use iroh::get::{get_data_path, get_missing_range, get_missing_ranges, pathbuf_from_name};
use iroh::protocol::{AuthToken, GetRequest, RangeSpecSeq};
use iroh::protocol::{GetRequest, RangeSpecSeq};
use iroh::provider::{Database, Provider, Ticket};
use iroh::rpc_protocol::*;
use iroh::rpc_protocol::{
Expand Down Expand Up @@ -119,9 +119,6 @@ enum Commands {
/// Listening address to bind to
#[clap(long, short, default_value_t = SocketAddr::from(provider::DEFAULT_BIND_ADDR))]
addr: SocketAddr,
/// Auth token, defaults to random generated
#[clap(long)]
auth_token: Option<String>,
/// RPC port, set to "disabled" to disable RPC
#[clap(long, default_value_t = ProviderRpcPort::Enabled(DEFAULT_RPC_PORT))]
rpc_port: ProviderRpcPort,
Expand Down Expand Up @@ -171,9 +168,6 @@ enum Commands {
/// PeerId of the provider
#[clap(long, short)]
peer: PeerId,
/// The authentication token to present to the server
#[clap(long)]
auth_token: String,
/// Address of the provider
#[clap(long, short, default_value_t = SocketAddr::from(get::DEFAULT_PROVIDER_ADDR))]
addr: SocketAddr,
Expand Down Expand Up @@ -509,7 +503,6 @@ async fn main_impl() -> Result<()> {
Commands::Get {
hash,
peer,
auth_token,
addr,
out,
single,
Expand All @@ -519,12 +512,9 @@ async fn main_impl() -> Result<()> {
peer_id: Some(peer),
keylog: cli.keylog,
};
let token = AuthToken::from_str(&auth_token)
.context("Wrong format for authentication token")?;
let get = GetInteractive::Hash {
hash: *hash.as_hash(),
opts,
token,
single,
};
tokio::select! {
Expand Down Expand Up @@ -553,7 +543,6 @@ async fn main_impl() -> Result<()> {
Commands::Provide {
path,
addr,
auth_token,
rpc_port,
} => {
let iroh_data_root = iroh_data_root()?;
Expand All @@ -574,15 +563,7 @@ async fn main_impl() -> Result<()> {
};
let key = Some(iroh_data_root.join("keypair"));

let provider = provide(
db.clone(),
addr,
auth_token,
key,
cli.keylog,
rpc_port.into(),
)
.await?;
let provider = provide(db.clone(), addr, key, cli.keylog, rpc_port.into()).await?;
let controller = provider.controller();

// task that will add data to the provider, either from a file or from stdin
Expand Down Expand Up @@ -695,7 +676,6 @@ async fn main_impl() -> Result<()> {

println!("Listening address: {}", response.listen_addr);
println!("PeerID: {}", response.peer_id);
println!("Auth token: {}", response.auth_token);
Ok(())
}
Commands::Add { path, rpc_port } => {
Expand Down Expand Up @@ -728,20 +708,15 @@ async fn main_impl() -> Result<()> {
async fn provide(
db: Database,
addr: SocketAddr,
auth_token: Option<String>,
key: Option<PathBuf>,
keylog: bool,
rpc_port: Option<u16>,
) -> Result<Provider> {
let keypair = get_keypair(key).await?;

let mut builder = provider::Provider::builder(db)
let builder = provider::Provider::builder(db)
.keylog(keylog)
.bind_addr(addr);
if let Some(ref encoded) = auth_token {
let auth_token = AuthToken::from_str(encoded)?;
builder = builder.auth_token(auth_token);
}
let provider = if let Some(rpc_port) = rpc_port {
let rpc_endpoint = make_rpc_endpoint(&keypair, rpc_port)?;
builder
Expand All @@ -754,7 +729,6 @@ async fn provide(

println!("Listening address: {}", provider.local_address());
println!("PeerID: {}", provider.peer_id());
println!("Auth token: {}", provider.auth_token());
println!();
Ok(provider)
}
Expand Down Expand Up @@ -811,7 +785,6 @@ enum GetInteractive {
Hash {
hash: Hash,
opts: get::Options,
token: AuthToken,
single: bool,
},
}
Expand Down Expand Up @@ -890,7 +863,7 @@ async fn get_to_dir(get: GetInteractive, out_dir: PathBuf) -> Result<()> {
GetInteractive::Ticket { ticket, keylog } => {
get::run_ticket(&ticket, request, keylog, MAX_CONCURRENT_DIALS).await?
}
GetInteractive::Hash { opts, token, .. } => get::run(request, token, opts).await?,
GetInteractive::Hash { opts, .. } => get::run(request, opts).await?,
};
let connected = response.next().await?;
progress!("{} Requesting ...", style("[2/3]").bold().dim());
Expand Down Expand Up @@ -1041,7 +1014,7 @@ async fn get_to_stdout(get: GetInteractive) -> Result<()> {
GetInteractive::Ticket { ticket, keylog } => {
get::run_ticket(&ticket, request, keylog, MAX_CONCURRENT_DIALS).await?
}
GetInteractive::Hash { opts, token, .. } => get::run(request, token, opts).await?,
GetInteractive::Hash { opts, .. } => get::run(request, opts).await?,
};
let connected = response.next().await?;
progress!("{} Requesting ...", style("[2/3]").bold().dim());
Expand Down
Loading

0 comments on commit 96d9378

Please sign in to comment.