secure restful api automation with javascript

39
Secure RESTful API Automation With JavaScript Jonathan LeBlanc (@jcleblanc) Head of Developer Evangelism PayPal North America

Upload: jonathan-leblanc

Post on 28-Jan-2015

147 views

Category:

Technology


4 download

DESCRIPTION

Pragmatic RESTful API principles, along with a solid consumption architecture, can allow for a great amount of automation in your program development. At the same time, securing the application can be extremely tricky from JavaScript. In this session we will explore several principles behind RESTful API design and consumption using JavaScript, many of the standards that were integrated in the redevelopment of the PayPal API architecture in the new RESTful APIs. We will cover many of these architecture standards, including: - Building in action automation using HATEOAS - OAuth 2 in the JavaScript model - The challenges behind secure resource consumption through JavaScript

TRANSCRIPT

Page 1: Secure RESTful API Automation With JavaScript

Secure RESTful API Automation With

JavaScript

Jonathan LeBlanc (@jcleblanc) Head of Developer Evangelism

PayPal North America

Page 2: Secure RESTful API Automation With JavaScript

Automation?

Page 3: Secure RESTful API Automation With JavaScript

What JavaScript Can Feel Like

Page 4: Secure RESTful API Automation With JavaScript

JavaScript Challenges

Page 5: Secure RESTful API Automation With JavaScript

The Same-Origin Policy

Page 6: Secure RESTful API Automation With JavaScript

Keeping Private Keys Private

Page 7: Secure RESTful API Automation With JavaScript

Not Providing a Hacked Experience

Page 8: Secure RESTful API Automation With JavaScript

How Did We Used to Do It?

Page 9: Secure RESTful API Automation With JavaScript

Server-side Proxies

Page 10: Secure RESTful API Automation With JavaScript

Flash / iFrame Proxies

Page 11: Secure RESTful API Automation With JavaScript

Private Token Storage

Page 12: Secure RESTful API Automation With JavaScript

Securing Content Negotiation

Page 13: Secure RESTful API Automation With JavaScript

A Modern Approach

CORS Easy Access Control

OAuth 2

Tight Access Control

Page 14: Secure RESTful API Automation With JavaScript

OAuth 2 User Agent Flow

Page 15: Secure RESTful API Automation With JavaScript

User Agent Flow: Redirect

Prepare the Redirect URIAuthorization Endpointclient_id response_type (token)scope redirect_uri

Browser RedirectRedirect URI

Page 16: Secure RESTful API Automation With JavaScript

User Agent Flow: Redirect

Building the redirect link

var auth_uri = auth_endpoint + "?response_type=token" + "&client_id=" + client_id + "&scope=profile" + "&redirect_uri=" + window.location; $("#auth_btn").attr("href", auth_uri);

Page 17: Secure RESTful API Automation With JavaScript

User Agent Flow: Hash Mod

Fetch the Hash Modaccess_tokenrefresh_tokenexpires_in

Extract Access Token

Page 18: Secure RESTful API Automation With JavaScript

User Agent Flow: Hash Mod

http://site.com/callback#access_token=rBEGu1FQr54AzqE3Q&refresh_token=rEBt51FZr54HayqE3V4a&expires_in=3600

var hash = document.location.hash;var match = hash.match(/access_token=(\w+)/);

Extracting the access token from the hash

Page 19: Secure RESTful API Automation With JavaScript

User Agent Flow: Get Resources

Set Request Headers + URIResource EndpointHeader: token type + access tokenHeader: accept data type

HTTPS Request

Page 20: Secure RESTful API Automation With JavaScript

User Agent Flow: Get Resources

$.ajax({ url: resource_uri, beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', 'OAuth ' + token); xhr.setRequestHeader('Accept', 'application/json'); }, success: function (response) { //use response object }});

Making an authorized request

Page 21: Secure RESTful API Automation With JavaScript

CORS Easy Access Control

Page 22: Secure RESTful API Automation With JavaScript

Cross Origin Issues and Options

Access to other domains / subdomains is restricted (same origin policy)

JSONP to request resources across domains

Only supports HTTP GET requests

Cross-origin resource sharing (CORS)Supports additional range of HTTP requests

Page 23: Secure RESTful API Automation With JavaScript

Can you use it?

http://caniuse.com/cors

Page 24: Secure RESTful API Automation With JavaScript

How Does it Work?

OPTIONS /v1/oauth2/token HTTP/1.1Origin: http://jcleblanc.comAccess-Control-Request-Method: PUTHost: api.sandbox.paypal.comAccept-Language: en-USConnection: keep-alive...

Site sends Origin header to server

Page 25: Secure RESTful API Automation With JavaScript

How Does it Work?

Server responds with matching Access-Control-Allow-Origin

header

Access-Control-Allow-Origin: http://jcleblanc.com

Access-Control-Allow-Methods: GET, POST, PUT

Content-Type: text/html; charset=utf-8

Page 26: Secure RESTful API Automation With JavaScript

A Lil’ Bit O’ Automation

Page 27: Secure RESTful API Automation With JavaScript

Uniform Interface Sub-Constraints

Resource Identification

Resources must be manipulated via representations

Self descriptive messages

Hypermedia as the engine of application state

Page 28: Secure RESTful API Automation With JavaScript

Uniform Interface Sub-Constraints

Resource Identification

Resources must be manipulated via representations

Self descriptive messages

Hypermedia as the engine of application state

Page 29: Secure RESTful API Automation With JavaScript

HATEOAS

Page 30: Secure RESTful API Automation With JavaScript

How we Normally Consume APIs

Page 31: Secure RESTful API Automation With JavaScript

Using HATEOAS to Automate

Page 32: Secure RESTful API Automation With JavaScript

How HATEOAS Works

curl -v -X GET https://api.sandbox.paypal.com/v1/payments/authorization/2DC87612EK520411B \

-H "Content-Type:application/json" \

-H "Authorization:Bearer ENxom5Fof1KqAffEsXtx1HTEK__KVdIsaCYF8C"

You make an API request

Page 33: Secure RESTful API Automation With JavaScript

"links": [ { "href":"https://api.sandbox.paypal.com/v1/payments/ authorization/6H149011U8307001M", "rel":"self", "method":"GET" },{ "href":"https://api.sandbox.paypal.com/v1/payments/ authorization/6H149011U8307001M/capture", "rel":"capture", "method":"POST" },{ "href":"https://api.sandbox.paypal.com/v1/payments/ authorization/6H149011U8307001M/void", "rel":"void", "method":"POST" }]

Page 34: Secure RESTful API Automation With JavaScript

Object Chaining

Page 35: Secure RESTful API Automation With JavaScript

Interactions Should be StatelessSend enough detail to not have to make

another request to the API

{ "id": "PAY-17S8410768582940NKEE66EQ", "create_time": "2013-01-31T04:12:02Z", "update_time": "2013-01-31T04:12:04Z", "state": "approved", "intent": "sale", "payer": {...}, "transactions": [{...}], "links": [{...}] }

Page 36: Secure RESTful API Automation With JavaScript

Resources and Representations

Manipulate a concept (e.g. payment) with the intended state

Page 37: Secure RESTful API Automation With JavaScript

Chaining Actions

var paymentObj = getPreAuth(paymentID) //build pay

object.getNextAction() //next

HATEOAS link.processNext(); //process

action

The first request builds the action objectSubsequent calls manipulate the object

Page 38: Secure RESTful API Automation With JavaScript

Security needs to allow you to work the browser security model

Always assume statelessness

Build to allow your developers to automate complexities

In Summation…

Page 39: Secure RESTful API Automation With JavaScript

Thanks! Questions?http://www.slideshare.net/jcleblanc

Jonathan LeBlanc (@jcleblanc) Head of Developer Evangelism

PayPal North America