-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Google JWT verification and build time environment variables.
- Loading branch information
Showing
12 changed files
with
476 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,3 +48,4 @@ serde = "1" | |
serde_bytes = "0.11" | ||
serde_cbor = "0.11" | ||
sha2 = "0.10" | ||
rsa = "0.9.7" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use std::path::Path; | ||
use std::{env, fs}; | ||
|
||
// OpenID Google client id used by tests | ||
const TEST_OPENID_GOOGLE_CLIENT_ID: &str = | ||
"45431994619-cbbfgtn7o0pp0dpfcg2l66bc4rcg7qbu.apps.googleusercontent.com"; | ||
|
||
// Write environment variables to constants during build time | ||
fn main() { | ||
let openid_google_client_id = | ||
env::var("II_OPENID_GOOGLE_CLIENT_ID").unwrap_or(TEST_OPENID_GOOGLE_CLIENT_ID.into()); | ||
fs::write( | ||
Path::new(&env::var("OUT_DIR").unwrap()).join("constants.rs"), | ||
format!("pub const OPENID_GOOGLE_CLIENT_ID: &str = \"{openid_google_client_id}\";"), | ||
) | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Include the generated constants.rs file | ||
include!(concat!(env!("OUT_DIR"), "/constants.rs")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,43 @@ | ||
use candid::{Deserialize, Principal}; | ||
use identity_jose::jws::Decoder; | ||
use internet_identity_interface::internet_identity::types::{MetadataEntryV2, Timestamp}; | ||
use std::collections::HashMap; | ||
|
||
mod google; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub struct OpenIdCredential { | ||
pub iss: String, | ||
pub sub: String, | ||
pub aud: String, | ||
pub principal: Principal, | ||
pub last_usage_timestamp: Timestamp, | ||
pub metadata: HashMap<String, MetadataEntryV2>, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct PartialClaims { | ||
iss: String, | ||
} | ||
|
||
pub fn setup_timers() { | ||
google::setup_timers(); | ||
} | ||
|
||
#[allow(unused)] | ||
pub fn verify( | ||
jwt: &str, | ||
session_principal: &Principal, | ||
session_salt: &[u8; 32], | ||
timestamp: Timestamp, | ||
) -> Result<OpenIdCredential, String> { | ||
let validation_item = Decoder::new() | ||
.decode_compact_serialization(jwt.as_bytes(), None) | ||
.map_err(|_| "Failed to decode JWT")?; | ||
let claims: PartialClaims = | ||
serde_json::from_slice(validation_item.claims()).map_err(|_| "Unable to decode claims")?; | ||
match claims.iss.as_str() { | ||
google::ISSUER => google::verify(jwt, session_principal, session_salt, timestamp), | ||
_ => Err(format!("Unsupported issuer: {}", claims.iss)), | ||
} | ||
} |
Oops, something went wrong.