SlideShare a Scribd company logo
1 of 59
Download to read offline
Securing Web Applications
with Token Authentication
Micah Silverman @afitnerd
Co-Author, Mastering Enterprise JavaBeans 3.0
Java Developer Evangelist, Stormpath
About Stormpath
• Authentication & User Management API
• Hosted data store w/ advanced crypto
• Centralize user login across your applications
• Multi-tenant support for your SaaS
• Active Directory, LDAP, social connections
• API authentication & token authentication
• Supported, Free tier for developers
Overview
• Security Concerns for Modern Web Apps
• Cookies: need to know
• Session ID Problems
• Token Authentication to the rescue!
• OAuth2 & Java Example
Security Concerns for Modern Web Apps
• SPAs and Mobile apps are ‘Untrusted Clients’
• Prevent malicious code
• Secure user credentials
• Secure server endpoints (API)
• Expose Access Control rules to the Client
Prevent Malicious Code
• Cross-Site Scripting (XSS) attacks are a real,
huge threat
Prevent Malicious Code
Cross-Site Scripting (XSS) attacks
https://www.owasp.org/index.php/XSS
XSS Attack
Demo
https://www.google.com/about/appsecurity/
learning/xss/#BasicExample
XSS Attack – What Can I Do?
Read EVERYTHING on this page:
https://www.owasp.org/index.php/XSS
And then do these things:
https://www.owasp.org/index.php/
XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
XSS Attack – What Can I Do?
Escape Content!
Dynamic HTML: use well-known, trusted
libraries. Do NOT roll your own.
DOM attacks: escape user input
XSS Attack – What Can I Do?
SPAs: frameworks like Angular probably do a lot
of work for you (e.g. preventing DOM attacks by
escaping user input).
You should still read up on it.
Secure User Credentials
• Traditionally, we have used Session IDs
• This is OK, as long as you do cookies ‘right’
• Authentication Tokens are better ☺ (more
on this later)
Overview
• Security Concerns for Modern Web Apps
• Cookies: need to know
• Session ID Problems
• Token Authentication to the rescue!
• Java Example
Session ID Cookies
Secure Server (API) Endpoints
• Traditionally use Session ID Cookies
• Session ID à Session à User identity
• Use framework like Apache Shiro or Spring
Security to assert security rules
Expose Access Control Rules to the Client
• Traditional solution:
• Session ID à Session à User data in your DB
• Provide a /me or /profile endpoint
• Access Tokens are better!
Let’s talk about cookies...
Cookies are OK! If you do them correctly
Cookies can be easily compromised:
• Man-in-the-Middle (MITM) attacks
• Cross-Site Request Forgery (CSRF)
Someone ‘listening on the wire’ between the
browser and server can see and copy the
cookie.
Solutions
• Use HTTPS everywhere
• TLS everywhere on internal networks
Cross-Site Request Forgery (CSRF)
"... occurs when a malicious web site, email,
blog, instant message or program causes a
user’s web browser to perform an unwanted
action on a trusted site for which the user is
currently authenticated"

https://www.owasp.org/index.php/
CrossSite_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet
Cross-Site Request Forgery (CSRF)
Attacker enables a user to request your server.
Example:

<a href="https://yoursite.com/
transferMoney?to=BadGuy&amount=10000">
See Cute Cats!
</a>
What happens?
Cross-Site Request Forgery (CSRF)
• The attacker cannot see your cookie values, BUT:
• The browser says, "The request is going to your
server, so I’ll happily send you your cookies."
• Your server transfers the money because it ‘sees’ a
valid, non-expired session id cookie for an
authenticated session.
Cross-Site Request Forgery (CSRF)
Solutions
• Synchronizer Token
• Double-Submit Cookie
• Origin header check
Synchronizer Token – Trusted Page
Synchronizer Token – Foreign Page
Synchronizer Token - Considerations
• Requires cooperation from your rendering
layer
• Requires you to store tokens in a data store
or cache
• Difficult to do with static SPA content
• Only protects against forged POST requests,
not GET requests!
Pro tip: never allow GETs to modify server state!
Double Submit Cookie
• Send two cookies: Session ID + Random
Value
• Send random value explicitly, browser Same-
Origin-Policy
• Best Way: send as a custom header
Double Submit Cookie
Double Submit Cookie Considerations
• Custom HTTP header, do what makes sense
for your app
• Still vulnerable to XSS - Random Value still
accessible to the JS environment.
• Protect against XSS!
Origin header check
• Browsers send Origin header
• Header value is the domain of the page
initiating the request
• Cannot be hacked via browser JS 

(could still be modified by a malicious HTTP proxy
server)
Overview
• Security Concerns for Modern Web Apps
• Cookies: need to know
• Session ID Problems
• Token Authentication to the rescue!
• Java Example
Session ID Problems
• They’re opaque and have no meaning
themselves (they’re just ‘pointers’).
• Service-oriented architectures might need a
centralized ID de-referencing service
Session ID Problems
• Opaque IDs mean clients can’t inspect them
and find out what it is allowed to do or not - it
needs to make more requests for this
information.
Session ID Problems
• Sessions = Server State!
• You need to store that state somewhere
• Session ID à look up server state on *every
request*.
• Really not good for distributed/clustered apps
• Really not good for scale
Overview
• Security Concerns for Modern Web Apps
• Cookies: need to know
• Session ID Problems
• Token Authentication to the rescue!
• Java Example
Token Authentication
• What is Authentication?
• What is a Token?
JSON Web Tokens (JWT)
• A URL-safe, compact, self-contained string with
meaningful information that is usually digitally
signed or encrypted.
• The string is ‘opaque’ and can be used as a
‘token’.
• Many OAuth2 implementations use JWTs as
OAuth2 Access Tokens.
JSON Web Tokens (JWT)
• You can store them in cookies! But all those
cookie rules still apply.
• You can entirely replace your session ID with
a JWT.
JSON Web Tokens (JWT)
In the wild they look like just another ugly string:
eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9.eyJ	
  
pc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQo	
  
gImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnV	
  
lfQ.dBjftJeZ4CVPmB92K27uhbUJU1p1r_wW1gFWFOEj	
  
Xk
JSON Web Tokens (JWT)
In the wild they look like just another ugly string:
eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9.eyJ	
  
pc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQo	
  
gImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnV	
  
lfQ.dBjftJeZ4CVPmB92K27uhbUJU1p1r_wW1gFWFOEj	
  
Xk
JSON Web Tokens (JWT)
In the wild they look like just another ugly string:
eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9.eyJ	
  
pc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQo	
  
gImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnV	
  
lfQ.dBjftJeZ4CVPmB92K27uhbUJU1p1r_wW1gFWFOEj	
  
Xk
JSON Web Tokens (JWT)
But they do have a three part structure. Each
part is a Base64-encoded string:
eyJ0eXAiOiJKV1QiLA0KICJhb	
  
GciOiJIUzI1NiJ9	
  
.	
  
eyJpc3MiOiJqb2UiLA0KICJle	
  
HAiOjEzMDA4MTkzODAsDQogIm	
  
h0dHA6Ly9leGFtcGxlLmNvbS9	
  
pc19yb290Ijp0cnVlfQ	
  
.	
  
dBjftJeZ4CVPmB92K27uhbUJU	
  
1p1r_wW1gFWFOEjXk
Header
Body	
  (‘Claims’)
Cryptographic	
  Signature
JSON Web Tokens (JWT)
Base64-decode the parts to find the juicy bits:
{	
  
	
  "typ":"JWT",	
  
	
  "alg":"HS256"	
  
}
{	
  
	
  "iss":"http://trustyapp.com/",	
  
	
  "exp":	
  1300819380,	
  
	
  "sub":	
  "users/8983462",	
  
	
  "scope":	
  "self	
  api/buy"	
  
}
tß´—™à%O˜v+nî…SZu¯ˉµ€U…8H×
Header
Body	
  (‘Claims’)
Cryptographic	
  Signature
JSON Web Tokens (JWT)
The claims body is the best part! It can tell:
{	
  
	
  "iss":"http://trustyapp.com/",	
  
	
  "exp":	
  1300819380,	
  
	
  "sub":	
  "users/8983462",	
  
	
  "scope":	
  "self	
  api/buy"	
  
}
Who	
  issued	
  the	
  token
JSON Web Tokens (JWT)
The claims body is the best part! It can tell:
{	
  
	
  "iss":"http://trustyapp.com/",	
  
	
  "exp":	
  1300819380,	
  
	
  "sub":	
  "users/8983462",	
  
	
  "scope":	
  "self	
  api/buy"	
  
}
Who	
  issued	
  the	
  token
When	
  it	
  expires
JSON Web Tokens (JWT)
The claims body is the best part! It can tell:
{	
  
	
  "iss":"http://trustyapp.com/",	
  
	
  "exp":	
  1300819380,	
  
	
  "sub":	
  "users/8983462",	
  
	
  "scope":	
  "self	
  api/buy"	
  
}
Who	
  issued	
  the	
  token
When	
  it	
  expires
Who	
  it	
  represents
JSON Web Tokens (JWT)
The claims body is the best part! It can tell:
{	
  
	
  "iss":"http://trustyapp.com/",	
  
	
  "exp":	
  1300819380,	
  
	
  "sub":	
  "users/8983462",	
  
	
  "scope":	
  "self	
  api/buy"	
  
}
Who	
  issued	
  the	
  token
When	
  it	
  expires
Who	
  it	
  represents
What	
  they	
  can	
  do
JSON Web Tokens (JWT)
Great! Why is this useful?

• Implicitly trusted because it is cryptographically
signed (verified not tampered).
• It is structured, enabling inter-op between services
• It can inform your client about basic access control
rules (permissions)*
• And the big one: statelessness!
*servers must always enforce access control policies
JSON Web Tokens (JWT)
So, what’s the catch?

• Implicit trust is a tradeoff – how long should
the token be good for? how will you revoke it?
(Another talk: refresh tokens)
• You still have to secure your cookies!
• You have to be mindful of what you store in
the JWT if they are not encrypted. No
sensitive info!
How do you do it on the JVM?
JJWT is awesome
https://github.com/jwtk/jjwt
How do you do it on the JVM?
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
byte[] key = getSignatureKey();
String jwt =
Jwts.builder().setIssuer("http://trustyapp.com/")
.setSubject("users/1300819380")
.setExpiration(expirationDate)
.put("scope", "self api/buy")
.signWith(SignatureAlgorithm.HS256,key)
.compact();
Create	
  a	
  JWT:
How do you do it on the JVM?
Verify	
  a	
  JWT:
try {
Jws<Claims> jwtClaims =
Jwts.parser().setSigningKey(key).parseClaimsJws(jwt);
//OK, we can trust this JWT
} catch (SignatureException e) {
//don't trust the JWT!
}
How do you get a Token?
Example: your SPA, your server
1. Token Request
POST /oauth/token HTTP/1.1
Origin: https://foo.com
Content-Type: application/x-www-form-urlencoded
grant_type=password&username=username&password=p
assword
*Assert	
  allowed	
  origin	
  for	
  browser-­‐based	
  apps
2. Token Response
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Cache-Control: no-store
Pragma: no-cache
{
"access_token":"2YotnFZFEjr1zCsicMWpAA...",
"token_type":"example",
"expires_in":3600,
"refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA...",
"example_parameter":"example_value"
}
3. Resource Request
GET /admin HTTP/1.1
Authorization: Bearer 2YotnFZFEjr1zCsicMW...
Example: Token Request using an API Key
POST /token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials&client_id=apiKeyId&clie
nt_secret=apiKeySecret
*Assert	
  allowed	
  origin	
  for	
  browser-­‐based	
  apps
Demo!
Thanks!
@afitnerd @goStormpath
• Token Authentication for Java, JEE, Spring and Spring Boot
• Free Supported Developer Tier
• Elegant API
• OSS Java SDKs + Tutorials
Get a Free-Forever Account: Stormpath.com

More Related Content

What's hot

Single-Page-Application & REST security
Single-Page-Application & REST securitySingle-Page-Application & REST security
Single-Page-Application & REST security
Igor Bossenko
 
Mohanraj - Securing Your Web Api With OAuth
Mohanraj - Securing Your Web Api With OAuthMohanraj - Securing Your Web Api With OAuth
Mohanraj - Securing Your Web Api With OAuth
fossmy
 
Rest Security with JAX-RS
Rest Security with JAX-RSRest Security with JAX-RS
Rest Security with JAX-RS
Frank Kim
 

What's hot (20)

Secure Your REST API (The Right Way)
Secure Your REST API (The Right Way)Secure Your REST API (The Right Way)
Secure Your REST API (The Right Way)
 
Build A Killer Client For Your REST+JSON API
Build A Killer Client For Your REST+JSON APIBuild A Killer Client For Your REST+JSON API
Build A Killer Client For Your REST+JSON API
 
Mobile Authentication for iOS Applications - Stormpath 101
Mobile Authentication for iOS Applications - Stormpath 101Mobile Authentication for iOS Applications - Stormpath 101
Mobile Authentication for iOS Applications - Stormpath 101
 
REST API Security: OAuth 2.0, JWTs, and More!
REST API Security: OAuth 2.0, JWTs, and More!REST API Security: OAuth 2.0, JWTs, and More!
REST API Security: OAuth 2.0, JWTs, and More!
 
JWTs for CSRF and Microservices
JWTs for CSRF and MicroservicesJWTs for CSRF and Microservices
JWTs for CSRF and Microservices
 
Super simple application security with Apache Shiro
Super simple application security with Apache ShiroSuper simple application security with Apache Shiro
Super simple application security with Apache Shiro
 
Single-Page-Application & REST security
Single-Page-Application & REST securitySingle-Page-Application & REST security
Single-Page-Application & REST security
 
JavaOne 2014 - Securing RESTful Resources with OAuth2
JavaOne 2014 - Securing RESTful Resources with OAuth2JavaOne 2014 - Securing RESTful Resources with OAuth2
JavaOne 2014 - Securing RESTful Resources with OAuth2
 
API Security & Federation Patterns - Francois Lascelles, Chief Architect, Lay...
API Security & Federation Patterns - Francois Lascelles, Chief Architect, Lay...API Security & Federation Patterns - Francois Lascelles, Chief Architect, Lay...
API Security & Federation Patterns - Francois Lascelles, Chief Architect, Lay...
 
Best Practices in Building an API Security Ecosystem
Best Practices in Building an API Security EcosystemBest Practices in Building an API Security Ecosystem
Best Practices in Building an API Security Ecosystem
 
Rest API Security
Rest API SecurityRest API Security
Rest API Security
 
ConFoo 2015 - Securing RESTful resources with OAuth2
ConFoo 2015 - Securing RESTful resources with OAuth2ConFoo 2015 - Securing RESTful resources with OAuth2
ConFoo 2015 - Securing RESTful resources with OAuth2
 
Mohanraj - Securing Your Web Api With OAuth
Mohanraj - Securing Your Web Api With OAuthMohanraj - Securing Your Web Api With OAuth
Mohanraj - Securing Your Web Api With OAuth
 
Intro to Apache Shiro
Intro to Apache ShiroIntro to Apache Shiro
Intro to Apache Shiro
 
REST Service Authetication with TLS & JWTs
REST Service Authetication with TLS & JWTsREST Service Authetication with TLS & JWTs
REST Service Authetication with TLS & JWTs
 
ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra
ApacheCon 2014: Infinite Session Clustering with Apache Shiro & CassandraApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra
ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra
 
Rest Security with JAX-RS
Rest Security with JAX-RSRest Security with JAX-RS
Rest Security with JAX-RS
 
Securty Testing For RESTful Applications
Securty Testing For RESTful ApplicationsSecurty Testing For RESTful Applications
Securty Testing For RESTful Applications
 
Securing RESTful APIs using OAuth 2 and OpenID Connect
Securing RESTful APIs using OAuth 2 and OpenID ConnectSecuring RESTful APIs using OAuth 2 and OpenID Connect
Securing RESTful APIs using OAuth 2 and OpenID Connect
 
Multi-Tenancy with Spring Boot
Multi-Tenancy with Spring Boot Multi-Tenancy with Spring Boot
Multi-Tenancy with Spring Boot
 

Viewers also liked

Viewers also liked (18)

Micro Web Service - Slim and JWT
Micro Web Service - Slim and JWTMicro Web Service - Slim and JWT
Micro Web Service - Slim and JWT
 
Getting Started With Angular
Getting Started With AngularGetting Started With Angular
Getting Started With Angular
 
Instant Security & Scalable User Management with Spring Boot
Instant Security & Scalable User Management with Spring BootInstant Security & Scalable User Management with Spring Boot
Instant Security & Scalable User Management with Spring Boot
 
Storing User Files with Express, Stormpath, and Amazon S3
Storing User Files with Express, Stormpath, and Amazon S3Storing User Files with Express, Stormpath, and Amazon S3
Storing User Files with Express, Stormpath, and Amazon S3
 
Building Beautiful REST APIs in ASP.NET Core
Building Beautiful REST APIs in ASP.NET CoreBuilding Beautiful REST APIs in ASP.NET Core
Building Beautiful REST APIs in ASP.NET Core
 
Custom Data Search with Stormpath
Custom Data Search with StormpathCustom Data Search with Stormpath
Custom Data Search with Stormpath
 
Stormpath 101: Spring Boot + Spring Security
Stormpath 101: Spring Boot + Spring SecurityStormpath 101: Spring Boot + Spring Security
Stormpath 101: Spring Boot + Spring Security
 
Spring Boot Authentication...and More!
Spring Boot Authentication...and More! Spring Boot Authentication...and More!
Spring Boot Authentication...and More!
 
JWTs in Java for CSRF and Microservices
JWTs in Java for CSRF and MicroservicesJWTs in Java for CSRF and Microservices
JWTs in Java for CSRF and Microservices
 
Beautiful REST+JSON APIs with Ion
Beautiful REST+JSON APIs with IonBeautiful REST+JSON APIs with Ion
Beautiful REST+JSON APIs with Ion
 
Build a REST API for your Mobile Apps using Node.js
Build a REST API for your Mobile Apps using Node.jsBuild a REST API for your Mobile Apps using Node.js
Build a REST API for your Mobile Apps using Node.js
 
Building Beautiful REST APIs with ASP.NET Core
Building Beautiful REST APIs with ASP.NET CoreBuilding Beautiful REST APIs with ASP.NET Core
Building Beautiful REST APIs with ASP.NET Core
 
Browser Security
Browser SecurityBrowser Security
Browser Security
 
Web 2.0 - From a Social to a Service Web
Web 2.0 - From a Social to a Service WebWeb 2.0 - From a Social to a Service Web
Web 2.0 - From a Social to a Service Web
 
Java & JWT Stateless authentication
Java & JWT Stateless authenticationJava & JWT Stateless authentication
Java & JWT Stateless authentication
 
Build a Node.js Client for Your REST+JSON API
Build a Node.js Client for Your REST+JSON APIBuild a Node.js Client for Your REST+JSON API
Build a Node.js Client for Your REST+JSON API
 
So long scrum, hello kanban
So long scrum, hello kanbanSo long scrum, hello kanban
So long scrum, hello kanban
 
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram VaswaniCreating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
 

Similar to Securing Web Applications with Token Authentication

OWASP Top 10 Security Vulnerabilities, and Securing them with Oracle ADF
OWASP Top 10 Security Vulnerabilities, and Securing them with Oracle ADFOWASP Top 10 Security Vulnerabilities, and Securing them with Oracle ADF
OWASP Top 10 Security Vulnerabilities, and Securing them with Oracle ADF
Brian Huff
 
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Michael Pirnat
 
Mr. Mohammed Aldoub - A case study of django web applications that are secur...
Mr. Mohammed Aldoub  - A case study of django web applications that are secur...Mr. Mohammed Aldoub  - A case study of django web applications that are secur...
Mr. Mohammed Aldoub - A case study of django web applications that are secur...
nooralmousa
 

Similar to Securing Web Applications with Token Authentication (20)

Building Secure User Interfaces With JWTs (JSON Web Tokens)
Building Secure User Interfaces With JWTs (JSON Web Tokens)Building Secure User Interfaces With JWTs (JSON Web Tokens)
Building Secure User Interfaces With JWTs (JSON Web Tokens)
 
Building Secure User Interfaces With JWTs
Building Secure User Interfaces With JWTsBuilding Secure User Interfaces With JWTs
Building Secure User Interfaces With JWTs
 
JWT Authentication with AngularJS
JWT Authentication with AngularJSJWT Authentication with AngularJS
JWT Authentication with AngularJS
 
Spa Secure Coding Guide
Spa Secure Coding GuideSpa Secure Coding Guide
Spa Secure Coding Guide
 
JDD2015: Security in the era of modern applications and services - Bolesław D...
JDD2015: Security in the era of modern applications and services - Bolesław D...JDD2015: Security in the era of modern applications and services - Bolesław D...
JDD2015: Security in the era of modern applications and services - Bolesław D...
 
Introduction to Web Security
Introduction to Web SecurityIntroduction to Web Security
Introduction to Web Security
 
Web security and OWASP
Web security and OWASPWeb security and OWASP
Web security and OWASP
 
[4developers2016] - Security in the era of modern applications and services (...
[4developers2016] - Security in the era of modern applications and services (...[4developers2016] - Security in the era of modern applications and services (...
[4developers2016] - Security in the era of modern applications and services (...
 
IAM Overview Identiverse 2018
IAM Overview Identiverse 2018IAM Overview Identiverse 2018
IAM Overview Identiverse 2018
 
Attacking Web Applications
Attacking Web ApplicationsAttacking Web Applications
Attacking Web Applications
 
Angular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and AuthorizationAngular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and Authorization
 
OWASP Top 10 Security Vulnerabilities, and Securing them with Oracle ADF
OWASP Top 10 Security Vulnerabilities, and Securing them with Oracle ADFOWASP Top 10 Security Vulnerabilities, and Securing them with Oracle ADF
OWASP Top 10 Security Vulnerabilities, and Securing them with Oracle ADF
 
Demystifying Web Application Security - JSFoo 2018
Demystifying Web Application Security - JSFoo 2018Demystifying Web Application Security - JSFoo 2018
Demystifying Web Application Security - JSFoo 2018
 
Complete Guide to Setup Secure Scheme for Restful APIs
Complete Guide to Setup Secure Scheme for Restful APIsComplete Guide to Setup Secure Scheme for Restful APIs
Complete Guide to Setup Secure Scheme for Restful APIs
 
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
 
Top 10 Web Security Vulnerabilities (OWASP Top 10)
Top 10 Web Security Vulnerabilities (OWASP Top 10)Top 10 Web Security Vulnerabilities (OWASP Top 10)
Top 10 Web Security Vulnerabilities (OWASP Top 10)
 
Con Foo 2017 - Don't Loose Sleep - Secure Your REST
Con Foo 2017 - Don't Loose Sleep - Secure Your RESTCon Foo 2017 - Don't Loose Sleep - Secure Your REST
Con Foo 2017 - Don't Loose Sleep - Secure Your REST
 
Pentesting RESTful WebServices v1.0
Pentesting RESTful WebServices v1.0Pentesting RESTful WebServices v1.0
Pentesting RESTful WebServices v1.0
 
Identity and Access Management - RSA 2017 Security Foundations Seminar
Identity and Access Management - RSA 2017 Security Foundations SeminarIdentity and Access Management - RSA 2017 Security Foundations Seminar
Identity and Access Management - RSA 2017 Security Foundations Seminar
 
Mr. Mohammed Aldoub - A case study of django web applications that are secur...
Mr. Mohammed Aldoub  - A case study of django web applications that are secur...Mr. Mohammed Aldoub  - A case study of django web applications that are secur...
Mr. Mohammed Aldoub - A case study of django web applications that are secur...
 

Recently uploaded

( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
nilamkumrai
 
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Chandigarh Call girls 9053900678 Call girls in Chandigarh
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
Call Girls In Delhi Whatsup 9873940964 Enjoy Unlimited Pleasure
 
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
@Chandigarh #call #Girls 9053900678 @Call #Girls in @Punjab 9053900678
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Recently uploaded (20)

Real Escorts in Al Nahda +971524965298 Dubai Escorts Service
Real Escorts in Al Nahda +971524965298 Dubai Escorts ServiceReal Escorts in Al Nahda +971524965298 Dubai Escorts Service
Real Escorts in Al Nahda +971524965298 Dubai Escorts Service
 
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
 
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
 
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
 
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
 
Al Barsha Night Partner +0567686026 Call Girls Dubai
Al Barsha Night Partner +0567686026 Call Girls  DubaiAl Barsha Night Partner +0567686026 Call Girls  Dubai
Al Barsha Night Partner +0567686026 Call Girls Dubai
 
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
 
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
 
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
 
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
 
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
 
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
 
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
 
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
 
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirt
 

Securing Web Applications with Token Authentication

  • 1. Securing Web Applications with Token Authentication Micah Silverman @afitnerd Co-Author, Mastering Enterprise JavaBeans 3.0 Java Developer Evangelist, Stormpath
  • 2. About Stormpath • Authentication & User Management API • Hosted data store w/ advanced crypto • Centralize user login across your applications • Multi-tenant support for your SaaS • Active Directory, LDAP, social connections • API authentication & token authentication • Supported, Free tier for developers
  • 3. Overview • Security Concerns for Modern Web Apps • Cookies: need to know • Session ID Problems • Token Authentication to the rescue! • OAuth2 & Java Example
  • 4. Security Concerns for Modern Web Apps • SPAs and Mobile apps are ‘Untrusted Clients’ • Prevent malicious code • Secure user credentials • Secure server endpoints (API) • Expose Access Control rules to the Client
  • 5. Prevent Malicious Code • Cross-Site Scripting (XSS) attacks are a real, huge threat
  • 6. Prevent Malicious Code Cross-Site Scripting (XSS) attacks https://www.owasp.org/index.php/XSS
  • 8. XSS Attack – What Can I Do? Read EVERYTHING on this page: https://www.owasp.org/index.php/XSS And then do these things: https://www.owasp.org/index.php/ XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
  • 9. XSS Attack – What Can I Do? Escape Content! Dynamic HTML: use well-known, trusted libraries. Do NOT roll your own. DOM attacks: escape user input
  • 10. XSS Attack – What Can I Do? SPAs: frameworks like Angular probably do a lot of work for you (e.g. preventing DOM attacks by escaping user input). You should still read up on it.
  • 11. Secure User Credentials • Traditionally, we have used Session IDs • This is OK, as long as you do cookies ‘right’ • Authentication Tokens are better ☺ (more on this later)
  • 12. Overview • Security Concerns for Modern Web Apps • Cookies: need to know • Session ID Problems • Token Authentication to the rescue! • Java Example
  • 14. Secure Server (API) Endpoints • Traditionally use Session ID Cookies • Session ID à Session à User identity • Use framework like Apache Shiro or Spring Security to assert security rules
  • 15. Expose Access Control Rules to the Client • Traditional solution: • Session ID à Session à User data in your DB • Provide a /me or /profile endpoint • Access Tokens are better!
  • 16. Let’s talk about cookies...
  • 17. Cookies are OK! If you do them correctly Cookies can be easily compromised: • Man-in-the-Middle (MITM) attacks • Cross-Site Request Forgery (CSRF)
  • 18. Someone ‘listening on the wire’ between the browser and server can see and copy the cookie. Solutions • Use HTTPS everywhere • TLS everywhere on internal networks
  • 19. Cross-Site Request Forgery (CSRF) "... occurs when a malicious web site, email, blog, instant message or program causes a user’s web browser to perform an unwanted action on a trusted site for which the user is currently authenticated"
 https://www.owasp.org/index.php/ CrossSite_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet
  • 20. Cross-Site Request Forgery (CSRF) Attacker enables a user to request your server. Example:
 <a href="https://yoursite.com/ transferMoney?to=BadGuy&amount=10000"> See Cute Cats! </a> What happens?
  • 21. Cross-Site Request Forgery (CSRF) • The attacker cannot see your cookie values, BUT: • The browser says, "The request is going to your server, so I’ll happily send you your cookies." • Your server transfers the money because it ‘sees’ a valid, non-expired session id cookie for an authenticated session.
  • 22. Cross-Site Request Forgery (CSRF) Solutions • Synchronizer Token • Double-Submit Cookie • Origin header check
  • 23. Synchronizer Token – Trusted Page
  • 24. Synchronizer Token – Foreign Page
  • 25. Synchronizer Token - Considerations • Requires cooperation from your rendering layer • Requires you to store tokens in a data store or cache • Difficult to do with static SPA content • Only protects against forged POST requests, not GET requests! Pro tip: never allow GETs to modify server state!
  • 26. Double Submit Cookie • Send two cookies: Session ID + Random Value • Send random value explicitly, browser Same- Origin-Policy • Best Way: send as a custom header
  • 28. Double Submit Cookie Considerations • Custom HTTP header, do what makes sense for your app • Still vulnerable to XSS - Random Value still accessible to the JS environment. • Protect against XSS!
  • 29. Origin header check • Browsers send Origin header • Header value is the domain of the page initiating the request • Cannot be hacked via browser JS 
 (could still be modified by a malicious HTTP proxy server)
  • 30. Overview • Security Concerns for Modern Web Apps • Cookies: need to know • Session ID Problems • Token Authentication to the rescue! • Java Example
  • 31. Session ID Problems • They’re opaque and have no meaning themselves (they’re just ‘pointers’). • Service-oriented architectures might need a centralized ID de-referencing service
  • 32. Session ID Problems • Opaque IDs mean clients can’t inspect them and find out what it is allowed to do or not - it needs to make more requests for this information.
  • 33. Session ID Problems • Sessions = Server State! • You need to store that state somewhere • Session ID à look up server state on *every request*. • Really not good for distributed/clustered apps • Really not good for scale
  • 34. Overview • Security Concerns for Modern Web Apps • Cookies: need to know • Session ID Problems • Token Authentication to the rescue! • Java Example
  • 35. Token Authentication • What is Authentication? • What is a Token?
  • 36. JSON Web Tokens (JWT) • A URL-safe, compact, self-contained string with meaningful information that is usually digitally signed or encrypted. • The string is ‘opaque’ and can be used as a ‘token’. • Many OAuth2 implementations use JWTs as OAuth2 Access Tokens.
  • 37. JSON Web Tokens (JWT) • You can store them in cookies! But all those cookie rules still apply. • You can entirely replace your session ID with a JWT.
  • 38. JSON Web Tokens (JWT) In the wild they look like just another ugly string: eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9.eyJ   pc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQo   gImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnV   lfQ.dBjftJeZ4CVPmB92K27uhbUJU1p1r_wW1gFWFOEj   Xk
  • 39. JSON Web Tokens (JWT) In the wild they look like just another ugly string: eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9.eyJ   pc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQo   gImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnV   lfQ.dBjftJeZ4CVPmB92K27uhbUJU1p1r_wW1gFWFOEj   Xk
  • 40. JSON Web Tokens (JWT) In the wild they look like just another ugly string: eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9.eyJ   pc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQo   gImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnV   lfQ.dBjftJeZ4CVPmB92K27uhbUJU1p1r_wW1gFWFOEj   Xk
  • 41. JSON Web Tokens (JWT) But they do have a three part structure. Each part is a Base64-encoded string: eyJ0eXAiOiJKV1QiLA0KICJhb   GciOiJIUzI1NiJ9   .   eyJpc3MiOiJqb2UiLA0KICJle   HAiOjEzMDA4MTkzODAsDQogIm   h0dHA6Ly9leGFtcGxlLmNvbS9   pc19yb290Ijp0cnVlfQ   .   dBjftJeZ4CVPmB92K27uhbUJU   1p1r_wW1gFWFOEjXk Header Body  (‘Claims’) Cryptographic  Signature
  • 42. JSON Web Tokens (JWT) Base64-decode the parts to find the juicy bits: {    "typ":"JWT",    "alg":"HS256"   } {    "iss":"http://trustyapp.com/",    "exp":  1300819380,    "sub":  "users/8983462",    "scope":  "self  api/buy"   } tß´—™à%O˜v+nî…SZu¯ˉµ€U…8H× Header Body  (‘Claims’) Cryptographic  Signature
  • 43. JSON Web Tokens (JWT) The claims body is the best part! It can tell: {    "iss":"http://trustyapp.com/",    "exp":  1300819380,    "sub":  "users/8983462",    "scope":  "self  api/buy"   } Who  issued  the  token
  • 44. JSON Web Tokens (JWT) The claims body is the best part! It can tell: {    "iss":"http://trustyapp.com/",    "exp":  1300819380,    "sub":  "users/8983462",    "scope":  "self  api/buy"   } Who  issued  the  token When  it  expires
  • 45. JSON Web Tokens (JWT) The claims body is the best part! It can tell: {    "iss":"http://trustyapp.com/",    "exp":  1300819380,    "sub":  "users/8983462",    "scope":  "self  api/buy"   } Who  issued  the  token When  it  expires Who  it  represents
  • 46. JSON Web Tokens (JWT) The claims body is the best part! It can tell: {    "iss":"http://trustyapp.com/",    "exp":  1300819380,    "sub":  "users/8983462",    "scope":  "self  api/buy"   } Who  issued  the  token When  it  expires Who  it  represents What  they  can  do
  • 47. JSON Web Tokens (JWT) Great! Why is this useful?
 • Implicitly trusted because it is cryptographically signed (verified not tampered). • It is structured, enabling inter-op between services • It can inform your client about basic access control rules (permissions)* • And the big one: statelessness! *servers must always enforce access control policies
  • 48. JSON Web Tokens (JWT) So, what’s the catch?
 • Implicit trust is a tradeoff – how long should the token be good for? how will you revoke it? (Another talk: refresh tokens) • You still have to secure your cookies! • You have to be mindful of what you store in the JWT if they are not encrypted. No sensitive info!
  • 49. How do you do it on the JVM? JJWT is awesome https://github.com/jwtk/jjwt
  • 50. How do you do it on the JVM? import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; byte[] key = getSignatureKey(); String jwt = Jwts.builder().setIssuer("http://trustyapp.com/") .setSubject("users/1300819380") .setExpiration(expirationDate) .put("scope", "self api/buy") .signWith(SignatureAlgorithm.HS256,key) .compact(); Create  a  JWT:
  • 51. How do you do it on the JVM? Verify  a  JWT: try { Jws<Claims> jwtClaims = Jwts.parser().setSigningKey(key).parseClaimsJws(jwt); //OK, we can trust this JWT } catch (SignatureException e) { //don't trust the JWT! }
  • 52. How do you get a Token?
  • 53. Example: your SPA, your server
  • 54. 1. Token Request POST /oauth/token HTTP/1.1 Origin: https://foo.com Content-Type: application/x-www-form-urlencoded grant_type=password&username=username&password=p assword *Assert  allowed  origin  for  browser-­‐based  apps
  • 55. 2. Token Response HTTP/1.1 200 OK Content-Type: application/json;charset=UTF-8 Cache-Control: no-store Pragma: no-cache { "access_token":"2YotnFZFEjr1zCsicMWpAA...", "token_type":"example", "expires_in":3600, "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA...", "example_parameter":"example_value" }
  • 56. 3. Resource Request GET /admin HTTP/1.1 Authorization: Bearer 2YotnFZFEjr1zCsicMW...
  • 57. Example: Token Request using an API Key POST /token HTTP/1.1 Content-Type: application/x-www-form-urlencoded grant_type=client_credentials&client_id=apiKeyId&clie nt_secret=apiKeySecret *Assert  allowed  origin  for  browser-­‐based  apps
  • 58. Demo!
  • 59. Thanks! @afitnerd @goStormpath • Token Authentication for Java, JEE, Spring and Spring Boot • Free Supported Developer Tier • Elegant API • OSS Java SDKs + Tutorials Get a Free-Forever Account: Stormpath.com