building apis with mvc 6 and oauth

50
Filip Ekberg Building APIs with MVC 6 and OAuth

Upload: filip-ekberg

Post on 11-Feb-2017

2.230 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Building APIs with MVC 6 and OAuth

Filip Ekberg

Building APIs with MVC 6 and OAuth

Page 2: Building APIs with MVC 6 and OAuth

@fekberg

I’m Filip EkbergAuthor. Blogger. Speaker. MS MVP. Xamarin MVP. Geek.

Senior Software Engineer @

Page 3: Building APIs with MVC 6 and OAuth

Agenda

ASP.NET 5 OAuth Consuming APIs

Page 4: Building APIs with MVC 6 and OAuth

Using ASP.NET 5

Page 5: Building APIs with MVC 6 and OAuth

• Everything!• Cross-platform• Open Source• Modular design (split into NuGet packages)• And much more..

What’s new in ASP.NET 5

Page 6: Building APIs with MVC 6 and OAuth

• Ctrl + H (Find and Replace) Upgrades• Until RTM

- anything can be renamed- anything can be removed

• Side-by-side versions makes it easy (dnvm upgrade)

Using Bleeding Edge Tech

Page 7: Building APIs with MVC 6 and OAuth

• Powershell, powershell and more powershell…

Continuous Delivery and Integration$out = (Get-Item -Path ".\" -Verbose).FullName

$(dnu restore --no-cache --lock --unlock --parallel)

get-childitem -recurse -filter 'project.json' -exclude '*artifacts*', '*Build*', '*Publish*' | Where-Object { !$_.Directory.FullName.Contains("artifacts")} | ForEach-Object { $res = $(cd $_.Directory;$?) -and $(dnu build | Out-Host;$?) -and $(dnu pack --configuration release --out $out\Build\Packages) if (!$res) { Write-Error "Build failed!" Exit 1 }}

$out = (Get-Item -Path ".\" -Verbose).FullName

get-childitem -recurse -filter 'project.json' -exclude '*artifacts*', '*Build*', '*Publish*' | Where-Object { $_.Directory.FullName.Contains("Tests")} | ForEach-Object { $(cd $_.Directory;$?) $testOutput = $(dnx . test | Write-Host)

if ($testOutput -contains "*[FAIL]*") { Write-Error "Tests failed!" Exit 1 }}

Page 8: Building APIs with MVC 6 and OAuth

• Use your own APIs• Find pain-points before your customers• Invite other teams to build something

Dogfooding

Page 9: Building APIs with MVC 6 and OAuth

• Allows you to introduce new tech early• Up-scale and prepare team for the future• Mitigating risk

Building on-top of legacy

Page 10: Building APIs with MVC 6 and OAuth

Building an API

Page 11: Building APIs with MVC 6 and OAuth

OAuth

Page 12: Building APIs with MVC 6 and OAuth

Disclaimer

Page 13: Building APIs with MVC 6 and OAuth

• Don’t rely on a third party for a critical system• Less headaches for your integrators• Could be added as an option

What about Twitter, Facebook, etc?

Page 14: Building APIs with MVC 6 and OAuth

Roll your own OAuth implementation?

Page 15: Building APIs with MVC 6 and OAuth

• Built by industry experts• Open Source• Allows you to use OAuth 2.0 and OpenId

Connect• Lots and lots of examples and help

available

IdentityServer

https://github.com/IdentityServer/IdentityServer3

Page 16: Building APIs with MVC 6 and OAuth

Tokens

Page 17: Building APIs with MVC 6 and OAuth

Tokens and Codes

Authorization CodeTrade code for an Access Token

Access TokenLets you access a given resource

Refresh TokenLets you keep your Access Token fresh

Page 18: Building APIs with MVC 6 and OAuth

Storing Tokens

Treat your Tokens like passwords!

Remember, they give you access to a potential private resource

Page 19: Building APIs with MVC 6 and OAuth

• JSON Web Token• Payload (Claims) include Scopes, User info,

etc• Signed

JWT

Page 20: Building APIs with MVC 6 and OAuth

What happens when you don’t validate a token?

Page 21: Building APIs with MVC 6 and OAuth

Build your software to assume tokens are invalid and expired

Page 22: Building APIs with MVC 6 and OAuth

Inspecting the Token

Page 23: Building APIs with MVC 6 and OAuth

Securing the API

Page 24: Building APIs with MVC 6 and OAuth

Choosing an OAuth Flow

Page 25: Building APIs with MVC 6 and OAuth

Authorization Code & Implicit Flow

Page 26: Building APIs with MVC 6 and OAuth

Resource Owner Password Flow

Page 27: Building APIs with MVC 6 and OAuth

Client Credential Flow

Page 28: Building APIs with MVC 6 and OAuth

Leverage current infrastructure

What if we already have authentication?

Identify this in pre-authentication and skip OAuth login screen

Authenticate against current system

Page 29: Building APIs with MVC 6 and OAuth

Authentication vs AuthorizationAuthentication is the process of ascertaining that somebody really is who they claims to be

Authorization refers to rules that determine who is allowed to do what. E.g. Filip may be authorized to create and delete databases, while Josh is only authorized to read.http://stackoverflow.com/a/6556548/39106

Page 30: Building APIs with MVC 6 and OAuth

Authentication vs AuthorizationAuthenticationlogin + password (who you are)

Authorizationpermissions (what you are allowed to do)

http://stackoverflow.com/a/20638421/39106

Page 31: Building APIs with MVC 6 and OAuth

• More than just “OK you access this resource” (OAuth)• Authorization (Permissions) +

Authentication (Login)• IdentityServer provides OAuth 2.0 + OpenId

Connect

OAuth + OpenId Connect

Page 32: Building APIs with MVC 6 and OAuth

Securing the API

Page 33: Building APIs with MVC 6 and OAuth

Consuming APIs

Page 34: Building APIs with MVC 6 and OAuth

Testing your API

Page 35: Building APIs with MVC 6 and OAuth

• Client Id• Secret• Scope(s)• Return URL• Grant type• Credentials / Authorization Code (Flow

dependent)

What I need to get a Token

Page 36: Building APIs with MVC 6 and OAuth

Resource Owner Password Token Retrieval

{ "access_token": "eyJ0eXAiO.....", "expires_in": 3600, "token_type": "Bearer", "refresh_token": "cfba7b409dcbb662216bfc5bba80afbc"}

Page 37: Building APIs with MVC 6 and OAuth

Using a Token

GET /api/products HTTP/1.1Host: localhost:1337Authorization: Bearer eyJ0eXAiOiJK...

Page 38: Building APIs with MVC 6 and OAuth

Getting data from the API

Page 39: Building APIs with MVC 6 and OAuth

Scopes

Page 40: Building APIs with MVC 6 and OAuth

Adding support for Scopes

[HttpDelete][Authorize("write")][Route("/accounts/{accountId}/documents/{documentId}")]public async Task<JsonResult> DeleteAsync(string accountId,

long documentId)

Page 41: Building APIs with MVC 6 and OAuth

LeveragingScopes

Page 42: Building APIs with MVC 6 and OAuth

Wrap-up

Page 43: Building APIs with MVC 6 and OAuth

ASP.NET 5

Open Source Go-Live! Cross-Platform

Page 44: Building APIs with MVC 6 and OAuth

Building Secure APIs

Don’t roll your own security framework

Read the OAuth 2.0 Specification

Page 45: Building APIs with MVC 6 and OAuth

OAuth

Know your flows

Authentication vs Authorization

Leverage Claims

Page 46: Building APIs with MVC 6 and OAuth

Want to introduce new and shiny tech?Build on-top of existing infrastructure

Start with non-mission critical parts of the business

Page 47: Building APIs with MVC 6 and OAuth

Download the code

http://bit.ly/ddd-oauth

Page 48: Building APIs with MVC 6 and OAuth

Please support our sponsors

Page 49: Building APIs with MVC 6 and OAuth

Fill out your feedbackTo go into the draw for prizes, please remember to complete your feedback at:

http://www.dddbrisbane.com/feedback

No feedback = No Prizes!

Page 50: Building APIs with MVC 6 and OAuth

@fekberg

Thank you, I’m Filip Ekberg!Author. Blogger. Speaker. MS MVP. Xamarin MVP. Geek.

Senior Software Engineer @