Skip to content

Commit

Permalink
gets new access token implicitly (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Wahome committed Oct 9, 2019
1 parent 537d766 commit a6f0f04
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 99 deletions.
36 changes: 0 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions src/app/services/actions/query-action-creator-util.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { ResponseType } from '@microsoft/microsoft-graph-client';
import { AuthenticationHandlerOptions, ResponseType } from '@microsoft/microsoft-graph-client';
import { MSALAuthenticationProviderOptions } from
'@microsoft/microsoft-graph-client/lib/src/MSALAuthenticationProviderOptions';
import { IAction } from '../../../types/action';
import { IQuery } from '../../../types/query-runner';
import { IRequestOptions } from '../../../types/request';
import { GraphClient } from '../graph-client';
import { authProvider } from '../graph-client/MsalAgent';
import { DEFAULT_USER_SCOPES } from '../graph-constants';
import { QUERY_GRAPH_SUCCESS } from '../redux-constants';
import { queryRunningStatus } from './query-loading-action-creators';

Expand Down Expand Up @@ -40,8 +44,9 @@ export async function anonymousRequest(dispatch: Function, query: IQuery) {
return fetch(graphUrl, options);
}

export function authenticatedRequest(dispatch: Function, query: IQuery) {
return makeRequest(query.selectedVerb)(dispatch, query);
export function authenticatedRequest(dispatch: Function, query: IQuery,
scopes: string[] = DEFAULT_USER_SCOPES.split(' ')) {
return makeRequest(query.selectedVerb, scopes)(dispatch, query);
}

export function isImageResponse(contentType: string) {
Expand Down Expand Up @@ -82,7 +87,7 @@ export function parseResponse(response: any, respHeaders: any): Promise<any> {
return response;
}

const makeRequest = (httpVerb: string): Function => {
const makeRequest = (httpVerb: string, scopes: string[]): Function => {
return async (dispatch: Function, query: IQuery) => {
const sampleHeaders: any = {};

Expand All @@ -94,8 +99,12 @@ const makeRequest = (httpVerb: string): Function => {
});
}

const msalAuthOptions = new MSALAuthenticationProviderOptions(scopes);
const middlewareOptions = new AuthenticationHandlerOptions(authProvider, msalAuthOptions);

const client = GraphClient.getInstance()
.api(query.sampleUrl)
.middlewareOptions([middlewareOptions])
.headers(sampleHeaders)
.responseType(ResponseType.RAW);

Expand Down
62 changes: 3 additions & 59 deletions src/app/services/graph-client/MsalService.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { AuthenticationParameters } from 'msal/lib-commonjs/AuthenticationParameters';
import { DEFAULT_USER_SCOPES } from '../graph-constants';
import { msalApplication } from './MsalAgent';

const defaultUserScopes = DEFAULT_USER_SCOPES.split(' ');
import { authProvider, msalApplication } from './MsalAgent';

export async function logIn(): Promise<any> {
const loginRequest: AuthenticationParameters = {
scopes: defaultUserScopes,
};

try {
await msalApplication.loginPopup(loginRequest);
const response = await msalApplication.acquireTokenPopup(loginRequest);
return response.accessToken;
const accessToken = await authProvider.getAccessToken();
return accessToken;
} catch (error) {
return null;
}
Expand All @@ -21,50 +12,3 @@ export async function logIn(): Promise<any> {
export function logOut() {
msalApplication.logout();
}

/**
* Generates a new access token from passed in scopes
* @param {string[]} scopes passed to generate token
* @returns {Promise.<any>}
*/
export async function acquireNewAccessToken(scopes: string[] = []): Promise<any> {
const hasScopes = (scopes.length > 0);
let listOfScopes = defaultUserScopes;
if (hasScopes) {
listOfScopes = scopes;
}

const loginRequest: AuthenticationParameters = {
scopes: listOfScopes,
};

try {
const response = await msalApplication.acquireTokenSilent(loginRequest);
return response.accessToken;
} catch (error) {
if (requiresInteraction(error)) {
try {
const response = await msalApplication.acquireTokenPopup(loginRequest);
return response.accessToken;
} catch (error) {
return null;
}
}
}
}

/**
* Checks whether the error requires user interaction
* Returns whether to load the POPUP/REDIRECT interaction
* @returns boolean
*/
function requiresInteraction(error: any): boolean {
const { errorCode } = error;
if (!errorCode || !errorCode.length) {
return false;
}
return errorCode === 'consent_required' ||
errorCode === 'interaction_required' ||
errorCode === 'login_required' ||
errorCode === 'token_renewal_error';
}

0 comments on commit a6f0f04

Please sign in to comment.