2015-07-20 00:09:47 -07:00
/* jslint node:true */
'use strict' ;
exports = module . exports = {
activate : activate ,
setupTokenAuth : setupTokenAuth ,
getStatus : getStatus ,
reboot : reboot ,
getProgress : getProgress ,
getConfig : getConfig ,
update : update ,
2015-08-04 14:31:40 +02:00
feedback : feedback
2015-07-20 00:09:47 -07:00
} ;
var assert = require ( 'assert' ) ,
cloudron = require ( '../cloudron.js' ) ,
config = require ( '../config.js' ) ,
progress = require ( '../progress.js' ) ,
2015-08-04 14:45:42 +02:00
mailer = require ( '../mailer.js' ) ,
2015-07-20 00:09:47 -07:00
CloudronError = cloudron . CloudronError ,
debug = require ( 'debug' ) ( 'box:routes/cloudron' ) ,
HttpError = require ( 'connect-lastmile' ) . HttpError ,
HttpSuccess = require ( 'connect-lastmile' ) . HttpSuccess ,
2016-01-14 11:13:00 -08:00
superagent = require ( 'superagent' ) ;
2015-07-20 00:09:47 -07:00
/ * *
* Creating an admin user and activate the cloudron .
*
* @ apiParam { string } username The administrator ' s user name
* @ apiParam { string } password The administrator ' s password
* @ apiParam { string } email The administrator ' s email address
*
* @ apiSuccess ( Created 201 ) { string } token A valid access token
* /
function activate ( req , res , next ) {
assert . strictEqual ( typeof req . body , 'object' ) ;
assert . strictEqual ( typeof req . query . setupToken , 'string' ) ;
if ( typeof req . body . username !== 'string' ) return next ( new HttpError ( 400 , 'username must be string' ) ) ;
if ( typeof req . body . password !== 'string' ) return next ( new HttpError ( 400 , 'password must be string' ) ) ;
if ( typeof req . body . email !== 'string' ) return next ( new HttpError ( 400 , 'email must be string' ) ) ;
2016-01-20 16:14:21 +01:00
if ( 'displayName' in req . body && typeof req . body . displayName !== 'string' ) return next ( new HttpError ( 400 , 'displayName must be string' ) ) ;
2015-07-20 00:09:47 -07:00
var username = req . body . username ;
var password = req . body . password ;
var email = req . body . email ;
2016-01-19 23:34:49 -08:00
var displayName = req . body . displayName || '' ;
2015-07-20 00:09:47 -07:00
var ip = req . headers [ 'x-forwarded-for' ] || req . connection . remoteAddress ;
debug ( 'activate: username:%s ip:%s' , username , ip ) ;
2016-01-19 23:34:49 -08:00
cloudron . activate ( username , password , email , displayName , ip , function ( error , info ) {
2015-07-20 00:09:47 -07:00
if ( error && error . reason === CloudronError . ALREADY _PROVISIONED ) return next ( new HttpError ( 409 , 'Already setup' ) ) ;
if ( error && error . reason === CloudronError . BAD _USERNAME ) return next ( new HttpError ( 400 , 'Bad username' ) ) ;
if ( error && error . reason === CloudronError . BAD _PASSWORD ) return next ( new HttpError ( 400 , 'Bad password' ) ) ;
if ( error && error . reason === CloudronError . BAD _EMAIL ) return next ( new HttpError ( 400 , 'Bad email' ) ) ;
if ( error ) return next ( new HttpError ( 500 , error ) ) ;
2015-12-29 15:56:37 +01:00
// only in caas case do we have to notify the api server about activation
if ( config . provider ( ) !== 'caas' ) return next ( new HttpSuccess ( 201 , info ) ) ;
2015-07-20 00:09:47 -07:00
// Now let the api server know we got activated
2015-12-29 15:56:37 +01:00
superagent . post ( config . apiServerOrigin ( ) + '/api/v1/boxes/' + config . fqdn ( ) + '/setup/done' ) . query ( { setupToken : req . query . setupToken } ) . end ( function ( error , result ) {
2015-12-15 09:12:52 -08:00
if ( error && ! error . response ) return next ( new HttpError ( 500 , error ) ) ;
2015-07-20 00:09:47 -07:00
if ( result . statusCode === 403 ) return next ( new HttpError ( 403 , 'Invalid token' ) ) ;
if ( result . statusCode === 409 ) return next ( new HttpError ( 409 , 'Already setup' ) ) ;
2015-12-29 16:07:04 +01:00
if ( result . statusCode !== 201 ) return next ( new HttpError ( 500 , result . text || 'Internal error' ) ) ;
2015-07-20 00:09:47 -07:00
next ( new HttpSuccess ( 201 , info ) ) ;
} ) ;
} ) ;
}
function setupTokenAuth ( req , res , next ) {
assert . strictEqual ( typeof req . query , 'object' ) ;
2015-12-29 11:24:45 +01:00
// skip setupToken auth for non caas case
if ( config . provider ( ) !== 'caas' ) return next ( ) ;
2015-07-20 00:09:47 -07:00
if ( typeof req . query . setupToken !== 'string' ) return next ( new HttpError ( 400 , 'no setupToken provided' ) ) ;
superagent . get ( config . apiServerOrigin ( ) + '/api/v1/boxes/' + config . fqdn ( ) + '/setup/verify' ) . query ( { setupToken : req . query . setupToken } ) . end ( function ( error , result ) {
2015-12-15 09:12:52 -08:00
if ( error && ! error . response ) return next ( new HttpError ( 500 , error ) ) ;
2015-07-20 00:09:47 -07:00
if ( result . statusCode === 403 ) return next ( new HttpError ( 403 , 'Invalid token' ) ) ;
if ( result . statusCode === 409 ) return next ( new HttpError ( 409 , 'Already setup' ) ) ;
2015-12-29 16:07:04 +01:00
if ( result . statusCode !== 200 ) return next ( new HttpError ( 500 , result . text || 'Internal error' ) ) ;
2015-07-20 00:09:47 -07:00
next ( ) ;
} ) ;
}
function getStatus ( req , res , next ) {
cloudron . getStatus ( function ( error , status ) {
if ( error ) return next ( new HttpError ( 500 , error ) ) ;
next ( new HttpSuccess ( 200 , status ) ) ;
} ) ;
}
function getProgress ( req , res , next ) {
return next ( new HttpSuccess ( 200 , progress . get ( ) ) ) ;
}
function reboot ( req , res , next ) {
// Finish the request, to let the appstore know we triggered the restore it
next ( new HttpSuccess ( 202 , { } ) ) ;
cloudron . reboot ( ) ;
}
function getConfig ( req , res , next ) {
cloudron . getConfig ( function ( error , cloudronConfig ) {
if ( error ) return next ( new HttpError ( 500 , error ) ) ;
next ( new HttpSuccess ( 200 , cloudronConfig ) ) ;
} ) ;
}
function update ( req , res , next ) {
// this only initiates the update, progress can be checked via the progress route
2016-01-14 11:13:00 -08:00
cloudron . updateToLatest ( function ( error ) {
if ( error && error . reason === CloudronError . ALREADY _UPTODATE ) return next ( new HttpError ( 422 , error . message ) ) ;
2015-07-20 00:09:47 -07:00
if ( error && error . reason === CloudronError . BAD _STATE ) return next ( new HttpError ( 409 , error . message ) ) ;
if ( error ) return next ( new HttpError ( 500 , error ) ) ;
next ( new HttpSuccess ( 202 , { } ) ) ;
} ) ;
}
2015-08-04 14:31:40 +02:00
function feedback ( req , res , next ) {
assert . strictEqual ( typeof req . user , 'object' ) ;
2015-08-06 17:34:40 +02:00
if ( req . body . type !== mailer . FEEDBACK _TYPE _FEEDBACK && req . body . type !== mailer . FEEDBACK _TYPE _TICKET && req . body . type !== mailer . FEEDBACK _TYPE _APP ) return next ( new HttpError ( 400 , 'type must be either "ticket", "feedback" or "app"' ) ) ;
2015-08-04 16:59:35 +02:00
if ( typeof req . body . subject !== 'string' || ! req . body . subject ) return next ( new HttpError ( 400 , 'subject must be string' ) ) ;
if ( typeof req . body . description !== 'string' || ! req . body . description ) return next ( new HttpError ( 400 , 'description must be string' ) ) ;
2015-08-04 14:31:40 +02:00
2015-08-04 15:39:14 +02:00
mailer . sendFeedback ( req . user , req . body . type , req . body . subject , req . body . description ) ;
next ( new HttpSuccess ( 201 , { } ) ) ;
2015-08-04 14:31:40 +02:00
}