Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In this call you will include any data that the integration will process.

Example code for trigging flow

Python

Code Block
pip install PyJWT requests cryptography

The private key is stored in a file called privateKey.txt in this example

Expand
titlePython
Code Block
import jwt
import requests
import time
import os
from pathlib import Path
import unittest

class TestIncomingJWTIntegration(unittest.TestCase):

    def setUp(self):
        self.timeout = 10  # seconds
        self.maxDiff = None

    def test_should_return_a_valid_bearer(self):
        headers = {
            "alg": "RS256",
            "typ": "JWT"
        }

        current_time = int(time.time())

        payload = {
            'iss': 'copy from trigger page',
            'sub': 'copy from trigger page',
            'aud': 'https://ihubprod.rixter.net/prod/incoming/token',
            'iat': current_time,
            'exp': current_time + 1200
        }

        private_key_path = Path(__file__).parent / 'privateKey.txt'
        with open(private_key_path, 'r') as file:
            private_key = file.read()

        token = jwt.encode(payload, private_key, algorithm='RS256', headers=headers)
        # print('Generated JWT:', token)

        data = {
            "grant_type": "urn:ihub:jwt:bearer",
            "assertion": token
        }

        access_token = None
        # test generate jwt
        try:
            response = requests.post(payload['aud'], json=data, headers={'Content-Type': 'application/json'})
            response.raise_for_status()
            access_token = response.json().get('access_token')
        except requests.exceptions.RequestException as error:
            print('Error:', error.response.json() if error.response else str(error))

        self.assertIsNotNone(access_token)

        # test trigger flow
        incoming_data = {
            "issueKey": "SERVICENOW-1",
            "description": "HEJ",
            "summary": "Ticket from test case"
        }

        try:
            response = requests.post(
                "https://ihubprod.rixter.net/prod/incoming/webhook/jwt",
                json=incoming_data,
                headers={
                    'Content-Type': 'application/json',
                    'Authorization': f'Bearer {access_token}'
                }
            )
            response.raise_for_status()
            print(response.json())
        except requests.exceptions.RequestException as error:
            print('Error:', error.response.json() if error.response else str(error))

if __name__ == '__main__':
    unittest.main()

Nodejs

The private key is stored in a file called privateKey.txt in this example

Expand
Code Block
var jwt = require('jsonwebtoken');
const axios = require('axios');
import { expect } from "chai";
const fs = require('fs');
const path = require('path');

describe('Incoming JWT integration test', function() {  // Use function() to access this.timeout
  this.timeout(10000);  // Increase timeout to 10 seconds

  it('should return a valid bearer', async () => {

    const headers = {
      "alg": "RS256",
      "typ": "JWT"
    };

    const currentTime = Math.floor(Date.now() / 1000);

    const payload = {
      iss: 'copy from trigger page',
      sub: 'copy from trigger page',
      aud: 'https://ihubprod.rixter.net/prod/incoming/token',
      iat: currentTime,
      exp: (currentTime + 1200)
    };

    const privateKeyPath = path.join(__dirname, 'privateKey.txt');
    const privateKey = fs.readFileSync(privateKeyPath, 'utf8');

    const token = jwt.sign(payload, privateKey, { algorithm: 'RS256', header: headers });
    //console.log('Generated JWT:', token);

    const data = {
      "grant_type": "urn:ihub:jwt:bearer",
      "assertion": token
    };

    let access_token;
    //test generate jwt
    try {
      const response = await axios.post(payload.aud, data, {
        headers: {
          'Content-Type': 'application/json'
        }
      });
      access_token = response.data.access_token;
    } catch (error) {
      console.error('Error:', error.response ? error.response.data : error.message);
    }
    expect(access_token).not.to.be.equal(undefined);
    //test trigger flow
    const incomingData = {
      "issueKey": "SERVICENOW-1",
      "description": "HEJ",
      "summary":"Ticket from test case"
    };
    try {
      const response = await axios.post("https://ihubprod.rixter.net/prod/incoming/webhook/jwt", incomingData, {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer '+access_token
        }
      });
      console.log(response.data)
    } catch (error) {
      console.error('Error:', error.response ? error.response.data : error.message);
    }

  });
});