Skip to content

Commit

Permalink
moved types to login
Browse files Browse the repository at this point in the history
  • Loading branch information
dynamite-bud committed Jul 9, 2023
1 parent ff13040 commit 70d676e
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 58 deletions.
60 changes: 51 additions & 9 deletions lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ tokio = { version = "1.28.1", features = ["macros", "rt-multi-thread"] }
once_cell = "1.17.1"
indicatif = "0.17.5"
opener = "0.6.1"
hyper = { version = "0.14.27", features = ["full"] }
hyper = { version = "0.14.27", features = ["server"] }

# NOTE: Must use different features for clap because the "color" feature does not
# work on wasi due to the anstream dependency not compiling.
Expand Down Expand Up @@ -159,29 +159,71 @@ unix_mode = "0.1.3"
[features]
# Don't add the compiler features in default, please add them on the Makefile
# since we might want to autoconfigure them depending on the availability on the host.
default = ["sys", "wat", "wast", "compiler", "wasmer-artifact-create", "static-artifact-create"]
default = [
"sys",
"wat",
"wast",
"compiler",
"wasmer-artifact-create",
"static-artifact-create",
]
backend = []
coredump = ["wasm-coredump-builder"]
sys = ["compiler", "wasmer-vm"]
jsc = ["backend", "wasmer/jsc", "wasmer/std"]
wast = ["wasmer-wast"]
host-net = ["virtual-net/host-net"]
wat = ["wasmer/wat"]
compiler = ["backend", "wasmer/compiler", "wasmer-compiler/translator", "wasmer-compiler/compiler"]
wasmer-artifact-create = ["compiler", "wasmer/wasmer-artifact-load", "wasmer/wasmer-artifact-create", "wasmer-compiler/wasmer-artifact-load", "wasmer-compiler/wasmer-artifact-create", "wasmer-object"]
static-artifact-create = ["compiler", "wasmer/static-artifact-load", "wasmer/static-artifact-create", "wasmer-compiler/static-artifact-load", "wasmer-compiler/static-artifact-create", "wasmer-object"]
wasmer-artifact-load = ["compiler", "wasmer/wasmer-artifact-load", "wasmer-compiler/wasmer-artifact-load"]
static-artifact-load = ["compiler", "wasmer/static-artifact-load", "wasmer-compiler/static-artifact-load"]
compiler = [
"backend",
"wasmer/compiler",
"wasmer-compiler/translator",
"wasmer-compiler/compiler",
]
wasmer-artifact-create = [
"compiler",
"wasmer/wasmer-artifact-load",
"wasmer/wasmer-artifact-create",
"wasmer-compiler/wasmer-artifact-load",
"wasmer-compiler/wasmer-artifact-create",
"wasmer-object",
]
static-artifact-create = [
"compiler",
"wasmer/static-artifact-load",
"wasmer/static-artifact-create",
"wasmer-compiler/static-artifact-load",
"wasmer-compiler/static-artifact-create",
"wasmer-object",
]
wasmer-artifact-load = [
"compiler",
"wasmer/wasmer-artifact-load",
"wasmer-compiler/wasmer-artifact-load",
]
static-artifact-load = [
"compiler",
"wasmer/static-artifact-load",
"wasmer-compiler/static-artifact-load",
]
experimental-io-devices = ["wasmer-wasix-experimental-io-devices"]
singlepass = ["wasmer-compiler-singlepass", "compiler"]
cranelift = ["wasmer-compiler-cranelift", "compiler"]
llvm = ["wasmer-compiler-llvm", "compiler"]
disable-all-logging = ["wasmer-wasix/disable-all-logging", "log/release_max_level_off"]
disable-all-logging = [
"wasmer-wasix/disable-all-logging",
"log/release_max_level_off",
]
headless = []
headless-minimal = ["headless", "disable-all-logging"]

# Optional
enable-serde = ["wasmer/enable-serde", "wasmer-vm/enable-serde", "wasmer-compiler/enable-serde", "wasmer-wasix/enable-serde"]
enable-serde = [
"wasmer/enable-serde",
"wasmer-vm/enable-serde",
"wasmer-compiler/enable-serde",
"wasmer-wasix/enable-serde",
]

[dev-dependencies]
assert_cmd = "2.0.11"
Expand Down
58 changes: 44 additions & 14 deletions lib/cli/src/commands/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use clap::Parser;
use dialoguer::{console::style, Input};
use reqwest::Method;

use serde::Deserialize;
use wasmer_registry::{
types::NewNonceOutput,
types::ValidatedNonceOutput,
wasmer_env::{Registry, WasmerEnv, WASMER_DIR},
RegistryClient,
};
Expand All @@ -21,6 +21,27 @@ use hyper::{

const WASMER_CLI: &str = "wasmer-cli";

/// Payload from the frontend after the user has authenticated.
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TokenStatus {
/// Signifying that the token is cancelled
Cancelled,
/// Signifying that the token is authorized
Authorized,
}

/// Payload from the frontend after the user has authenticated.
///
/// This has the token that we need to set in the WASMER_TOML file.
#[derive(Clone, Debug, Deserialize)]
pub struct ValidatedNonceOutput {
/// Token Received from the frontend
pub token: Option<String>,
/// Status of the token , whether it is authorized or cancelled
pub status: TokenStatus,
}

/// Enum for the boolean like prompt options
#[derive(Debug, Clone, PartialEq)]
pub enum BoolPromptOptions {
Expand Down Expand Up @@ -271,10 +292,10 @@ impl Login {
match wasmer_registry::login::login_and_save_token(env.dir(), registry.as_str(), &token)? {
Some(s) => {
print!("Done!");
println!("\n✅ Login for Wasmer user \"{:?}\" saved", s)
println!("\n✅ Login for Wasmer user {:?} saved", s)
}
None => println!(
"\nError: no user found on registry \"{:?}\" with token \"{:?}\". Token saved regardless.",
"\nError: no user found on registry {:?} with token \"{:?}\". Token saved regardless.",
registry, token
),
};
Expand Down Expand Up @@ -321,22 +342,25 @@ async fn handle_post_save_token(
} = serde_json::from_slice::<ValidatedNonceOutput>(&body)?;

// send the AuthorizationState based on token_status to the main thread and get the response message
let response_message = match token_status {
wasmer_registry::types::TokenStatus::Cancelled => {
let (response_message, parse_failure) = match token_status {
TokenStatus::Cancelled => {
token_tx
.send(AuthorizationState::Cancelled)
.await
.expect("Failed to send token");

"Token Cancelled by the user"
("Token Cancelled by the user", false)
}
wasmer_registry::types::TokenStatus::Authorized => {
token_tx
.send(AuthorizationState::TokenSuccess(token.clone()))
.await
.expect("Failed to send token");

"Token Authorized"
TokenStatus::Authorized => {
if let Some(token) = token {
token_tx
.send(AuthorizationState::TokenSuccess(token.clone()))
.await
.expect("Failed to send token");
("Token Authorized", false)
} else {
("Token not found", true)
}
}
};

Expand All @@ -345,8 +369,14 @@ async fn handle_post_save_token(
.await
.expect("Failed to send shutdown signal");

let status = if parse_failure {
StatusCode::BAD_REQUEST
} else {
StatusCode::OK
};

Ok(Response::builder()
.status(StatusCode::OK)
.status(status)
.header("Access-Control-Allow-Origin", "*") // FIXME: this is not secure, Don't allow all origins. @syrusakbary
.header("Access-Control-Allow-Headers", "Content-Type")
.header("Access-Control-Allow-Methods", "POST, OPTIONS")
Expand Down
35 changes: 0 additions & 35 deletions lib/registry/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use serde::Deserialize;

/// Payload for publishing a new Deploy app.
#[derive(Clone, Debug)]
pub struct PublishDeployAppRawVars {
Expand Down Expand Up @@ -31,36 +29,3 @@ pub struct PublishDeployAppOutput {
pub struct NewNonceOutput {
pub auth_url: String,
}

/// Payload from the frontend after the user has authenticated.
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TokenStatus {
Cancelled,
Authorized,
}

fn token_status_deserializer<'de, D>(deserializer: D) -> Result<TokenStatus, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
match s.as_str() {
"cancelled" => Ok(TokenStatus::Cancelled),
"authorized" => Ok(TokenStatus::Authorized),
_ => Err(serde::de::Error::custom(format!(
"invalid token status: {}",
s
))),
}
}

/// Payload from the frontend after the user has authenticated.
///
/// This has the token that we need to set in the WASMER_TOML file.
#[derive(Clone, Debug, Deserialize)]
pub struct ValidatedNonceOutput {
pub token: String,
#[serde(deserialize_with = "token_status_deserializer")]
pub status: TokenStatus,
}

0 comments on commit 70d676e

Please sign in to comment.