Which OpenID Connect/OAuth 2.0 Flow is the right One?

That is probably the most common question we get – and the answer is of course: it depends!

Machine to Machine Communication
This one is easy – since there is no human directly involved, client credentials are used to request tokens.

Browser-based Applications
This might be a JavaScript-based application or a “traditional” server-rendered web application. For those scenarios, you typically want to use the implicit flow (OpenID Connect / OAuth 2.0).

A side effect of the implicit flow is, that all tokens (identity and access tokens) are delivered through the browser front-channel. If you want to use the access token purely on the server side, this would result in an unnecessary exposure of the token to the client. In that case I would prefer the authorization code flow – or hybrid flow.

Native Applications
Strictly speaking, a native application has very similar security properties compared to a JavaScript application. Still they are generally considered a bit more easy to secure because you often have stronger platform support for protecting data and isolation.

That’s the reason why the current consensus is, that an authorization code based flow gives you “a bit more” security than implicit. The much more important reason IMO is, that there are a couple of (upcoming) protocols that are optimized for native clients, and they use code exchange and the token endpoint as a foundation – e.g. PKCE, Proof of Possession and AC/DC.

Remark 1: With native applications I mean applications that have access to platform-native APIs like data protection or maybe the system browser. Cordova applications are e.g. written in JavaScript, but I would not consider them to be a “browser-based application”.

Remark 2: For code based flows, you need to embed the client secret in the client application. Of course you can’t treat that as a secret anymore – no matter how good you protect it, a motivated attacker will be able to reverse engineer it. It is still a bit better than no secret at all. Specs like PKCE make it a bit better as well.

Remark 3: I often hear the argument that the client application does not care who the user is, it just needs an access token – thus we rather do OAuth 2.0 than OpenID Connect. While this might be strictly speaking true – OIDC is the superior protocol as it includes a couple of extra security features like nonces for replay protection or c_hash and at_hash to link the (verifiable) identity token to the (unverifiable) access token.

Remark 4: As an extension to remark 3 – always use OpenID Connect – and not OAuth 2.0 on its own. There should be client libraries for every platform of interest by now. ASP.NET has middleware, we have a library for JavaScript. Other platforms should be fine as as well.

Remark 5: Whenever you think about using authorization code flow – rather use hybrid flow. This gives you a verifiable token first before you make additional roundtrips (another extensions of remark 3 and 4).


This entry was posted in .NET Security, IdentityServer, OAuth, OpenID Connect, WebAPI. Bookmark the permalink.

35 Responses to Which OpenID Connect/OAuth 2.0 Flow is the right One?

  1. Pieter De Rycke says:
  2. Alex White says:

    Do you have any views on the pros and cons of using server side proxies for javascript use, as Alex Bilbie blogged about here: http://alexbilbie.com/2014/11/oauth-and-javascript/

  3. Regimantas says:


    Thanks for comparison, it was really helpful. I want to clear some things to myself.
    As I understand authorization code flow is the most secure one, because client secret is stored in client server ? Are there any other significant security differences between auth code/hybrid flows?
    Trying to come up with design for [native client -> idSrv -> native client (for authentication)] and got lost a bit in those flows.

  4. According to the OIDC specs you are allowed to call the Authorization Endpoint in the Hybrid flow with a response type of code id_token token in which case both the ID Token as well as the Access Token is returned from the Authorization Endpoint, Why would you use this over just using Implicit flow then?

    • code id_token returns an identity token and a code. not an access token.

      • robbaman says:

        Yeah, I said a response type of “code id_token token” but I can see how that’s easily overlooked :)

      • ah ok ;)

        well – that gives you an access token immediately – in addition to getting a code. Maybe you want to use that access token to secure the first hop to deliver the code to some backend.

        I personally never used that combination.

  5. Lukas says:

    Is the following correct: Because we use the Client Credential Flow for Client to API calls without user interaction:
    – for the first call, the client must always: 1) first ask Identity Server for a token, 2) then go to the API. (make two calls)
    – For the client credentials flow, only scopes with type “resource” are allowed.

  6. Hi, thanks for the effort…
    How can I use a hybrid flow’s AccessToken and UserInfo to secure WebSockets communicating a Web Socket Server, Is it possible to pass the token to Socket Server (another IS3 client) to validate it via IS3 like a SSO scenario.

  7. Hi, interesting article, thanks.

    With regards to your Remark 5, “Whenever you think about using authorization code flow – rather use hybrid flow. This gives you a verifiable token first before you make additional roundtrips (another extensions of remark 3 and 4).”:

    How do you reconcile your statement with the guidance that “ID Tokens MUST NOT be encoded in the query string.”? (from http://openid.net/specs/oauth-v2-multiple-response-types-1_0.html#Security) below.

    Should they also use response mode form post? (and is that really achievable with mobile apps?)

    7. Security Considerations
    There are security implications to encoding response values in the query string. The HTTP Referer header includes query parameters, and so any values encoded in query parameters will leak to third parties. Thus, while it is safe to encode an Authorization Code as a query parameter when using a Confidential Client (because it can’t be used without the Client Secret, which third parties won’t have), more sensitive information such as Access Tokens and ID Tokens MUST NOT be encoded in the query string. In no case should a set of Authorization Response parameters whose default Response Mode is the fragment encoding be encoded using the query encoding.


  8. eeidfn says:

    Hi Dominick,
    For machine to machine flow (client credentials), is there an Open ID equivalent?

  9. Hi Dominick!
    Do you know real world native applications using OIDC Authorization Code – invoking browser for authorization? As I see most apps just ask login/password…

  10. Google/Android is a first party relationship wouldn’t you agree. Anyways – have a look around – browsers are everywhere when you look closer.

    • First party for sure :)) I was reading their documentation – how to implement google sign in. Seems they have much proprietary communication imlemented and it is not clear how it all works… (play services is not open source).
      Browser login really seems a nice and clear solution!

    • Dominick! One more question – with the browser approach how can we manage a 2FA scenario when SMS/Push mesage comes to mobile and the app automatically receives it (without needing the user to manually enter the code)?

  11. OK – interesting – well they must use some device specific APIs

    • Sure – your app must have permissions for that :) I think maybe on receiving sms/push by app we can reopen the browser (actual browser is waiting for 2FA code) with 2FA url and filled code parameter?

  12. Søren Olesen says:

    Hi, I’m pretty new to identityserver, and trying to figure out what to use in my setup. The setup consist of a client (SPA), an app-api, and a number of data-api’s. The client will access the app-api which will pull informationfrom the different data-api’s. In the client a user should be able to login using username/password, so I guess I should use HybridFlow here, but what should I use between the app-api and the data-api.
    My data-api is public, so I’d like to be able to control which app-api’s can access it, so I guess I need to use a Client Credentials flow? But I still need to know who the user is, so do I pass my userid in all calls, or do I use a combination of Client Credentials and Hybrid flow?? How is this normally done?

    • SPAs == implicit flow.

      For the server to server communication you can either forward the userid as payload – or use a token delegation approach.

      • Søren Olesen says:

        Just, userid as payload – or use a token delegation approach?
        or Client Credentials + (userid as payload – or use a token delegation approach)

        Token delegation, would that just be forwarding the Authorization header from the request to the app-api to the data-api?

        Is this the right place to ask for these tings, or is there a forum that I should use?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s