mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-26 08:30:59 +03:00
Merge pull request #857 from AleoHQ/feature/cli-dpii-updates
[CLI, Feature] Adds username substitution to manifest, improves login and error messages
This commit is contained in:
commit
efa3f78362
@ -1,7 +1,15 @@
|
||||
# leo login & logout
|
||||
|
||||
$LEO new my-app && cd my-app || exit 1
|
||||
$LEO login -u "$ALEO_PM_USERNAME" -p "$ALEO_PM_PASSWORD"
|
||||
$LEO new my-app && cd my-app || exit 1
|
||||
|
||||
cat Leo.toml
|
||||
|
||||
# verify that in Leo.toml there's no line with [AUTHOR];
|
||||
# since CI does not allow showing credentials, we won't see it in the file;
|
||||
# so the only way to test is to make sure that there's just no [AUTHOR] there
|
||||
[[ $(cat Leo.toml | grep "\[AUTHOR\]" | wc -l) -eq 0 ]] || exit 1
|
||||
|
||||
$LEO add howard/silly-sudoku
|
||||
$LEO remove silly-sudoku
|
||||
$LEO logout
|
||||
|
@ -1,4 +1,9 @@
|
||||
$LEO new hello-world
|
||||
ls -la
|
||||
cd hello-world && ls -la
|
||||
|
||||
# verify that in Leo.toml there's a placeholder for author
|
||||
# because at the time of calling `leo new` user is not logged in
|
||||
[[ $(cat Leo.toml | grep "\[AUTHOR\]" | wc -l) -eq 1 ]] || exit 1
|
||||
|
||||
$LEO run
|
||||
|
0
.circleci/r
Normal file
0
.circleci/r
Normal file
20
leo/api.rs
20
leo/api.rs
@ -20,7 +20,7 @@ use reqwest::{
|
||||
Method,
|
||||
StatusCode,
|
||||
};
|
||||
use serde::Serialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Trait describes API Routes and Request bodies, struct which implements
|
||||
/// Route MUST also support Serialize to be usable in Api::run_route(r: Route)
|
||||
@ -184,12 +184,18 @@ impl Route for Login {
|
||||
|
||||
/// Handler for 'my_profile' route. Meant to be used to get profile details but
|
||||
/// in current application is used to check if user is logged in. Any non-200 response
|
||||
/// is treated as Unauthorized
|
||||
/// is treated as Unauthorized.
|
||||
#[derive(Serialize)]
|
||||
pub struct Profile {}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct ProfileResponse {
|
||||
username: String,
|
||||
}
|
||||
|
||||
impl Route for Profile {
|
||||
type Output = bool;
|
||||
// Some with Username is success, None is failure.
|
||||
type Output = Option<String>;
|
||||
|
||||
const AUTH: bool = true;
|
||||
const METHOD: Method = Method::GET;
|
||||
@ -197,6 +203,12 @@ impl Route for Profile {
|
||||
|
||||
fn process(&self, res: Response) -> Result<Self::Output> {
|
||||
// this may be extended for more precise error handling
|
||||
Ok(res.status() == 200)
|
||||
let status = res.status();
|
||||
if status == StatusCode::OK {
|
||||
let body: ProfileResponse = res.json()?;
|
||||
return Ok(Some(body.username));
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{commands::Command, context::Context};
|
||||
use crate::{commands::Command, config::*, context::Context};
|
||||
use leo_package::LeoPackage;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
@ -43,6 +43,11 @@ impl Command for Init {
|
||||
// Derive the package directory path.
|
||||
let path = current_dir()?;
|
||||
|
||||
// Check that the current package directory path exists.
|
||||
if !path.exists() {
|
||||
return Err(anyhow!("Directory does not exist"));
|
||||
}
|
||||
|
||||
// Check that the given package name is valid.
|
||||
let package_name = path
|
||||
.file_stem()
|
||||
@ -53,12 +58,9 @@ impl Command for Init {
|
||||
return Err(anyhow!("Invalid Leo project name"));
|
||||
}
|
||||
|
||||
// Check that the current package directory path exists.
|
||||
if !path.exists() {
|
||||
return Err(anyhow!("Directory does not exist"));
|
||||
}
|
||||
let username = read_username().ok();
|
||||
|
||||
LeoPackage::initialize(&package_name, false, &path)?;
|
||||
LeoPackage::initialize(&package_name, false, &path, username)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -14,7 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{commands::Command, context::Context};
|
||||
use crate::{commands::Command, config::*, context::Context};
|
||||
use leo_package::LeoPackage;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
@ -49,6 +49,8 @@ impl Command for New {
|
||||
return Err(anyhow!("Invalid Leo project name"));
|
||||
}
|
||||
|
||||
let username = read_username().ok();
|
||||
|
||||
// Derive the package directory path.
|
||||
let mut path = current_dir()?;
|
||||
path.push(&package_name);
|
||||
@ -61,7 +63,7 @@ impl Command for New {
|
||||
// Create the package directory
|
||||
fs::create_dir_all(&path).map_err(|err| anyhow!("Could not create directory {}", err))?;
|
||||
|
||||
LeoPackage::initialize(&package_name, false, &path)?;
|
||||
LeoPackage::initialize(&package_name, false, &path, username)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -59,34 +59,15 @@ impl Command for Login {
|
||||
}
|
||||
|
||||
fn apply(self, context: Context, _: Self::Input) -> Result<Self::Output> {
|
||||
// quick hack to check if user is already logged in. ;)
|
||||
if context.api.auth_token().is_some() {
|
||||
tracing::info!("You are already logged in");
|
||||
return Ok(context.api.auth_token().unwrap());
|
||||
};
|
||||
|
||||
let mut api = context.api;
|
||||
let mut api = context.clone().api;
|
||||
|
||||
// ...or trying to use arguments to either get token or user-pass
|
||||
let token = match (self.token, self.user, self.pass) {
|
||||
// Login using existing token, use get_profile route for that
|
||||
(Some(token), _, _) => {
|
||||
tracing::info!("Token passed, checking...");
|
||||
|
||||
api.set_auth_token(token.clone());
|
||||
|
||||
let is_ok = api.run_route(ProfileRoute {})?;
|
||||
if !is_ok {
|
||||
return Err(anyhow!("Supplied token is incorrect"));
|
||||
};
|
||||
|
||||
token
|
||||
}
|
||||
|
||||
// Login using username and password
|
||||
let (token, username) = match (self.token, self.user, self.pass) {
|
||||
// Login using username and password if they were passed. Even if token already
|
||||
// exists login procedure will be done first (we need that for expired credentials).
|
||||
(None, Some(email_username), Some(password)) => {
|
||||
let login = LoginRoute {
|
||||
email_username,
|
||||
email_username: email_username.clone(),
|
||||
password,
|
||||
};
|
||||
|
||||
@ -98,17 +79,48 @@ impl Command for Login {
|
||||
return Err(anyhow!("Unable to get token"));
|
||||
};
|
||||
|
||||
tok_opt.unwrap()
|
||||
(tok_opt.unwrap(), email_username)
|
||||
}
|
||||
|
||||
// Login with token, use get_profile route to verify that.
|
||||
(Some(token), _, _) => {
|
||||
tracing::info!("Token passed, checking...");
|
||||
|
||||
api.set_auth_token(token.clone());
|
||||
|
||||
match api.run_route(ProfileRoute {})? {
|
||||
Some(username) => (token, username),
|
||||
None => return Err(anyhow!("Supplied token is incorrect")),
|
||||
}
|
||||
}
|
||||
|
||||
// In case token or login/pass were not passed as arguments
|
||||
(_, _, _) => return Err(anyhow!("No credentials provided")),
|
||||
(_, _, _) => {
|
||||
// Check locally stored token if there is.
|
||||
let token = context.api.auth_token();
|
||||
|
||||
match token {
|
||||
Some(token) => {
|
||||
tracing::info!("Found locally stored credentials, verifying...");
|
||||
|
||||
if let Some(username) = api.run_route(ProfileRoute {})? {
|
||||
(token, username)
|
||||
} else {
|
||||
remove_token_and_username()?;
|
||||
return Err(anyhow!(
|
||||
"Stored credentials are incorrect or expired, please login again"
|
||||
));
|
||||
}
|
||||
}
|
||||
None => return Err(anyhow!("No credentials provided")),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// write token either after logging or if it was passed
|
||||
write_token(token.as_str())?;
|
||||
write_token_and_username(token.as_str(), username.as_str())?;
|
||||
|
||||
tracing::info!("Success! You are now logged in!");
|
||||
tracing::info!("Success! You are logged in!");
|
||||
|
||||
Ok(token)
|
||||
}
|
||||
|
@ -14,7 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{commands::Command, config::remove_token, context::Context};
|
||||
use crate::{commands::Command, config::remove_token_and_username, context::Context};
|
||||
|
||||
use anyhow::Result;
|
||||
use std::io::ErrorKind;
|
||||
@ -41,7 +41,7 @@ impl Command for Logout {
|
||||
fn apply(self, _context: Context, _: Self::Input) -> Result<Self::Output> {
|
||||
// the only error we're interested here is NotFound
|
||||
// however err in this case can also be of kind PermissionDenied or other
|
||||
if let Err(err) = remove_token() {
|
||||
if let Err(err) = remove_token_and_username() {
|
||||
match err.kind() {
|
||||
ErrorKind::NotFound => {
|
||||
tracing::info!("you are not logged in");
|
||||
|
@ -19,7 +19,10 @@ use crate::{
|
||||
commands::Command,
|
||||
context::{Context, PACKAGE_MANAGER_URL},
|
||||
};
|
||||
use leo_package::{outputs::OutputsDirectory, root::ZipFile};
|
||||
use leo_package::{
|
||||
outputs::OutputsDirectory,
|
||||
root::{ZipFile, AUTHOR_PLACEHOLDER},
|
||||
};
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use reqwest::{
|
||||
@ -70,11 +73,19 @@ impl Command for Publish {
|
||||
};
|
||||
|
||||
let package_remote = manifest.get_package_remote().unwrap();
|
||||
let username = package_remote.clone().author;
|
||||
|
||||
// Create the output directory
|
||||
// Prevent most common error before accessing API.
|
||||
if username == AUTHOR_PLACEHOLDER {
|
||||
return Err(anyhow!(
|
||||
"Package author is not set. Specify package author in [remote] section of Leo.toml"
|
||||
));
|
||||
}
|
||||
|
||||
// Create the output directory.
|
||||
OutputsDirectory::create(&path)?;
|
||||
|
||||
// Create zip file
|
||||
// Create zip file.
|
||||
let zip_file = ZipFile::new(&package_name);
|
||||
if zip_file.exists_at(&path) {
|
||||
tracing::debug!("Existing package zip file found. Clearing it to regenerate.");
|
||||
|
@ -32,6 +32,7 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
pub const LEO_CREDENTIALS_FILE: &str = "credentials";
|
||||
pub const LEO_CONFIG_FILE: &str = "config.toml";
|
||||
pub const LEO_USERNAME_FILE: &str = "username";
|
||||
|
||||
lazy_static! {
|
||||
pub static ref LEO_CONFIG_DIRECTORY: PathBuf = {
|
||||
@ -44,6 +45,11 @@ lazy_static! {
|
||||
path.push(LEO_CREDENTIALS_FILE);
|
||||
path
|
||||
};
|
||||
pub static ref LEO_USERNAME_PATH: PathBuf = {
|
||||
let mut path = LEO_CONFIG_DIRECTORY.to_path_buf();
|
||||
path.push(LEO_USERNAME_FILE);
|
||||
path
|
||||
};
|
||||
pub static ref LEO_CONFIG_PATH: PathBuf = {
|
||||
let mut path = LEO_CONFIG_DIRECTORY.to_path_buf();
|
||||
path.push(LEO_CONFIG_FILE);
|
||||
@ -129,7 +135,7 @@ impl Config {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_token(token: &str) -> Result<(), io::Error> {
|
||||
pub fn write_token_and_username(token: &str, username: &str) -> Result<(), io::Error> {
|
||||
let config_dir = LEO_CONFIG_DIRECTORY.clone();
|
||||
|
||||
// Create Leo config directory if it not exists
|
||||
@ -139,6 +145,10 @@ pub fn write_token(token: &str) -> Result<(), io::Error> {
|
||||
|
||||
let mut credentials = File::create(&LEO_CREDENTIALS_PATH.to_path_buf())?;
|
||||
credentials.write_all(&token.as_bytes())?;
|
||||
|
||||
let mut username_file = File::create(&LEO_USERNAME_PATH.to_path_buf())?;
|
||||
username_file.write_all(&username.as_bytes())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -149,7 +159,15 @@ pub fn read_token() -> Result<String, io::Error> {
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
pub fn remove_token() -> Result<(), io::Error> {
|
||||
pub fn read_username() -> Result<String, io::Error> {
|
||||
let mut username = File::open(&LEO_USERNAME_PATH.to_path_buf())?;
|
||||
let mut buf = String::new();
|
||||
username.read_to_string(&mut buf)?;
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
pub fn remove_token_and_username() -> Result<(), io::Error> {
|
||||
fs::remove_file(&LEO_CREDENTIALS_PATH.to_path_buf())?;
|
||||
fs::remove_file(&LEO_USERNAME_PATH.to_path_buf())?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -33,8 +33,13 @@ pub struct LeoPackage;
|
||||
|
||||
impl LeoPackage {
|
||||
/// Initializes a Leo package at the given path.
|
||||
pub fn initialize(package_name: &str, is_lib: bool, path: &Path) -> Result<(), PackageError> {
|
||||
package::Package::initialize(package_name, is_lib, path)
|
||||
pub fn initialize(
|
||||
package_name: &str,
|
||||
is_lib: bool,
|
||||
path: &Path,
|
||||
author: Option<String>,
|
||||
) -> Result<(), PackageError> {
|
||||
package::Package::initialize(package_name, is_lib, path, author)
|
||||
}
|
||||
|
||||
/// Returns `true` if the given Leo package name is valid.
|
||||
|
@ -197,7 +197,12 @@ impl Package {
|
||||
}
|
||||
|
||||
/// Creates a package at the given path
|
||||
pub fn initialize(package_name: &str, is_lib: bool, path: &Path) -> Result<(), PackageError> {
|
||||
pub fn initialize(
|
||||
package_name: &str,
|
||||
is_lib: bool,
|
||||
path: &Path,
|
||||
author: Option<String>,
|
||||
) -> Result<(), PackageError> {
|
||||
// First, verify that this directory is not already initialized as a Leo package.
|
||||
{
|
||||
if !Self::can_initialize(package_name, is_lib, path) {
|
||||
@ -210,7 +215,7 @@ impl Package {
|
||||
// Next, initialize this directory as a Leo package.
|
||||
{
|
||||
// Create the manifest file.
|
||||
Manifest::new(&package_name)?.write_to(&path)?;
|
||||
Manifest::new(&package_name, author)?.write_to(&path)?;
|
||||
|
||||
// Verify that the .gitignore file does not exist.
|
||||
if !Gitignore::exists_at(&path) {
|
||||
|
@ -26,6 +26,7 @@ use std::{
|
||||
};
|
||||
|
||||
pub const MANIFEST_FILENAME: &str = "Leo.toml";
|
||||
pub const AUTHOR_PLACEHOLDER: &str = "[AUTHOR]";
|
||||
|
||||
#[derive(Clone, Deserialize)]
|
||||
pub struct Remote {
|
||||
@ -39,10 +40,10 @@ pub struct Manifest {
|
||||
}
|
||||
|
||||
impl Manifest {
|
||||
pub fn new(package_name: &str) -> Result<Self, ManifestError> {
|
||||
pub fn new(package_name: &str, author: Option<String>) -> Result<Self, ManifestError> {
|
||||
Ok(Self {
|
||||
project: Package::new(package_name)?,
|
||||
remote: None,
|
||||
remote: author.map(|author| Remote { author }),
|
||||
})
|
||||
}
|
||||
|
||||
@ -90,6 +91,11 @@ impl Manifest {
|
||||
}
|
||||
|
||||
fn template(&self) -> String {
|
||||
let author = self
|
||||
.remote
|
||||
.clone()
|
||||
.map_or(AUTHOR_PLACEHOLDER.to_string(), |remote| remote.author);
|
||||
|
||||
format!(
|
||||
r#"[project]
|
||||
name = "{name}"
|
||||
@ -98,9 +104,10 @@ description = "The {name} package"
|
||||
license = "MIT"
|
||||
|
||||
[remote]
|
||||
author = "[AUTHOR]" # Add your Aleo Package Manager username, team's name, or organization's name.
|
||||
author = "{author}" # Add your Aleo Package Manager username, team's name, or organization's name.
|
||||
"#,
|
||||
name = self.project.name
|
||||
name = self.project.name,
|
||||
author = author
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +32,29 @@ fn initialize_valid_package() {
|
||||
assert!(Package::can_initialize(TEST_PACKAGE_NAME, false, &test_directory));
|
||||
|
||||
// Initialize a package at the `test_directory`
|
||||
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory).is_ok());
|
||||
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory, None).is_ok());
|
||||
|
||||
// Ensure a package is initialized at the `test_directory`
|
||||
assert!(Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn initialize_valid_package_with_author() {
|
||||
let test_directory = test_dir();
|
||||
|
||||
// Ensure a package can be initialized at the `test_directory`
|
||||
assert!(Package::can_initialize(TEST_PACKAGE_NAME, false, &test_directory));
|
||||
|
||||
// Initialize a package at the `test_directory`
|
||||
assert!(
|
||||
Package::initialize(
|
||||
TEST_PACKAGE_NAME,
|
||||
false,
|
||||
&test_directory,
|
||||
Some(String::from("test_user"))
|
||||
)
|
||||
.is_ok()
|
||||
);
|
||||
|
||||
// Ensure a package is initialized at the `test_directory`
|
||||
assert!(Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
|
||||
@ -52,13 +74,13 @@ fn initialize_fails_with_existing_manifest() {
|
||||
assert!(Package::can_initialize(TEST_PACKAGE_NAME, false, &test_directory));
|
||||
|
||||
// Manually add a manifest file to the `test_directory`
|
||||
Manifest::new(TEST_PACKAGE_NAME)
|
||||
Manifest::new(TEST_PACKAGE_NAME, None)
|
||||
.unwrap()
|
||||
.write_to(&test_directory)
|
||||
.unwrap();
|
||||
|
||||
// Attempt to initialize a package at the `test_directory`
|
||||
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory).is_err());
|
||||
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory, None).is_err());
|
||||
|
||||
// Ensure package is not initialized at the `test_directory`
|
||||
assert!(!Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
|
||||
@ -76,7 +98,7 @@ fn initialize_fails_with_existing_library_file() {
|
||||
LibraryFile::new(TEST_PACKAGE_NAME).write_to(&test_directory).unwrap();
|
||||
|
||||
// Attempt to initialize a package at the `test_directory`
|
||||
assert!(Package::initialize(TEST_PACKAGE_NAME, true, &test_directory).is_err());
|
||||
assert!(Package::initialize(TEST_PACKAGE_NAME, true, &test_directory, None).is_err());
|
||||
|
||||
// Ensure package is not initialized at the `test_directory`
|
||||
assert!(!Package::is_initialized(TEST_PACKAGE_NAME, true, &test_directory));
|
||||
@ -94,7 +116,15 @@ fn initialize_fails_with_existing_input_file() {
|
||||
InputFile::new(TEST_PACKAGE_NAME).write_to(&test_directory).unwrap();
|
||||
|
||||
// Attempt to initialize a package at the `test_directory`
|
||||
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory).is_err());
|
||||
assert!(
|
||||
Package::initialize(
|
||||
TEST_PACKAGE_NAME,
|
||||
false,
|
||||
&test_directory,
|
||||
Some(String::from("test_user"))
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
|
||||
// Ensure package is not initialized at the `test_directory`
|
||||
assert!(!Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
|
||||
@ -112,7 +142,7 @@ fn initialize_fails_with_existing_state_file() {
|
||||
StateFile::new(TEST_PACKAGE_NAME).write_to(&test_directory).unwrap();
|
||||
|
||||
// Attempt to initialize a package at the `test_directory`
|
||||
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory).is_err());
|
||||
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory, None).is_err());
|
||||
|
||||
// Ensure package is not initialized at the `test_directory`
|
||||
assert!(!Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
|
||||
@ -130,7 +160,7 @@ fn initialize_fails_with_existing_main_file() {
|
||||
MainFile::new(TEST_PACKAGE_NAME).write_to(&test_directory).unwrap();
|
||||
|
||||
// Attempt to initialize a package at the `test_directory`
|
||||
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory).is_err());
|
||||
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory, None).is_err());
|
||||
|
||||
// Ensure package is not initialized at the `test_directory`
|
||||
assert!(!Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
|
||||
|
Loading…
Reference in New Issue
Block a user