DataWeave JWT Library icon

DataWeave JWT Library

(2 reviews)

RSA

jwt::RSA

This module provides functionality to create signed JSON Web Tokens using RSA signatures.

The supported RSA algorithms are:
- RS256
- RS384
- RS512

RSA private keys must be in PKCS#1 or PKCS#8 format.

Index

Functions

NameDescription
JWTGenerate JWT with header, payload, and signature by specific algorithm.
algHelper function to validate the algorithm provided for signing.
signJWTHelper function to sign the JWT.

Variables

NameDescription
algMappingSupported RSA algorithms.

Functions

JWT ↑↑

JWT(header: Object, payload: Object, signingKey: String, algorithm: String): String

Generate JWT with header, payload, and signature by specific algorithm.

Parameters
NameTypeDescription
headerObjectJWT header.
payloadObjectJWT payload.
signingKeyStringSigning key.
algorithmStringRSA algorithm.
Example

This example shows how the JWT behaves with the sample input.

Source
%dw 2.0
import * from jwt::RSA

output application/json
input key application/json
---
{
    token: JWT(
      {
          header: "value"
      },
        {
            iss: "some@email.com",
            aud: 'https://oauth2.googleapis.com/token',
            scope: 'https://www.googleapis.com/auth/drive.readonly',
            iat: now() as Number { unit: 'seconds' },
            exp: (now() + |PT3600S|) as Number { unit: 'seconds' }
        },
        key,
      'Sha384withRSA'
    ),
    expiration: now() + |PT3550S|
}
Output
{
   token: "eyJhbGCI6IkpXVCJ9.eyJ4iLCJpYXQiOjE0MjI3Nzk2Mzh9.gzSraSYS8EXBxLN_oWnFSRgCz",
   "expiration": "2031-11-18T15:11:31.060175Z"
}

JWT(payload: Object, signingKey: String): String

Generate JWT with a payload, automatically generated header, and key signed with RS256.

Parameters
NameTypeDescription
payloadObjectJWT payload.
signingKeyStringSigning key.
Example

This example shows how the JWT behaves with the sample input.

Source
%dw 2.0
import * from jwt::RSA

output application/json
input key application/json
---
{
    token: JWT(
        {
            iss: "some@email.com",
            aud: 'https://oauth2.googleapis.com/token',
            scope: 'https://www.googleapis.com/auth/drive.readonly',
            iat: now() as Number { unit: 'seconds' },
            exp: (now() + |PT3600S|) as Number { unit: 'seconds' }
        },
        key
    ),
    expiration: now() + |PT3550S|
}
Output
{
   token: "eyJhbGCI6IkpXVCJ9.eyJ4iLCJpYXQiOjE0MjI3Nzk2Mzh9.gzSraSYS8EXBxLN_oWnFSRgCz",
   "expiration": "2031-11-18T15:11:31.060175Z"
}

alg ↑↑

alg(algorithm: String): String | Null

Helper function to validate the algorithm provided for signing.

Parameters
NameTypeDescription
algorithmStringAlgorithm provided for signing.

signJWT ↑↑

signJWT(jwt: String, privateKey: String, algorithm: String): String

Helper function to sign the JWT.

Parameters
NameTypeDescription
jwtStringHeader and payload parts of the JWT.
privateKeyStringSigning key.
algorithmStringAlgorithm provided for signing.

Variables

algMapping ↑↑

Supported RSA algorithms.


Reviews

TypeDataWeave Library
OrganizationMuleSoft
Published by
MuleSoft Organization
Published onMar 23, 2022
Asset overview

Asset versions for 1.0.x

Asset versions
VersionActions
1.0.0