45
loading...
This website collects cookies to deliver better user experience
Tip: This view is available at the All operations level, as well as each individual operation. You may also notice that there's an All APIs option in the left hand column as well, which presents you with the same view.
Why is this important? You may want to set your API Management policies at different levels of scope -
You can even apply policies to Backends, so that you can enforce certain requirements when communicating with your backend APIs (e.g. conversion from XML to JSON). You can also apply policies to the Products that you have configured within the API Management service. We won't be exploring this in the blog post, but do feel free to explore if you may have a valid use case (e.g. enforcing certain requirements based upon a product that a user has access to).
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid.">
<openid-config url="https://login.microsoftonline.com/cloudwithchris.com/.well-known/openid-configuration" />
<audiences>
<audience>d3414b61-53f8-4ad5-aa1d-1e2a15579f60</audience>
</audiences>
</validate-jwt>
d3414b61-53f8-4ad5-aa1d-1e2a15579f60
. This means that I have an App Registration in Azure Active Directory with the Client ID d3414b61-53f8-4ad5-aa1d-1e2a15579f60
. Of course, if you're working through this blog post from beginning to end - you may not have an App Registration.Tip: If you're following this post in your own subscription with your own Azure Active Directory Tenant, you'll likely have the appropriate access to create an app registration within your subscription.
However, if you're working on this within a corporate environment - It's possible and even likely that the creation/management of app registrations in Azure Active Directory is restricted to a subset of users. This is because App Registrations can be used... as the name implies... to register your applications to Azure Active Directory. What does that mean? It means you can pass in an identifier/representation to your application, so that you can call APIs that are available (e.g. Microsoft Graph, other Application APIs, etc.)
This blog post is by no means a recommended or best practice way of setting up your App Registrations, but merely a demonstration of API Management Policies being used for JWT validation within your API Management service as a Façade, before reaching your backend APIs. If you're looking for something more rigorous, I would encourage you to review the Azure Docs relating to Azure Active Directory / Azure Active Directory B2C.
App Registrations
in the Azure Portal. You can find this by either going to Azure Active Directory > App Registrations
or by searching for App Registrations
in the search bar at the top. Assuming that you have the appropriate access, create a new App Registration.Tip: I'll only be creating a single app registration. However, if you had a user interface (e.g. Single Page Application), that interacted with multiple backend APIs, then it's likely you would have multiple app registrations. You would have a registration for the Single Page App (SPA) frontend, and separate app registrations for the custom-built APIs that you will be accessing. (I say custom-built, as there are built-in APIs available for access to Microsoft Services, such as the Microsoft Graph API).
Then it's a case of exposing an API from the app registration that is associated to your backend API, and granting API permissions to the app registration that is aligned to your Single Page App. When you expose the API, you provide it a name (e.g. myapi.read). That is then the scope that you would pass in when you acquire the access token for the call. This example shows how to do this for a Single Page Application calling the Microsoft Graph (so, not a custom API - which is the slight difference in what we described here).
Why all of this complexity? Because, we don't necessarily want to grant one application access to all other applications. Likewise, as an organization - we may not want to grant admin consent to this, and in fact give this freedom of choice back to the end-user. Remember those dialogues when you use a social identity provider to login to a site? You're commonly told they'll access your User Name, E-Mail, etc.? This is the similar concept to consider with API permissions.
If there is interest, I can write a blog post up on this topic. However, the purpose of this blog post is really to introduce the concept and power of API Management policies. This scenario could be a blog post in its own right.
Tip: Speaking of security policy, you may notice that there's an option to upload certificates against this App Registration. Client Secrets are useful in proof of concept scenarios. Though ask yourself, how well do we typically store those passwords? How often do we rotate them? Could a certificate be more secure in this instance? For ease, I'll be obtaining my access token in postman by using a Client Secret, though you may want to consider this point in a production implementation. There's a great stack exchange discussion on the same pros/cons of each.
Tip: If you are new to identity, it's certainly worth reviewing the OAuth 2.0 and OpenID Connect protocols. It's important to understand the different type of flows, and which one makes the most sense in your scenario.
For example, it was common for interactive user logins to use the implicit grant flow. However, the strong recommendation is to use authorization code flow, especially for Single Page Applications.
https://login.microsoftonline.com/cloudwithchris.com/oauth2/v2.0/token
application/x-www-form-urlencoded
/.default
appended to the end, e.g. d3414b61-53f8-4ad5-aa1d-1e2a15579f60/.default
.HTTP Status 200 OK
response! This response contains a JSON object, with an access_token property - similar to the below.{
"token_type": "Bearer",
"expires_in": "3599",
"ext_expires_in": "3599",
"expires_on": "1624882328",
"not_before": "1624878428",
"resource": "d3414b61-53f8-4ad5-aa1d-1e2a15579f60",
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Im5PbzNaRHJPRFhFSzFqS1doWHNsSFJfS1hFZyIsImtpZCI6Im5PbzNaRHJPRFhFSzFqS1doWHNsSFJfS1hFZyJ...A Load of extra characters here"
}
access_token
property key. You'll need this as we call our API Management resource.Tip: What is that long token value anyway? It's a JSON Web Token, and is a common transfer format to show a claims of a given entity, commonly used in OAuth workflows. You can actually look inside the contents of those files using tools like jwt.ms/.
Hopefully this comes as an obvious health warning. Be careful on where you share your *** The idea of ***** is give access to the bearer of this token. It comes from the same concept as a bearer cheque if you have heard of that previously. In which case, like any secrets - please don't share your *** openly!
Tip: Remember that you can apply your API Management Policies at several levels -
HTTP 401 Unauthorized
error, and a message of Unauthorized. Access token is missing or invalid.Authorization
as a key in the headers. The value should be Bearer {AccessToken}
, where you'll replace {AccessToken}
(including the curly braces, with your access token from earlier).HTTP Status Code 200 OK
(or whatever is appropriate for your API, if you have used a different API!). Either way, you should no longer be receiving an HTTP 401 Unauthorized
. If you are, you may need to check if the access token that you have provided matches against the expected details in API Management.Tip: This is where things get interesting. You can use API Management to even inspect the claims inside of a JWT. Consider the scenario where certain APIs should only be accessible by certain groups of the business (e.g. IT or Finance). Or, consider the scenario where it should only be allowed by a certain role (e.g. Admin or Moderator). These are pieces of information (claims) that you could pass back within a JWT within your login/token request process (out of scope for this blog post), and used as part of your JWT validation step in the API Management Policy.
45