import AuthenticationContext from 'adal-angular' ; import adalConfig from './adalConfig' ; import q from 'q' ; var _adal = new AuthenticationContext ( adalConfig ) ; var _oauthData = { isAuthenticated : false , userName : '' , loginError : '' , profile : '' } ; var processAdalCallback = function ( ) { var hash = window . location . hash ; if ( _adal . isCallback ( hash ) ) { var requestInfo = _adal . getRequestInfo ( hash ) ; _adal . saveTokenFromHash ( requestInfo ) ; window . location . hash = '' ; if ( requestInfo . requestType !== _adal . REQUEST_TYPE . LOGIN ) { if ( ( window as any ) . parent . AuthenticationContext === 'function' && ( window as any ) . parent . AuthenticationContext ( ) ) { _adal . callback = ( window as any ) . parent . AuthenticationContext ( ) . callback ; } if ( requestInfo . requestType === _adal . REQUEST_TYPE . RENEW_TOKEN ) { _adal . callback = ( window as any ) . parent . callBackMappedToRenewStates [ requestInfo . stateResponse ] ; } } if ( requestInfo . stateMatch ) { if ( typeof _adal . callback === 'function' ) { if ( requestInfo . requestType === _adal . REQUEST_TYPE . RENEW_TOKEN ) { if ( requestInfo . parameters [ 'access_token' ] ) { _adal . callback ( _adal . _getItem ( _adal . CONSTANTS . STORAGE . ERROR_DESCRIPTION ) , requestInfo . parameters [ 'access_token' ] ) ; return ; } else if ( requestInfo . parameters [ 'id_token' ] ) { _adal . callback ( _adal . _getItem ( _adal . CONSTANTS . STORAGE . ERROR_DESCRIPTION ) , requestInfo . parameters [ 'id_token' ] ) ; return ; } } } else { updateDataFromCache ( _adal . config . loginResource ) ; if ( _oauthData . userName ) { window . setTimeout ( function ( ) { updateDataFromCache ( _adal . config . loginResource ) ; var loginStartPage = _adal . _getItem ( _adal . CONSTANTS . STORAGE . START_PAGE ) ; if ( loginStartPage ) { ( window as any ) . location . path ( loginStartPage ) ; } } , 1 ) ; } } } } } ; var updateDataFromCache = function ( resource ) { var token = _adal . getCachedToken ( resource ) ; _oauthData . isAuthenticated = token !== null && token . length > 0 ; var user = _adal . getCachedUser ( ) || { userName : '' } ; _oauthData . userName = user . userName ; _oauthData . profile = user . profile ; _oauthData . loginError = _adal . getLoginError ( ) ; } ; var isAuthenticated = function ( ) { var deferred = q . defer ( ) ; updateDataFromCache ( _adal . config . loginResource ) ; if ( ! _adal . _renewActive && ! _oauthData . isAuthenticated && ! _oauthData . userName ) { if ( ! _adal . _getItem ( _adal . CONSTANTS . STORAGE . FAILED_RENEW ) ) { _adal . acquireToken ( _adal . config . loginResource , function ( error , tokenOut ) { if ( error ) { _adal . error ( 'adal:loginFailure' , 'auto renew failure' ) ; deferred . reject ( ) ; } else { if ( tokenOut ) { _oauthData . isAuthenticated = true ; deferred . resolve ( ) ; } else { deferred . reject ( ) ; } } } ) ; } else { deferred . resolve ( ) ; } } else { deferred . resolve ( ) ; } return deferred . promise ; } ; var makeRequest = function ( settings ) { var deferred = q . defer ( ) ; var xhr = new XMLHttpRequest ( ) ; xhr . onreadystatechange = function ( ) { if ( this . readyState === 4 ) { if ( this . status === 200 ) { deferred . resolve ( this . response ) ; } else if ( this . status >= 400 ) { deferred . reject ( ) ; } } } ; xhr . open ( settings . method || 'GET' , settings . url , true ) ; for ( var header in settings . headers ) { xhr . setRequestHeader ( header , settings . headers [ header ] ) ; } xhr . responseType = settings . dataType || 'json' ; xhr . send ( settings . data ) ; return deferred . promise ; } ; var adalRequest = function ( settings ) { var deferred = q . defer ( ) ; isAuthenticated ( ) . then ( function ( ) { var resource = _adal . getResourceForEndpoint ( settings . url ) ; if ( ! resource ) { _adal . info ( 'No resource configured for \'' + settings . url + '\'' ) ; deferred . reject ( ) ; return deferred . promise ; } var tokenStored = _adal . getCachedToken ( resource ) ; if ( tokenStored ) { if ( ! settings . headers ) { settings . headers = { } ; } settings . headers . Authorization = 'Bearer ' + tokenStored ; makeRequest ( settings ) . then ( deferred . resolve , deferred . reject ) ; } else { var isEndpoint = false ; for ( var endpointUrl in _adal . config . endpoints ) { if ( settings . url . indexOf ( endpointUrl ) > - 1 ) { isEndpoint = true ; } } if ( _adal . loginInProgress ( ) ) { _adal . info ( 'Login already in progress' ) ; deferred . reject ( ) ; } else if ( isEndpoint ) { _adal . acquireToken ( resource , function ( error , tokenOut ) { if ( error ) { debugger ; deferred . reject ( ) ; _adal . error ( error ) ; } else { debugger ; if ( tokenOut ) { _adal . verbose ( 'Token is available' ) ; if ( ! settings . headers ) { settings . headers = { } ; } settings . headers . Authorization = 'Bearer ' + tokenOut ; makeRequest ( settings ) . then ( deferred . resolve , deferred . reject ) ; } } } ) ; } } } , function ( ) { _adal . login ( ) ; } ) ; return deferred . promise ; } ; export { adalRequest , processAdalCallback } ;