Skip to content

Commit 2dfbd17

Browse files
Add JWT verification to OpenID Connect registration flow (#2988)
* add jwt verification to openid registration and add integration test that checks that a faulty jwt will fail * 🤖 cargo-fmt auto-update * clippy fix * 🤖 cargo-fmt auto-update * CLIPPY FIX * 🤖 cargo-fmt auto-update --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 689d431 commit 2dfbd17

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

src/internet_identity/src/main.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ mod openid_api {
778778
remove_openid_credential, update_openid_credential,
779779
};
780780
use crate::authz_utils::{anchor_operation_with_authz_check, IdentityUpdateError};
781-
use crate::openid::{self, OpenIdCredentialKey};
781+
use crate::openid::{self, verify, OpenIdCredentialKey};
782782
use crate::storage::anchor::AnchorError;
783783
use crate::{
784784
state, IdentityNumber, OpenIdCredentialAddError, OpenIdCredentialRemoveError,
@@ -806,9 +806,12 @@ mod openid_api {
806806
fn openid_identity_registration_finish(
807807
arg: OpenIDRegFinishArg,
808808
) -> Result<IdRegFinishResult, IdRegFinishError> {
809-
registration::registration_flow_v2::identity_registration_finish(
810-
CreateIdentityData::OpenID(arg),
811-
)
809+
match verify(&arg.jwt, &arg.salt) {
810+
Ok(_) => registration::registration_flow_v2::identity_registration_finish(
811+
CreateIdentityData::OpenID(arg),
812+
),
813+
Err(err) => Err(IdRegFinishError::InvalidAuthnMethod(err)),
814+
}
812815
}
813816

814817
#[update]

src/internet_identity/tests/integration/openid.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,32 @@ fn can_register_with_google() -> Result<(), CallError> {
220220
Ok(())
221221
}
222222

223+
/// Verifies that you cannot register with a faulty jwt
224+
#[test]
225+
#[should_panic]
226+
fn cannot_register_with_faulty_jwt() {
227+
let env = env();
228+
229+
let canister_id = setup_canister(&env);
230+
231+
let (_jwt, salt, _claims, test_time, test_principal, _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PrWSWZzDy9S4GygjOQeg13NXg_H23X2-IY_OVHKqtrAibhZZUppvczijqZja7-HmUivoAJIGsMOk1IxbJdalOhE5yQtsYEx4ZBxFemR7CTfMzopsAaRWgPHI7T0MENuiCbkSy_NYQPBzNpmGcKoZoyUbleFUzej8gbkqpoIUVdfwuNtoe_TMjED5eqJxi1Pip85iy4wJTa2RKUTZxUfqVCaTEftVt8U-PV1UgPsxpu0mKS5z5bXylmgclUzcNnmh";
234+
235+
let time_to_advance = Duration::from_millis(test_time) - Duration::from_nanos(time(&env));
236+
env.advance_time(time_to_advance);
237+
238+
// Create identity - this will panic if it doesn't work. It should panic as we are using a faulty jwt.
239+
240+
let _identity_number = create_identity_with_openid_credential(
241+
&env,
242+
canister_id,
243+
faulty_jwt,
244+
&salt,
245+
test_principal,
246+
);
247+
}
248+
223249
static CLIENT_ID: &str = "360587991668-63bpc1gngp1s5gbo1aldal4a50c1j0bb.apps.googleusercontent.com";
224250

225251
#[derive(Deserialize)]

0 commit comments

Comments
 (0)