JWT Authentication
Custom Authentication
Section titled “Custom Authentication”The Custom JWT authentication provider allows users to authenticate with an authentication system that is independent from Pingstreams. The external system must return a signed JSON Web Token that contains a unique ID value for the authenticated user.
Pingstreams uses the JWT to identify your application’s users and authenticate their requests but does not impose any restrictions on the external authentication system’s requirements or authentication methods.
To create a Custom JWT Token you must generate a Project Shared Secret as described below.
Generating a Project Shared Secret
Section titled “Generating a Project Shared Secret”A Project Shared Secret is a security setting, intended to be generated, copied, and pasted into a communication with your engineering team, or directly into your codebase, in a single sitting. It should not be entered into a browser.
To generate the shared secret required for custom authentication you need:
- Open the Dashboard and go to Project Name > Project Settings.
- Go to the Visitor Authentication tab and click the Generate button.

Create a Pingstreams JWT token
Section titled “Create a Pingstreams JWT token”To create a JWT token you must set the following required fields of the user object:
_idis the custom unique user identifier of the external authentication system. It must start with<YOUR_PROJECT_ID>_(example: 5e5f4e220b28440012117be4_12345678)subJWTs describe their subject in the sub claim. For custom authentication sub field must be equal to valueuserexternalaudJWTs describe their audience in the aud claim. For custom authentication must behttps://pingstreams.com/projects/<YOUR_PROJECT_ID>whether you use the cloud version of Pingstreams or if you install it on-premise.emailIt’s the user email
Optional fields:
firstnameIt’s the user firstnamelastnameIt’s the user lastnameattributesother custom jwt claims.
The external authentication system must create the JWT signing the user object with the Project Shared Secret code.
User object example:
{
"_id": "5e5f4e220b28440012117be4_12345678",
"firstname": "Andrea",
"lastname": "Leo",
"email": "user@example.com",
"attributes": {
"attribute1": "value"
},
"sub": "userexternal",
"aud": "https://pingstreams.com/projects/5c81593adf767b0017d1aa68"
}Generate JWT Token Server Side
Section titled “Generate JWT Token Server Side”Find the template below that fits your language needs. Customize the sample as needed, making sure to replace the #{details} placeholders with your own information.
If none of these samples match your needs, JWT has a more extensive list of JWT libraries to explore.
NodeJS
Section titled “NodeJS”Install jsonwebtoken:
npm install jsonwebtoken --save-devThen, generate a token using the shared secret:
var jwt = require('jsonwebtoken');
var payload = {
_id: '#{customerIdentifier}',
firstname: '#{customerFirstname}',
lastname: '#{customerLastname}',
email: '#{customerEmail}',
sub: 'userexternal',
aud: 'https://pingstreams.com/projects/#{YOUR_PROJECT_ID}',
};
var token = jwt.sign(payload, '#{yourProjectSharedSecret}');You can find a NodeJs Custom Jwt Authentication example here.
Download PHP-JWT:
composer require firebase/php-jwtGenerate a token using the shared secret:
$payload = {
'_id' => '#{customerIdentifier}',
'firstname' => '#{customerFirstname}',
'lastname' => '#{customerLastname}',
'email' => '#{customerEmail}',
'sub' => 'userexternal',
'aud' => 'https://pingstreams.com/projects/#{YOUR_PROJECT_ID}'
};
$token = JWT::encode($payload, '#{yourProjectSharedSecret}');Authentication with Java is covered with a simple Java (Maven) example on the public repo PingstreamsJavaJWTSign
We use the JJWT library to implement the Pingstreams JWT sign operation.
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
byte[] apiKeySecretBytes;
apiKeySecretBytes = SECRET_KEY.getBytes();
Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());
JwtBuilder builder = Jwts.builder().setId(id)
.setIssuedAt(now)
.setSubject(subject)
.setIssuer(issuer)
.claim("firstname", firstname)
.claim("lastname", lastname)
.claim("email", email)
.signWith(signingKey, signatureAlgorithm);
return builder.compact();Please refer to the above mentioned repo for further details.
Verify the token
Section titled “Verify the token”You can verify the JWT token using jwt.io following these steps:
- Paste the secret code
- Paste the jwt code in the left column
- Check the “Signature Verified” label
Widget Authentication
Section titled “Widget Authentication”See how to setup custom authentication for the widget using the JWT token.