import { Utilities } from '../helpers/utilities' ; import { Storage , StorageType } from '../helpers/storage' ; export const DefaultEndpoints = { Google : 'Google' , Microsoft : 'Microsoft' , Facebook : 'Facebook' , AzureAD : 'AzureAD' , Dropbox : 'Dropbox' } ; export interface IEndpointConfiguration { provider ? : string ; clientId ? : string ; baseUrl ? : string ; authorizeUrl ? : string ; redirectUrl ? : string ; tokenUrl ? : string ; scope ? : string ; resource ? : string ; state ? : boolean ; nonce ? : boolean ; responseType ? : string ; extraQueryParameters ? : { [ index ] : string ; } ; } /**
 * Helper for creating and registering OAuth Endpoints.
 */ export class EndpointStorage extends Storage < IEndpointConfiguration > { constructor constructor ( storageType = StorageType . LocalStorage ) { super ( 'OAuth2Endpoints' , storageType ) ; } param { string } provider param { object } config see IEndpointConfiguration .
     *  return { object } add ( provider , config ) { if ( config . redirectUrl == null ) { config . redirectUrl = window . location . origin ; } config . provider = provider ; return super . set ( provider , config ) ; } param { string } clientId param { object } config return { object } registerGoogleAuth ( clientId , overrides ? ) { let defaults = < IEndpointConfiguration > { clientId : clientId , baseUrl : 'https://accounts.google.com' , authorizeUrl : '/o/oauth2/v2/auth' , resource : 'https://www.googleapis.com' , responseType : 'token' , scope : 'https://www.googleapis.com/auth/plus.me' , state : true } ; let config = { ... defaults , ... overrides } ; return this . add ( DefaultEndpoints . Google , config ) ; } param { string } clientId param { object } config return { object } registerMicrosoftAuth ( clientId , overrides ? ) { let defaults = < IEndpointConfiguration > { clientId : clientId , baseUrl : 'https://login.microsoftonline.com/common/oauth2/v2.0' , authorizeUrl : '/authorize' , responseType : 'token' , scope : 'https://graph.microsoft.com/user.read' , extraQueryParameters : { response_mode : 'fragment' } , nonce : true , state : true } ; let config = { ... defaults , ... overrides } ; this . add ( DefaultEndpoints . Microsoft , config ) ; } param { string } clientId param { object } config return { object } registerFacebookAuth ( clientId , overrides ? ) { let defaults = < IEndpointConfiguration > { clientId : clientId , baseUrl : 'https://www.facebook.com' , authorizeUrl : '/dialog/oauth' , resource : 'https://graph.facebook.com' , responseType : 'token' , scope : 'public_profile' , nonce : true , state : true } ; let config = { ... defaults , ... overrides } ; this . add ( DefaultEndpoints . Facebook , config ) ; } param { string } clientId param { string } tenant param { object } config return { object } registerAzureADAuth ( clientId , tenant , overrides ? ) { let defaults = < IEndpointConfiguration > { clientId : clientId , baseUrl : `https://login.windows.net/${ tenant }` , authorizeUrl : '/oauth2/authorize' , resource : 'https://graph.microsoft.com' , responseType : 'token' , nonce : true , state : true } ; let config = { ... defaults , ... overrides } ; this . add ( DefaultEndpoints . AzureAD , config ) ; } param { string } clientId param { object } config return { object } registerDropboxAuth ( clientId , overrides ? ) { let defaults = < IEndpointConfiguration > { clientId : clientId , baseUrl : `https://www.dropbox.com/1` , authorizeUrl : '/oauth2/authorize' , responseType : 'token' , state : true } ; let config = { ... defaults , ... overrides } ; this . add ( DefaultEndpoints . Dropbox , config ) ; } param { object } config return { object } static getLoginParams ( endpointConfig ) { let scope = ( endpointConfig . scope ) ? encodeURIComponent ( endpointConfig . scope ) : null ; let resource = ( endpointConfig . resource ) ? encodeURIComponent ( endpointConfig . resource ) : null ; let state = endpointConfig . state && Utilities . generateCryptoSafeRandom ( ) ; let nonce = endpointConfig . nonce && Utilities . generateCryptoSafeRandom ( ) ; let urlSegments = [ `response_type=${ endpointConfig . responseType }` , `client_id=${ encodeURIComponent ( endpointConfig . clientId ) }` , `redirect_uri=${ encodeURIComponent ( endpointConfig . redirectUrl ) }` ] ; if ( scope ) { urlSegments . push ( `scope=${ scope }` ) ; } if ( resource ) { urlSegments . push ( `resource=${ resource }` ) ; } if ( state ) { urlSegments . push ( `state=${ state }` ) ; } if ( nonce ) { urlSegments . push ( `nonce=${ nonce }` ) ; } if ( endpointConfig . extraQueryParameters ) { for ( let param of Object . keys ( endpointConfig . extraQueryParameters ) ) { urlSegments . push ( `${ param }=${ encodeURIComponent ( endpointConfig . extraQueryParameters [ param ] ) }` ) ; } } return { url : `${ endpointConfig . baseUrl }${ endpointConfig . authorizeUrl }?${ urlSegments . join ( '&' ) }` , state : state } ; } }