SlideShare a Scribd company logo
JWT! JWT!
Let it all out!
John SJ Anderson | @genehack | Hack Salem | 14 Sep 2016
Hack Salem | 14 Sep 2016 | @genehack 1
JSON Web Tokens
want to rule your world
John SJ Anderson | @genehack | Hack Salem | 14 Sep 2016
Hack Salem | 14 Sep 2016 | @genehack 2
Hi, I'm John
Hack Salem | 14 Sep 2016 | @genehack 3
VP, Tech
Infinity
Interactive
Hack Salem | 14 Sep 2016 | @genehack 4
So, what's a JWT?
Hack Salem | 14 Sep 2016 | @genehack 5
jwt.io
Hack Salem | 14 Sep 2016 | @genehack 6
What
Does
That
Even
MeanHack Salem | 14 Sep 2016 | @genehack 7
Think of it as…
• A lightweight alternative to cookies
• A form of access control without authentication
• Cross-site single sign on (SSO) without cross domain pain
Hack Salem | 14 Sep 2016 | @genehack 8
Made of stuff you already know
• JSON Objects
• Cryptographically signed and hashed
• Transmitted during HTTP request
Hack Salem | 14 Sep 2016 | @genehack 9
JWT teardown
• dot-delimited string ('.')
• 3 parts
• header
• payload
• signature
• Example: xxx.yyyyy.zzz
Hack Salem | 14 Sep 2016 | @genehack 10
JWT teardown: header
• Plain ole JSON object
• Base64Url encoded
• Typically indicates token type and hashing algorithm
{
"alg": "HS256",
"typ": "JWT"
}
Hack Salem | 14 Sep 2016 | @genehack 11
JWT teardown: payload
• Also plain ole JSON object
• Contains "claims" -- really just data
• Reserved, public, private
• Also Base64Url encoded
{
"name": "Hack Salem",
"admin": false
}
Hack Salem | 14 Sep 2016 | @genehack 12
JWT teardown: signature
• Encoded header, plus
• Encoded payload, plus
• A secret, plus
• Signing algorithm from header
HMACSHA256(
base64UrlEncode(header) + "." + base64UrlEncode(payload),
secret
)
Hack Salem | 14 Sep 2016 | @genehack 13
Putting it all together
Hack Salem | 14 Sep 2016 | @genehack 14
Putting it all together
Hack Salem | 14 Sep 2016 | @genehack 15
More advanced usage
• Encrypted payloads (JWE)
• Nested JWT
(Won't get any further into these tonight…)
Hack Salem | 14 Sep 2016 | @genehack 16
Libraries for DAYS
• .NET, Python, Node, Java, Javascript, Ruby, Perl, Go, PHP
• Haskell, Rust, Lua, Scala, Clojure, ObjectiveC, Swift, Delphi
• Support for your favorite language/platform is probably not
an issue
Hack Salem | 14 Sep 2016 | @genehack 17
OK,
you've
got
my
attentionHack Salem | 14 Sep 2016 | @genehack 18
How
Do
I
Use
It?Hack Salem | 14 Sep 2016 | @genehack 19
Basic auth/authz usage
(stolen from jwt.io)
Hack Salem | 14 Sep 2016 | @genehack 20
Things to be aware of
• Payload/header NOT encrypted (in this setup)
• …don't send anything sensitive!
• Need to control expiration, re-issue, etc.
• Some APIs will send a fresh JWT to the client per-request
• Sites other than issuing site can receive JWT
• …if they share the secret
Hack Salem | 14 Sep 2016 | @genehack 21
How is it actually transmitted?
• Up to you! Various methods:
• As part of the URL
• In the POST body
• In the Authorization header using bearer scheme:
Authorization: Bearer <token>
Hack Salem | 14 Sep 2016 | @genehack 22
Authorization without authentication
• Scenario:
• You have an API
• You don't want to make anybody authenticate to use it
• You don't want it wide open to the Internet either
• a/k/a authz without authn
Hack Salem | 14 Sep 2016 | @genehack 23
Solution: JWT with RSA keys
• Alternative to secret in previous scenario: RSA key-pair
• Can include the public key in the JWT header using JWK
• JSON Web Key, natch
• Allows API client to produce claims in a verifiable way
Hack Salem | 14 Sep 2016 | @genehack 24
To set it up:
• Give authorized user a RSA key-pair
• You can even let them generate it — you just need to:
• Record the fingerprint of the public key (important later!)
Hack Salem | 14 Sep 2016 | @genehack 25
On their side:
• They make JWT using the private key
• They include the public key in the header
• Include iat (issued-at) and exp (expires) claims
• Send JWT in Authorization header with API request
Hack Salem | 14 Sep 2016 | @genehack 26
On our side:
• Get the public key out of the JWT header
• Validate the JWT signature using the key
• Validate that public key fingerprint is white-listed
• Signature produced with private key
• Public key is white-listed
• Therefore we know JWT is valid!
Hack Salem | 14 Sep 2016 | @genehack 27
Things to be aware of:
• You still want to validate iat and exp and any other rules
• Your library should probably do that stuff for you, mostly
• Again, nothing is encrypted, so don't plan on sensitive stuff
in the payload or header
Hack Salem | 14 Sep 2016 | @genehack 28
Let's see some code!
• May look a bit strange
• New experimental language, Nacre
• Transpiles to Javascript
Hack Salem | 14 Sep 2016 | @genehack 29
Client side
my $pri_key = Crypt::PK::RSA->new('./key.pri');
my $pub_key = Crypt::PK::RSA->new('./key.pub');
my $token = encode_jwt(
alg => 'RS512',
extra_headers => {
jwk => $pub_key->export_key_jwk('public', 1),
nonce => undef ,
},
key => $pri_key ,
payload => { iat => time() },
relative_exp => 1800,
);
HTTP::Request->new(
'POST' => 'https://example.com/endpoint',
['Authorization' => "Bearer $token"],
encode_json({ request => 'body' })
);
Hack Salem | 14 Sep 2016 | @genehack 30
Client side: making the token
my $token = encode_jwt(
alg => 'RS512',
extra_headers => {
jwk => $pub_key->export_key_jwk('public', 1),
nonce => undef ,
},
key => $pri_key ,
payload => { iat => time() },
relative_exp => 1800,
);
Hack Salem | 14 Sep 2016 | @genehack 31
Client side: adding the public key
extra_headers => {
jwk => $pub_key->export_key_jwk('public', 1),
},
Key: find an RSA library that supports export to JWK format!
Hack Salem | 14 Sep 2016 | @genehack 32
BTW,
just
kidding
about
Nacre…Hack Salem | 14 Sep 2016 | @genehack 33
it's
Perl!
(of course)
Hack Salem | 14 Sep 2016 | @genehack 34
Server side
my $auth_header = request_header 'Authorization' ;
status_401 unless ( $token ) = $auth_header =~ /^Bearer (.*)$/;
# try to decode it and confirm valid sig,
# and valid iat and exp claims
my( $header, $payload );
try {
( $header, $payload ) = decode_jwt( token => $token , decode_header => 1 ,
accepted_alg => 'RS512' ,
verify_iat => 1 , verify_exp => 1 );
};
# no catch block, just drop the error, we're out of here in that case
status_401 unless $header and $payload;
# check that expiration time is less than one hour
status_401 unless $payload->{exp} - $payload->{iat} < 3600;
# check that the included public key is on the whitelist
my $pk = Crypt::PK::RSA->new;
$pk->import_key($header->{jwk});
my $thumbprint = $pk->export_key_jwk_thumbprint;
status_401 unless config->{whitelist}{$thumbprint};
# if we get here, we're all good!
...
Hack Salem | 14 Sep 2016 | @genehack 35
Server side: get the token
my $auth_header = request_header 'Authorization' ;
status_401 unless ( $token ) = $auth_header =~ /^Bearer (.*)$/;
Hack Salem | 14 Sep 2016 | @genehack 36
Server side: decode the token
# try to decode it and confirm valid sig,
# and valid iat and exp claims
my( $header, $payload );
try {
( $header, $payload ) = decode_jwt( token => $token , decode_header => 1 ,
accepted_alg => 'RS512' ,
verify_iat => 1 , verify_exp => 1 );
};
# no catch block, just drop the error, we're out of here in that case
status_401 unless $header and $payload;
Hack Salem | 14 Sep 2016 | @genehack 37
Server side: decode the token
• Key in header wrong? FAILS
• Not right algorithm? FAILS
• Doesn't have iat and exp? FAILS
ALL that validation is happening inside the library, so I don't
have to worry about it.
• Me? WINS
Hack Salem | 14 Sep 2016 | @genehack 38
Server side: do more checks
# check that expiration time is less than one hour
status_401 unless $payload->{exp} - $payload->{iat} < 3600;
# check that the included public key is on the whitelist
my $pk = Crypt::PK::RSA->new;
$pk->import_key($header->{jwk});
my $thumbprint = $pk->export_key_jwk_thumbprint;
status_401 unless config->{whitelist}{$thumbprint};
Hack Salem | 14 Sep 2016 | @genehack 39
Server side: more checks
• We specify in the API docs that tokens can only be valid for
one hour
• Have to check that ourselves
• Also need to make sure this isn't some random RSA keypair
• Need to make sure we know this public key
Hack Salem | 14 Sep 2016 | @genehack 40
Server side: THAT'S ALL FOLKS
# if we get here, we're all good!
• If we know the public key in the header,
• then we know the private key was used to sign the JWT
• (or it wouldn't validate)
• and therefore the JWT is from the private key holder
• (who is, by definition, authorized!)
Hack Salem | 14 Sep 2016 | @genehack 41
IMPORTANT NOTE!
This does, of course, depend on the client keeping the private
key actually private
…
But access revocation is as simple as removing a public
keyprint from the whitelist.
Hack Salem | 14 Sep 2016 | @genehack 42
Conclusions
• JWTs solve some really common problems.
• JWTs solve them in a pretty elegant way.
• This is really pretty damn cool.
Hack Salem | 14 Sep 2016 | @genehack 43
Conclusions
• JWTs solve some really common problems.
• JWTs solve them in a pretty elegant way.
• This is really pretty damn cool!!!
• You should think about using JWTs.
Hack Salem | 14 Sep 2016 | @genehack 44
Questions?
Hack Salem | 14 Sep 2016 | @genehack 45
Thanks!
Hack Salem | 14 Sep 2016 | @genehack 46

More Related Content

PDF
amani_rwc_password
PDF
Password Storage and Attacking in PHP
PDF
Distributed Search in Riak - Integrating Search in a NoSQL Database: Presente...
PDF
Logstash for SEO: come monitorare i Log del Web Server in realtime
PDF
Encryption: It's For More Than Just Passwords
PDF
Python Cryptography & Security
PPTX
PSGI and Plack from first principles
POT
Twentyten
amani_rwc_password
Password Storage and Attacking in PHP
Distributed Search in Riak - Integrating Search in a NoSQL Database: Presente...
Logstash for SEO: come monitorare i Log del Web Server in realtime
Encryption: It's For More Than Just Passwords
Python Cryptography & Security
PSGI and Plack from first principles
Twentyten

What's hot (20)

PDF
Kamailio - Surfing Big Waves Of SIP With Style
PDF
We-built-a-honeypot-and-p4wned-ransomware-developers-too
PPTX
I See You
PDF
Kamailio and VoIP Wild World
PDF
Code obfuscation, php shells & more
PDF
Eve - REST API for Humans™
PDF
Php web backdoor obfuscation
PDF
Cracking Salted Hashes
PPTX
Advanced Weapons Training for the Empire
PDF
JSON Web Tokens Will Improve Your Life
PDF
Cryptography in PHP: use cases
PDF
PHP Backdoor: The rise of the vuln
PDF
Gauntlt: Go Ahead, Be Mean to your Code
PDF
A Modest Introduction To Swift
PDF
Bitcoin Keys, Addresses & Wallets
PDF
Malicious Payloads vs Deep Visibility: A PowerShell Story
PDF
Revoke-Obfuscation
PDF
Don't Loose Sleep - Secure Your Rest - php[tek] 2017
KEY
Deploying Plack Web Applications: OSCON 2011
KEY
Plack at YAPC::NA 2010
Kamailio - Surfing Big Waves Of SIP With Style
We-built-a-honeypot-and-p4wned-ransomware-developers-too
I See You
Kamailio and VoIP Wild World
Code obfuscation, php shells & more
Eve - REST API for Humans™
Php web backdoor obfuscation
Cracking Salted Hashes
Advanced Weapons Training for the Empire
JSON Web Tokens Will Improve Your Life
Cryptography in PHP: use cases
PHP Backdoor: The rise of the vuln
Gauntlt: Go Ahead, Be Mean to your Code
A Modest Introduction To Swift
Bitcoin Keys, Addresses & Wallets
Malicious Payloads vs Deep Visibility: A PowerShell Story
Revoke-Obfuscation
Don't Loose Sleep - Secure Your Rest - php[tek] 2017
Deploying Plack Web Applications: OSCON 2011
Plack at YAPC::NA 2010
Ad

Similar to JWT! JWT! Let it all out! (20)

PDF
JSON Web Tokens Will Improve Your Life
KEY
plackdo, plack-like web interface on perl6
PDF
Safely Protect PostgreSQL Passwords - Tell Others to SCRAM
PPTX
Get Your Insecure PostgreSQL Passwords to SCRAM
KEY
PSGI/Plack OSDC.TW
PDF
6.2. Hacking most popular websites
PDF
Approach to find critical vulnerabilities
PDF
Обмен учетными данными между iOS 8 приложениями и вебом, Константин Чернухо, ...
ODP
Introduction To Encryption in Lasso 8.5
PDF
RoR Workshop - Web applications hacking - Ruby on Rails example
PDF
Workshop KrakYourNet2016 - Web applications hacking Ruby on Rails example
PDF
JSON Web Tokens Will Improve Your Life
PDF
Password (in)security
PDF
How to reverse engineer Android applications
PDF
How to reverse engineer Android applications—using a popular word game as an ...
PDF
Jwt == insecurity?
PDF
CIS14: I Left My JWT in San JOSE
PDF
JWT: jku x5u
PDF
Tatu: ssh as a service
PDF
Selenium sandwich-3: Being where you aren't.
JSON Web Tokens Will Improve Your Life
plackdo, plack-like web interface on perl6
Safely Protect PostgreSQL Passwords - Tell Others to SCRAM
Get Your Insecure PostgreSQL Passwords to SCRAM
PSGI/Plack OSDC.TW
6.2. Hacking most popular websites
Approach to find critical vulnerabilities
Обмен учетными данными между iOS 8 приложениями и вебом, Константин Чернухо, ...
Introduction To Encryption in Lasso 8.5
RoR Workshop - Web applications hacking - Ruby on Rails example
Workshop KrakYourNet2016 - Web applications hacking Ruby on Rails example
JSON Web Tokens Will Improve Your Life
Password (in)security
How to reverse engineer Android applications
How to reverse engineer Android applications—using a popular word game as an ...
Jwt == insecurity?
CIS14: I Left My JWT in San JOSE
JWT: jku x5u
Tatu: ssh as a service
Selenium sandwich-3: Being where you aren't.
Ad

More from John Anderson (20)

PDF
#speakerlife
PDF
Introduction to Git (even for non-developers)
PDF
Logs are-magic-devfestweekend2018
PDF
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
PDF
A static site generator should be your next language learning project
PDF
Do you want to be right or do you want to WIN?
PDF
An Introduction to Git (even for non-developers)
PDF
You got chocolate in my peanut butter! .NET on Mac & Linux
PDF
A static site generator should be your next language learning project
PDF
Old Dogs & New Tricks: What's New with Perl5 This Century
PDF
Introduction to Git (even for non-developers!)
PDF
Introduction to Git for Non-Developers
PDF
A static site generator should be your next language learning project
PDF
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
PDF
Old Dogs & New Tricks: What's New With Perl5 This Century
PDF
A Modest Introduction to Swift
PDF
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
PDF
Friends Don't Let Friends Browse Unencrypted: Running a VPN for friends and f...
PDF
A Modest Introduction To Swift
PDF
Logs Are Magic! Why git workflows & commit structure should matter to you
#speakerlife
Introduction to Git (even for non-developers)
Logs are-magic-devfestweekend2018
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
A static site generator should be your next language learning project
Do you want to be right or do you want to WIN?
An Introduction to Git (even for non-developers)
You got chocolate in my peanut butter! .NET on Mac & Linux
A static site generator should be your next language learning project
Old Dogs & New Tricks: What's New with Perl5 This Century
Introduction to Git (even for non-developers!)
Introduction to Git for Non-Developers
A static site generator should be your next language learning project
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
Old Dogs & New Tricks: What's New With Perl5 This Century
A Modest Introduction to Swift
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
Friends Don't Let Friends Browse Unencrypted: Running a VPN for friends and f...
A Modest Introduction To Swift
Logs Are Magic! Why git workflows & commit structure should matter to you

Recently uploaded (20)

PDF
Testing WebRTC applications at scale.pdf
PPTX
international classification of diseases ICD-10 review PPT.pptx
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
PPTX
innovation process that make everything different.pptx
PPTX
Introduction to Information and Communication Technology
PDF
The Internet -By the Numbers, Sri Lanka Edition
PPTX
presentation_pfe-universite-molay-seltan.pptx
PPTX
Internet___Basics___Styled_ presentation
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PPTX
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
DOCX
Unit-3 cyber security network security of internet system
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PDF
RPKI Status Update, presented by Makito Lay at IDNOG 10
PPTX
SAP Ariba Sourcing PPT for learning material
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
Testing WebRTC applications at scale.pdf
international classification of diseases ICD-10 review PPT.pptx
introduction about ICD -10 & ICD-11 ppt.pptx
INTERNET------BASICS-------UPDATED PPT PRESENTATION
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
Introuction about ICD -10 and ICD-11 PPT.pptx
Module 1 - Cyber Law and Ethics 101.pptx
innovation process that make everything different.pptx
Introduction to Information and Communication Technology
The Internet -By the Numbers, Sri Lanka Edition
presentation_pfe-universite-molay-seltan.pptx
Internet___Basics___Styled_ presentation
Job_Card_System_Styled_lorem_ipsum_.pptx
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
Slides PDF The World Game (s) Eco Economic Epochs.pdf
Unit-3 cyber security network security of internet system
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
RPKI Status Update, presented by Makito Lay at IDNOG 10
SAP Ariba Sourcing PPT for learning material
Tenda Login Guide: Access Your Router in 5 Easy Steps

JWT! JWT! Let it all out!

  • 1. JWT! JWT! Let it all out! John SJ Anderson | @genehack | Hack Salem | 14 Sep 2016 Hack Salem | 14 Sep 2016 | @genehack 1
  • 2. JSON Web Tokens want to rule your world John SJ Anderson | @genehack | Hack Salem | 14 Sep 2016 Hack Salem | 14 Sep 2016 | @genehack 2
  • 3. Hi, I'm John Hack Salem | 14 Sep 2016 | @genehack 3
  • 4. VP, Tech Infinity Interactive Hack Salem | 14 Sep 2016 | @genehack 4
  • 5. So, what's a JWT? Hack Salem | 14 Sep 2016 | @genehack 5
  • 6. jwt.io Hack Salem | 14 Sep 2016 | @genehack 6
  • 7. What Does That Even MeanHack Salem | 14 Sep 2016 | @genehack 7
  • 8. Think of it as… • A lightweight alternative to cookies • A form of access control without authentication • Cross-site single sign on (SSO) without cross domain pain Hack Salem | 14 Sep 2016 | @genehack 8
  • 9. Made of stuff you already know • JSON Objects • Cryptographically signed and hashed • Transmitted during HTTP request Hack Salem | 14 Sep 2016 | @genehack 9
  • 10. JWT teardown • dot-delimited string ('.') • 3 parts • header • payload • signature • Example: xxx.yyyyy.zzz Hack Salem | 14 Sep 2016 | @genehack 10
  • 11. JWT teardown: header • Plain ole JSON object • Base64Url encoded • Typically indicates token type and hashing algorithm { "alg": "HS256", "typ": "JWT" } Hack Salem | 14 Sep 2016 | @genehack 11
  • 12. JWT teardown: payload • Also plain ole JSON object • Contains "claims" -- really just data • Reserved, public, private • Also Base64Url encoded { "name": "Hack Salem", "admin": false } Hack Salem | 14 Sep 2016 | @genehack 12
  • 13. JWT teardown: signature • Encoded header, plus • Encoded payload, plus • A secret, plus • Signing algorithm from header HMACSHA256( base64UrlEncode(header) + "." + base64UrlEncode(payload), secret ) Hack Salem | 14 Sep 2016 | @genehack 13
  • 14. Putting it all together Hack Salem | 14 Sep 2016 | @genehack 14
  • 15. Putting it all together Hack Salem | 14 Sep 2016 | @genehack 15
  • 16. More advanced usage • Encrypted payloads (JWE) • Nested JWT (Won't get any further into these tonight…) Hack Salem | 14 Sep 2016 | @genehack 16
  • 17. Libraries for DAYS • .NET, Python, Node, Java, Javascript, Ruby, Perl, Go, PHP • Haskell, Rust, Lua, Scala, Clojure, ObjectiveC, Swift, Delphi • Support for your favorite language/platform is probably not an issue Hack Salem | 14 Sep 2016 | @genehack 17
  • 18. OK, you've got my attentionHack Salem | 14 Sep 2016 | @genehack 18
  • 19. How Do I Use It?Hack Salem | 14 Sep 2016 | @genehack 19
  • 20. Basic auth/authz usage (stolen from jwt.io) Hack Salem | 14 Sep 2016 | @genehack 20
  • 21. Things to be aware of • Payload/header NOT encrypted (in this setup) • …don't send anything sensitive! • Need to control expiration, re-issue, etc. • Some APIs will send a fresh JWT to the client per-request • Sites other than issuing site can receive JWT • …if they share the secret Hack Salem | 14 Sep 2016 | @genehack 21
  • 22. How is it actually transmitted? • Up to you! Various methods: • As part of the URL • In the POST body • In the Authorization header using bearer scheme: Authorization: Bearer <token> Hack Salem | 14 Sep 2016 | @genehack 22
  • 23. Authorization without authentication • Scenario: • You have an API • You don't want to make anybody authenticate to use it • You don't want it wide open to the Internet either • a/k/a authz without authn Hack Salem | 14 Sep 2016 | @genehack 23
  • 24. Solution: JWT with RSA keys • Alternative to secret in previous scenario: RSA key-pair • Can include the public key in the JWT header using JWK • JSON Web Key, natch • Allows API client to produce claims in a verifiable way Hack Salem | 14 Sep 2016 | @genehack 24
  • 25. To set it up: • Give authorized user a RSA key-pair • You can even let them generate it — you just need to: • Record the fingerprint of the public key (important later!) Hack Salem | 14 Sep 2016 | @genehack 25
  • 26. On their side: • They make JWT using the private key • They include the public key in the header • Include iat (issued-at) and exp (expires) claims • Send JWT in Authorization header with API request Hack Salem | 14 Sep 2016 | @genehack 26
  • 27. On our side: • Get the public key out of the JWT header • Validate the JWT signature using the key • Validate that public key fingerprint is white-listed • Signature produced with private key • Public key is white-listed • Therefore we know JWT is valid! Hack Salem | 14 Sep 2016 | @genehack 27
  • 28. Things to be aware of: • You still want to validate iat and exp and any other rules • Your library should probably do that stuff for you, mostly • Again, nothing is encrypted, so don't plan on sensitive stuff in the payload or header Hack Salem | 14 Sep 2016 | @genehack 28
  • 29. Let's see some code! • May look a bit strange • New experimental language, Nacre • Transpiles to Javascript Hack Salem | 14 Sep 2016 | @genehack 29
  • 30. Client side my $pri_key = Crypt::PK::RSA->new('./key.pri'); my $pub_key = Crypt::PK::RSA->new('./key.pub'); my $token = encode_jwt( alg => 'RS512', extra_headers => { jwk => $pub_key->export_key_jwk('public', 1), nonce => undef , }, key => $pri_key , payload => { iat => time() }, relative_exp => 1800, ); HTTP::Request->new( 'POST' => 'https://example.com/endpoint', ['Authorization' => "Bearer $token"], encode_json({ request => 'body' }) ); Hack Salem | 14 Sep 2016 | @genehack 30
  • 31. Client side: making the token my $token = encode_jwt( alg => 'RS512', extra_headers => { jwk => $pub_key->export_key_jwk('public', 1), nonce => undef , }, key => $pri_key , payload => { iat => time() }, relative_exp => 1800, ); Hack Salem | 14 Sep 2016 | @genehack 31
  • 32. Client side: adding the public key extra_headers => { jwk => $pub_key->export_key_jwk('public', 1), }, Key: find an RSA library that supports export to JWK format! Hack Salem | 14 Sep 2016 | @genehack 32
  • 33. BTW, just kidding about Nacre…Hack Salem | 14 Sep 2016 | @genehack 33
  • 34. it's Perl! (of course) Hack Salem | 14 Sep 2016 | @genehack 34
  • 35. Server side my $auth_header = request_header 'Authorization' ; status_401 unless ( $token ) = $auth_header =~ /^Bearer (.*)$/; # try to decode it and confirm valid sig, # and valid iat and exp claims my( $header, $payload ); try { ( $header, $payload ) = decode_jwt( token => $token , decode_header => 1 , accepted_alg => 'RS512' , verify_iat => 1 , verify_exp => 1 ); }; # no catch block, just drop the error, we're out of here in that case status_401 unless $header and $payload; # check that expiration time is less than one hour status_401 unless $payload->{exp} - $payload->{iat} < 3600; # check that the included public key is on the whitelist my $pk = Crypt::PK::RSA->new; $pk->import_key($header->{jwk}); my $thumbprint = $pk->export_key_jwk_thumbprint; status_401 unless config->{whitelist}{$thumbprint}; # if we get here, we're all good! ... Hack Salem | 14 Sep 2016 | @genehack 35
  • 36. Server side: get the token my $auth_header = request_header 'Authorization' ; status_401 unless ( $token ) = $auth_header =~ /^Bearer (.*)$/; Hack Salem | 14 Sep 2016 | @genehack 36
  • 37. Server side: decode the token # try to decode it and confirm valid sig, # and valid iat and exp claims my( $header, $payload ); try { ( $header, $payload ) = decode_jwt( token => $token , decode_header => 1 , accepted_alg => 'RS512' , verify_iat => 1 , verify_exp => 1 ); }; # no catch block, just drop the error, we're out of here in that case status_401 unless $header and $payload; Hack Salem | 14 Sep 2016 | @genehack 37
  • 38. Server side: decode the token • Key in header wrong? FAILS • Not right algorithm? FAILS • Doesn't have iat and exp? FAILS ALL that validation is happening inside the library, so I don't have to worry about it. • Me? WINS Hack Salem | 14 Sep 2016 | @genehack 38
  • 39. Server side: do more checks # check that expiration time is less than one hour status_401 unless $payload->{exp} - $payload->{iat} < 3600; # check that the included public key is on the whitelist my $pk = Crypt::PK::RSA->new; $pk->import_key($header->{jwk}); my $thumbprint = $pk->export_key_jwk_thumbprint; status_401 unless config->{whitelist}{$thumbprint}; Hack Salem | 14 Sep 2016 | @genehack 39
  • 40. Server side: more checks • We specify in the API docs that tokens can only be valid for one hour • Have to check that ourselves • Also need to make sure this isn't some random RSA keypair • Need to make sure we know this public key Hack Salem | 14 Sep 2016 | @genehack 40
  • 41. Server side: THAT'S ALL FOLKS # if we get here, we're all good! • If we know the public key in the header, • then we know the private key was used to sign the JWT • (or it wouldn't validate) • and therefore the JWT is from the private key holder • (who is, by definition, authorized!) Hack Salem | 14 Sep 2016 | @genehack 41
  • 42. IMPORTANT NOTE! This does, of course, depend on the client keeping the private key actually private … But access revocation is as simple as removing a public keyprint from the whitelist. Hack Salem | 14 Sep 2016 | @genehack 42
  • 43. Conclusions • JWTs solve some really common problems. • JWTs solve them in a pretty elegant way. • This is really pretty damn cool. Hack Salem | 14 Sep 2016 | @genehack 43
  • 44. Conclusions • JWTs solve some really common problems. • JWTs solve them in a pretty elegant way. • This is really pretty damn cool!!! • You should think about using JWTs. Hack Salem | 14 Sep 2016 | @genehack 44
  • 45. Questions? Hack Salem | 14 Sep 2016 | @genehack 45
  • 46. Thanks! Hack Salem | 14 Sep 2016 | @genehack 46