Azure AD JWT Token Validation options - azure

In the Microsoft Azure AD Documentation the JWT Token validation process is described as validating the token signature and then validating its claims.
So, the validation happens entirely on the client side, without asking the Azure AD server whether the token is still valid.
Is there any option for "extended" validation of the token, i.e. by querying for the Azure server, so that the application granting the access to the resource could be sure that the person is still authorized and has an access to that resource.

No, access is granted for the lifetime of the token.
Tokens have an expiration date that you can read from the exp claim. You can control the expiration of a token which then requires the user to reauthenticate after expiration.
If you want to do extended validation, you will need to implement that logic.
ID Tokens
Access Tokens
Configurable token lifetimes in Azure Active Directory

Related

Does updating the Refresh token life Azure AD B2C User flows expire current Refresh tokens

I have a client with mobile apps that uses Azure AD B2C User flows for authentication.
There is a Policy for SignIn that has the Refresh token lifetime (days), this was set too low and we need to increase it.
If we change this policy setting, will it cause currently valid Refresh token's to expire or remain valid?
It is not listed as a reason in Token revocation
No, change the policy setting won't cause currently valid Refresh token's to expire.
The old refresh token will still be valid.
You can have a quick verification by using ROPC flow:
Acquire an access token/refresh token pair.
Change the Refresh token lifetime in ROPC user flow.
Use the refresh token above to acquire a new access token.

What is the logic behind validating a user entity with Microsft Graph/Azure AD as the the authenticator of my API?

I already made the authentication flow with the Microsoft Graph/Azure AD authentication. Once I get the authenticated user's token I store them in his cookies. To validate the user's token I call the Microsft Graph API resource /me. This does not seem a good approach because basically everytime time a client does a request to my API, he is basically doing 2 requests because my API requests Azure AD for validation.
Is this a good flow?
No, it isn't.
Your front-end should acquire an access token for your API, which the API can verify using its digital signature.
The token will contain some info about the user as well as the app that acquired it.
The way in which the front-end acquires the token depends on the type of application.
Front-end single page apps use implicit grant flow for example.
Do note that you have to specifically ask for an access token for your API.
As long as your back-end is then configured with standard JWT Bearer authentication,
all is handled.
This is done by specifying the authority as your Azure AD tenant (or the common endpoint if it's multi-tenant),
and the standard bits for JWT authentication should download the public keys from Azure AD's metadata endpoint, which it can then use to verify validity of any access token it receives.
You do not have to validate tokens for an api that's not yours (issued to your AppId Uri).
For example, Graph validates the tokens that are sent to it (issued for "https://graph.microsoft.com).
If you build and register in Azure AD an Api of your own (say AppIdUri="https://myapi.mydomain.com"), your clients will request and receive access tokens with aud claim set to "https://myapi.mydomain.com".
The clients themselves don't need to validate the access token issued for your Api But your Api, when it receives those access tokens, has to validate them. The validations, among other things will validate the access token was issued to "https://myapi.mydomain.com".
Try out this sample, to get a good understanding around concepts of token validation.

Azure SSO JWT token is still valid after signing out

I have a JWT token which is generated client side and stored in session storage. It is passed to ASP.NET WebApi endpoints that are decorated with the Authorize attribute. If a user signs out of my application then I can delete the token and sign the user out of Azure.
However if the user goes to a different Azure site and signs out then the token that is used in my application remains valid (despite the user being signed out.) How can I force WebApi to call back to Azure and revalidate the token on each request?
Thanks,
Joe
Azure AD JWT access tokens are self signed and the payload contains the exp timestamp, datetime until which the token is valid. The relying party application does not communicate with Azure AD to validate the token, instead, it uses the self-signed information.
The only way to log out, as you mention, is to delete the token.
When you log out from Azure AD SSO you only log out of the possibility to create new access tokens, but already issued access tokens will continue to work until they expire.
Try to log out from Azure AD SSO but keep the access token to your application: You will still be able to access it.
So if you want to log out from both applications, you will need to delete access tokens for both applications.

Azure Ad access token clarifications

I need clarification with sample or reference link for below items:
How to enable automatic renewal of access token?
How to get active access token and expiry time ?
Is there any possible ways to get Ad username, password and client id again from access token?
How to validate access token ?
It all needs to be implement in c# not in powershell.
How to enable automatic renewal of access token?
Upon successful authentication , Azure AD returns two tokens: a JWT access token and a JWT refresh token .When the access token expires, the client application will receive an error that indicates the user needs to authenticate again. If the application has a valid refresh token, it can be used to acquire a new access token without prompting the user to sign in again. If the refresh token expires, the application will need to interactively authenticate the user once again.
How to get active access token and expiry time ?
For how to authenticate users and get an Azure AD access token for your azure ad app , you could refer to Authentication Scenarios for Azure AD .The Azure Active Directory Authentication Library (ADAL) enables client application developers to easily authenticate users to cloud or on-premises Active Directory (AD), and obtain access tokens for securing API calls. ADAL is available on a variety of platforms. You could find code samples and common scenario in this document .
Is there any possible ways to get Ad username, password and client id again from access token?
You could get decode the access token , find the upn claim which Stores the user name of the user principal ; appid claim identifies the application that is using the token to access a resource. Please refer to document :Azure AD token reference .And of course ,you can't get password information .
How to validate access token ?
JWT tokens are signed, but not encrypted when received. It must validate the signature to prove the token's authenticity and validate a few claims in the token to prove its validity. The claims validated by an app vary depending on scenario requirements, but there are some common claim validations that your app must perform in every scenario.For example, we need to verify the iss and aud claim if you were developing a single tenant app. And you also need to verify the nbf to ensure the token is not expired. For more details , please refer to Validating tokens .
Here is a code sample for how to manually validating a JWT access token in a web API . And if you were using the OWIN components in your project, it is more easy to verify the token by using UseWindowsAzureActiveDirectoryBearerAuthentication extension , code sample here is for your reference .

Azure AD - Differentiate between App token and User token

I am building a asp.net webapi which is protected by Azure AD Oauth bearer token authentication. I am using Azure AD Bearer token validation OWIN middle-ware to validate the token and extract the claims.
I have a requirement to differentiate when a request is coming from a service context and when the request coming from user context. I am aware that App Tokens (Issued by AD for a APP context) will not have any UPN claims using which i can easily identify but i was wondering is their any standard way to do this?
Quoting from an internal forum:
The appidacr claim indicates the type of client authentication performed. For a confidential client, the value is 1 when a shared secret (a password) is used as a client secret and 2 when a certificate is used as a client secret. The value 0 indicates a public client, which does not provide a client secret and therefore does not authenticate to the STS. Since confidential clients can acquire both user delegated and app only access tokens, the appidacr claim alone does not help to distinguish a user token from an app-only token.
If you want to distinguish between app-only access tokens, user-delegated access tokens, and id tokens issued by Azure AD (all of which are JWTs signed by the same key), follow this guidance:
First of all, validate the ver claim's value is 1.0.
Next, check to see if the JWT is an access token or an id token. The most reliable way to distinguish between the two is the presence of the appid and appidacr claims. These claims will be present in access tokens, but not id tokens.
If the JWT is an id token, then it represents a user. The subject of an id token issued by Azure AD is always a user. Never accept an id token as proof of authentication, always require an access token.
If the JWT is an access token, the presence of an scp (scope) claim informs you that the token is a user delegated access token. The value of the scp claim tells you what authorization the client has been granted by the user.
If the access token does not have an scp claim, it is an app-only access token. In this case, it may have a roles claim.
Don't rely on UPN and email claims to determine the type of token, they're not as reliable.
Per the Microsoft Docs
Your application may receive tokens on behalf of a user (the usual flow) or directly from an application (through the client credentials flow). These app-only tokens indicate that this call is coming from an application and does not have a user backing it. These tokens are handled largely the same, with some differences:
App-only tokens will not have a scp claim, and may instead have a roles claim. This is where application permission (as opposed to delegated permissions) will be recorded. For more information about delegated and application permissions, see permission and consent in v1.0 and v2.0.
Many human-specific claims will be missing, such as name or upn.
The sub and oid claims will be the same.
Personally in my code, to determine if a token is an App token, I use a combination of checking that the claims: "oid" and "sub" both exist and are the same, as well as checking that the token does not contain a name claim.
In practice I have found that tokens issued using different flows can contain different claims, which is why I have found using a combination of a couple of these properties can lead to better being able to differentiate between a user and application token.
There is now a supported way of telling whether or not the token is for an App or not.
Azure Ad supports the configuring of access tokens, for your protected resource to have some optional claims. The claim needed to answer "Is token for App?" is "idtyp"
See Configuring optional claims for how to set it up

Resources