Skip to content

Commit

Permalink
Add JWT issuer test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeshLK committed May 30, 2024
1 parent 140cae0 commit 0a415bf
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions ballerina/tests/jwt_issuer_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import ballerina/lang.'string;
import ballerina/test;
import ballerina/crypto;
import ballerina/io;

@test:Config {}
isolated function testIssueJwtWithAllFields() returns Error? {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0a415bf

Please sign in to comment.