{"id":6309,"date":"2017-10-08T21:57:14","date_gmt":"2017-10-08T21:57:14","guid":{"rendered":"http:\/\/www.jitendrazaa.com\/blog\/?p=6309"},"modified":"2017-10-10T13:45:15","modified_gmt":"2017-10-10T13:45:15","slug":"using-jwt-flow-to-authenticate-nodejs-application-with-salesforce","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/using-jwt-flow-to-authenticate-nodejs-application-with-salesforce\/","title":{"rendered":"Using JWT Flow to Authenticate Nodejs application with Salesforce"},"content":{"rendered":"<p style=\"text-align: justify;\"><a href=\"https:\/\/help.salesforce.com\/articleView?id=remoteaccess_oauth_jwt_flow.htm&amp;type=0\">JWT OAuth<\/a> uses digital signature to authenticate external application with Salesforce. JWT can be used to request an OAuth access token from Salesforce when a client wants to use a previous authorization.<\/p>\n<p>Complete Source code is <a href=\"https:\/\/github.com\/JitendraZaa\/JWTDemo\">available here<\/a>.<\/p>\n<h5>How JWT OAuth works<\/h5>\n<ol>\n<li style=\"text-align: justify;\">Developer creates a connected app and provides digital certificate in OAuth settings. You can <a href=\"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/use-lightning-components-on-external-websites-lightning-out\/\">refer this post to understand how openssl can be used to create digital certificate locally<\/a>.<\/li>\n<li style=\"text-align: justify;\">We need to make sure this connected app is already pre &#8211; approved either by using WebServer, User Agent or any other flow. You can find this step in <a href=\"https:\/\/www.youtube.com\/watch?v=Iez9xdKbeuk\">video recorded<\/a>.<\/li>\n<li style=\"text-align: justify;\">We need to generate JWT token and sign it with certificate. This code snippet is available <a href=\"https:\/\/github.com\/JitendraZaa\/JWTDemo\/blob\/master\/Server.js\">in this file<\/a> in method\u00a0<em>getJWTSignedToken_nJWTLib().\u00a0<\/em>I have used\u00a0<strong>njwt\u00a0<\/strong>module of Nodejs to create a JWT token. <a href=\"https:\/\/jwt.io\/\">This is very useful website to validate and generate JWT token as per digital certificate<\/a>.<\/li>\n<li style=\"text-align: justify;\">Next we need to send JWT request to token URL &#8211;\u00a0 <em>https:\/\/login.salesforce.com\/services\/oauth2\/token<\/em><\/li>\n<li style=\"text-align: justify;\">If JWT request is valid then Salesforce returns access_token , which can be used in subsequent requests to perform allowed operations in Salesforce via Nodejs application.<\/li>\n<\/ol>\n<p><!--more--><\/p>\n<h5>How to run this on your system<\/h5>\n<p><strong>Step 1<\/strong><\/p>\n<p style=\"text-align: justify;\">Clone <a href=\"https:\/\/github.com\/JitendraZaa\/JWTDemo\">this repository<\/a> (Assuming Node.js already installed on system).<\/p>\n<p><strong>Step 2<\/strong><\/p>\n<p style=\"text-align: justify;\">Create a ssl certificate or reuse same certificate uploaded in this repository. <a href=\"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/use-lightning-components-on-external-websites-lightning-out\/\">Refer this post to learn how to create ssl certificate using openssl<\/a>.<\/p>\n<p><strong>Step 3<\/strong><\/p>\n<p style=\"text-align: justify;\">Create Connected App in your Salesforce instance with callback URL &#8211; <em>https:\/\/localhost:8081\/oauthcallback.html<\/em>. Make sure to upload <a href=\"https:\/\/github.com\/JitendraZaa\/JWTDemo\/blob\/master\/server.crt\">server.crt<\/a> as a digital certificate in connected app. You can use your own certificate as well.<\/p>\n<p><strong>Step 4<\/strong><\/p>\n<p style=\"text-align: justify;\">Copy consumer key &amp; secret created in connected app and update jwt_consumer_key and client_secret variable defined in <a href=\"https:\/\/github.com\/JitendraZaa\/JWTDemo\/blob\/master\/Server.js\">Server.js <\/a>file.<\/p>\n<p><strong>Step 5<\/strong><\/p>\n<p style=\"text-align: justify;\">Run <em>npm install<\/em> command in the directory where this code is downloaded. It will download all the required node modules. Then run <em>npm start<\/em>, or <em>nodemon Server.js<\/em> (if nodemon installed previously) it will start the server.<\/p>\n<p><strong>Step 6<\/strong><\/p>\n<p style=\"text-align: justify;\">Navigate to <em>https:\/\/localhost:8081\/<\/em> in your browser and you would see option for all 3 Auth flow &#8211;<strong> JWT, User Agent and Web Server<\/strong>.<\/p>\n<p><iframe loading=\"lazy\" title=\"Demo and source code of JWT , Webserver and User agent OAuth flow in Salesforce\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/Iez9xdKbeuk?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Video tutorial and Complete source code to use Salesforce JWT OAuth to authenticate Nodejs Application<\/p>\n","protected":false},"author":1,"featured_media":6312,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9],"tags":[428,267,260,392,212],"class_list":["post-6309","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","tag-jwt","tag-nodejs","tag-oauth","tag-openssl","tag-video-tutorial"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/10\/JWT-OAuth.png?fit=862%2C604&ssl=1","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":5987,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/use-lightning-components-on-external-websites-lightning-out\/","url_meta":{"origin":6309,"position":0},"title":"Use Lightning Components on external websites &#8211; Lightning Out","author":"Jitendra","date":"April 5, 2017","format":false,"excerpt":"How to use Lightning component in node.js (External websites) by using Lightning Out. It shows how to enable CORS and SSL in Node.js with Video and complete source code.","rel":"","context":"In &quot;Lightning&quot;","block_context":{"text":"Lightning","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/lightning\/"},"img":{"alt_text":"Show Lightning Component in Node.js using Lightning Out","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/04\/Show-Lightning-Component-in-Node.js-using-Lightning-Out.png?fit=1162%2C637&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/04\/Show-Lightning-Component-in-Node.js-using-Lightning-Out.png?fit=1162%2C637&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/04\/Show-Lightning-Component-in-Node.js-using-Lightning-Out.png?fit=1162%2C637&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/04\/Show-Lightning-Component-in-Node.js-using-Lightning-Out.png?fit=1162%2C637&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/04\/Show-Lightning-Component-in-Node.js-using-Lightning-Out.png?fit=1162%2C637&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":31310,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-mcp-server-for-claude-code-mcp-clients-setup\/","url_meta":{"origin":6309,"position":1},"title":"Salesforce MCP Server for Claude Code &amp; MCP Clients: Setup Guide 2026","author":"Jitendra Zaa","date":"January 30, 2026","format":false,"excerpt":"Master Salesforce MCP Server setup for Claude Code and other MCP clients. Step-by-step guide covering DX MCP Server, OAuth setup, hosted servers, and mcp-remote configuration.","rel":"","context":"In &quot;AI&quot;","block_context":{"text":"AI","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/ai\/"},"img":{"alt_text":"Salesforce MCP Server for Claude Code & MCP Clients - Complete Developer Guide 2026","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2026\/01\/salesforce-mcp-server-claude-code-guide-featured.png?fit=1200%2C675&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2026\/01\/salesforce-mcp-server-claude-code-guide-featured.png?fit=1200%2C675&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2026\/01\/salesforce-mcp-server-claude-code-guide-featured.png?fit=1200%2C675&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2026\/01\/salesforce-mcp-server-claude-code-guide-featured.png?fit=1200%2C675&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2026\/01\/salesforce-mcp-server-claude-code-guide-featured.png?fit=1200%2C675&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":7147,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-integration-with-nodejs-based-applications-using-canvas\/","url_meta":{"origin":6309,"position":2},"title":"Salesforce Integration with Nodejs based applications using Canvas","author":"Jitendra","date":"July 20, 2020","format":false,"excerpt":"How to use Canvas Signed Request Authentication with web based applications like Nodejs & how to use Canvas LifeCycle Handler Class","rel":"","context":"In &quot;Integration&quot;","block_context":{"text":"Integration","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/integration\/"},"img":{"alt_text":"Salesforce Canvas Integration","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/07\/Salesforce-Canvas-Integration-e1624579092898.png?fit=700%2C567&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/07\/Salesforce-Canvas-Integration-e1624579092898.png?fit=700%2C567&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/07\/Salesforce-Canvas-Integration-e1624579092898.png?fit=700%2C567&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/07\/Salesforce-Canvas-Integration-e1624579092898.png?fit=700%2C567&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":6951,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/mastering-visual-studio-and-salesforce-dx\/","url_meta":{"origin":6309,"position":3},"title":"Mastering Visual Studio and Salesforce DX","author":"Jitendra","date":"January 14, 2020","format":false,"excerpt":"3.5 hours of video to become Master on using and navigating Visual Studio, Need of scratch org and building modular application using Unlocked Packages","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce DX on Udemy","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/01\/Salesforce-DX-on-Udemy.png?fit=1200%2C693&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/01\/Salesforce-DX-on-Udemy.png?fit=1200%2C693&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/01\/Salesforce-DX-on-Udemy.png?fit=1200%2C693&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/01\/Salesforce-DX-on-Udemy.png?fit=1200%2C693&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/01\/Salesforce-DX-on-Udemy.png?fit=1200%2C693&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3951,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/creating-salesforce-query-builder-in-node-js-and-angularjs-using-salesforce-rest-api-and-oauth2\/","url_meta":{"origin":6309,"position":4},"title":"Creating Salesforce Query Builder in Node.js and AngularJs using Salesforce REST API and OAuth2 with Heroku Button","author":"Jitendra","date":"August 18, 2014","format":false,"excerpt":"In this post, we will be creating a\u00a0Salesforce Query (SOQL) Builder.\u00a0It uses Google's Angularjs MVC Javascript library and REST API provided by Salesforce. For Authentication, it uses OAuth2. Node.js is used as a primary language and hosted on Heroku. If you want to directly deploy to your Heroku Account, You\u2026","rel":"","context":"In &quot;HTML&quot;","block_context":{"text":"HTML","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/webtech\/web\/"},"img":{"alt_text":"SOQL Builder Using Node.js AngularJs Salesforce REST API on Heroku","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/08\/SOQL-Builder-Using-Node.js-AngularJs-Salesforce-REST-API-on-Heroku-1024x504.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/08\/SOQL-Builder-Using-Node.js-AngularJs-Salesforce-REST-API-on-Heroku-1024x504.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/08\/SOQL-Builder-Using-Node.js-AngularJs-Salesforce-REST-API-on-Heroku-1024x504.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3150,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/merge-pdf-in-salesforce-using-java-itextpdf-and-oauth-2\/","url_meta":{"origin":6309,"position":5},"title":"Merge PDF in Salesforce Using Java, ITextPDF and OAuth 2","author":"Jitendra","date":"December 9, 2012","format":false,"excerpt":"Its long time, since i wrote any article because of my busy schedule However this time i came with advance one. In this article we are going to use the J2EE (Servlet) to Merge PDF attachment inside salesforce with the help of OAuth and ITextPDF jar file. The reason of\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Create Remote Access in Salesforce.com for OAuth 2","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/12\/Create-Remote-Access-in-Salesforce.com_.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/12\/Create-Remote-Access-in-Salesforce.com_.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/12\/Create-Remote-Access-in-Salesforce.com_.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/6309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=6309"}],"version-history":[{"count":4,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/6309\/revisions"}],"predecessor-version":[{"id":6314,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/6309\/revisions\/6314"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media\/6312"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=6309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=6309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=6309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}