Swan Lake Beta2 is here.
import ballerina/http;
import ballerina/io;

// Defines the HTTP client to call the JWT auth secured APIs.
// The client is enriched with the `Authorization: Bearer <token>` header by
// passing the `http:JwtIssuerConfig` for the `auth` configuration of the
// client. A self-signed JWT is issued before the request is sent.
http:Client securedEP = check new("https://localhost:9090",
    auth = {
        username: "ballerina",
        issuer: "wso2",
        audience: ["ballerina", "ballerina.org", "ballerina.io"],
        keyId: "5a0b754-895f-4279-8843-b745e11a57e9",
        jwtId: "JlbmMiOiJBMTI4Q0JDLUhTMjU2In",
        customClaims: { "scp": "admin" },
        expTime: 3600,
        signatureConfig: {
            config: {
                keyFile: "../resource/path/to/private.key"
            }
        }
    },
    secureSocket = {
        cert: "../resource/path/to/public.crt"
    }
);

public function main() returns error? {
    string response = check securedEP->get("/foo/bar");
    io:println(response);
}

Client - Self Signed JWT Auth

A client, which is secured with self-signed JWT can be used to connect to a secured service.
The client is enriched with the Authorization: Bearer <token> header by passing the http:JwtIssuerConfig to the auth configuration of the client. A self-signed JWT is issued before the request is sent.

For more information on the underlying module, see the OAuth2 module.

import ballerina/http;
import ballerina/io;
http:Client securedEP = check new("https://localhost:9090",
    auth = {
        username: "ballerina",
        issuer: "wso2",
        audience: ["ballerina", "ballerina.org", "ballerina.io"],
        keyId: "5a0b754-895f-4279-8843-b745e11a57e9",
        jwtId: "JlbmMiOiJBMTI4Q0JDLUhTMjU2In",
        customClaims: { "scp": "admin" },
        expTime: 3600,
        signatureConfig: {
            config: {
                keyFile: "../resource/path/to/private.key"
            }
        }
    },
    secureSocket = {
        cert: "../resource/path/to/public.crt"
    }
);

Defines the HTTP client to call the JWT auth secured APIs. The client is enriched with the Authorization: Bearer <token> header by passing the http:JwtIssuerConfig for the auth configuration of the client. A self-signed JWT is issued before the request is sent.

public function main() returns error? {
    string response = check securedEP->get("/foo/bar");
    io:println(response);
}
# As a prerequisite, start a sample service secured with OAuth2.
# You may need to change the trusted certificate file path and private key file path.
bal run http_client_self_signed_jwt_auth.bal
Hello, World!
import ballerina/http;
import ballerina/io;

// Defines the HTTP client to call the JWT auth secured APIs.
// The client is enriched with the `Authorization: Bearer <token>` header by
// passing the `http:JwtIssuerConfig` for the `auth` configuration of the
// client. A self-signed JWT is issued before the request is sent.
http:Client securedEP = check new("https://localhost:9090",
    auth = {
        username: "ballerina",
        issuer: "wso2",
        audience: ["ballerina", "ballerina.org", "ballerina.io"],
        keyId: "5a0b754-895f-4279-8843-b745e11a57e9",
        jwtId: "JlbmMiOiJBMTI4Q0JDLUhTMjU2In",
        customClaims: { "scp": "admin" },
        expTime: 3600,
        signatureConfig: {
            config: {
                keyFile: "../resource/path/to/private.key"
            }
        }
    },
    secureSocket = {
        cert: "../resource/path/to/public.crt"
    }
);

public function main() returns error? {
    string response = check securedEP->get("/foo/bar");
    io:println(response);
}

Client - Self Signed JWT Auth

A client, which is secured with self-signed JWT can be used to connect to a secured service.
The client is enriched with the Authorization: Bearer <token> header by passing the http:JwtIssuerConfig to the auth configuration of the client. A self-signed JWT is issued before the request is sent.

For more information on the underlying module, see the OAuth2 module.

import ballerina/http;
import ballerina/io;
http:Client securedEP = check new("https://localhost:9090",
    auth = {
        username: "ballerina",
        issuer: "wso2",
        audience: ["ballerina", "ballerina.org", "ballerina.io"],
        keyId: "5a0b754-895f-4279-8843-b745e11a57e9",
        jwtId: "JlbmMiOiJBMTI4Q0JDLUhTMjU2In",
        customClaims: { "scp": "admin" },
        expTime: 3600,
        signatureConfig: {
            config: {
                keyFile: "../resource/path/to/private.key"
            }
        }
    },
    secureSocket = {
        cert: "../resource/path/to/public.crt"
    }
);

Defines the HTTP client to call the JWT auth secured APIs. The client is enriched with the Authorization: Bearer <token> header by passing the http:JwtIssuerConfig for the auth configuration of the client. A self-signed JWT is issued before the request is sent.

public function main() returns error? {
    string response = check securedEP->get("/foo/bar");
    io:println(response);
}
# As a prerequisite, start a sample service secured with OAuth2.
# You may need to change the trusted certificate file path and private key file path.
bal run http_client_self_signed_jwt_auth.bal
Hello, World!

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand