OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

53

description

API’s are the new apps. They can be consumed by everyone using a web browser or a mobile application on their smartphone or tablet. How would you build your API if you want these apps to be a full-fledged front-end to your service without compromising security? In this session, Maarten will explain how to build an API using the ASP.NET Web API framework and how the Windows Azure Access Control service can be used to almost completely outsource all security and OAuth-related tasks.

Transcript of OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Page 1: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013
Page 2: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

OAuth-as-a-serviceusing ASP.NET Web API and Windows Azure Access Control

Maarten Balliauw@maartenballiauw

Page 3: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Who am I?

Maarten BalliauwTechnical Evangelist, JetBrainsMyGet.orgAZUGFocus on web ASP.NET MVC, Windows Azure, SignalR, ... MVP Windows Azure & ASPInsider

Buy me a beer! http://amzn.to/pronuget

http://blog.maartenballiauw.be @maartenballiauw Shameless self promotion: Pro NuGet -

http://amzn.to/pronuget

Page 4: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Agenda

Why would I need an API?API characteristicsASP.NET MVC Web APIWindows Azure ACS

Page 5: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Why would I need an API?

Page 6: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Consuming the web

2000-2008: Desktop browser2008-2012: Mobile browser2008-2012: iPhone and Android apps2010-2014: Tablets, tablets, tablets2014-2016: Your fridge (Internet of Things)

Page 8: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Twitter & FacebookBy show of hands

Page 9: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Make everyone API(as the French say)

Page 10: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Expose services to 3rd partiesValuableFlexibleManagedSupportedHave a plan

Page 11: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Reach More Clients

Page 12: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

You’re not the only one

Source: http://blog.programmableweb.com/2012/04/16/open-apis-have-become-an-essential-piece-to-the-startup-model/

Page 13: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

API Characteristics

Page 14: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

What is an API?

Software-to-Software interfaceContract between software and developers Functionalities, constraints (technical / legal) Programming instructions

and standards

Open services to other software developers (public or private)

Page 15: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Flavours

Transport HTTP Sockets

Message contract SOAP XML Binary JSON HTML …

Page 16: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Technical

Most API’s use HTTP and REST extensively Addressing HTTP Verbs Media types HTTP status codes Hypermedia (*)

Page 17: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

The Web is an API

Demo

Page 18: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

HTTP VerbsGET – return dataHEAD – check if the data existsPOST – create or update dataPUT – put dataMERGE – merge values with existing dataDELETE – delete data

Page 19: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Status codes

200 OK – Everything is OK, your expected data is in the response.401 Unauthorized – You either have to log in or you are not allowed to access the resource.404 Not Found – The resource could not be found.500 Internal Server Error – The server failed processing your request.…

Page 20: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Be detailed!

Think RFC2324!

Page 21: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

ASP.NET Web API

Page 22: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

ASP.NET Web API

Part of ASP.NET MVC 4Framework to build HTTP Services (REST)Solid features Modern HTTP programming model Content negotiation (e.g. xml, json, ...) Query composition (OData query support) Model binding and validation (conversion to .NET objects) Routes Filters (e.g. Validation, exception handling, ...) And more!

Page 23: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

ASP.NET Web API is easy!

HTTP Verb = action“Content-type” header = data format in“Accept” header = data format outReturn meaningful status code

Page 24: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Creating an APIusing ASP.NET Web API

Demo

Page 25: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Securing your API

No authenticationBasic/Windows authentication[Authorize] attribute

Page 26: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Securing your API

Demo

Page 27: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

The world of API clients is complex

CLIENTS

HTML5+JSSPANative appsServer-to-server

AUTHN + AUTHZ

Username/password?Basic auth?NTLM / Kerberos?Client certificate?Shared secret?

Page 28: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

A lot of public API’s…

“your API consumer isn’t really your user,but an application acting on behalf of a user”

(or: API consumer != user)

Page 29: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

OAuth2

Page 30: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013
Page 31: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

TechDays badges

“I received a ticket with a Barcode I can hand to the Reception which gives me a

Badge stating Microsoft gives Me access to Kinepolis as a Speaker on 5-7 March”

Page 32: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

TechDays badges

+--------+ +---------------+ | |--(A)– Register for TechDays-->| Resource | | | | Owner | | |<-(B)-Sure! Here’s an e-ticket-| Microsoft | | | +---------------+ | | . | | +---------------+ | Client |--(C)----- Was invited! ------>| Authorization | | Me | | Server | | |<-(D)---- Here’s a badge! -----| Reception | | | (5-7 March;speaker) +---------------+ | | . | | +---------------+ | |--(E)------ Show badge ------->| Resource | | | | Server | | |<-(F)-- Enter speakers room ---| Kinepolis | +--------+ +---------------+

Next year, I will have to refresh my badge

Page 33: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

TechDays badges

“I received a ticket with a Barcode I can hand to the Reception which gives me a Badge stating Microsoft gives Me access to Kinepolis as a

Speaker on 5-7 March”

Me = ClientBarcode = Access CodeReception = Authorization ServerMicrosoft = Resource OwnerKinepolis = Resource ServerBadge = Access TokenSpeaker = Scope5-7 March = Token Lifetime

Del

egat

ion

Maarten Balliauw
Page 34: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013
Page 35: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

OAuth2

+--------+ +---------------+ | |--(A)- Authorization Request ->| Resource | | | | Owner | | |<-(B)-- Authorization Grant ---| | | | +---------------+ | | . | | +---------------+ | |--(C)-- Authorization Grant -->| Authorization | | Client | | Server | | |<-(D)----- Access Token -------| | | | +---------------+ | | . | | +---------------+ | |--(E)----- Access Token ------>| Resource | | | | Server | | |<-(F)--- Protected Resource ---| | +--------+ +---------------+

Figure 1: Abstract Protocol Flow http://tools.ietf.org/html/draft-ietf-oauth-v2-31

Page 36: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013
Page 37: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

On the Web

Demo

Page 38: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Quick side note…

There are 3 major authentication flowsBased on type of clientVariants possible

Page 39: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

OAuth2 – Initial flow

Page 40: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

OAuth2 – “Refresh” (one of those variants)

Page 41: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Access tokens / Refresh tokens

In theory: whatever format you wantWidely used: JWT (“JSON Web Token”)Less widely used: SWT (“Simple Web Token”)Signed / Encrypted

Page 42: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

JWT

Header:{"alg":"none"}

Token:{"iss":"joe", "exp":1300819380, "http://some.ns/read":true}

Page 43: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Is OAuth2 different from OpenID?Yes.OpenID = authNOAuth2 = authN (optional) + authZ

http://softwareas.com/oauth-openid-youre-barking-up-the-wrong-tree-if-you-think-theyre-the-same-thinghttp://blogs.msdn.com/b/vbertocci/archive/2013/01/02/oauth-2-0-and-sign-in.aspx

Page 44: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

What you have to implement

OAuth authorization serverKeep track of supported consumersKeep track of user consentOAuth token expiration & refreshOh, and your API

Page 45: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013
Page 46: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Windows AzureAccess Control Service

Page 47: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

ACS - Identity in Windows Azure

Active Directory federationGraph APIWeb SSOLink apps to identity providers using rulesSupport WS-Security, WS-Federation, SAMLLittle known feature: OAuth2 delegation

Page 48: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

OAuth flow using ACS

Page 49: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

ASP.NET Web API, OAuth2Windows Azure ACS

Demo

Page 50: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

OAuth2 delegation?

You: OAuth authorization serverACS: Keep track of supported consumersACS: Keep track of user consentACS: OAuth token expiration & refreshYou: Your API

Page 51: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Conclusion

Page 52: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Key takeaways

API’s are the new appsValuableHTTPASP.NET Web APIOAuth2Windows Azure Access Control Service

Page 53: OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control - TechDays Belgium 2013

Thank you!

http://blog.maartenballiauw.be

@maartenballiauw

http://amzn.to/pronuget