Skip to content

Commit

Permalink
perf: NANOS_IN_SECONDS const
Browse files Browse the repository at this point in the history
  • Loading branch information
ilbertt committed Apr 11, 2024
1 parent b3d1095 commit 2bbdfd1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/ic_backend/src/id_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};

use crate::{
state,
utils::{base64_decode, unix_timestamp},
utils::{base64_decode, unix_timestamp, NANOS_IN_SECONDS},
};

/// The maximum age of an ID token (checked against the `iat` claim).
Expand All @@ -24,15 +24,17 @@ pub type IdTokenResult<T> = std::result::Result<T, ErrorKind>;
pub struct JWTClaims {
pub iss: String,
pub aud: String,
/// Issued at (seconds since unix epoch)
pub iat: u64,
/// Expires at (seconds since unix epoch)
pub exp: u64,
pub sub: String,
pub nonce: String,
}

impl JWTClaims {
pub fn expiration_timestamp_ns(&self) -> u64 {
self.exp * 1_000_000_000
self.exp * NANOS_IN_SECONDS
}

pub fn validate(&self) -> Result<(), ValidationError> {
Expand Down
4 changes: 3 additions & 1 deletion src/ic_backend/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use jsonwebtoken_rustcrypto::errors::ErrorKind;

use crate::id_token::IdTokenResult;

pub const NANOS_IN_SECONDS: u64 = 1_000_000_000;

/// Returns the current unix timestamp in seconds
pub fn unix_timestamp() -> u64 {
time() / 1_000_000_000
time() / NANOS_IN_SECONDS
}

pub fn base64_decode(input: &str) -> IdTokenResult<Vec<u8>> {
Expand Down

0 comments on commit 2bbdfd1

Please sign in to comment.