Skip to content

Commit

Permalink
rename struct
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Jun 14, 2024
1 parent f3696be commit 808b066
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct Policy {

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Session {
pub struct SessionDetails {
/// The expiration date of the session.
pub expires_at: String,
/// The policies that the session is allowed to execute.
Expand All @@ -36,20 +36,20 @@ pub struct Session {
#[serde(rename_all = "camelCase")]
pub struct SessionCredentials {
/// The signing key of the session.
pub private_key: String,
pub authorization: Vec<String>,
pub private_key: FieldElement,
pub authorization: Vec<FieldElement>,
}

/// Retrieves the session for the given chain id.
pub fn get(chain_id: FieldElement) -> anyhow::Result<Session> {
pub fn get(chain_id: FieldElement) -> anyhow::Result<SessionDetails> {
let credentials = Credentials::load()?;
let username = credentials.account.expect("id must exist").id;
let contents = fs::read_to_string(get_file_path(&username, chain_id))?;
Ok(serde_json::from_str(&contents)?)
}

/// Stores the session on-disk.
pub fn store(chain_id: FieldElement, session: Session) -> anyhow::Result<()> {
pub fn store(chain_id: FieldElement, session: SessionDetails) -> anyhow::Result<()> {
// TODO: maybe can store the authenticated user in a global variable so that
// we don't have to call load again if we already did it before.
let credentials = Credentials::load()?;
Expand All @@ -72,7 +72,7 @@ pub fn store(chain_id: FieldElement, session: Session) -> anyhow::Result<()> {
}

#[tracing::instrument(level = "trace", skip(rpc_url), fields(policies = policies.len()))]
pub async fn create<U>(rpc_url: U, policies: &[Policy]) -> anyhow::Result<Session>
pub async fn create<U>(rpc_url: U, policies: &[Policy]) -> anyhow::Result<SessionDetails>
where
U: Into<Url>,
{
Expand All @@ -91,11 +91,11 @@ fn open_session_creation_page(
username: &str,
rpc_url: &str,
policies: &[Policy],
) -> anyhow::Result<Receiver<Session>> {
) -> anyhow::Result<Receiver<SessionDetails>> {
let params = prepare_query_params(username, rpc_url, policies)?;
let url = format!("https://x.cartridge.gg/slot/session?{params}");

let (tx, rx) = channel::<Session>(1);
let (tx, rx) = channel::<SessionDetails>(1);
let server = callback_server(tx)?;

// get the callback server url
Expand Down Expand Up @@ -131,8 +131,9 @@ fn prepare_query_params(
}

/// Create the callback server that will receive the session token from the browser.
fn callback_server(tx: Sender<Session>) -> anyhow::Result<LocalServer> {
let handler = move |State(tx): State<Sender<Session>>, Json(session): Json<Session>| async move {
fn callback_server(tx: Sender<SessionDetails>) -> anyhow::Result<LocalServer> {
let handler = move |State(tx): State<Sender<SessionDetails>>,
Json(session): Json<SessionDetails>| async move {
trace!("Received session token from the browser.");
tx.send(session).await.expect("qed; channel closed");
};
Expand Down

0 comments on commit 808b066

Please sign in to comment.