Skip to content

Commit

Permalink
chore: update lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
pxseu committed Jul 30, 2022
1 parent 5737e6a commit ac77744
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 78 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions scripts/bump.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

if [ -z "$1" ]; then
echo "Usage: $0 <tag>"
exit 1
fi

TAG=$1

echo "Bumping to $TAG"

if ! command -v cargo bump -h 2> /dev/null; then
echo "error: you do not have 'cargo bump' installed which is required for this script."
exit 1
fi

cargo bump $TAG

git add Cargo.*
git commit -m "feat: v$TAG" -S
git tag -a v$TAG -m v$TAG -s

echo "Done!"
echo "To push the changes and tag, run: git push --follow-tags"
161 changes: 84 additions & 77 deletions src/commands/auth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,88 +12,23 @@ use crate::state::State;
const WEB_AUTH_URL: &str = "https://console.hop.io/cli-auth";
const PAT_FALLBACK_URL: &str = "https://console.hop.io/settings/pats";

#[derive(Debug, Parser)]
#[derive(Debug, Parser, PartialEq, Default)]
#[clap(about = "Login to Hop")]
pub struct LoginOptions {
#[clap(name = "pat", help = "Personal Access Token")]
#[clap(long = "pat", help = "Personal Access Token")]
pub pat: Option<String>,
}

async fn request_handler(
req: Request<Body>,
sender: Sender<String>,
) -> Result<Response<Body>, Infallible> {
let query = req.uri().query();

// only send if it's an actual token
if let Some(query) = query {
// parse the query
// since pat should be a URL safe string we can just split on '='
let query: Vec<(String, String)> = query
.split("&")
.map(|s| parse_key_val(s).unwrap())
.collect::<Vec<_>>();

// if query has a key called "token"
if let Some(token) = query.iter().find(|(k, _)| k.to_owned() == "token") {
// send it to the main thread
sender.send(token.1.to_string()).await.unwrap();
return Ok(Response::new("You've been authorized".into()));
}
}

return Ok(Response::builder()
.status(400)
.body("You're not authorized".into())
.unwrap());
}

async fn web_auth(port: u16) -> Result<String, std::io::Error> {
let (sender, mut receiver) = channel::<String>(1);

let timeouter = sender.clone();

let timeout = task::spawn(async move {
tokio::time::sleep(tokio::time::Duration::from_secs(120)).await;
timeouter.send("timeout".to_string()).await.unwrap();
});

let service = make_service_fn(move |_| {
let sender = sender.clone();

async move {
Ok::<_, Infallible>(service_fn(move |req: Request<Body>| {
request_handler(req, sender.clone())
}))
}
});

let address = ([127, 0, 0, 1], port).into();

let server = Server::bind(&address).serve(service);

let runtime = task::spawn(async move {
if let Err(error) = server.await {
eprintln!("Server error: {}", error);
}
timeout.abort();
});

let response = receiver.recv().await;

runtime.abort();

if Some("timeout".to_string()) == response {
panic!("Reached the 2 minute timeout");
}

Ok(response.unwrap())
#[clap(short = 'u', long = "username", help = "Username")]
pub username: Option<String>,
#[clap(short = 'p', long = "password", help = "Password")]
pub password: Option<String>,
}

pub async fn handle_login(options: LoginOptions, mut state: State) -> Result<(), std::io::Error> {
let token = match options.pat {
Some(pat) => pat,
None => browser_login().await,
let token = if LoginOptions::default() == options {
browser_login().await
} else {
todo!();
// flags_login(options).await
};

// update the token assuming it's a valid PAT
Expand Down Expand Up @@ -143,6 +78,78 @@ async fn browser_login() -> String {
dialoguer::Password::new()
.with_prompt("Enter your token")
.interact()
.unwrap()
.ok()
.expect("Failed to get token")
}
}

async fn web_auth(port: u16) -> Result<String, std::io::Error> {
let (sender, mut receiver) = channel::<String>(1);

let timeouter = sender.clone();

let timeout = task::spawn(async move {
tokio::time::sleep(tokio::time::Duration::from_secs(120)).await;
timeouter.send("timeout".to_string()).await.unwrap();
});

let service = make_service_fn(move |_| {
let sender = sender.clone();

async move {
Ok::<_, Infallible>(service_fn(move |req: Request<Body>| {
request_handler(req, sender.clone())
}))
}
});

let address = ([127, 0, 0, 1], port).into();

let server = Server::bind(&address).serve(service);

let runtime = task::spawn(async move {
if let Err(error) = server.await {
eprintln!("Server error: {}", error);
}
timeout.abort();
});

let response = receiver.recv().await;

runtime.abort();

if Some("timeout".to_string()) == response {
panic!("Reached the 2 minute timeout");
}

Ok(response.unwrap())
}

async fn request_handler(
req: Request<Body>,
sender: Sender<String>,
) -> Result<Response<Body>, Infallible> {
let query = req.uri().query();

// only send if it's an actual token
if let Some(query) = query {
// parse the query
// since pat should be a URL safe string we can just split on '='
let query: Vec<(String, String)> = query
.split("&")
.map(|s| parse_key_val(s).unwrap())
.collect::<Vec<_>>();

// if query has a key called "token"
if let Some(token) = query.iter().find(|(k, _)| k.to_owned() == "token") {
// send it to the main thread
sender.send(token.1.to_string()).await.unwrap();
return Ok(Response::new("You've been authorized".into()));
}
}

return Ok(Response::builder()
.status(400)
.body("You're not authorized".into())
.unwrap());
}

0 comments on commit ac77744

Please sign in to comment.