import ballerina/auth;
import ballerina/config;
import ballerina/http;
import ballerina/log;
auth:OutboundBasicAuthProvider outboundBasicAuthProvider = new ({
    username: "tom",
    password: "1234"
});
http:BasicAuthHandler outboundBasicAuthHandler =
                                            new (outboundBasicAuthProvider);http:Client httpEndpoint = new ("https://localhost:9090", {
    auth: {
        authHandler: outboundBasicAuthHandler
    },
    secureSocket: {
        trustStore: {
            path: config:getAsString("b7a.home") +
                  "/bre/security/ballerinaTruststore.p12",
            password: "ballerina"
        }
    }
});public function main() {
    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.", response);
    }
}# To test the client, first start the sample service by executing the below
# command by specifying the configuration file name and passing Ballerina home
# path as a system property.
ballerina run secured_service_with_basic_auth.bal --b7a.config.file=sample-users.toml --b7a.home=<ballerina_home_path>
[ballerina/http] started HTTPS/WSS listener 0.0.0.0:9090# Then start the client by executing the below command by passing Ballerina home
# path as a system property.
ballerina run secured_client_with_basic_auth.bal --b7a.home=<ballerina_home_path>
INFO  [ballerina/log] - Hello, World!!!import ballerina/auth;
import ballerina/config;
import ballerina/http;
import ballerina/log;
auth:InboundBasicAuthProvider inboundBasicAuthProvider = new;
http:BasicAuthHandler inboundBasicAuthHandler = new (inboundBasicAuthProvider);listener http:Listener ep = new (9090, config = {
    auth: {
        authHandlers: [inboundBasicAuthHandler],
        scopes: ["hello"]
    },
    secureSocket: {
        keyStore: {
            path: config:getAsString("b7a.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", result);
        }
    }
}

Secured Client with Basic Auth

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.

import ballerina/auth;
import ballerina/config;
import ballerina/http;
import ballerina/log;
auth:OutboundBasicAuthProvider outboundBasicAuthProvider = new ({
    username: "tom",
    password: "1234"
});

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.

http:BasicAuthHandler outboundBasicAuthHandler =
                                            new (outboundBasicAuthProvider);

Creates a Basic Auth handler with the created Basic Auth provider.

http:Client httpEndpoint = new ("https://localhost:9090", {
    auth: {
        authHandler: outboundBasicAuthHandler
    },
    secureSocket: {
        trustStore: {
            path: config:getAsString("b7a.home") +
                  "/bre/security/ballerinaTruststore.p12",
            password: "ballerina"
        }
    }
});
public function main() {
    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.", response);
    }
}

Send a GET request to the specified endpoint.

# To test the client, first start the sample service by executing the below
# command by specifying the configuration file name and passing Ballerina home
# path as a system property.
ballerina run secured_service_with_basic_auth.bal --b7a.config.file=sample-users.toml --b7a.home=<ballerina_home_path>
[ballerina/http] started HTTPS/WSS listener 0.0.0.0:9090
# Then start the client by executing the below command by passing Ballerina home
# path as a system property.
ballerina run secured_client_with_basic_auth.bal --b7a.home=<ballerina_home_path>
INFO  [ballerina/log] - Hello, World!!!
import ballerina/auth;
import ballerina/config;
import ballerina/http;
import ballerina/log;
auth:InboundBasicAuthProvider inboundBasicAuthProvider = new;
http:BasicAuthHandler inboundBasicAuthHandler = new (inboundBasicAuthProvider);

Defines the sample backend service, which is secured with Basic Auth authentication.

listener http:Listener ep = new (9090, config = {
    auth: {
        authHandlers: [inboundBasicAuthHandler],
        scopes: ["hello"]
    },
    secureSocket: {
        keyStore: {
            path: config:getAsString("b7a.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", result);
        }
    }
}