2017-11-03 02:09:34 +02:00
const { _ } = require ( 'lib/locale.js' ) ;
const { netUtils } = require ( 'lib/net-utils.js' ) ;
2017-07-18 20:21:03 +02:00
2019-07-30 09:35:42 +02:00
const http = require ( 'http' ) ;
const urlParser = require ( 'url' ) ;
2017-07-06 20:58:01 +02:00
const enableServerDestroy = require ( 'server-destroy' ) ;
class OneDriveApiNodeUtils {
constructor ( api ) {
this . api _ = api ;
2017-10-24 20:03:12 +02:00
this . oauthServer _ = null ;
2017-07-06 20:58:01 +02:00
}
api ( ) {
return this . api _ ;
}
possibleOAuthDancePorts ( ) {
2017-07-28 20:13:07 +02:00
return [ 9967 , 8967 , 8867 ] ;
2017-07-06 20:58:01 +02:00
}
2017-07-26 23:07:27 +02:00
makePage ( message ) {
const header = `
< ! doctype html >
< html > < head > < meta charset = "utf-8" > < / h e a d > < b o d y > ` ;
const footer = `
< / b o d y > < / h t m l >
` ;
return header + message + footer ;
}
2017-10-24 20:03:12 +02:00
cancelOAuthDance ( ) {
if ( ! this . oauthServer _ ) return ;
this . oauthServer _ . destroy ( ) ;
}
2017-07-06 20:58:01 +02:00
async oauthDance ( targetConsole = null ) {
if ( targetConsole === null ) targetConsole = console ;
this . api ( ) . setAuth ( null ) ;
2017-10-30 23:29:36 +02:00
const port = await netUtils . findAvailablePort ( this . possibleOAuthDancePorts ( ) , 0 ) ;
2017-07-18 20:49:47 +02:00
if ( ! port ) throw new Error ( _ ( 'All potential ports are in use - please report the issue at %s' , 'https://github.com/laurent22/joplin' ) ) ;
2017-07-06 20:58:01 +02:00
let authCodeUrl = this . api ( ) . authCodeUrl ( 'http://localhost:' + port ) ;
2019-07-30 09:35:42 +02:00
return new Promise ( ( resolve , reject ) => {
2017-10-24 20:03:12 +02:00
this . oauthServer _ = http . createServer ( ) ;
2017-07-06 20:58:01 +02:00
let errorMessage = null ;
2017-10-24 20:03:12 +02:00
this . oauthServer _ . on ( 'request' , ( request , response ) => {
2017-10-25 19:23:45 +02:00
const url = urlParser . parse ( request . url , true ) ;
if ( url . pathname === '/auth' ) {
2019-07-30 09:35:42 +02:00
response . writeHead ( 302 , { Location : authCodeUrl } ) ;
2017-10-25 19:23:45 +02:00
response . end ( ) ;
return ;
}
const query = url . query ;
2017-07-06 20:58:01 +02:00
2017-07-26 23:07:27 +02:00
const writeResponse = ( code , message ) => {
2019-07-30 09:35:42 +02:00
response . writeHead ( code , { 'Content-Type' : 'text/html' } ) ;
2017-07-26 23:07:27 +02:00
response . write ( this . makePage ( message ) ) ;
2017-07-06 20:58:01 +02:00
response . end ( ) ;
2019-07-30 09:35:42 +02:00
} ;
2017-07-06 20:58:01 +02:00
2017-10-19 00:13:53 +02:00
// After the response has been received, don't destroy the server right
// away or the browser might display a connection reset error (even
// though it worked).
const waitAndDestroy = ( ) => {
setTimeout ( ( ) => {
2017-10-24 20:03:12 +02:00
this . oauthServer _ . destroy ( ) ;
this . oauthServer _ = null ;
2017-10-19 00:13:53 +02:00
} , 1000 ) ;
2019-07-30 09:35:42 +02:00
} ;
2017-10-19 00:13:53 +02:00
2017-07-06 20:58:01 +02:00
if ( ! query . code ) return writeResponse ( 400 , '"code" query parameter is missing' ) ;
2019-07-30 09:35:42 +02:00
this . api ( )
. execTokenRequest ( query . code , 'http://localhost:' + port . toString ( ) )
. then ( ( ) => {
writeResponse ( 200 , _ ( 'The application has been authorised - you may now close this browser tab.' ) ) ;
targetConsole . log ( '' ) ;
targetConsole . log ( _ ( 'The application has been successfully authorised.' ) ) ;
waitAndDestroy ( ) ;
} )
. catch ( error => {
writeResponse ( 400 , error . message ) ;
targetConsole . log ( '' ) ;
targetConsole . log ( error . message ) ;
waitAndDestroy ( ) ;
} ) ;
2017-07-06 20:58:01 +02:00
} ) ;
2017-10-24 20:03:12 +02:00
this . oauthServer _ . on ( 'close' , ( ) => {
2017-07-06 20:58:01 +02:00
if ( errorMessage ) {
reject ( new Error ( errorMessage ) ) ;
} else {
resolve ( this . api ( ) . auth ( ) ) ;
}
} ) ;
2017-10-24 20:03:12 +02:00
this . oauthServer _ . listen ( port ) ;
2017-07-06 20:58:01 +02:00
2017-10-24 20:03:12 +02:00
enableServerDestroy ( this . oauthServer _ ) ;
2017-07-06 20:58:01 +02:00
2017-10-25 19:23:45 +02:00
// Rather than displaying authCodeUrl directly, we go throught the local
// server. This is just so that the URL being displayed is shorter and
// doesn't get cut in terminals (especially those that don't handle multi
// lines URLs).
2017-08-05 00:15:08 +02:00
targetConsole . log ( _ ( 'Please open the following URL in your browser to authenticate the application. The application will create a directory in "Apps/Joplin" and will only read and write files in this directory. It will have no access to any files outside this directory nor to any other personal data. No data will be shared with any third party.' ) ) ;
2017-07-06 20:58:01 +02:00
targetConsole . log ( '' ) ;
2017-10-25 19:23:45 +02:00
targetConsole . log ( 'http://127.0.0.1:' + port + '/auth' ) ;
2017-07-06 20:58:01 +02:00
} ) ;
}
}
2019-07-30 09:35:42 +02:00
module . exports = { OneDriveApiNodeUtils } ;