The Things to Keep in Mind about Auth
Tags: beginners, security, learning, cybersecurity
Published: 2021-10-29 18:52:42

---
title: The Things to Keep in Mind about Auth
published: true
description: Five things to keep in mind about auth for the busy developer.
tags: beginners, security, learning, cybersecurity
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r6hiulxjdo8shqwa746y.png
canonical_url: https://developer.okta.com/blog/2021/10/29/things-to-keep-in-mind-about-auth
---

_It's National Cat day and Cybersecurity awareness month so I'm celebrating by reposting a blog post I wrote with permission from [Okta Dev blog](https://developer.okta.com/blog/2021/10/29/things-to-keep-in-mind-about-auth)._

There's a lot of information out there about adding authentication to your app, which is helpful! But also overwhelming. It can be hard to find relevant and _up-to-date_ information. Security best practices and technologies change, so refreshing your understanding and keeping up with current best practices is a good thing. Here are some notes I took while I reviewed my knowledge and applied my experience implementing auth.

![The 5 things to keep in mind with authing](https://developer.okta.com/assets-jekyll/blog/things-to-keep-in-mind-about-auth/overview-989d18daef0214a53e67b3406736f7cbc5520ec146b62c39f70290fc15f4939c.png)

## Prefer OAuth 2.0 and OpenID Connect

If you're implementing authentication to a new application, the best practice is to use OAuth 2.0 with OpenID Connect (OIDC). The combination of OAuth 2.0 with OIDC provides consistency across many integration providers, standardized ways to access information, and security. 

Let's establish a baseline for the rest of this post by familiarizing ourselves with the basics. This [Illustrated Guide to OAuth and OpenID Connect](https://developer.okta.com/blog/2019/10/21/illustrated-guide-to-oauth-and-oidc) is **excellent**. 

What I really like about using OAuth 2.0 and OpenID Connect together is that it separates "the auths" and adds structure to each. We learned from the blog post above that OAuth 2.0 is designed for **authorization** – access to data (resources). And we learned that OIDC is a thin layer on top of OAuth 2.0 that adds login and profile information. Thus, **authentication** is the act of establishing the login session that confirms the user logging in is who they say they are. Now we can be specific in our vocabulary and understand how each standard complements the other.

When you're using OpenID Connect, don't forget that you can inspect the [OpenID Connect Discovery](https://developer.okta.com/docs/reference/api/oidc/#well-known-openid-configuration) document to get a listing of endpoints and supported usages. You'll see references to some of the metadata available in the discovery response below.  

Armed with this knowledge, let's continue!

## Know your tokens

There are three different tokens at play in OAuth 2.0 and OpenID Connect. Depending on the grant type you use (more on that coming up), you might not have all three tokens. It's good to remember the kinds of tokens you're working with and what each does. This makes documentation even easier to parse and team conversations less confusing by specifying the exact type of token instead of only using the word "token".

![ID token](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fn211ya3mm7bw6psjiox.jpg)
**ID token**: The token returned from OpenID Connect containing information about the authentication of the end-user in [JSON Web Token](https://developer.okta.com/blog/2020/12/21/beginners-guide-to-jwt) (JWT) format.

![Access token](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zweznb3oxcyq0ow3z8y0.jpg)
**Access token**: The token returned from OAuth flows that allows you to access the resource. Access tokens returned from Okta are in JWT format.

![Refresh token](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fj10h7fcmefptyiuwumu.jpg)
**Refresh token**: A long-lived token that you exchange for the short-lived access token. Not all grant types support refresh tokens.

## Claim wisely

A JWT has the capability to add custom claims to it. Adding custom metadata directly to the payload of access and ID tokens means you can get properties tailored for your application right from the get-go, right? What a great idea! Or is it? 

Custom claims are powerful, but remember that the contents of a JWT are visible to anyone who has one, such as an external developer calling your system and potentially the end user. You don't want to add private information with the expectation that it's safe from prying eyes. You also don't want to overload the token with a bunch of custom claims that will bloat the token. Keep your tokens lightweight and make adding custom claims a considered decision.

## Use the right grant type

I think this is where developers start feeling detail overload. There are different grant types, and depending on who the caller is and what sort of software process you're working on, the optimal grant type changes. To add further confusion, some grant types (or flows) are OAuth 2.0 standards, and some are from OpenID Connect. Plus, there are updated security practices, and not all blog posts reflect the current recommendations. Yikes! Let's cut out the extraneous stuff and focus on the need-to-knows. 

First, let's start with listing all the available grant types. We'll define a high-level overview of each:

![Graphical summarization of all the grant types](https://developer.okta.com/assets-jekyll/blog/things-to-keep-in-mind-about-auth/all-grants-38c2642f8d9838075fd89ef56a515076c26bab699da5449c9defc781de035c9d.png)

![Authorization Code](https://developer.okta.com/assets-jekyll/blog/things-to-keep-in-mind-about-auth/grants/authorization-code-5bd928513ab39270a8a203aee9e35895698b955fa036f70339fc11b0c2bec307.jpg)
**Authorization Code** is a grant type used by web apps where the source code is private, such as a server-side web app. An authorization code and a client secret are required to get an access token. You can make the authorization code grant type even more secure for server-side web apps by using PKCE as well! More on that below.

![Client Credentials](https://developer.okta.com/assets-jekyll/blog/things-to-keep-in-mind-about-auth/grants/client-credentials-414c63503c5ca3c6385276d32b652b334a22fc6c57dc5ac94571dcb452dbf758.jpg)
**Client Credentials** is a grant type used for back-end communications, such as API to API. Users aren't involved in this flow, so there isn't an ID token available.

![Device Code](https://developer.okta.com/assets-jekyll/blog/things-to-keep-in-mind-about-auth/grants/device-code-5145e0c5e0e28b850087c2fc8329e006bd963150b394cb73eb3853f7d85ef5c7.jpg)
**Device Code** is a grant type primarily used for IoT and smart devices. The flow delegates user authentication and authorization through an external device, such as a smartphone app or browser. Device Code is available as an early access feature in Okta, or follow the steps in this post to [add OAuth device flow to any server](https://developer.okta.com/blog/2019/02/19/add-oauth-device-flow-to-any-server).

![Refresh Token](https://developer.okta.com/assets-jekyll/blog/things-to-keep-in-mind-about-auth/grants/refresh-3dbffeb659cf7f2f4bc2df2355b79a97a160fb229c63b7fe8a0e9f285280ac12.jpg)
**Refresh Token** is not a grant type, per se. It's a long-lived token the application may receive to get longer access to resources. Authorization Code, Device Code, Hybrid, and Resource Owner Password flows support refresh tokens if the authorization server is configured to give the app refresh tokens.

![Proof Key for Code Exchange](https://developer.okta.com/assets-jekyll/blog/things-to-keep-in-mind-about-auth/grants/pkce-8c8acde1f7472705c3305ba8d21561eb020edc049a70f7fad0d70b03c4375105.jpg)
**Proof Key for Code Exchange** (PKCE) is a flow to create a secret to use before exchanging the authorization code for tokens. This is not a grant type used on its own, but as an extra layer of security to the Authorization Code flow.

![Hybrid](https://developer.okta.com/assets-jekyll/blog/things-to-keep-in-mind-about-auth/grants/hybrid-0458664ae2ab421160385edffab30a0df9deacff9d66040d0e28092d96aab7d1.jpg)
**Hybrid** is a set of grant types from the OpenID Connect spec. The `code id_token` flow is the most common and combines two grants. It returns an access token via the Authorization Code grant and an ID token via the Implicit grant.

![Implicit](https://developer.okta.com/assets-jekyll/blog/things-to-keep-in-mind-about-auth/grants/implicit-37e879e1a432f31a0214b9a202c832a24ac2aa33c8b2396c237b6ae7c567bd7a.jpg)
**Implicit** is a grant type that used to be recommended for native and JavaScript apps. It is a simplified version of the Authorization Code grant but doesn't require the authorization code exchange for the access token. Because of the security risks, OAuth 2.0 no longer recommends this grant type and it will be dropped in the upcoming OAuth 2.1 spec.

![Resource Owner Password](https://developer.okta.com/assets-jekyll/blog/things-to-keep-in-mind-about-auth/grants/resource-owner-3d48e8e1ba8575fdc08d4dc4c8eb416e7e1718ad3552e12f70f7b60dc12c8ca9.jpg)
**Resource Owner Password** grant type is a flow used by trusted first-party clients. Because of the risks involved in applications directly handling credentials, OAuth 2.0 no longer recommends this grant type and it will be dropped in the upcoming OAuth 2.1. There might be legacy applications requiring this flow, but that's the only reason to use it.

That's still a lot of flows! And a lot to keep in mind. We're busy developers! So let's distill this down to the preferred grants.

![The three recommended grants](https://developer.okta.com/assets-jekyll/blog/things-to-keep-in-mind-about-auth/recommended-grants-a86f273468ff524ed018a904e71cbd2d2616bd776bd1717a108f84bd70ff32a9.png)

That's much better! We now have a reasonable number of grant types to work with: Device Code, Client Credentials, and a combination grant type – Authorization Code with PKCE.

![Authorization Code with PKCE](https://developer.okta.com/assets-jekyll/blog/things-to-keep-in-mind-about-auth/grants/authorization-code-pkce-5eabfc6cc0a03b6c94e235439ff85b9ea31d3983b4065ea74756c59e7da70810.jpg)
**Authorization Code with PKCE** is the Authorization Code grant type powered by ~~pixies~~ proof key for code exchange. The flow adds a step to first generate a code challenge as an extra layer of security.

Now that we have an updated list of recommended grant types, how do we know which one to use? You may need to use multiple grant types in a complete software system depending on the scenario and actors involved. You may have a SPA, a native application, back-end services and processes, and integrations with external parties. Each of these requires consideration for which grant type to use. Don't worry; by checking the discovery endpoint you'll know if your identity provider is up to the task of securely handling your needs!

We can use this handy-dandy flow chart to identify which grant type to use:

![Use Device Code for input constrained devices, use Client Credentials for back-end processes, else use Authorization Code with PKCE](https://developer.okta.com/assets-jekyll/blog/things-to-keep-in-mind-about-auth/grant-flow-chart-31453fd40c44ac95b00e02045974988c38db061323e6dccf0f5968338ab3528e.png)

## Keep it as simple as possible

We're busy. And adding authentication and access security to our software is foundational, but it's not the only feature of the software. As product developers, we need to get auth into the system and have the peace of mind that we're covered without having to worry about all the details. To keep adding auth simple, use SDKs that already handle all those nitty-gritty details. Okta has SDKs for mobile, front-end, and back-end languages to get going quickly.

What's that? You say that your needs are a little more complicated than simply adding an Okta SDK to your software system? When you need something more customized, incorporate [OIDC certified libraries](https://openid.net/developers/certified/) into your system that allow you to still follow auth best practices while implementing custom needs.

## Next steps

These are the key takeaways that helped me better understand auth and incorporate team discussion points we had when implementing auth in the software system I worked on. This post skims over the surface though and provides just the bare essentials for busy developers. If you want to dig in further and learn more, please check out the following links
  * [OAuth 2.0](https://oauth.net/2/)
  * [OpenID Connect Foundation](https://openid.net/)
  * [It's Time for OAuth 2.1](https://aaronparecki.com/2019/12/12/21/its-time-for-oauth-2-dot-1)
  * [OAuth 2.0 Simplified](https://www.oauth.com/)
  * [OAuth 2.0 and OpenID Connect Overview](https://developer.okta.com/docs/concepts/oauth-openid/)

If you like content about auth, don't forget to follow OktaDev on [Twitter](https://twitter.com/oktadev) and subscribe to our [YouTube channel](https://www.youtube.com/c/OktaDev/) for more great tutorials. 

What are your tips to keep in mind when adding auth?