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

Refactor outbound authentication with custom providers and handlers #15696

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
e0bb2ea
Rename auth provider as inbound auth provider
ldclakmal Jun 2, 2019
86539ff
Refactor inbound auth types of JWT
ldclakmal Jun 5, 2019
d6d3d5f
Add outbound auth provider
ldclakmal Jun 5, 2019
ddb8d2a
Update constants
ldclakmal Jun 5, 2019
8bc4931
Rename authn handler as inbound authn handler
ldclakmal Jun 5, 2019
52e503f
Add outbound authn handler and default implementations
ldclakmal Jun 5, 2019
33baea3
Fix integration tests for renaming
ldclakmal Jun 5, 2019
6859ba7
Fix auth integration tests
ldclakmal Jun 5, 2019
cc2ce7f
Add outbound auth provider implementations
ldclakmal Jun 5, 2019
cdffd9b
Update http client endpoint
ldclakmal Jun 5, 2019
d2c54d9
Merge branch 'master' of https://github.com/ballerina-platform/baller…
ldclakmal Jun 5, 2019
1ac08fa
Fix bbe related to auth
ldclakmal Jun 5, 2019
c764a0a
Fix http unit tests related to auth
ldclakmal Jun 5, 2019
097fa28
Remove unused resources
ldclakmal Jun 5, 2019
7404081
Fix a bug in integration tests
ldclakmal Jun 5, 2019
d8f2d48
Fix a typo
ldclakmal Jun 5, 2019
c185514
Fix a bug in bbe related to auth
ldclakmal Jun 5, 2019
b307d18
Merge branch 'master' of https://github.com/ballerina-platform/baller…
ldclakmal Jun 6, 2019
f753825
Fix inbound-oauth2 integration tests
ldclakmal Jun 6, 2019
0740fde
Refactor inbound, outbound separation
ldclakmal Jun 7, 2019
ebc89b8
Fix auth related integration tests
ldclakmal Jun 7, 2019
933c4c1
Fix websub related integration tests
ldclakmal Jun 7, 2019
604f4fe
Refactor oauth2 outbound auth related logic
ldclakmal Jun 8, 2019
97db53b
Fix build of oauth2 module
ldclakmal Jun 8, 2019
f39f363
Fix oauth2 client integration tests
ldclakmal Jun 8, 2019
676c3f6
Update few constants related to auth and http
ldclakmal Jun 8, 2019
4ced9fc
Fix and refactor bbe related to auth
ldclakmal Jun 9, 2019
a0c9f27
Refactor authn handlers
ldclakmal Jun 10, 2019
2f77d95
Add inspect method for outbound auth provider
ldclakmal Jun 10, 2019
10bd6ed
Add inspect method for auth handlers
ldclakmal Jun 10, 2019
e182423
Update secure client
ldclakmal Jun 10, 2019
a13ccc9
Refactor code
ldclakmal Jun 10, 2019
d8deb1a
Expose client config for grant types
ldclakmal Jun 10, 2019
05b18bd
Fix lang-server related bal files
ldclakmal Jun 11, 2019
6d5bb20
Update module md of auth related modules
ldclakmal Jun 11, 2019
c852091
Refactor JWT records
ldclakmal Jun 11, 2019
6200038
Fix JWT unit tests
ldclakmal Jun 11, 2019
393c5af
Merge branch 'master' of https://github.com/ballerina-platform/baller…
ldclakmal Jun 11, 2019
ca1c2d9
Improve unit tests related to auth module
ldclakmal Jun 12, 2019
2344afe
Refactor auth unit tests related to http module
ldclakmal Jun 12, 2019
567485e
Refactor unit tests related to jwt module
ldclakmal Jun 12, 2019
bafaf91
Enable auth related tests in http module
ldclakmal Jun 12, 2019
a1042b2
Refactor integration tests
ldclakmal Jun 13, 2019
61c5d9b
Update custom auth provider integration test case
ldclakmal Jun 13, 2019
bf338d1
Refactor integration tests
ldclakmal Jun 13, 2019
f7e6a83
Merge branch 'master' of https://github.com/ballerina-platform/baller…
ldclakmal Jun 13, 2019
685a25d
Merge branch 'master' of https://github.com/ballerina-platform/baller…
ldclakmal Jun 14, 2019
8bdfdcb
Refactor code
ldclakmal Jun 14, 2019
f5a5d29
Fix a bug in inspecting oauth2 provider
ldclakmal Jun 14, 2019
8edfa23
Merge branch 'master' of https://github.com/ballerina-platform/baller…
ldclakmal Jun 14, 2019
60a0a69
Fix a bug in oauth2 outbound provider
ldclakmal Jun 14, 2019
85a1467
Merge branch 'master' of https://github.com/ballerina-platform/baller…
ldclakmal Jun 14, 2019
e941f28
Enable auth tests of http module
ldclakmal Jun 14, 2019
211ca1f
Fix a bug in bbe
ldclakmal Jun 15, 2019
e6df674
Fix a bug in oauth2 caching
ldclakmal Jun 17, 2019
71a281e
Refactor outbound oauth2 configurations
ldclakmal Jun 17, 2019
2657238
Merge branch 'master' of https://github.com/ballerina-platform/baller…
ldclakmal Jun 17, 2019
9220758
Refactor code
ldclakmal Jun 18, 2019
53a4865
Refactor basic auth configurations
ldclakmal Jun 18, 2019
9590a85
Refactor jwt auth configurations
ldclakmal Jun 18, 2019
b727133
Refactor ldap auth configurations
ldclakmal Jun 18, 2019
563948c
Fix integration tests with config refactoring
ldclakmal Jun 18, 2019
47f2a42
Update module md
ldclakmal Jun 18, 2019
4e7396e
Refactor code
ldclakmal Jun 19, 2019
e84f375
Merge branch 'master' of https://github.com/ballerina-platform/baller…
ldclakmal Jun 19, 2019
dc9b645
Refactor provider constructors
ldclakmal Jun 19, 2019
1e482a5
Apply suggestions for BBE from code review
ldclakmal Jun 21, 2019
9c601fd
Apply suggestions from code review
ldclakmal Jun 21, 2019
475b363
Apply suggestions from code review
ldclakmal Jun 21, 2019
e70ddf0
Reformat docs
ldclakmal Jun 21, 2019
936bfcd
Merge branch 'jballerina' of https://github.com/ballerina-platform/ba…
ldclakmal Jun 21, 2019
f6f647f
Merge branch 'jballerina' of https://github.com/ballerina-platform/ba…
ldclakmal Jun 24, 2019
4d1ce53
Merge branch 'jballerina' of https://github.com/ballerina-platform/ba…
ldclakmal Jun 24, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@ import ballerina/config;
import ballerina/http;
import ballerina/log;

// Define the basic auth client endpoint to call the backend services.
// Basic authentication is enabled by setting the `scheme: http:BASIC_AUTH`
// The `username` and `password` should be specified as needed.
// Defines the Basic Auth client endpoint to call the backend services.
// Basic Authentication is enabled by creating an
// `auth:OutboundBasicAuthProvider` with the `username` and `password`
// passed as a record.
auth:OutboundBasicAuthProvider outboundBasicAuthProvider = new({
username: "tom",
password: "1234"
});

// Creates a Basic Auth handler with the created Basic Auth provider.
http:BasicAuthHandler outboundBasicAuthHandler =
new(outboundBasicAuthProvider);

http:Client httpEndpoint = new("https://localhost:9090", config = {
auth: {
scheme: http:BASIC_AUTH,
config: {
username: "tom",
password: "1234"
}
authHandler: outboundBasicAuthHandler
}
});

Expand All @@ -31,13 +37,13 @@ public function main() {
}
}

// Create a Basic authentication handler with the relevant configurations.
auth:ConfigAuthStoreProvider basicAuthProvider = new;
http:BasicAuthHeaderAuthnHandler basicAuthnHandler = new(basicAuthProvider);

// Defines the sample backend service, which is secured with Basic Auth
// authentication.
auth:InboundBasicAuthProvider inboundBasicAuthProvider = new(());
http:BasicAuthHandler inboundBasicAuthHandler = new(inboundBasicAuthProvider);
listener http:Listener ep = new(9090, config = {
auth: {
authnHandlers: [basicAuthnHandler]
authHandlers: [inboundBasicAuthHandler]
},
secureSocket: {
keyStore: {
Expand All @@ -47,19 +53,8 @@ listener http:Listener ep = new(9090, config = {
}
});

@http:ServiceConfig {
basePath: "/hello",
auth: {
enabled: true
}
}
service echo on ep {

@http:ResourceConfig {
methods: ["GET"],
path: "/sayHello"
}
resource function hello(http:Caller caller, http:Request req) {
service hello on ep {
resource function sayHello(http:Caller caller, http:Request req) {
error? result = caller->respond("Hello, World!!!");
if (result is error) {
log:printError("Error in responding to caller", err = result);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// A secured client with basic authentication is used to connect to a
// secured service with basic authentication. An additional `auth`
// field is added to the HTTP client endpoint initialization in order
// to make the simple HTTP client endpoint into a secure client endpoint.
// Inside the `auth` field the `scheme`, `username` and `password`
// is defined for basic authentication.
// A client, which is secured with Basic authentication should be used to
// connect to a service, which is secured with Basic authentication.
// The `auth:OutboundBasicAuthProvider` is initialized with the `username` and
// `password` and the `http:BasicAuthHandler` is initialized by providing
// the created `auth:OutboundBasicAuthProvider`. An additional `auth` field is
// added to the HTTP client endpoint initialization in order to secure the
// simple HTTP client endpoint.
// The `authHandler` field is defined inside the `auth` field with the value of
// it being the reference of the created `http:BearerAuthHandler`.
166 changes: 80 additions & 86 deletions examples/secured-client-with-jwt-auth/secured_client_with_jwt_auth.bal
Original file line number Diff line number Diff line change
@@ -1,86 +1,80 @@
// TODO: Resolve with https://github.com/ballerina-platform/ballerina-lang/issues/15487
//import ballerina/http;
//import ballerina/jwt;
//import ballerina/log;
//import ballerina/runtime;
//
//// Define the JWT auth client endpoint to call the backend services.
//// JWT authentication is enabled by setting the `scheme: http:JWT_AUTH`
//http:Client httpEndpoint = new("https://localhost:9090", config = {
// auth: {
// scheme: http:JWT_AUTH
// }
//});
//
//public function main() {
// // Set the JWT token into runtime invocation context mentioning
// // scheme as `jwt`
// string token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJiYWxsZXJ" +
// "pbmEiLCJpc3MiOiJiYWxsZXJpbmEiLCJleHAiOjI4MTg0MTUwMTksImlhdCI6MTUyND" +
// "U3NTAxOSwianRpIjoiZjVhZGVkNTA1ODVjNDZmMmI4Y2EyMzNkMGMyYTNjOWQiLCJhdW" +
// "QiOlsiYmFsbGVyaW5hIiwiYmFsbGVyaW5hLm9yZyIsImJhbGxlcmluYS5pbyJdLCJzY" +
// "29wZSI6ImhlbGxvIn0.bNoqz9_DzgeKSK6ru3DnKL7NiNbY32ksXPYrh6Jp0_O3ST7W" +
// "fXMs9WVkx6Q2TiYukMAGrnMUFrJnrJvZwC3glAmRBrl4BYCbQ0c5mCbgM9qhhCjC1tB" +
// "A50rjtLAtRW-JTRpCKS0B9_EmlVKfvXPKDLIpM5hnfhOin1R3lJCPspJ2ey_Ho6fDhs" +
// "KE3DZgssvgPgI9PBItnkipQ3CqqXWhV-RFBkVBEGPDYXTUVGbXhdNOBSwKw5ZoVJrCU" +
// "iNG5XD0K4sgN9udVTi3EMKNMnVQaq399k6RYPAy3vIhByS6QZtRjOG8X93WJw-9GLiH" +
// "vcabuid80lnrs2-mAEcstgiHVw";
// runtime:getInvocationContext().authenticationContext.scheme = "jwt";
// runtime:getInvocationContext().authenticationContext.authToken = token;
//
// // Send a `GET` request to the specified endpoint.
// var response = httpEndpoint->get("/hello/sayHello");
// if (response is http:Response) {
// var result = response.getTextPayload();
// log:printInfo((result is error) ? "Failed to retrieve payload."
// : result);
// } else {
// log:printError("Failed to call the endpoint.", err = response);
// }
//}
//
//// Create a JWT authentication provider with the relevant configurations.
//jwt:JWTAuthProvider jwtAuthProvider = new({
// issuer: "ballerina",
// audience: ["ballerina.io"],
// certificateAlias: "ballerina",
// trustStore: {
// path: "${ballerina.home}/bre/security/ballerinaTruststore.p12",
// password: "ballerina"
// }
//});
//
//// Create a JWT authentication handler with the created JWT auth provider.
//http:BearerAuthHeaderAuthnHandler jwtAuthnHandler = new(jwtAuthProvider);
//
//listener http:Listener ep = new(9090, config = {
// auth: {
// authnHandlers: [jwtAuthnHandler]
// },
// secureSocket: {
// keyStore: {
// path: "${ballerina.home}/bre/security/ballerinaKeystore.p12",
// password: "ballerina"
// }
// }
//});
//
//@http:ServiceConfig {
// basePath: "/hello",
// auth: {
// enabled: true
// }
//}
//service echo on ep {
//
// @http:ResourceConfig {
// methods: ["GET"],
// path: "/sayHello"
// }
// resource function hello(http:Caller caller, http:Request req) {
// error? result = caller->respond("Hello, World!!!");
// if (result is error) {
// log:printError("Error in responding to caller", err = result);
// }
// }
//}
import ballerina/http;
import ballerina/jwt;
import ballerina/log;
import ballerina/runtime;

// Defines the JWT auth client endpoint to call the backend services.
// JWT authentication is enabled by creating a `jwt:OutboundJWTAuthProvider`
// with/without passing the JWT issuer configurations as a record. If the JWT
// issuer configurations are passed, a new JWT will be issued and it will be
// used for the outbound authentication.
jwt:OutboundJwtAuthProvider outboundJwtAuthProvider = new(());

// Create a Bearer Auth handler with the created JWT Auth provider.
http:BearerAuthHandler outboundJwtAuthHandler = new(outboundJwtAuthProvider);

http:Client httpEndpoint = new("https://localhost:9090", config = {
auth: {
authHandler: outboundJwtAuthHandler
}
});

public function main() {
// Sets the JWT token into the runtime invocation context mentioning
// the scheme as `jwt`.
string token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJiYWxsZXJ" +
"pbmEiLCJpc3MiOiJiYWxsZXJpbmEiLCJleHAiOjI4MTg0MTUwMTksImlhdCI6MTUyND" +
"U3NTAxOSwianRpIjoiZjVhZGVkNTA1ODVjNDZmMmI4Y2EyMzNkMGMyYTNjOWQiLCJhd" +
"WQiOlsiYmFsbGVyaW5hIiwiYmFsbGVyaW5hLm9yZyIsImJhbGxlcmluYS5pbyJdLCJz" +
"Y29wZSI6ImhlbGxvIn0.bNoqz9_DzgeKSK6ru3DnKL7NiNbY32ksXPYrh6Jp0_O3ST7" +
"WfXMs9WVkx6Q2TiYukMAGrnMUFrJnrJvZwC3glAmRBrl4BYCbQ0c5mCbgM9qhhCjC1t" +
"BA50rjtLAtRW-JTRpCKS0B9_EmlVKfvXPKDLIpM5hnfhOin1R3lJCPspJ2ey_Ho6fDh" +
"sKE3DZgssvgPgI9PBItnkipQ3CqqXWhV-RFBkVBEGPDYXTUVGbXhdNOBSwKw5ZoVJrC" +
"UiNG5XD0K4sgN9udVTi3EMKNMnVQaq399k6RYPAy3vIhByS6QZtRjOG8X93WJw-9GLi" +
"Hvcabuid80lnrs2-mAEcstgiHVw";
runtime:getInvocationContext().authenticationContext.scheme = "jwt";
runtime:getInvocationContext().authenticationContext.authToken = token;

// Sends a `GET` request to the specified endpoint.
var response = httpEndpoint->get("/hello/sayHello");
if (response is http:Response) {
var result = response.getTextPayload();
log:printInfo((result is error) ? "Failed to retrieve payload."
: result);
} else {
log:printError("Failed to call the endpoint.", err = response);
}
}

// Defines the sample backend service, which is secured with JWT Auth
// authentication.
jwt:InboundJwtAuthProvider inboundJwtAuthProvider = new({
issuer: "ballerina",
audience: ["ballerina.io"],
certificateAlias: "ballerina",
trustStore: {
path: "${ballerina.home}/bre/security/ballerinaTruststore.p12",
password: "ballerina"
}
});
http:BearerAuthHandler inboundJwtAuthHandler = new(inboundJwtAuthProvider);
listener http:Listener ep = new(9090, config = {
auth: {
authHandlers: [inboundJwtAuthHandler]
},
secureSocket: {
keyStore: {
path: "${ballerina.home}/bre/security/ballerinaKeystore.p12",
password: "ballerina"
}
}
});

service hello on ep {
resource function sayHello(http:Caller caller, http:Request req) {
error? result = caller->respond("Hello, World!!!");
if (result is error) {
log:printError("Error in responding to caller", err = result);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// A secured client with JWT authentication is used to connect to a
// secured service with JWT authentication. An additional `auth`
// field is added to the HTTP client endpoint initialization in order
// to make the simple HTTP client endpoint into a secure client endpoint.
// Inside the `auth` field the `scheme` is defined for JWT authentication.
// A secured client, which is secured with JWT authentication should be used
// to connect to a service, which is secured with JWT authentication.
// The `jwt:OutboundJWTAuthProvider` is initialized with/without passing
// the JWT issuer configurations as a record. If the JWT issuer configurations
// are passed, a new JWT will be issued and it will be used for the outbound
// authentication. The `http:BearerAuthHandler` is initialized by providing
// the created `jwt:OutboundBasicAuthProvider`. An additional `auth` field is
// added to the HTTP client endpoint initialization in order to secure the
// simple HTTP client endpoint.
// The `authHandler` field is defined inside the `auth` field with the value
// of it being the reference of the created `http:BearerAuthHandler`.

Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
// TODO: Resolve with https://github.com/ballerina-platform/ballerina-lang/issues/15487
//import ballerina/test;
//import ballerina/log;
//
//string log = "";
//
//// This is the mock function which will replace the real function
//@test:Mock {
// moduleName: "ballerina/log",
// functionName: "printInfo"
//}
//public function mockPrintInfo(string|(function () returns (string)) msg) {
// if (msg is string) {
// log = msg;
// }
//}
//
//@test:Config
//function testFunc() {
// // Invoking the main function
// main();
// test:assertEquals(log, "Hello, World!!!");
//}
import ballerina/test;
import ballerina/log;

string log = "";

// This is the mock function, which will replace the real function.
@test:Mock {
moduleName: "ballerina/log",
functionName: "printInfo"
}
public function mockPrintInfo(string|(function () returns (string)) msg) {
if (msg is string) {
log = msg;
}
}

@test:Config
function testFunc() {
// Invokes the main function,
main();
test:assertEquals(log, "Hello, World!!!");
}
Loading