Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect Username and Password Extraction #1283

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading