module import Config , { AuthorizationFlowType } from './config' ; import { DebugOptions } from './debug' ; import StateClient from './state-client' ; import { Position , TwitchAuth , TwitchContext } from './twitch' ; import { consolePrint , CurrentEnvironment } from './util' ; const TEST_AUTH_TIMEOUT_MS = 25 * 60 * 1000 ; export default class Ext { public static extensionID : string ; public static fetchTestAuth ( opts , cb ) { StateClient . fetchTestAuth ( this . extensionID , opts ) . then ( ( auth ) => { cb ( auth ) ; } ) . catch ( cb ) ; } public static fetchAdminAuth ( debug , cb ) { const allowedOrigins = [ '^https://.*?\\.muxy\\.io$' , '^https://.*?\\.staging\\.muxy\\.io$' , '^https?://localhost' ] ; let connectionAttempts = 0 ; const connection = setInterval ( ( ) => { connectionAttempts ++ ; if ( connectionAttempts > 60 ) { if ( connectionAttempts % 10 !== 0 ) { return ; } } window . parent . postMessage ( { type : 'connect' , id : this . extensionID } , '*' ) ; } , 150 ) ; window . addEventListener ( 'message' , ( auth ) => { let allowed = false ; allowedOrigins . forEach ( ( origin ) => { const r = new RegExp ( origin ) ; if ( r . test ( auth . origin ) ) { allowed = true ; } } ) ; if ( ! allowed ) { return ; } if ( auth . data . type === 'jwt' ) { StateClient . setEnvironment ( null , debug ) ; const resp = Object . assign ( new TwitchAuth ( ) , { channelId : debug . channelID , clientId : this . extensionID , token : auth . data . jwt , userId : debug . userID || 'T12345678' } ) ; clearInterval ( connection ) ; return cb ( resp ) ; } } ) ; } public static onAuthorized ( opts , cb ) { if ( opts . jwt ) { const auth = this . authFromJWT ( opts . jwt ) ; return setTimeout ( ( ) => { StateClient . setEnvironment ( null , opts ) ; cb ( auth ) ; } ) ; } const flowType = Config . GetAuthorizationFlowType ( CurrentEnvironment ( ) ) ; switch ( flowType ) { case AuthorizationFlowType . TestAuth : Ext . fetchTestAuth ( opts , cb ) ; setInterval ( Ext . fetchTestAuth , TEST_AUTH_TIMEOUT_MS , opts , cb ) ; break ; case AuthorizationFlowType . AdminAuth : Ext . fetchAdminAuth ( opts , cb ) ; break ; case AuthorizationFlowType . TwitchAuth : { const timer = setTimeout ( cb , 1000 * 15 ) ; window . Twitch . ext . onAuthorized ( ( auth ) => { clearTimeout ( timer ) ; StateClient . setEnvironment ( null , opts ) ; cb ( auth ) ; } ) ; break ; } default : consolePrint ( `No authorization callback for ${ CurrentEnvironment ( ) }` , { type : 'error' } ) ; } } public static onContext ( cb ) { if ( Config . CanUseTwitchAPIs ( CurrentEnvironment ( ) ) ) { window . Twitch . ext . onContext ( cb ) ; } } public static beginPurchase ( sku ) { if ( Config . CanUseTwitchAPIs ( CurrentEnvironment ( ) ) ) { window . Twitch . ext . purchases . beginPurchase ( sku ) ; } else { consolePrint ( `beginPurchase not supported for ${ CurrentEnvironment ( ) }` , { type : 'error' } ) ; } } public static getPrices ( cb ) { if ( Config . CanUseTwitchAPIs ( CurrentEnvironment ( ) ) ) { window . Twitch . ext . purchases . getPrices ( ) . then ( ( prices ) => { cb ( prices ) ; } ) . catch ( cb ) ; } else { consolePrint ( `getPrices not supported for ${ CurrentEnvironment ( ) }` , { type : 'error' } ) ; } } public static onReloadEntitlements ( cb ) { if ( Config . CanUseTwitchAPIs ( CurrentEnvironment ( ) ) ) { window . Twitch . ext . purchases . onReloadEntitlements ( cb ) ; } else { consolePrint ( `onReloadEntitlements not supported for ${ CurrentEnvironment ( ) }` , { type : 'error' } ) ; } } public static onVisibilityChanged ( callback ) { if ( Config . CanUseTwitchAPIs ( CurrentEnvironment ( ) ) ) { window . Twitch . ext . onVisibilityChanged ( callback ) ; } else { consolePrint ( `onVisibilityChanged not supported for ${ CurrentEnvironment ( ) }` , { type : 'error' } ) ; } } public static onPositionChanged ( callback ) { if ( Config . CanUseTwitchAPIs ( CurrentEnvironment ( ) ) ) { window . Twitch . ext . onPositionChanged ( callback ) ; } else { consolePrint ( `onVisibilityChanged not supported for ${ CurrentEnvironment ( ) }` , { type : 'error' } ) ; } } public static onHighlightChanged ( callback ) { if ( Config . CanUseTwitchAPIs ( CurrentEnvironment ( ) ) ) { window . Twitch . ext . onHighlightChanged ( callback ) ; } else { consolePrint ( `onHighlightChanged not supported for ${ CurrentEnvironment ( ) }` , { type : 'error' } ) ; } } private static authFromJWT ( jwt ) { const claims = JSON . parse ( atob ( jwt . split ( '.' ) [ 1 ] ) ) ; const res = new TwitchAuth ( ) ; res . token = jwt ; res . channelId = claims . channel_id ; res . userId = claims . user_id ; res . clientId = Ext . extensionID ; return res ; } }