Skip to content

Commit

Permalink
Merge pull request #588 from ldclakmal/dev
Browse files Browse the repository at this point in the history
Add OAuth2 JWT bearer grant type support for client
  • Loading branch information
ldclakmal authored Aug 17, 2021
2 parents aa4002f + f6bca0d commit 389a5b8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
46 changes: 46 additions & 0 deletions ballerina-tests/tests/auth_client_auth_handler_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,49 @@ isolated function testClientOAuth2HandlerForRefreshTokenGrant() {
test:assertFail(msg = "Test Failed! " + result3.message());
}
}

@test:Config {}
isolated function testClientOAuth2HandlerForJwtBearerGrant() {
http:OAuth2JwtBearerGrantConfig config = {
tokenUrl: "https://localhost:" + stsPort.toString() + "/oauth2/token",
assertion: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
clientId: "3MVG9YDQS5WtC11paU2WcQjBB3L5w4gz52uriT8ksZ3nUVjKvrfQMrU4uvZohTftxStwNEW4cfStBEGRxRL68",
clientSecret: "9205371918321623741",
scopes: ["token-scope1", "token-scope2"],
clientConfig: {
secureSocket: {
cert: {
path: TRUSTSTORE_PATH,
password: "ballerina"
}
}
}
};

http:Request request = createDummyRequest();
http:ClientOAuth2Handler handler = new(config);
http:Request|http:ClientAuthError result1 = handler->enrich(request);
if (result1 is http:Request) {
string header = checkpanic result1.getHeader(http:AUTH_HEADER);
test:assertEquals(header, "Bearer 2YotnFZFEjr1zCsicMWpAA");
} else {
test:assertFail(msg = "Test Failed! " + result1.message());
}

map<string|string[]> headers = {};
map<string|string[]>|http:ClientAuthError result2 = handler.enrichHeaders(headers);
if (result2 is map<string|string[]>) {
string header = <string>result2.get(http:AUTH_HEADER);
test:assertEquals(header, "Bearer 2YotnFZFEjr1zCsicMWpAA");
} else {
test:assertFail(msg = "Test Failed! " + result2.message());
}

map<string|string[]>|http:ClientAuthError result3 = handler.getSecurityHeaders();
if (result3 is map<string|string[]>) {
string header = <string>result3.get(http:AUTH_HEADER);
test:assertEquals(header, "Bearer 2YotnFZFEjr1zCsicMWpAA");
} else {
test:assertFail(msg = "Test Failed! " + result3.message());
}
}
7 changes: 6 additions & 1 deletion ballerina/auth_types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ public type OAuth2RefreshTokenGrantConfig record {|
*oauth2:RefreshTokenGrantConfig;
|};

# Represents OAuth2 JWT bearer grant configurations for OAuth2 authentication.
public type OAuth2JwtBearerGrantConfig record {|
*oauth2:JwtBearerGrantConfig;
|};

# Represents OAuth2 grant configurations for OAuth2 authentication.
public type OAuth2GrantConfig OAuth2ClientCredentialsGrantConfig|OAuth2PasswordGrantConfig|OAuth2RefreshTokenGrantConfig;
public type OAuth2GrantConfig OAuth2ClientCredentialsGrantConfig|OAuth2PasswordGrantConfig|OAuth2RefreshTokenGrantConfig|OAuth2JwtBearerGrantConfig;

# Represents file user store configurations for Basic Auth authentication.
public type FileUserStoreConfig record {|
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- [Introduce http:CacheConfig annotation to the resource signature](https://github.com/ballerina-platform/ballerina-standard-library/issues/1533)
- [Add service specific media-type prefix support in http:ServiceConfig annotation](https://github.com/ballerina-platform/ballerina-standard-library/issues/1620)
- [Add support for Map Json as query parameter](https://github.com/ballerina-platform/ballerina-standard-library/issues/1670)
- [Add OAuth2 JWT bearer grant type support](https://github.com/ballerina-platform/ballerina-standard-library/issues/1788)

## Fixed
- [Fix incorrect behaviour of client with mtls](https://github.com/ballerina-platform/ballerina-standard-library/issues/1708)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ stdlibMimeVersion=2.0.0-20210817-124100-383d13d
stdlibCacheVersion=3.0.0-20210817-124100-cda7525
stdlibAuthVersion=2.0.0-20210817-124700-9d2bc48
stdlibJwtVersion=2.0.0-20210817-124700-6c6216d
stdlibOAuth2Version=2.0.0-20210817-125000-9bf05fc
stdlibOAuth2Version=2.0.0-20210817-153400-d6dec5e
stdlibUuidVersion=1.0.0-20210817-124200-043a791

0 comments on commit 389a5b8

Please sign in to comment.