Updated ADAL JS implementation (#28)

This commit is contained in:
Waldek Mastykarz 2016-09-30 09:09:02 +02:00 committed by GitHub
parent 7f4df35a98
commit 67795ba337
5 changed files with 93 additions and 5 deletions

View File

@ -13,7 +13,6 @@
"@microsoft/sp-client-base": "~0.3.0",
"@microsoft/sp-client-preview": "~0.4.0",
"adal-angular": "^1.0.12",
"expose-loader": "^0.7.1",
"office-ui-fabric-react": "0.36.0",
"react": "0.14.8",
"react-dom": "0.14.8"

View File

@ -1,4 +1,5 @@
export interface IAdalConfig extends adal.Config {
popUp?: boolean;
callback?: (error: any, token: string) => void;
webPartId?: string;
}

View File

@ -0,0 +1,83 @@
const AuthenticationContext = require('adal-angular');
AuthenticationContext.prototype._getItemSuper = AuthenticationContext.prototype._getItem;
AuthenticationContext.prototype._saveItemSuper = AuthenticationContext.prototype._saveItem;
AuthenticationContext.prototype.handleWindowCallbackSuper = AuthenticationContext.prototype.handleWindowCallback;
AuthenticationContext.prototype._renewTokenSuper = AuthenticationContext.prototype._renewToken;
AuthenticationContext.prototype.getRequestInfoSuper = AuthenticationContext.prototype.getRequestInfo;
AuthenticationContext.prototype._getItem = function (key) {
if (this.config.webPartId) {
key = this.config.webPartId + '_' + key;
}
return this._getItemSuper(key);
};
AuthenticationContext.prototype._saveItem = function (key, object) {
if (this.config.webPartId) {
key = this.config.webPartId + '_' + key;
}
return this._saveItemSuper(key, object);
};
AuthenticationContext.prototype.handleWindowCallback = function (hash) {
if (hash == null) {
hash = window.location.hash;
}
if (!this.isCallback(hash)) {
return;
}
var requestInfo = this.getRequestInfo(hash);
if (requestInfo.requestType === this.REQUEST_TYPE.LOGIN) {
return this.handleWindowCallbackSuper(hash);
}
var resource = this._getResourceFromState(requestInfo.stateResponse);
if (!resource || resource.length === 0) {
return;
}
if (this._getItem(this.CONSTANTS.STORAGE.RENEW_STATUS + resource) === this.CONSTANTS.TOKEN_RENEW_STATUS_IN_PROGRESS) {
return this.handleWindowCallbackSuper(hash);
}
}
AuthenticationContext.prototype._renewToken = function (resource, callback) {
this._renewTokenSuper(resource, callback);
var _renewStates = this._getItem('renewStates');
if (_renewStates) {
_renewStates = _renewStates.split(';');
}
else {
_renewStates = [];
}
_renewStates.push(this.config.state);
this._saveItem('renewStates', _renewStates);
}
AuthenticationContext.prototype.getRequestInfo = function (hash) {
var requestInfo = this.getRequestInfoSuper(hash);
var _renewStates = this._getItem('renewStates');
if (!_renewStates) {
return requestInfo;
}
_renewStates = _renewStates.split(';');
for (var i = 0; i < _renewStates.length; i++) {
if (_renewStates[i] === requestInfo.stateResponse) {
requestInfo.requestType = this.REQUEST_TYPE.RENEW_TOKEN;
requestInfo.stateMatch = true;
break;
}
}
return requestInfo;
}
window.AuthenticationContext = function() {
return undefined;
}

View File

@ -20,7 +20,8 @@ export default class UpcomingMeetingsWebPart extends BaseClientSideWebPart<IUpco
public render(): void {
const element: React.ReactElement<IUpcomingMeetingsProps> = React.createElement(UpcomingMeetings, {
httpClient: this.context.httpClient,
title: this.properties.title
title: this.properties.title,
webPartId: this.context.instanceId
});
ReactDom.render(element, this.domElement);

View File

@ -8,14 +8,16 @@ import {
import styles from '../UpcomingMeetings.module.scss';
import { IUpcomingMeetingsWebPartProps } from '../IUpcomingMeetingsWebPartProps';
import { HttpClient } from '@microsoft/sp-client-base';
const AuthenticationContext = require('expose?AuthenticationContext!adal-angular');
const AuthenticationContext = require('adal-angular');
import adalConfig from '../AdalConfig';
import { IAdalConfig } from '../IAdalConfig';
import { IAdalConfig } from '../../IAdalConfig';
import '../../WebPartAuthenticationContext';
import { ListItem } from './ListItem';
import { IMeeting } from './IMeeting';
export interface IUpcomingMeetingsProps extends IUpcomingMeetingsWebPartProps {
httpClient: HttpClient;
webPartId: string;
}
export interface IUpcomingMeetingsState {
@ -63,6 +65,7 @@ export default class UpcomingMeetings extends React.Component<IUpcomingMeetingsP
const config: IAdalConfig = adalConfig;
config.popUp = true;
config.webPartId = this.props.webPartId;
config.callback = (error: any, token: string): void => {
this.setState((previousState: IUpcomingMeetingsState, currentProps: IUpcomingMeetingsProps): IUpcomingMeetingsState => {
previousState.error = error;
@ -72,6 +75,7 @@ export default class UpcomingMeetings extends React.Component<IUpcomingMeetingsP
};
this.authCtx = new AuthenticationContext(config);
AuthenticationContext.prototype._singletonInstance = undefined;
}
public componentDidMount(): void {