SlideShare a Scribd company logo
1 of 38
Download to read offline
REST APIs in the context of 
single-page applications 
@YoranBrondsema 
August 25th 2014
About me 
CTO of Hstry 
Organizer of Belgium Ember.js meetups
A bit of background 
application has a Ruby on Rails REST API and an Hstry Ember.js front-end 
Server-client communication is all JSON 
API is not public: just one front-end
Today I'm talking about 
1. What is REST? 
2. Authentication 
3. Authorization 
4. HTTP status codes 
5. JSON API
What is REST? 
Architecture for the World Wide Web
Separation of client and server
Stateless
Unique identification of resources through URIs 
http://www.example.com/posts/15
Standard HTTP methods 
GET 
POST 
PUT 
DELETE 
(PATCH)
Authentication
Implies some form of state 
REST is stateless so stored on client
Token-based authentication 
1. At login, generate token on server 
2. Return token in response 
3. Client includes token with every request
Where to store token?
In memory 
Single-page application so no refreshes 
Does not persist when user closes and opens tab
Cookies 
Automatically sent with every request 
Also sends other stored information 
Stores text, not objects 
Not very RESTful
sessionStorage and localStorage 
Part of Web Storage specification 
Secure, per-domain storage 
Stores Javascript objects, not text 
Stays on client 
Send token through query parameter 
Browser support is good (caniuse.com)
All of this requires HTTPS!
Implementation in Devise, unfortunately...
Implementation vulnerable to timing attacks 
Maintainer provided secure implementation, not yet merged in Devise (see here)
Authorization
Deals with permissions 
Is User X allowed to perform Action Y? 
Comes after authentication
Need context-aware DSL that is expressive enough 
ALLOWED User with id 15 requests PUT /api/user/15/profile 
FORBIDDEN User with id 16 requests PUT /api/user/15/profile
Define roles 
e.g. admin, editor, user 
Specify permissions for each role.
declarative_authorization gem 
role :guest do 
... 
end 
role :student do 
# Include all permissions from guest 
includes :guest 
has_permission_on :timelines, to: :show do 
# Can only see timelines that are made by himself 
if_attribute :type => is { "UserTimeline" }, 
:author => is { user } 
end 
end
HTTP status codes
Adds semantics to HTTP responses 
Both for success (2xx) and error (4xx)
REST verbs 
GET 200 OK 
POST 201 Created 
PUT 204 No content (200 OK if include response) 
DELETE 204 No content
Error codes 
Wrong authentication 401 Unauthorized 
Wrong authorization 403 Forbidden 
Parameter is missing 412 Precondition failed 
Other error 422 Unprocessable entity
Nice overview on http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
JSON API
jsonapi.org 
Initiative by Steve Klabnik and Yehuda Katz 
Standard for representation of JSON responses 
Belief that shared conventions increase productivity through generalized tooling
Specifies... 
...how resources are represented in JSON 
{ 
"links": { 
"posts.author": { 
"href": "http://example.com/people/{posts.author}", 
"type": "people" 
}, 
"posts.comments": { 
"href": "http://example.com/comments/{posts.comments}", 
"type": "comments" 
} 
}, 
"posts": [{ 
"id": "1", 
"title": "Rails is Omakase", 
"links": { 
"author": "9", 
"comments": [ "5", "12", "17", "20" ] 
} 
}] 
}
...HTTP status codes and Location header 
When one or more resources has been created, the server 
MUST return a 201 Created status code. 
The response MUST include a Location header identifying the 
location of all resources created by the request.
...structure for errors 
{ 
"errors": [{ 
"id": "forbidden", 
"href": "http://help.example.com/authorization_error", 
"status": "403", 
"code": "ERROR_12345", 
"title": "Authorization error", 
"detail": "The requesting user does not have the permissions to perform this action" 
}] 
}
...structure for PATCH 
PATCH /posts/1 
Content-Type: application/json-patch+json 
[ 
{ "op": "replace", "path": "/title", "value": "A new title" } 
] 
Replace attribute title of resource /posts/1 with value A new title
Implementations 
Ruby (0.9.0 released ActiveModel::Serializers last Friday) 
Javascript Ember Data 
... other languages too
Thank you

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
 

What's hot (20)

Securty Testing For RESTful Applications
Securty Testing For RESTful ApplicationsSecurty Testing For RESTful Applications
Securty Testing For RESTful Applications
 
Rest API Security
Rest API SecurityRest API Security
Rest API Security
 
REST Easy with AngularJS - ng-grid CRUD EXAMPLE
REST Easy with AngularJS - ng-grid CRUD EXAMPLEREST Easy with AngularJS - ng-grid CRUD EXAMPLE
REST Easy with AngularJS - ng-grid CRUD EXAMPLE
 
DDD Melbourne 2014 security in ASP.Net Web API 2
DDD Melbourne 2014 security in ASP.Net Web API 2DDD Melbourne 2014 security in ASP.Net Web API 2
DDD Melbourne 2014 security in ASP.Net Web API 2
 
Indivo X Hospital Connectivity
Indivo X Hospital ConnectivityIndivo X Hospital Connectivity
Indivo X Hospital Connectivity
 
Fine Uploader S3
Fine Uploader S3Fine Uploader S3
Fine Uploader S3
 
Authorization and Authentication using IdentityServer4
Authorization and Authentication using IdentityServer4Authorization and Authentication using IdentityServer4
Authorization and Authentication using IdentityServer4
 
How to Build an Indivo X Personal Health App
How to Build an Indivo X Personal Health AppHow to Build an Indivo X Personal Health App
How to Build an Indivo X Personal Health App
 
Openid & Oauth: An Introduction
Openid & Oauth: An IntroductionOpenid & Oauth: An Introduction
Openid & Oauth: An Introduction
 
OAuth2 and LinkedIn
OAuth2 and LinkedInOAuth2 and LinkedIn
OAuth2 and LinkedIn
 
Single-Page-Application & REST security
Single-Page-Application & REST securitySingle-Page-Application & REST security
Single-Page-Application & REST security
 
REST API Design
REST API DesignREST API Design
REST API Design
 
User Management with LastUser
User Management with LastUserUser Management with LastUser
User Management with LastUser
 
Securing Web Applications with Token Authentication
Securing Web Applications with Token AuthenticationSecuring Web Applications with Token Authentication
Securing Web Applications with Token Authentication
 
Protecting Your APIs Against Attack & Hijack
Protecting Your APIs Against Attack & Hijack Protecting Your APIs Against Attack & Hijack
Protecting Your APIs Against Attack & Hijack
 
NodeJS - Creating a Restful API
NodeJS - Creating a Restful APINodeJS - Creating a Restful API
NodeJS - Creating a Restful API
 
Browser Security 101
Browser Security 101 Browser Security 101
Browser Security 101
 
OAuth2 and Spring Security
OAuth2 and Spring SecurityOAuth2 and Spring Security
OAuth2 and Spring Security
 
Introduction To CodeIgniter
Introduction To CodeIgniterIntroduction To CodeIgniter
Introduction To CodeIgniter
 
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
 

Similar to REST APIs in the context of single-page applications

RESTful services
RESTful servicesRESTful services
RESTful services
gouthamrv
 
Jordi Romero Api for-the-mobile-era
Jordi Romero Api for-the-mobile-eraJordi Romero Api for-the-mobile-era
Jordi Romero Api for-the-mobile-era
.toster
 

Similar to REST APIs in the context of single-page applications (20)

RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
RESTful services
RESTful servicesRESTful services
RESTful services
 
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
 
Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.
 
Kotlin server side frameworks
Kotlin server side frameworksKotlin server side frameworks
Kotlin server side frameworks
 
Creating Restful Web Services with restish
Creating Restful Web Services with restishCreating Restful Web Services with restish
Creating Restful Web Services with restish
 
Rest with Spring
Rest with SpringRest with Spring
Rest with Spring
 
Node.js introduction
Node.js introductionNode.js introduction
Node.js introduction
 
Build your APIs with apigility
Build your APIs with apigilityBuild your APIs with apigility
Build your APIs with apigility
 
API Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIsAPI Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIs
 
Api Design and More (Friday Training at Itnig)
Api Design and More (Friday Training at Itnig)Api Design and More (Friday Training at Itnig)
Api Design and More (Friday Training at Itnig)
 
Networked APIs with swift
Networked APIs with swiftNetworked APIs with swift
Networked APIs with swift
 
Security enforcement of Java Microservices with Apiman & Keycloak
Security enforcement of Java Microservices with Apiman & KeycloakSecurity enforcement of Java Microservices with Apiman & Keycloak
Security enforcement of Java Microservices with Apiman & Keycloak
 
Switch to Backend 2023
Switch to Backend 2023Switch to Backend 2023
Switch to Backend 2023
 
Jordi Romero Api for-the-mobile-era
Jordi Romero Api for-the-mobile-eraJordi Romero Api for-the-mobile-era
Jordi Romero Api for-the-mobile-era
 
Service approach for development Rest API in Symfony2
Service approach for development Rest API in Symfony2Service approach for development Rest API in Symfony2
Service approach for development Rest API in Symfony2
 
Together Cheerfully to Walk with Hypermedia
Together Cheerfully to Walk with HypermediaTogether Cheerfully to Walk with Hypermedia
Together Cheerfully to Walk with Hypermedia
 
eZ Publish REST API v2
eZ Publish REST API v2eZ Publish REST API v2
eZ Publish REST API v2
 
E zsc2012 rest-api-v2
E zsc2012 rest-api-v2E zsc2012 rest-api-v2
E zsc2012 rest-api-v2
 
Andrei shakirin rest_cxf
Andrei shakirin rest_cxfAndrei shakirin rest_cxf
Andrei shakirin rest_cxf
 

More from yoranbe (6)

Test with YouTube
Test with YouTubeTest with YouTube
Test with YouTube
 
A client-side image uploader in Ember.js
A client-side image uploader in Ember.jsA client-side image uploader in Ember.js
A client-side image uploader in Ember.js
 
Changes in Ember.js 1.9 and the Road to Ember.js 2.0
Changes in Ember.js 1.9 and the Road to Ember.js 2.0Changes in Ember.js 1.9 and the Road to Ember.js 2.0
Changes in Ember.js 1.9 and the Road to Ember.js 2.0
 
Ember Data and JSON API
Ember Data and JSON APIEmber Data and JSON API
Ember Data and JSON API
 
From Ember.js 1.5 to 1.7
From Ember.js 1.5 to 1.7From Ember.js 1.5 to 1.7
From Ember.js 1.5 to 1.7
 
Ember.js Brussels Meetup #3 - Testing your Ember.js app
Ember.js Brussels Meetup #3 - Testing your Ember.js appEmber.js Brussels Meetup #3 - Testing your Ember.js app
Ember.js Brussels Meetup #3 - Testing your Ember.js app
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
 

Recently uploaded (20)

Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 

REST APIs in the context of single-page applications

  • 1. REST APIs in the context of single-page applications @YoranBrondsema August 25th 2014
  • 2. About me CTO of Hstry Organizer of Belgium Ember.js meetups
  • 3. A bit of background application has a Ruby on Rails REST API and an Hstry Ember.js front-end Server-client communication is all JSON API is not public: just one front-end
  • 4. Today I'm talking about 1. What is REST? 2. Authentication 3. Authorization 4. HTTP status codes 5. JSON API
  • 5. What is REST? Architecture for the World Wide Web
  • 6. Separation of client and server
  • 8. Unique identification of resources through URIs http://www.example.com/posts/15
  • 9. Standard HTTP methods GET POST PUT DELETE (PATCH)
  • 11. Implies some form of state REST is stateless so stored on client
  • 12. Token-based authentication 1. At login, generate token on server 2. Return token in response 3. Client includes token with every request
  • 13.
  • 14. Where to store token?
  • 15. In memory Single-page application so no refreshes Does not persist when user closes and opens tab
  • 16. Cookies Automatically sent with every request Also sends other stored information Stores text, not objects Not very RESTful
  • 17. sessionStorage and localStorage Part of Web Storage specification Secure, per-domain storage Stores Javascript objects, not text Stays on client Send token through query parameter Browser support is good (caniuse.com)
  • 18. All of this requires HTTPS!
  • 19. Implementation in Devise, unfortunately...
  • 20. Implementation vulnerable to timing attacks Maintainer provided secure implementation, not yet merged in Devise (see here)
  • 22. Deals with permissions Is User X allowed to perform Action Y? Comes after authentication
  • 23. Need context-aware DSL that is expressive enough ALLOWED User with id 15 requests PUT /api/user/15/profile FORBIDDEN User with id 16 requests PUT /api/user/15/profile
  • 24. Define roles e.g. admin, editor, user Specify permissions for each role.
  • 25. declarative_authorization gem role :guest do ... end role :student do # Include all permissions from guest includes :guest has_permission_on :timelines, to: :show do # Can only see timelines that are made by himself if_attribute :type => is { "UserTimeline" }, :author => is { user } end end
  • 27. Adds semantics to HTTP responses Both for success (2xx) and error (4xx)
  • 28. REST verbs GET 200 OK POST 201 Created PUT 204 No content (200 OK if include response) DELETE 204 No content
  • 29. Error codes Wrong authentication 401 Unauthorized Wrong authorization 403 Forbidden Parameter is missing 412 Precondition failed Other error 422 Unprocessable entity
  • 30. Nice overview on http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
  • 32. jsonapi.org Initiative by Steve Klabnik and Yehuda Katz Standard for representation of JSON responses Belief that shared conventions increase productivity through generalized tooling
  • 33. Specifies... ...how resources are represented in JSON { "links": { "posts.author": { "href": "http://example.com/people/{posts.author}", "type": "people" }, "posts.comments": { "href": "http://example.com/comments/{posts.comments}", "type": "comments" } }, "posts": [{ "id": "1", "title": "Rails is Omakase", "links": { "author": "9", "comments": [ "5", "12", "17", "20" ] } }] }
  • 34. ...HTTP status codes and Location header When one or more resources has been created, the server MUST return a 201 Created status code. The response MUST include a Location header identifying the location of all resources created by the request.
  • 35. ...structure for errors { "errors": [{ "id": "forbidden", "href": "http://help.example.com/authorization_error", "status": "403", "code": "ERROR_12345", "title": "Authorization error", "detail": "The requesting user does not have the permissions to perform this action" }] }
  • 36. ...structure for PATCH PATCH /posts/1 Content-Type: application/json-patch+json [ { "op": "replace", "path": "/title", "value": "A new title" } ] Replace attribute title of resource /posts/1 with value A new title
  • 37. Implementations Ruby (0.9.0 released ActiveModel::Serializers last Friday) Javascript Ember Data ... other languages too