In this article we will look into how the example provided on the Microsoft HowTo site works and then discuss some practical implementations thereof.
In the previous article (https://xomino365.com/2016/02/01/using-azure-ad-to-allow-authorized-cors-access-to-o365-data/) we looked at how to create an Azure AD application and securely access O365 data from within another web domain. The Microsoft How to site provides and example application on how to get data from your One Drive. But in this case we are going to look at how to get data from a SharePoint site collection.
The basic site
The site does have a good working example, you can copy and paste the code. What I wanted to show here was the end result and how it works. I modified mine slightly to pull data from a SharePoint list rather than OneDrive but it all works.
Starting at the site root (which is anonymously accessible), we click the Get Token button.
The application then makes a request to the Azure authentication pages
Combining the values added to the example application we get the following values added to the Query_String of the address. These values must coincide with the values in the application manifest.
If the user is not signed in then they are asked to do so
Once successful authentication has been achieved the login end point returns the user to the “redirect_uri” and tacks the OAuth token onto the end of the returned URL
The application also sets a cookie – this is the Authentication cookie which says that you are logged into O365 – but that is very different from the Authorization (OAuth) token which is currently in the URL. You will also note if you look carefully that there is an expiry time in the Query_String parameters (3599) so assuming this is in second and not minutes – the OAuth token is only good for 60 minutes….
The example application waits for the response and if it find an access_token in the URL it parses it, and then moves it into the OAuth Token field
In this example the Endpoint is a hard coded URL which represents the Root folder of the root site in my O365 site. It is an out of the box REST URL.
If we pull that up in a new tab we get the following
The reason I am getting this is because I have not actually authenticated with xomino.sharepoint.com at this time – I have logged into Azure AD. This is really interesting because if I run the example right now it will work – AzureAD has given me enough permission to make the request but not to go directly to the site. I say interesting, but it is actually slightly annoying from a developers perspective. Every time a user moves around O365 they have to go through the Single Sign on process – which is very cool, in that it works, but not the ideal user experience.
If I open https://xomino.sharepoint.com in a new tab – I get the cycle login screen and my SharePoint sites open automagically – SSO works. When I go back to the REST endpoint again now I get an authorized page.
The only reason I even bring this up is to show that Authorization is not the same as authentication. The Access_Token actually grants us permission to retrieve data and interact with the SharePoint site, even though technically we are not logged into it (yet). Pretty smart to be honest!
But this is XML – old school ATOM service. – we want new school JSON and the example app obliges.
Getting JSON Data
When the demo site realizes there is an access_token in the URL it hides the GET TOKEN button and shows the Make CORS Request button.
Clicking the Make CORS request button creates a very specific AJAX request – let’s look at the code directly.
var xhr = new XMLHttpRequest(); xhr.open("GET", getEndpointUrl()); xhr.setRequestHeader("authorization", "Bearer " + token); xhr.setRequestHeader("accept", "application/json"); xhr.send();
In this case the token is the very long access_token from the URL and the getEndpointUrl() is the xomino.sharepoint value in the EndpointURL field.
Once the AJAX call is made you can see what happens in the transfer using the browser developer tools (in this case firebug). The Accept Header is what will cause the O365 server to respond with JSON and not ATOM XML.
The Request Headers
This is what is going out to the O365 servers – as you can see the Accept Header and the Authorization header have been set.
The Response Headers
The critical thing to note here is that the Access-Control-Allow-Origin Header = “*” this allows the xomino365.azurewebsites.net domain to successfully request data from the xomino.sharepoint.com domain.
The response itself
The response is a JSON data object
making it more readable we can see the object is an array of “results” within d. Each item within the array represents the verbose detail of each file within the site collection.
In this article we have looked in some detail at how the Microsoft HowTo website example works and how the authorization/authentication mechanisms play with the AJAX requests to retrieve data from one domain into another.
In a future article we will look closer at what happens when we start to play with these requests and try and break the system…