Decrypt a JSON Web Token

   Journey Manager (JM) The transaction engine for the platform. |   System Manager / DevOps |  All versions This feature is related to all versions.

Manager comes with the library that allows you to run a Groovy script to decrypt a JSON Web Token (JWT).

To decrypt a JSON Web Token:

  1. Save the certificate string which contains the public key as a service parameter.
  2. Import the necessary libraries:
    import groovy.json.JsonSlurper
    import java.io.InputStream;
    import java.security.PublicKey;
    import java.nio.charset.StandardCharsets;
    import java.security.cert.CertificateException;
    import java.security.cert.CertificateFactory;
    import java.security.cert.X509Certificate;
    import com.auth0.jwt.JWTVerifier;
  3. Update the script:
    def certificate = serviceParameters?.PublicAuthorizationCert
    InputStream is = new ByteArrayInputStream(certificate.getBytes(StandardCharsets.UTF_8))
    CertificateFactory f = CertificateFactory.getInstance("X.509")
    def cert = (X509Certificate)f.generateCertificate(is)
    PublicKey pk = cert.getPublicKey()
    JWTVerifier jwtVerifier = new JWTVerifier(pk);
    Map<String, Object> verify =jwtVerifier.verify(appToken);
  4. You get a Map object which you can use to extract data in the JWT. If for any reason like the public key is incorrect, or the token has expired, etc, an error will be thrown.
  5. To extract data from JWT, use this script:
    def field1= verify.get("<path to the data>");

Next, learn how to view services.