Search code examples
javascriptadaloffice-addinsoffice-jsadal.js

Authenticating using OfficeDev/office-js-helpers rather than adal


I'm working on an Office Add-in that currently uses adal to obtain an auth token.

As I want to use the Fabric front end I am changing it to React and I notice that the officer-js-helpers have implemented authenticators that seem to do the same job as the adal library. Am I correct in this assumption? If so, how do I duplicate this adal config using the office-js-helpers authentication functions:

var adalConfig = {
    instance: 'https://login.microsoftonline.com/',
    tenant: 'myprivatesite.onmicrosoft.com',
    clientId: 'xxx-xxx-xxx-xxx-xxx',
    endpoints: {
      'https://my.private.url.endpoint/path': 'https://myprivatesite.onmicrosoft.com/path.to.something',
    }

And this token request:

var authContext = new AuthenticationContext(adalConfig);
 authContext.acquireToken('https://myprivatesite.onmicrosoft.com/path.to.something', function (error, token) {
        console.log(token)
      });

UPDATE: I have got the adal.js library working in my react app. I have used some of the code from the init function in the adalAuthenticationService angular provider to retrieve the authentication token.

So the question remains. Can I use the office-js-helpers to do the same thing?


Solution

  • OK It appears it is extremely easy. All that is required from the adal configuration is the client Id and the tenant.

    if (OfficeHelpers.Authenticator.isAuthDialog()) {
      return;
    }
    
    var authenticator = new OfficeHelpers.Authenticator();
    
    authenticator.endpoints.registerAzureADAuth('xxx-xxx-xxx-xxx-xxx', //clientId
    'myprivatesite.onmicrosoft.com' // tenant
    );
    
    authenticator.authenticate(OfficeHelpers.DefaultEndpoints.AzureAD)
      .then(function (token) {
        console.log(token);
      .catch(function(error) {
        console.log(error);
      });