Pak
Pak

Reputation: 736

Proper OAuth2 authentication flow for a web API using the EWS Managed API

I've been reading through a bunch of documentation for using OAuth with Azure AD, but am still completely confused about how to properly implement things for my situation. Hopefully someone can steer me in the right direction.

I have created an ASP.NET Web API application that uses the EWS Managed API to access Exchange on behalf of different users. My application exposes endpoints such as /Mailbox/Messages and /Appointments with the intent that some front end web application will eventually use them to retrieve a user's emails and appointments. Currently the endpoints are working using basic http authentication, but I'd like to update them to use OAuth. The application has been registered in my Azure AD instance and I've configured it to require the "Access mailboxes as the signed-in user via Exchange Web Services" API permission.

Since the front end hasn't been implemented yet, I've been trying to test by manually calling the authentication endpoint. This prompts me to log in and provide consent. If I consent, I'm redirected to the callback URL that I provided when I registered the app with the authorization code contained in the query parameters. I'm still not quite sure how I'm supposed to be using this callback, but for the sake of testing I currently have the callback redeem the authorization code for an access token. This is done by calling the AcquireTokenByAuthorizationCode method on an instance of the AuthenticationContext class and providing my application's id and secret. Again, just for the sake of testing I return the access token to the browser. I can then call my aforementioned endpoints (after some modifications) with this access token and get the emails for the user. I'm guessing much of this is not the correct way to be doing things.

Some of my points of confusion:

  1. What should the callback that I registered in Azure AD actually be doing when it gets the authorization code? Is this intended for a different type of application? Perhaps one that isn't just playing the role of a middle man.
  2. I'm trying to make my application somewhat RESTful, so I don't want to have to maintain the access tokens on my end between requests. As such, does it make sense for my endpoints to expect that the access token be provided in the authentication header for each request? If so, does that mean the front end application should be responsible acquiring the access token and passing it to me?

Being completely new to OAuth and Azure, I'm not sure if any other details are pertinent, but I can provide more information as needed.

Upvotes: 0

Views: 587

Answers (1)

juunas
juunas

Reputation: 58773

What you are implementing is this scenario: https://learn.microsoft.com/en-us/azure/active-directory/active-directory-authentication-scenarios#daemon-or-server-application-to-web-api

Here's how it works:

  1. Your client app redirects the user to sign in at the authorization endpoint
  2. Your client app gets back an authorization code (if using the auth code grant flow, there are others)
  3. The client app exchanges the code for an access token for your API app
    1. It will need to provide its client id and secret along with the code and the API's resource URI to get it
  4. The client app calls to your API app, passing the access token in the Authorization header
  5. Your API app then validates the access token, and requests for another access token from Azure AD for the Exchange API
    1. It will pass the access token sent by the client app, along with its client id and secret and the Exchange API's resource URI to Azure AD
  6. Your API app receives an access token so you can call to the Exchange API as the user

And to answer your two questions:

  1. Authorization code flow is not used with APIs, only with apps that have a user signing in, thus the redirect URL is basically never used
  2. Your API can and must expect and authenticate the access token for it to be in every request. But the access token it uses to call the Exchange API can and should be cached on the API's side. This is provided out-of-the-box with ADAL, though the tokens are only in memory.

Upvotes: 1

Related Questions