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+wZH5DBX2GhLlNZi/8sIiF\ Mo+jouQn+l6Qc4G65vnnoZ+yEuf9fTJPnTHBFMViUcmTPsdbD4eLfrRXwAE9GYrv\ R/RVusABAoGBAPgsQ4kAChpzU2aP21NQV1XTBW+eoHVbcJoYuOlmwB6x5o8lDUz/\ EQVVYZavfNY1AjhEkfltCDjm1GHyWofrtGKTy7DHSZwPw5CxuqDtaiC6PMpFEu+O\ xa09s7IZxpgInlrhY5JskOkH495BQ0xIU8UDxuP6sdtVNeQmWGjKG7kBAoGBAPPp\ Nid4QEV4XleyAXT/JQGugdpa7TirWOEATNo10YPPqz7GphRhucT0ipNKMi/0XKh3\ U0IC7XxjUvtE2LP9TVGAcV/Wzi4EYp1fziFuF9QcUds2tJ60SpfgIQrmVcF1zHxn\ 4/mSABoIyFxZSb4Tq9f+KXPAO5/l0NjgrVwk6gVZAoGAbMVZxE4UH4u0XhtnEZkA\ 7kjS9R0dTtKJA8EaKpIyWkG2v76JmdmhaCkH4LeBi5EoK+lB4YR8OhRRuawzKaeR\ JDOK7ywpgxEVsfFzzty/yyBVTIIBzqVQ1qFYhRLvC+ubHFH1BlQ3HyuqH9uS13hL\ 3unM3lceZPdv61MzJJqQlAECgYAWg0MFV5sPDnIexAZQZzBiPFot7lCQ93fHpMBz\ L557/RIARFOV9AMyg6O6vpFtTa+zuPfNUvnajkxddthNnKajTCiqwOfc5Xi4r9wV\ x9SZNlfz1NPNBjUQWZaTK/lkVtwd63TmVyx9OqxLoc4lpikpUYM/9NFMC+k/61T0\ +U9EWQKBgCdZV3yxwkz3pi6/E40EXfUsj8HQG/UtFJGeUNQiysBrxTmtmwLyvJeC\ GruG96j1JcehpbcWKV+ObyMQuk65dM94uM7Wa+2NCA/MvorVcU7wdPbq7/eczZU4\ xMd+OWT6JsInVM1ASh1mcn+Q0/Z3WqxxetCQLqaMs+FATn059dGf"; // Token taken from microsoft docs: https://docs.microsoft.com/en-us/azure/active-directory/develop/id-tokens fn test_token_header() -> String { format!( r#"{{ "typ": "JWT", "alg": "RS256", "kid": "i6lGk3FZzxRcUb2C3nEQ7syHJlY" }}"# ) } // Token taken from microsift docs: https://docs.microsoft.com/en-us/azure/active-directory/develop/id-tokens fn test_token_claims() -> String { format!( r#"{{ "aud": "6e74172b-be56-4843-9ff4-e66a39bb12e3", "iss": "https://login.microsoftonline.com/72f988bf-86f1-41af-91ab-2d7cd011db47/v2.0", "iat": {}, "nbf": {}, "exp": {}, "aio": "AXQAi/8IAAAAtAaZLo3ChMif6KOnttRB7eBq4/DccQzjcJGxPYy/C3jDaNGxXd6wNIIVGRghNRnwJ1lOcAnNZcjvkoyrFxCttv33140RioOFJ4bCCGVuoCag1uOTT22222gHwLPYQ/uf79QX+0KIijdrmp69RctzmQ==", "azp": "6e74172b-be56-4843-9ff4-e66a39bb12e3", "name": "Abe Lincoln", "azpacr": "0", "oid": "690222be-ff1a-4d56-abd1-7e4f7d38e474", "preferred_username": "abeli@microsoft.com", "rh": "I", "scp": "access_as_user", "sub": "HKZpfaHyWadeOouYlitjrI-KffTm222X5rrV3xDqfKQ", "tid": "72f988bf-86f1-41af-91ab-2d7cd011db47", "uti": "fqiBqXLPj0eQa82S-IYFAA", "ver": "2.0" }}"#, chrono::Utc::now().timestamp() - 1000, chrono::Utc::now().timestamp() - 2000, chrono::Utc::now().timestamp() + 1000 ) } // We create a test token from parts here. We use the v2 token used as example // in https://docs.microsoft.com/en-us/azure/active-directory/develop/id-tokens fn generate_test_token() -> String { let private_key = jwt::EncodingKey::from_base64_secret(PRIVATE_KEY_TEST).expect("priv_key"); // we need to construct the calims in a function since we need to set // the expiration relative to current time let test_token_playload = test_token_claims(); let test_token_header = test_token_header(); // we base64 (url-safe-base64) the header and claims and arrange // as a jwt payload -> header_as_base64.claims_as_base64 let test_token = [ base64::encode_config(&test_token_header, base64::URL_SAFE), base64::encode_config(&test_token_playload, base64::URL_SAFE), ] .join("."); // we create the signature using our private key let signature = jwt::crypto::sign(&test_token, &private_key, jwt::Algorithm::RS256).expect("Singed."); // we construct a complete token which looks like: header.claims.signature let complete_token = format!("{}.{}", test_token, signature); complete_token } #[test] fn decode_token() { let token = generate_test_token(); // we need to construct our own key object that matches on `kid` field // just as it should if we used the fetched keys from microsofts servers. let key = Jwk { kid: "i6lGk3FZzxRcUb2C3nEQ7syHJlY".to_string(), n: PUBLIC_KEY_N.to_string(), e: PUBLIC_KEY_E.to_string(), }; let mut az_auth = AzureAuth::new("6e74172b-be56-4843-9ff4-e66a39bb12e3").unwrap(); // We manually overwrite the keys so we use the ones we have for testing az_auth.set_public_keys(vec![key]); az_auth.validate_token(&token).expect("validated"); }