Created
August 9, 2022 18:21
-
-
Save harsh4870/2b636f9a16eb3c4144cf74067bc292a6 to your computer and use it in GitHub Desktop.
JWT assertion Node JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var jwt = require('jsonwebtoken'); | |
const fs = require('fs'); | |
var request = require("request"); | |
var querystring = require('querystring'); | |
var privateKey = fs.readFileSync('./private_key.pem'); | |
#idcs or keycloak URL | |
var url = "https://example.com/oauth2/v1/token" | |
var headers = { | |
'Authorization': 'Basic <BASE-64 ENCODED client ID & Secret>', | |
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' | |
}; | |
var token = jwt.sign({ | |
"sub": "ADMIN", | |
"jti": "8c7df446-bfae-40be-be09-0ab55c655436", | |
"iat": 1589889699, | |
"exp": 1691579765, | |
"iss": "<Issue or client ID>", | |
"aud": "https://identity.oraclecloud.com/" | |
}, privateKey, { algorithm: 'RS256', header: { | |
"alg": "RS256", | |
"typ": "JWT", | |
"kid": "assert" | |
} }); | |
var data = querystring.stringify({ | |
grant_type : 'urn:ietf:params:oauth:grant-type:jwt-bearer', | |
assertion : token, | |
scope: 'rest-api' | |
}); | |
request.post({headers: headers, url: url, body: data, method: 'POST'}, function(error, response, body) { | |
if (!error && response.statusCode == 200) { | |
console.log("Sucess", response.statusCode); | |
console.log("Body", response.body); | |
}else{ | |
console.log("Failed", response.statusCode); | |
console.log("Error", error) | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment