Skip to content

Commit

Permalink
Merge pull request #1283 from ballerina-platform/fix-passwrd
Browse files Browse the repository at this point in the history
Fix incorrect Username and Password Extraction
  • Loading branch information
DimuthuMadushan authored Jul 25, 2024
2 parents f4ccd44 + 79115d5 commit 95834cb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
6 changes: 3 additions & 3 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerina"
name = "auth"
version = "2.11.0"
version = "2.11.1"
authors = ["Ballerina"]
keywords = ["security", "authentication", "basic auth"]
repository = "https://github.com/ballerina-platform/module-ballerina-auth"
Expand All @@ -15,5 +15,5 @@ graalvmCompatible = true
[[platform.java17.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "auth-native"
version = "2.11.0"
path = "../native/build/libs/auth-native-2.11.0.jar"
version = "2.11.1"
path = "../native/build/libs/auth-native-2.11.1-SNAPSHOT.jar"
4 changes: 2 additions & 2 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ distribution-version = "2201.9.0"
[[package]]
org = "ballerina"
name = "auth"
version = "2.11.0"
version = "2.11.1"
dependencies = [
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "jballerina.java"},
Expand All @@ -26,7 +26,7 @@ modules = [
[[package]]
org = "ballerina"
name = "crypto"
version = "2.7.0"
version = "2.7.2"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "time"}
Expand Down
14 changes: 8 additions & 6 deletions ballerina/auth_utils.bal
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ public isolated function extractUsernameAndPassword(string credential) returns [
if base64Decoded is byte[] {
string|error base64DecodedResults = 'string:fromBytes(base64Decoded);
if base64DecodedResults is string {
string[] decodedCredentials = re `:`.split(base64DecodedResults);
if decodedCredentials.length() != 2 ||
decodedCredentials[0].length() == 0 || decodedCredentials[1].length() == 0 {
return prepareError("Incorrect credential format. Format should be username:password");
} else {
return [decodedCredentials[0], decodedCredentials[1]];
int? colonIndex = base64DecodedResults.indexOf(":");
if colonIndex is int {
string username = base64DecodedResults.substring(0, colonIndex);
string password = base64DecodedResults.substring(colonIndex + 1);
if username.length() != 0 && password.length() != 0 {
return [username, password];
}
}
return prepareError("Incorrect credential format. Format should be username:password");
} else {
return prepareError("Failed to convert byte[] credential to string.", base64DecodedResults);
}
Expand Down
16 changes: 16 additions & 0 deletions ballerina/tests/auth_utils_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,19 @@ isolated function testExtractUsernameAndPasswordForEmptyUsername() returns Error
test:assertFail("Expected error not found.");
}
}

@test:Config {}
isolated function testExtractUsernameAndPasswordWherePasswordIncludesColon() returns Error? {
string usernameAndPassword = "YWxpY2U6YWxpY2U6QDU=";
[string, string] [username, password] = check extractUsernameAndPassword(usernameAndPassword);
test:assertEquals(username, "alice");
test:assertEquals(password, "alice:@5");
}

@test:Config {}
isolated function testExtractUsernameAndPasswordWherePasswordEndsWithColon() returns Error? {
string usernameAndPassword = "YWxpY2U6YWxpY2UxMjM6YWxpY2U6";
[string, string] [username, password] = check extractUsernameAndPassword(usernameAndPassword);
test:assertEquals(username, "alice");
test:assertEquals(password, "alice123:alice:");
}
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Fixed
- [Fix Incorrect Username and Password Extraction](https://github.com/ballerina-platform/ballerina-library/issues/6773)

## [2.11.0] - 2024-05-03

- This version maintains compatibility with dependencies without any external changes.
Expand Down

0 comments on commit 95834cb

Please sign in to comment.