Last active
April 10, 2016 11:44
-
-
Save ziogaschr/bc4592d687e934f07244919765b3a051 to your computer and use it in GitHub Desktop.
JWT to JSON converter that can be used as a Slack Command at https://putsreq.com
This file contains 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
// Parse JWT and respond with a JSON | |
// Used for slack command | |
// | |
// Created by Chris Ziogas on 4/9/2016 | |
// | |
// For using it copy the following code in https://putsreq.com | |
// and then register a new Slack command | |
// polyfill for window.btoa and window.atob | |
!function(){function t(t){this.message=t}var r="undefined"!=typeof exports?exports:this,e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";t.prototype=new Error,t.prototype.name="InvalidCharacterError",r.btoa||(r.btoa=function(r){for(var o,n,a=String(r),i=0,c=e,d="";a.charAt(0|i)||(c="=",i%1);d+=c.charAt(63&o>>8-i%1*8)){if(n=a.charCodeAt(i+=.75),n>255)throw new t("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.");o=o<<8|n}return d}),r.atob||(r.atob=function(r){var o=String(r).replace(/=+$/,"");if(o.length%4==1)throw new t("'atob' failed: The string to be decoded is not correctly encoded.");for(var n,a,i=0,c=0,d="";a=o.charAt(c++);~a&&(n=i%4?64*n+a:a,i++%4)?d+=String.fromCharCode(255&n>>(-2*i&6)):0)a=e.indexOf(a);return d})}(); | |
function base64urlDecode(str) { | |
str += Array(5 - (str.length % 4 || 4)).join('='); | |
str = str.replace(/\-/g, '+').replace(/_/g, '/'); | |
return decodeURIComponent(escape(atob(str))); | |
} | |
// parse JWT from Slack command | |
var segments = request.params.text ? request.params.text.split('.') : []; | |
// All segment should be base64 | |
var headerSeg = segments[0]; | |
var payloadSeg = segments[1]; | |
var signatureSeg = segments[2]; | |
// base64 decode and parse JSON | |
var header = JSON.parse(base64urlDecode(headerSeg)) || undefined; | |
var payload = JSON.parse(base64urlDecode(payloadSeg)) || undefined; | |
var res = { | |
"header": header, | |
"payload": payload, | |
"signature": signatureSeg | |
}; | |
// respond with a JSON | |
response.headers["Content-Type"] = "application/json"; | |
response.body = { | |
// share with rest in channel | |
"response_type": "in_channel", | |
"text": JSON.stringify(res, null, 2), | |
"attachments": [ | |
{ | |
"title": "Check JWT at jwt.io", | |
"title_link": "https://jwt.io/?value=" + request.params.text, | |
"color": "#764FA5" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment