• Software developer and consultant • Telecommunication and media industries • Focus on Web APIs, Identity and Access Management • Designing Evolvable Web APIs with ASP.NET, O’Reilly, 2014 See http://webapibook.net/ 3
2002 – ASP.NET • 2006 – WCF • 2008 – ASP.NET MVC • 2009 – WCF REST Starter KIT • 2012 – ASP.NET Web API, OWIN - Open Web Server Interface for .NET • 2013 – Katana 2.0, MVC 5 and Web API 2 • MVC 5 and Web API 2 – two different code bases with similar models • ASP.NET based on a 12 year old model • Practically impossible to make it cross-platform • 2014..2016 – ASP.NET 5 and MVC 6 9
Based on and compatible with OWIN • Modular • Isolation and side-by-side execution • Cross-platform - CoreCLR • Based on a new execution model • New hosting model: self-hosting (Kestrel) + reverse proxy (IIS, nginx) 10
it explicit • Do not use serialized internal objects as representations • Representation-first design • If using serialization, create representation model classes • Be conservative – if field not needed, do not include it • Design for evolution – if field needed, make it extensible • Be consistent – create a field glossary 14
• Use purpose specific media-types where applicable • application/calendar+json • application/problem+json • application/pdf • Use general purpose hypermedia enabled types • application/hal+json • application/vnd.siren+json 15
“d:image”:{“href”:“https://some.cdn.com/images/123”} } } GET https://some.cdn.com/images/123 20 An intermediary that understand these can make a prefetch
is next page on the collection? • E.g. Where are the comments for the current issue • Embed external resources • E.g. Break a representation into multiple parts • Workflow • E.g. How can I search this collection? • E.g. How to add an element to the collection? • Access control (hinting, not enforcement) • E.g. Can I edit the represented resource? Suggestion: http://vimeo.com/49484938 26
information to add to representations? • Data - fields • Control - links • New representation formats and content negotiation • Adding new links may require new targets (aka “endpoints”) • However that is a consequence • And a server side concern 32
outcomes • Success comes in multiple flavours – 200, 201, 202, 304, ... • Do not use a 2xx for a non-success outcome • Differentiate between 4xx and 5xx • Avoid “overly creative” interpretations of HTTP status codes • Non-success also deserves well-designed representations • “Problem Details for HTTP APIs”, https://tools.ietf.org/html/draft-ietf-appsawg-http-problem-01 35
"https://example.com/probs/out-of-credit", "title": "You do not have enough credit.", "detail": "Your current balance is 30, but that costs 50.", "instance": "/account/12345/msgs/abc", "balance": 30, "accounts": ["/account/12345", "/account/67890"] } 36 Adapted from https://tools.ietf.org/html/draft-ietf-appsawg-http-problem-01
"https://example.com/probs/out-of-credit", "title": "You do not have enough credit.", "detail": "Your current balance is 30, but that costs 50.", "instance": "/account/12345/msgs/abc", "balance": 30, "accounts": ["/account/12345", "/account/67890"] } 37
"https://example.com/probs/out-of-credit", "title": "You do not have enough credit.", "detail": "Your current balance is 30, but that costs 50.", "instance": "/account/12345/msgs/abc", "balance": 30, "accounts": ["/account/12345", "/account/67890"] } 38
and authorization • Avoid cookies – CSRF • Use the Authorization header • Avoid secrets in the URL or custom headers • Use the Two-party requester model: user + client application • Distinct client ID per client type and version • Helps feature usage visibility and breaking changes • Apply transversally - e.g. logging 39
provide tokens to client apps • Client Credential Flow – Server to Server • Authorization Code Grant – Client to Server • Resource Owner Password Credentials Grant – Client to Server • No browser user-agent available • Use OpenID Connect to authenticate with external IdPs • And no, OAuth 2.0 isn’t an authentication protocol 40
validate generic JWT • Use IdentityServer3 for token generation • https://github.com/IdentityServer/IdentityServer3 • Authorization Server - token issuer • Federation Gateway - interface with external IdPs • Hostable on ASP.NET 5 with full .NET framework (no CoreCLR support yet) • IdentityServer3.AccessTokenValidation.Integration.AspNet Nuget • Reference token validation 41
Take advantage of DNX source based dependency resolution • Middleware + Filters + Binders/Formatters + Controllers + Actions • Focus on hypermedia representation design and evolution • Errors deserve well crafted representations too • Token based access control and the two-party model • https://github.com/pmhsfelix/AspNet5Mvc6FactsAndSamples • http://nordicapis.com/designing-evolvable-apis-web-interaction 42
UI is an important feature • Experimentation purposes • RPC mindset – emphasis on the “endpoints” • List of operations and parameters • Operation result schema and status codes • Not hypermedia aware • Alternative: HAL browser – https://api.foxycart.com/hal-browser/browser.html#/ • Not a substitute for human-crafted transversal documentation 45