Git Product home page Git Product logo

jwt-kong-api-gateway-postman's Introduction

Automatic auth/JWT token generation for Kong gateway in Postman using Postman only.

I made this repository to share a pre-request script for Postman to generate a Kong API Gateway JWT token and write it to the Postman global variable 'ACCESS_TOKEN' for further authorization. It also signs token with private key.

This variable can then be used in the request header for authorization.

It works without Node.js and uses only Postman itself.

To use this script in Postman, you first need to cache third-party librariy jsrsasign. To do this, you need to add this line on tests tab script:

pm.globals.set("jsrsasign-js", responseBody);

And perform a GET request in Postman:

GET https://kjur.github.io/jsrsasign/jsrsasign-latest-all-min.js

After that add this code in Pre request Script tab and set Bearer Token authorization value as {{ACCESS_TOKEN}} on Authorization tab:

var navigator = {};
var window = {};
eval(pm.globals.get("jsrsasign-js")); //need to be cached before (read README first)
var JWS = KJUR.jws.JWS;
var uuid = require('uuid');
var myUUID = uuid.v4();

var currentTimestamp = Math.floor(Date.now() / 1000)

var data = {
	'iss': "ea235345465fha675767a", //your path to iss
	'aud': "https://login.microsoftonline.com/6djfbhjTESWC666SSDShh1/v2.0", //your path to aud
    'sub': "ea235345465fha675767a", //your path to sub
	'iat': currentTimestamp,
	'exp': currentTimestamp + 60,
	'jti': myUUID
}

var sPayload = JSON.stringify(data);

var header = {
	"x5t": "KL2+/t4645MMTrECfGTTho=", //Your header value
    "alg" : "RS256",
    "typ" : "JWT"
};

var sHeader = JSON.stringify(header);

//Your private key:
var privateKey = "-----BEGIN PRIVATE KEY-----\
GKRTyjdnjfgjmnf973456fthjTeh4jfikUYTGFFGFRFGRFGRE5576YYYY3334\
GKRTyjdnjfgjmnf973456fthjTeh4jfikUYTGFFGFRFGRFGRE5576YYYY3334\
GKRTyjdnjfgjmnf973456fthjTeh4jfikUYTGFFGFRFGRFGRE5576YYYY3334\
GKRTyjdnjfgjmnf973456fthjTeh4jfikUYTGFFGFRFGRFGRE5576YYYY3334\
GKRTyjdnjfgjmnf973456fthjTeh4jfikUYTGFFGFRFGRFGRE5576YYYY3334\
GKRTyjdnjfgjmnf973456fthjTeh4jfikUYTGFFGFRFGRFGRE5576YYYY3334\
GKRTyjdnjfgjmnf973456fthjTeh4jfikUYTGFFGFRFGRFGRE5576YYYY3334\
GKRTyjdnjfgjmnf973456fthjTeh4jfikUYTGFFGFRFGRFGRE5576YYYY3334\
GKRTyjdnjfgjmnf973456fthjTeh4jfikUYTGFFGFRFGRFGRE5576YYYY3334\
GKRTyjdnjfgjmnf973456fthjTeh4jfikUYTGFFGFRFGRFGRE5576YYYY3334\
GKRTyjdnjfgjmnf973456fthjTeh4jfikUYTGFFGFRFGRFGRE5576YYYY3334\
GKRTyjdnjfgjmnf973456fthjTeh4jfikUYTGFFGFRFGRFGRE5576YYYY3334\
GKRTyjdnjfgjmnf973456fthjTeh4jfikUYTGFFGFRFGRFGRE5576YYYY3334\
GKRTyjdnjfgjmnf973456fthjTeh4jfikUYTGFFGFRFGRFGRE5576YYYY3334\
CE=\
-----END PRIVATE KEY-----";

var token = JWS.sign(header.alg, sHeader, sPayload, privateKey);

var scope = "e376nfjgjf8874654756775hgjfg84e/.default"; //your scope, can be different for each application

const postRequest = {
  url: 'https://login.microsoftonline.com/e1234567890f/oauth2/v2.0/token', //your path to oauth2
  method: 'POST',
  timeout: 0,
  header: {
    "Content-Type": "application/x-www-form-urlencoded"
  },
  body: {
    mode: 'urlencoded',
    urlencoded: [
        {key:"grant_type", value: "client_credentials"},
        {key:"scope", value: scope},
        {key:"client_id", value: "e1234567890f"}, //your Client Id
        {key:"client_assertion_type", value: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"},
        {key:"client_assertion", value: token}
    ]}
};
pm.sendRequest(postRequest, function (err, res) {
    var responseJson = res.json();
    pm.environment.set('ACCESS_TOKEN', responseJson['access_token']);
});

After that, Pre request Script will generate and add authorization Bearer {{ACCESS_TOKEN}} to each request header.

jwt-kong-api-gateway-postman's People

Contributors

sergey-rubtsov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.