8 ms·
The complete guide to protecting your APIs with OAuth2
- z3t4 5y agoI was expecting how to implement OAuth2 in pseudo code or JavaScript...
- weinzierl 5y agoFor another quick way to get started with OAuth2, I can recommend reading the 2.1 spec draft[1]. For a spec it is quite readable and pretty short. One reason for this is that it "[..] is an in-progress effort to consolidate and simplify the most commonly used features of OAuth 2.0." [2] It does away with most of the complicated grants/flows of previous version and basically boils down to just two[3]: - Authorization Code Grant - Client Credentials Grant Authorization Code Grant (which now implies PKCE) is what you most probably want in most cases. So instead of complicated decision trees[4] for most people it will be: "Learn one grant and be done". [1] https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-05 https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-... [2] https://oauth.net/2.1/ https://oauth.net/2.1/ [3] There is also the Refresh Token Grant, but it cannot be used alone. There is also an extension mechanism to define new grants and we will have to see if it is used. [4] https://alexbilbie.com/images/oauth-grants.svg https://alexbilbie.com/images/oauth-grants.svg
- mooreds 5y agoAuthor here. Yes, I think that reading the OAuth 2.1 spec is a great place to start. They've taken 10ish years of real world learning and condensed it down. I first wrote about it 2 years ago[0] but have been following along on the email list and they seem to be getting closer and closer. I don't think it is at 'last comment' yet, but I know they've done a lot of work. I am also excited about GNAP, which is a 'from scratch' attempt to solve much the same problem. They've made some great progress[1]. I briefly cover both of those in the article, in the "OAuth's future" section 0: https://fusionauth.io/blog/2020/04/15/whats-new-in-oauth-2-1 https://fusionauth.io/blog/2020/04/15/whats-new-in-oauth-2-1 1: https://justinsecurity.medium.com/the-gnapathon-57ee110508ac https://justinsecurity.medium.com/the-gnapathon-57ee110508ac
- 9dev 5y agoAh, I just found out the Device Authorization Grant didn't make it in there, which is a bit sad - that was a really pragmatic way of building user-friendly authorisation for all kinds of devices where you don't have your password manager available. I even used it to build a CLI authorisation flow once. It seems I have missed quite a bit of development here and will have to catch up, so thank you for the links!
- kache_ 5y agoI've got Opinions and Thoughts on OAuth2 (& all those other protocols, SAML/OIDC) They all model sharing trust over to untrusted boundaries using signed messages generated by one signing authority. There's always a client, different clients have different properties, they're trying to access a resource, and there is a privileged signatory. Lots can be learned from their specs, but please don't limit yourself to them, and don't even worry about following the spec accurately (no one really does). Just know WHY the spec is designed the way they are so that you avoid the same security pitfalls. And understand WHAT you're doing: granting signed assertions based on some user interactions Not only that, but just because it got through the IETF gauntlet doesn't mean they don't have problems. These specs slide around like crazy. My OAuth is different from your OAuth. The antidote? Just write Good Docs and provide good libraries for your integration path. A smart kid in a vacuum solving the same problem OAuth solves will come up with OAuth. Try to emulate that smart kid. And understand why certain concessions are made on the protocols you're taking inspiration from because you might actually not need to make those concessions This stuff hasn't been fully figured out, so push the needle forward and don't be limited by the past. All protocols are incorrect given a large enough time span.
- mooreds 5y ago> A smart kid in a vacuum solving the same problem OAuth solves will come up with OAuth. Try to emulate that smart kid. And understand why certain concessions are made on the protocols you're taking inspiration from because you might actually not need to make those concessions. I have a different perspective. I work for an auth company now, but before I did, I didn't want to think about this at all. I wanted to get some code shipped and build differentiating features. I agree that the IETF and other standards bodies are not perfect (there are specs that get published that never get implemented widely and standards that are implemented that are retrofitted specs). I do think that building on top of a spec is a great way to "stand on the shoulders of giants", get access to people's experience in use cases you haven't seen yet, and accelerate your development effort. Also, there are a lot of folks who've written client libraries, docs, and tutorials around OAuth that people can leverage. How many devs will have experience with your "antidote"? I dunno, but my guess is the number rounds to zero.
- 5y ago
- intrasight 5y agoAuthentication is just one piece of the "protecting your APIs" puzzle. Other pieces including rate-limiting and billing. Those are more challenging.
- suyash 5y agoYes, also don't forget securing API credentials properly. As a matter of fact, I have a blog post coming about this topic soon.
- mooreds 5y agoWould love to see it. Whether tokens or API keys, I'm always interested in how folks recommend securing them. There's also some interesting stuff around token binding happening. DPoP is moving towards becoming a standard: https://datatracker.ietf.org/doc/html/draft-ietf-oauth-dpop-07 https://datatracker.ietf.org/doc/html/draft-ietf-oauth-dpop-...
- brap 5y agoAs someone who has never used OAuth2 before, this article was not helpful at all. Calling it a "complete guide" is a total overstatement.
- mooreds 5y agoAuthor here. @brap, sorry it wasn't helpful. It's aimed at folks who want to protect APIs, but maybe I missed the mark. This article (which I co-authored) may be more helpful: https://fusionauth.io/learn/expert-advice/oauth/modern-guide-to-oauth https://fusionauth.io/learn/expert-advice/oauth/modern-guide... It was also discussed here on HN, if you want some different perspectives: https://news.ycombinator.com/item?id=29752918 https://news.ycombinator.com/item?id=29752918
- magicalhippo 5y agoNot op, but I found it a bit lacking given the title. From the article: While I’ll dive further into how you actually use OAuth to protect an API in your system below, including code examples, [...] I failed to find much concrete around how to use OAuth to protect an API, and no code samples. Is this a part one in a series? If so, it should be more clear about that perhaps. edit: For example, I just had to secure my non-Azure API using access tokens issued by Azure AD. Lot of details[1] about how to verify that they're valid, actually issued by Azure AD (one does not simply download a certificate), and it's issued for the right thing. That said, I found the why and standards section very nice, and the grant selection sections were helpful. [1]: https://docs.microsoft.com/en-us/azure/active-directory/develop/access-tokens#validating-tokens https://docs.microsoft.com/en-us/azure/active-directory/deve...
- mooreds 5y ago> I failed to find much concrete around how to use OAuth to protect an API, and no code samples. I think this may have been an oversight during the final editing process. I ended up having to split this up into two articles, as you surmised. The second one is a blow by blow of the authorization code grant (and how an API should validate a token) and will be published later this week. But I should have caught that we promised the code in the intro and removed that.
- tptacek 5y agoIt'd be useful to capture why OIDC exists (it addresses actual vulnerabilities that crop up when you try to do OAuth as a single-signon scheme rather than just as a way of delegating out the ability to schedule a tweet with a 3rd party service), and to pull out JWT --- you may not be able to avoid JWT if you're doing OIDC, but it's helpful to know what problems JWT addresses and what ones it doesn't, because if you can avoid JWT, you'll almost certainly be better off.
- paxys 5y agoThe only real advantage of OIDC is that it enforces a strict user structure, so different services can understand common fields like id and email. The security aspects of it can all be implemented with plain OAuth2 as well without the added OIDC layer.
- beachy 5y ago> In general, use the Authorization Code grant if there is a human being involved and the Client Credentials grant if you are performing server to server communication. This is the crux of the article. We're integrating with a vendor now that has imo chosen incorrectly to use auth code instead of client credentials. This leads to several problems. One is that we need to store their refresh tokens, which go stale after 2 weeks. Since it's theoretically possible that the customer doesn't use the integration for 2 weeks, we need to preemptively use the refresh tokens ourselves behind the scenes every 2 weeks to prevent this. When the vendor changes tech behind the scenes, they just chuck away all existing tokens, so we then need to contact all customers asking them to log in again in order to get new refresh tokens. It sucks and it's brittle as hell. Tldr; don't use auth code grants (the ones most people think oauth is) for server to server integrations.
- koprulusector 5y agoHoly crap... either you're someone I work with incognito or this is more common a problem than one would expect.... We hacked together a laughable solution that uses an Event Bridge schedule task to invoke a Lambda that goes out and refreshes the tokens daily, lol.
- tbirdny 5y agoI can see the benefit of OAuth if you have an API that multiple customers will use since it allows your customers to manage the security themselves, to create and revoke tokens. I'm wondering, is there any benefit to OAuth if the web service is for your own company, between your own servers, even if the traffic goes over the internet, compared to using a simple token in a header that you generate by hand and the api checks for a match?
- paxys 5y agoBetween your own servers – not really. For that use case OAuth2 will be just as effective as a single shared secret.
- Too 5y agoIt gives you a single place to manage roles and access, RBAC. Saving you the hassle of managing api key generation- and provisioning for N consumers multiplied by M services. Second thing would be that you don’t have to think about api key rotations and revocation everywhere, the only auth is done to the idp (possibly with mfa) and from there everything else is short lived tokens.
- tomjen3 5y agoI really hate the idea of OAuth. I expect that your API can be used with an app-generated password that I can stuff into an Authorization header. I don't like APIs that are not curlabel.
- laerus 5y agoWasn't there an on the making successor to OAuth2 from the same people that was supposed to be a totally different take? I remember hearing something about it but I cannot recall or find the source.
- mooreds 5y agoI think you are thinking of GNAP. The doc is still in progress, you can see the 9th draft here: https://datatracker.ietf.org/doc/draft-ietf-gnap-core-protocol/ https://datatracker.ietf.org/doc/draft-ietf-gnap-core-protoc... (scroll down). I looked at it a year ago and wrote up some of the benefits: https://fusionauth.io/blog/2021/01/07/gnap-next-gen-oauth https://fusionauth.io/blog/2021/01/07/gnap-next-gen-oauth
- laerus 5y agothanks!
- methyl 5y agoI think oauth is abused as a way of protecting your API. Often it’s just an unnecessary complication which makes your API harder to use and your libraries bloated. If Stripe can get away with just public and secret key pair, there is a big chance it’s enough for you as well.
- pbreit 5y agoAmen! I do not understand the rash of OAuth-style authentication mechanisms for pure server-to-server applications.
- tehbeard 5y agoSimplicity on the the resource provider's end? So I implement separate IAM flows for my website, app and third party api access? Or... Make use of OAuth to standardize it, using code auth+pkce for web/app, and client credentials (not user/pass, this is client id + secret, scoped to permissions you want) for third party APIs. The scoping is so useful, api keys tend to ignore that, so platforms like digital ocean only give you an all or nothing key, meaning I can't plumb an acme client in to do DNS challenges, without also letting it spool up a VM...
- pbreit 5y agoSimplicity on the client side. I didn’t understand the rest if your reply. Scoping is easy to implement on a user/key.
- tremon 5y agoWhat user authentication does Stripe handle? As a user I've interacted with Stripe's services a lot, but I've never needed to authenticate myself to them.
- pbreit 5y agohttps://stripe.com/docs/api/authentication https://stripe.com/docs/api/authentication Even pure server-to-server applications with "no user involved" are starting to use OAuth authentication which I don't quite understand. https://www.verygoodsecurity.com/docs/payment-optimization/authentication https://www.verygoodsecurity.com/docs/payment-optimization/a...
- pbreit 5y agoCan someone explain the increasing usage of Oauth credentials for pure server-to-server applications (like Stripe payment processing)?
- mooreds 5y agoI haven't worked with Stripe recently, but if I had to guess, I'd say it is some combination of: * Time limited (rotation built in). * If using signed tokens, integrity of contents can be verified. * Lots of libraries and knowledge around how to use it. * Tokens allow for more interesting/useful payloads than an API key.
- z3t4 5y agoAm I the only one who thinks Oath is over-complicated bordering to security through obscurity and vendor lock-in ? The protocol could be as simple as this (using password) app => auth-server (can user:pass login to app?) auth-server => app (no | yes) Or using "crypto": app => auth-server (can user login to app ?) auth-server => app (only if user can solve challange=XXXX with answer=YYYY) Asking for capabilities: app => auth-server (what permissions do user have on app ?) auth-server => app (user has access to: X, Y, Z)
- paisawalla 5y agoFirst scenario: user's password can be captured and stored by the app. Second scenario: could work if we're talking about a trusted third party app (e.g. Authy) generating the response. Otherwise presumably you enter your secret into the app, where the OTP is derived. This has the same problem as above. Third scenario: no authentication required to query for capabilities? Puts on black hat Ok let me quickly check [A-Z]{1,18}@whitehouse.gov and see which accounts I'm most interested in.
- mooreds 5y agoSure, if all you need to know is "can this user login" and you don't have multiple servers that want to authorize the user, you probably don't need OAuth. You can use sessions and cookies. See my comment elsewhere in this post about this: https://news.ycombinator.com/item?id=31007003 https://news.ycombinator.com/item?id=31007003 Interestingly, your first scenario is the resource owner password credentials grant: https://datatracker.ietf.org/doc/html/rfc6749#section-4.3 https://datatracker.ietf.org/doc/html/rfc6749#section-4.3 The second scenario is, I believe, the JWT bearer grant: https://www.rfc-editor.org/rfc/rfc7523 https://www.rfc-editor.org/rfc/rfc7523 Of course in the OAuth grants, the auth-server returns a token to be presented to other resources instead of a "yes" or "no" answer.
- jamesxaviers23 5y ago
- BlueTemplar 5y agoSo, as a user trying to access a service protected with OAuth... why am I being forced to use a limited list of providers none of which I might want to use / create an account with (happened 2 times already), rather than just pick one myself ?
- zmxz 5y agoI work as OAuth2 implementor, I found this guide to be excellent because it links all the necessary RFC's and categorizes them. It's really easy to navigate and I believe it took a while to write it up, this is some quality work. Thanks for writing it up, it's pretty much BRUTALLY useful for me personally and as a reference guide / explanation guide. Great work, 10/10 will read multiple times :) Looking forward to part 2!
- dekhn 5y agoI dunno about anybody else but I simply stopped using oauth2 because I found it made no sense. Things never worked, the constraints on things working had nothing to do with my needs, and the libraries were all out of date and incompatible with each other, then being replaced by new ones that didn't work yet. I also had a sincerely challenging time understanding the security model, and I'm quite comfortable with encyrption, access control, client certs, etc.