import Debug from "debug" ; import { OAuthConfigType , WindowOptions } from "../" ; import { BrowserWindow } from "electron" ; import { validate , needWindowForGrantType } from "./helper" ; import { flowTaskFor } from "./tasks" ; import { EventEmitter } from "events" ; import { ResponseType } from "../utils" ; const debug = Debug ( "eoh:oauth2" ) ; export interface OAuth2EmitterType { emit ( event , parameters ) : boolean ; emit ( event , parameters , headers ) : boolean ; } export default interface OAuth2Provider { on ( event , listener ) : this ; on ( event , listener ) : this ; } export default class OAuth2Provider extends EventEmitter implements OAuth2EmitterType { config : OAuthConfigType ; finished : boolean ; userCancelError ? : Error ; constructor ( config ) { super ( ) ; const error = validate ( config ) ; if ( error ) { throw error ; } this . config = config ; this . finished = false ; } example async perform ( window ? , windowOptions ? ) { const config = this . config ; const task = flowTaskFor ( config ) ; if ( needWindowForGrantType ( config ) ) { this . finished = false ; if ( ! window ) { return Promise . reject ( new Error ( "window is required." ) ) ; } window . once ( "show" , ( ) => { window . once ( "close" , ( ) => { if ( this . finished === false ) { this . userCancelError = new Error ( "User cancelled" ) ; } } ) ; } ) ; } debug ( "start OAUTH2" ) ; const resp = await task ( config , this , window , windowOptions ) ; this . finished = true ; if ( this . userCancelError ) { debug ( "User cancelled" ) ; return Promise . reject ( this . userCancelError ) ; } debug ( "success" , resp ) ; return Promise . resolve ( resp ) ; } }