Secure REST API with OAuth 2.0 Client Credentials Flow using Azure AD. Part 1
Introduction
The following post will describe how to secure Spring Boot REST API with OAuth2 2.0 Client Credentials Flow (M2M) using Azure AD as Authorization Server. The focus will be on Azure AD setup and related Spring Boot/Spring Security configuration nuances. The post will be divided into 2 parts:
Both parts are based on misc documentation resources, tutorials and examples provided by Microsoft, Spring and https://www.baeldung.com/. The links will be provided in References
section.
Part 1. Overview and Azure AD setup
Overview
OAuth2 2.0 Client Credentials Flow (M2M) is intended to cover Machine-to-Machine (M2M) authentication when a human interaction is not available or applicable (ex: a scheduled job calls a secured api). The flow includes 3 parties ( Authorization Server
, Resource Server
and Client
) and contains the following major steps:
-
Client
sends request to Authorization Server to get an access token. Client is usingClient ID
andClient Secret
(as credentials) and providesScope
of the request.Scope
identifies the resource/access the client is trying to get. -
Authorization Server
authenticates theClient
and provides back anaccess token
. -
Client
calls theResource Server
and providesaccess token
as a part of the request. -
Resource Server
verifiesaccess token
and provides access to the requested resource.
This Example/Approach
1. Authorization Server
Will be using Microsoft Azure Active Directory (Azure AD) as Authorization Server. Azure AD supports OAuth2 2.0 Client Credentials Flow and provides all the necessary configuration options.
2. Resource Server
The example will have a Spring Boot based REST API with 2 endpoints. Will be using Spring Security OAuth 2.0 Resource Server to protect the API and integrate with the Authorization Server.
3. Client
Will be using Curl as our HTTP client to demonstrate that our approach is pure HTTP based, compliant with OAuth 2.0 and client technology agnostic.
Azure AD Setup
API Registration
-
Create API (Resource Server) registration in Azure AD by following steps in https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-app. Please, note that step with
Redirect URI
is optional - no need to provide anything there. -
Setup permissions (Application Roles) for API by modifying
appRoles
section of App Manifest file:
Notes:
- Two app roles were setup (
CallHiApiRole
andCallHiApiRole
) so they can be granted separately if needed Manifest
is available viaAzure AD->App Registrations-><Your App>->Manifest
.Id
param in role setup must be a GUID and you will need to generate it manually (ex: using https://www.guidgenerator.com/)Value
param contains the name of the role/permission
Client Registration
-
Register Client App in Azure AD by following steps in https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-app. These are the same steps as in step #1 of
API Registration
. - Grant API permissions (App Roles) to Client App Registration using
Azure AD->App Registrations-><Your Client>->API Permissions
screen:- Click
Add a permission
button - Select
My APIs-><Your API>->Application Permissions
- Tick
CallHiApiRole
andCallHelloApiRole
- Click
-
Provide Admin consent to Client for new permissions in
Azure AD->App Registrations-><Your Client>->API Permissions
screen by clickingGrant admin consent for <Your Azure AD Instance>
button. - Create Client Secret by using
Azure AD->App Registrations-><Your Client>->Certificates and secrets
screen by clickingNew client secret
.
Verify Client and API Registration
Will be checking the setup by performing a Request Token
call to the Authorization Server (Azure AD):
Request
Notes:
<azure_ad_tenant_id>
is the Tenant ID of your Azure AD instance (<Azure AD->Overview>
)- Scope: the scope defines requested scope. For Azure AD the format is
api://<api_application_id>/.default
<api_application_id>
is the Application ID of the API (Azure AD->App Registrations-><Your App>-<Overview>
)<client_id>
is the Application ID of the Client registration in AD (Azure AD->App Registrations-><Your Client>->Overview
)<client_secret>
is the secret defined for Client in step #4 ofClient Registration
step
Response
Will be getting a JWT access token as our response (successful), ex.:
Notes:
aud
claim contains the audience which is your API URIroles
claim contains list of granted permissions for the client for the request scope