Securing RESTful APIs using OAuth 2 and OpenID Connect

40
Securing RESTful APIs Using OAuth 2 and OpenID Connect Jonathan LeBlanc (@jcleblanc) Head of Developer Evangelism PayPal North America

description

Constructing a successful and simple API is the lifeblood of your developer community, and REST is a simple standard through which this can be accomplished. As we construct our API and need to secure the system to authenticate and track applications making requests, the open standard of OAuth 2 provides us with a secure and open source method of doing just this. In this talk, we will explore REST and OAuth 2 as standards for building out a secure API infrastructure, exploring many of the architectural decisions that PayPal took in choosing variations in the REST standard and specific implementations of OAuth 2.

Transcript of Securing RESTful APIs using OAuth 2 and OpenID Connect

Page 1: Securing RESTful APIs using OAuth 2 and OpenID Connect

Securing RESTful APIsUsing OAuth 2 and OpenID Connect

Jonathan LeBlanc (@jcleblanc) Head of Developer Evangelism

PayPal North America

Page 2: Securing RESTful APIs using OAuth 2 and OpenID Connect

What We’re Covering

Auth History and REST Concepts

Adding in an Auth Mechanism

Integration in Practice (server + client side integrations)

Page 3: Securing RESTful APIs using OAuth 2 and OpenID Connect

What We Want

Page 4: Securing RESTful APIs using OAuth 2 and OpenID Connect

The Ultimate Decision

Security Usability

Page 5: Securing RESTful APIs using OAuth 2 and OpenID Connect

Path to the Standard

Page 6: Securing RESTful APIs using OAuth 2 and OpenID Connect

The Insecure, Unmanageable Start

Page 7: Securing RESTful APIs using OAuth 2 and OpenID Connect

Very Secure, Long to Implement

Page 8: Securing RESTful APIs using OAuth 2 and OpenID Connect

Two Currently Widely Used Specs

Page 9: Securing RESTful APIs using OAuth 2 and OpenID Connect

REST Architecture

Page 10: Securing RESTful APIs using OAuth 2 and OpenID Connect

What a RESTful API isn’t

Our API is RESTful, we support GET, PUT, POST, and DELETE requests

No…actually you just support HTTP…like the rest of the web.

Page 11: Securing RESTful APIs using OAuth 2 and OpenID Connect

What a RESTful API is

Honor HTTP request verbs

Use proper HTTP status codes

No version numbering in URIs

Return format via HTTP Accept header

Page 12: Securing RESTful APIs using OAuth 2 and OpenID Connect

Does Anyone Actually Do That?

Very few APIs follow pragmatic REST principles

Page 13: Securing RESTful APIs using OAuth 2 and OpenID Connect

HATEOAS

Page 14: Securing RESTful APIs using OAuth 2 and OpenID Connect

"links": [{ "href": "https://api.sandbox.paypal.com/v1/payments/ payment/PAY-6RV75EKEYSZ6Y", "rel": "self", "method": "GET" },{ "href": "https://www.sandbox.paypal.com/webscr? cmd=_express-checkout&token=EC-6019609", "rel": "approval_url", "method": "REDIRECT" },{ "href": "https://api.sandbox.paypal.com/v1/payments/ payment/PAY-6RV75EKEYSZ6Y/execute", "rel": "execute", "method": "POST" }]

Page 15: Securing RESTful APIs using OAuth 2 and OpenID Connect

Adding Auth Mechanisms

Page 16: Securing RESTful APIs using OAuth 2 and OpenID Connect

Reasons for Auth

Rate Limiting and Attack Vector Protection

Having the ability to revoke application access

Needing to allow users to revoke an applications access to their data

Page 17: Securing RESTful APIs using OAuth 2 and OpenID Connect

When You Need Access Security

Page 18: Securing RESTful APIs using OAuth 2 and OpenID Connect

A Few Different Flavors of Usage

User login (authentication)

Application only (bearer tokens)

User Involvement (authorization)

Page 19: Securing RESTful APIs using OAuth 2 and OpenID Connect

Practical Implementation

Page 20: Securing RESTful APIs using OAuth 2 and OpenID Connect

Fetching the Access Token

Fetch the Access TokenAccess Token Endpointclient_id grant_typeclient_secret

HTTP POSTAccess Token Endpoint

Page 21: Securing RESTful APIs using OAuth 2 and OpenID Connect

Fetching the Access Token

curl https://api.sandbox.paypal.com/v1/oauth2/token \ -H "Accept: application/json" \ -H "Accept-Language: en_US" \ -u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFd…" \ -d "grant_type=client_credentials"

Page 22: Securing RESTful APIs using OAuth 2 and OpenID Connect

Access Token Response

{ "scope": "https://api.paypal.com/v1/payments/.* https://api.paypal.com/v1/vault/credit-card", "access_token": "EEwJ6tF9x5WCIZDYzyZGaz6K…", "token_type": "Bearer", "app_id": "APP-6XR95014SS315863X", "expires_in": 28800}

Page 23: Securing RESTful APIs using OAuth 2 and OpenID Connect

Using the Access Token

Fetch Privileged ResourcesResource EndpointToken Type (Authorization header) Access Token (Authorization header)

HTTP GET / PUT / POST / DELETEResource Endpoint

Page 24: Securing RESTful APIs using OAuth 2 and OpenID Connect

Using the Access Token

curl -v https://api.sandbox.paypal.com/v1/payments/payment \-H "Content-Type:application/json" \-H "Authorization:Bearer EMxItHE7Zl4cMdkv…" \-d "{...}"

Page 25: Securing RESTful APIs using OAuth 2 and OpenID Connect

A few implementation differences

Endpoints

Scopes (dynamic / static)

Using the Access Token in a request

Page 26: Securing RESTful APIs using OAuth 2 and OpenID Connect

OAuth 2 & JavaScript?

Page 27: Securing RESTful APIs using OAuth 2 and OpenID Connect

The Complexities of JavaScript

The same-origin policy

Keeping private keys private

Not having to provide a hacked

experience

Page 28: Securing RESTful APIs using OAuth 2 and OpenID Connect

The Ways we Made it Work

Server-side proxy

Flash / iframe proxy

Private token storage mechanism

Page 29: Securing RESTful APIs using OAuth 2 and OpenID Connect

User Agent Flow: Redirect

Prepare the Redirect URIAuthorization Endpointclient_id response_type (token)scope redirect_uri

Browser RedirectRedirect URI

Page 30: Securing RESTful APIs using OAuth 2 and OpenID Connect

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 31: Securing RESTful APIs using OAuth 2 and OpenID Connect

User Agent Flow: Hash Mod

Fetch the Hash Modaccess_tokenrefresh_tokenexpires_in

Extract Access Token

Page 32: Securing RESTful APIs using OAuth 2 and OpenID Connect

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 33: Securing RESTful APIs using OAuth 2 and OpenID Connect

User Agent Flow: Get Resources

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

HTTPS Request

Page 34: Securing RESTful APIs using OAuth 2 and OpenID Connect

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 35: Securing RESTful APIs using OAuth 2 and OpenID Connect

Using the Skeleton Key

Page 36: Securing RESTful APIs using OAuth 2 and OpenID Connect

How it’s Normally Used

Access user details

Push data throughuser social streams

Page 37: Securing RESTful APIs using OAuth 2 and OpenID Connect

But why?

Access token as a control structure

Improve Existing Products

Our showcase: Seamless Checkout

Page 38: Securing RESTful APIs using OAuth 2 and OpenID Connect

The Last Considerations

REST and OAuth are specifications, not religions

Don’t alienate your developers with security

Open source is your friend

Page 39: Securing RESTful APIs using OAuth 2 and OpenID Connect

A Few Code Links

OAuth2 & OpenID Connect Sampleshttps://github.com/jcleblanc/oauthhttps://github.com/paypal/paypal-access

Log in with PayPalhttp://bit.ly/loginwithpaypal

Page 40: Securing RESTful APIs using OAuth 2 and OpenID Connect

Thank You! Questions?

http://slideshare.net/jcleblancJonathan LeBlanc (@jcleblanc)

Head of Developer Evangelism PayPal North America