Create a JWT token using your private key
import jsonwebtoken from "jsonwebtoken";
import fs from "node:fs";
const privateKey = fs.readFileSync("private.pem").toString();
const jwt = jsonwebtoken.sign({}, privateKey, {
issuer: this.url,
subject: this.company_id,
audience: 'bankingservice.ai',
algorithm: 'RS256',
});
console.log(jwt);
import jwt
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
with open("private.pem", "rb") as key_file:
private_key = serialization.load_pem_private_key(
key_file.read(),
password=None,
backend=default_backend()
)
token = jwt.encode({}, private_key, algorithm="RS256", headers={
"iss": "YourIssuerUrl",
"sub": "YourCompanyId",
"aud": "bankingservice.ai"
})
print(token)
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
$privateKey = file_get_contents('private.pem');
$token = JWT::encode([], $privateKey, 'RS256', null, [
'iss' => 'YourIssuerUrl',
'sub' => 'YourCompanyId',
'aud' => 'bankingservice.ai'
]);
echo $token;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Cryptography;
using Microsoft.IdentityModel.Tokens;
using System.IO;
var privateKey = File.ReadAllText("private.pem");
var rsa = RSA.Create();
rsa.ImportFromPem(privateKey.ToCharArray());
var signingCredentials = new SigningCredentials(new RsaSecurityKey(rsa), SecurityAlgorithms.RsaSha256);
var jwt = new JwtSecurityToken(
issuer: "YourIssuerUrl",
audience: "bankingservice.ai",
claims: null,
signingCredentials: signingCredentials
);
var token = new JwtSecurityTokenHandler().WriteToken(jwt);
Console.WriteLine(token);
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.PrivateKey;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
String privateKeyContent = new String(Files.readAllBytes(Paths.get("private.pem")))
.replaceAll("\\n", "").replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "");
byte[] pkcs8EncodedBytes = Base64.getDecoder().decode(privateKeyContent);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pkcs8EncodedBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
String jws = Jwts.builder()
.setIssuer("YourIssuerUrl")
.setSubject("YourCompanyId")
.setAudience("bankingservice.ai")
.signWith(privateKey)
.compact();
System.out.println(jws);
package main
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"github.com/golang-jwt/jwt"
"io/ioutil"
"log"
)
func main() {
privateKeyData, err := ioutil.ReadFile("private.pem")
if err != nil {
log.Fatal(err)
}
block, _ := pem.Decode(privateKeyData)
if block == nil {
log.Fatal("failed to parse PEM block containing the key")
}
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
log.Fatal(err)
}
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
"iss": "YourIssuerUrl",
"sub": "YourCompanyId",
"aud": "bankingservice.ai",
})
tokenString, err := token.SignedString(privateKey)
if err != nil {
log.Fatal(err)
}
log.Println(tokenString)
}
Last updated
Was this helpful?