import Debug from "debug" ; import { requestOAuthToken , requestAccessToken , redirectAuthPage , OAuth1Config , OAuth1URLs , } from "./helper" ; import { BrowserWindow } from "electron" ; import { pick } from "../utils" ; const debug = Debug ( "eoh:oauth1" ) ; export default class OAuth1Provider { public config : OAuth1Config ; public urls : OAuth1URLs ; private finished : Boolean = false ; constructor ( config ) { this . config = pick ( config , [ "oauth_consumer_key" , "oauth_consumer_secret" , "oauth_callback" , ] ) ; this . urls = pick ( config , [ "request_token_url" , "authenticate_url" , "access_token_url" , ] ) ; } public perform ( window ) { this . finished = false ; return new Promise ( ( resolve , reject ) => { return requestOAuthToken ( this . urls . request_token_url , this . config ) . then ( oauth_token => { debug ( "Step 1 response" , oauth_token ) ; window . once ( "close" , ( ) => { if ( this . finished === false ) { reject ( new Error ( "window closed" ) ) ; } } ) ; return redirectAuthPage ( this . urls . authenticate_url , this . config , oauth_token , window ) ; } ) . then ( resp => { debug ( "Step 2 response" , resp ) ; return requestAccessToken ( this . urls . access_token_url , this . config , resp . oauth_token , resp . oauth_verifier ) ; } ) . then ( resp => { debug ( "Step 3 response" , resp ) ; this . finished = true ; resolve ( resp ) ; } ) . catch ( error => reject ( error ) ) ; } ) ; } }