diff --git a/ballerina/tests/jwt_issuer_test.bal b/ballerina/tests/jwt_issuer_test.bal index f3719c48..e08365ef 100644 --- a/ballerina/tests/jwt_issuer_test.bal +++ b/ballerina/tests/jwt_issuer_test.bal @@ -18,6 +18,8 @@ import ballerina/lang.'string; import ballerina/test; +import ballerina/crypto; +import ballerina/io; @test:Config {} isolated function testIssueJwtWithAllFields() returns Error? { @@ -365,6 +367,44 @@ isolated function testIssueJwtWithEncryptedPrivateKey() returns Error? { assertDecodedJwt(result, expectedHeader, expectedPayload); } +@test:Config {} +isolated function testIssueJwtWithCryptoPrivateKey() returns io:Error|crypto:Error|Error? { + byte[] privateKeyContent = check io:fileReadBytes(PRIVATE_KEY_PATH); + crypto:PrivateKey privateKey = check crypto:decodeRsaPrivateKeyFromContent(privateKeyContent); + IssuerConfig issuerConfig = { + username: "John", + issuer: "wso2", + audience: ["ballerina", "ballerinaSamples"], + expTime: 600, + signatureConfig: { + config: privateKey + } + }; + string result = check issue(issuerConfig); + string expectedHeader = "{\"alg\":\"RS256\", \"typ\":\"JWT\"}"; + string expectedPayload = "{\"iss\":\"wso2\", \"sub\":\"John\", \"aud\":[\"ballerina\", \"ballerinaSamples\"]"; + assertDecodedJwt(result, expectedHeader, expectedPayload); +} + +@test:Config {} +isolated function testIssueJwtWithEncryptedCryptoPrivateKey() returns io:Error|crypto:Error|Error? { + byte[] privateKeyContent = check io:fileReadBytes(ENCRYPTED_PRIVATE_KEY_PATH); + crypto:PrivateKey encryptedPrivateKey = check crypto:decodeRsaPrivateKeyFromContent(privateKeyContent, "ballerina"); + IssuerConfig issuerConfig = { + username: "John", + issuer: "wso2", + audience: ["ballerina", "ballerinaSamples"], + expTime: 600, + signatureConfig: { + config: encryptedPrivateKey + } + }; + string result = check issue(issuerConfig); + string expectedHeader = "{\"alg\":\"RS256\", \"typ\":\"JWT\"}"; + string expectedPayload = "{\"iss\":\"wso2\", \"sub\":\"John\", \"aud\":[\"ballerina\", \"ballerinaSamples\"]"; + assertDecodedJwt(result, expectedHeader, expectedPayload); +} + isolated function assertDecodedJwt(string jwt, string header, string payload) { string[] parts = re `\.`.split(jwt); // check header