Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use env vars for keychain and api URLs #68

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions cli/src/command/auth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ use serde::Deserialize;
use slot::{
account::Account,
api::Client,
browser, constant,
browser,
credential::Credentials,
graphql::auth::{
me::{ResponseData, Variables},
AccountTryFromGraphQLError, Me,
},
server::LocalServer,
vars,
};
use tokio::sync::mpsc::Sender;

Expand All @@ -34,7 +35,9 @@ impl LoginArgs {
let port = server.local_addr()?.port();
let callback_uri = format!("http://localhost:{port}/callback");

let url = format!("https://x.cartridge.gg/slot/auth?callback_uri={callback_uri}");
let url = vars::get_cartridge_keychain_url();

let url = format!("{url}/slot/auth?callback_uri={callback_uri}");

browser::open(&url)?;
server.start().await?;
Expand Down Expand Up @@ -131,15 +134,15 @@ async fn handler(

Ok(Redirect::permanent(&format!(
"{}/slot/auth/success",
constant::CARTRIDGE_KEYCHAIN_URL
vars::get_cartridge_keychain_url()
)))
}
None => {
error!("User denied consent. Try again.");

Ok(Redirect::permanent(&format!(
"{}/slot/auth/failure",
constant::CARTRIDGE_KEYCHAIN_URL
vars::get_cartridge_keychain_url()
)))
}
}
Expand Down
5 changes: 3 additions & 2 deletions cli/src/command/deployments/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use clap::Args;
use slot::graphql::deployments::fork_deployment::ForkDeploymentForkDeployment::KatanaConfig;
use slot::graphql::deployments::{fork_deployment::*, ForkDeployment};
use slot::graphql::{GraphQLQuery, Response};
use slot::{api::Client, credential::Credentials};
use slot::{api::Client, credential::Credentials, vars};
use starknet::providers::{jsonrpc::HttpTransport, JsonRpcClient, Provider};
use url::Url;

Expand Down Expand Up @@ -72,9 +72,10 @@ impl ForkArgs {
let block_number = if let Some(block_number) = config.fork_block_number {
block_number
} else {
let url = vars::get_cartridge_api_url();
// Workaround to get latest block number. Perhaps Katana could default to latest if none is supplied
let rpc_client = JsonRpcClient::new(HttpTransport::new(Url::parse(&format!(
"https://api.cartridge.gg/x/{}/katana",
"{url}/x/{}/katana",
self.project
))?));
rpc_client.block_number().await?
Expand Down
4 changes: 2 additions & 2 deletions slot/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
use url::Url;

use crate::error::Error;
use crate::{constant, credential::AccessToken};
use crate::{credential::AccessToken, vars};

#[derive(Debug)]
pub struct Client {
Expand All @@ -18,7 +18,7 @@ impl Client {
Self {
access_token: None,
client: reqwest::Client::new(),
base_url: Url::parse(constant::CARTRIDGE_API_URL).expect("valid url"),
base_url: Url::parse(vars::get_cartridge_api_url().as_str()).expect("valid url"),
}
}

Expand Down
3 changes: 0 additions & 3 deletions slot/src/constant.rs

This file was deleted.

2 changes: 1 addition & 1 deletion slot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
pub mod account;
pub mod api;
pub mod browser;
pub mod constant;
pub mod credential;
pub mod graphql;
pub mod server;
pub mod session;
pub mod vars;

pub(crate) mod error;
pub(crate) mod utils;
Expand Down
11 changes: 7 additions & 4 deletions slot/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use url::Url;
use crate::credential::Credentials;
use crate::error::Error;
use crate::utils::{self};
use crate::{browser, server::LocalServer};
use crate::{browser, server::LocalServer, vars};

const SESSION_CREATION_PAGE: &str = "https://x.cartridge.gg/slot/session";
const SESSION_CREATION_PATH: &str = "/slot/session";
const SESSION_FILE_BASE_NAME: &str = "session.json";

/// A policy defines what action can be performed by the session key.
Expand Down Expand Up @@ -146,7 +146,9 @@ where
/// Creates a new session token for the given user. This will open a browser to the Cartridge
/// Controller keychain page to prompt user to create a new session for the given policies and
/// network. Returns the newly created session token.
#[tracing::instrument(name = "create_session", level = "trace", skip(rpc_url), fields(policies = policies.len()))]
#[tracing::instrument(name = "create_session", level = "trace", skip(rpc_url), fields(
policies = policies.len()
))]
pub async fn create_user_session<U>(
username: &str,
rpc_url: U,
Expand All @@ -168,7 +170,8 @@ fn open_session_creation_page(
policies: &[Policy],
) -> anyhow::Result<Receiver<SessionDetails>> {
let params = prepare_query_params(username, rpc_url, policies)?;
let url = format!("{SESSION_CREATION_PAGE}?{params}");
let host = vars::get_cartridge_keychain_url();
let url = format!("{host}{SESSION_CREATION_PATH}?{params}");

let (tx, rx) = channel::<SessionDetails>(1);
let server = callback_server(tx)?;
Expand Down
16 changes: 16 additions & 0 deletions slot/src/vars.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::env;

pub fn get_cartridge_keychain_url() -> String {
get_env("CARTRIDGE_KEYCHAIN_URL", "https://x.cartridge.gg")
}

pub fn get_cartridge_api_url() -> String {
get_env("CARTRIDGE_API_URL", "https://api.cartridge.gg")
}

pub fn get_env(key: &str, default: &str) -> String {
match env::var(key) {
Ok(val) => val,
Err(_e) => default.to_string(),
}
}
Loading