2017-10-28 22:18:07 +02:00
'use strict' ;
exports = module . exports = {
add : add ,
get : get ,
getAll : getAll ,
update : update ,
2018-01-01 19:19:07 -08:00
del : del ,
setAdmin : setAdmin
2017-10-28 22:18:07 +02:00
} ;
var assert = require ( 'assert' ) ,
domains = require ( '../domains.js' ) ,
DomainError = domains . DomainError ,
HttpError = require ( 'connect-lastmile' ) . HttpError ,
HttpSuccess = require ( 'connect-lastmile' ) . HttpSuccess ;
function add ( req , res , next ) {
assert . strictEqual ( typeof req . body , 'object' ) ;
if ( typeof req . body . domain !== 'string' ) return next ( new HttpError ( 400 , 'domain must be a string' ) ) ;
2018-01-09 14:46:38 -08:00
if ( typeof req . body . provider !== 'string' ) return next ( new HttpError ( 400 , 'provider must be a string' ) ) ;
2017-10-28 22:18:07 +02:00
if ( typeof req . body . config !== 'object' ) return next ( new HttpError ( 400 , 'config must be an object' ) ) ;
2017-11-07 02:15:55 +01:00
if ( 'zoneName' in req . body && typeof req . body . zoneName !== 'string' ) return next ( new HttpError ( 400 , 'zoneName must be a string' ) ) ;
2017-11-09 02:06:36 +01:00
if ( 'fallbackCertificate' in req . body && typeof req . body . fallbackCertificate !== 'object' ) return next ( new HttpError ( 400 , 'fallbackCertificate must be a object with cert and key strings' ) ) ;
if ( req . body . fallbackCertificate && ( ! req . body . cert || typeof req . body . cert !== 'string' ) ) return next ( new HttpError ( 400 , 'fallbackCertificate.cert must be a string' ) ) ;
if ( req . body . fallbackCertificate && ( ! req . body . key || typeof req . body . key !== 'string' ) ) return next ( new HttpError ( 400 , 'fallbackCertificate.key must be a string' ) ) ;
2018-01-31 17:42:26 +01:00
if ( 'tlsConfig' in req . body && typeof req . body . tlsConfig !== 'object' ) return next ( new HttpError ( 400 , 'tlsConfig must be a object with a provider string property' ) ) ;
if ( req . body . tlsConfig && ( ! req . body . tlsConfig . provider || typeof req . body . tlsConfig . provider !== 'string' ) ) return next ( new HttpError ( 400 , 'tlsConfig.provider must be a string' ) ) ;
2017-10-28 22:18:07 +02:00
2018-03-08 09:27:56 -08:00
// some DNS providers like DigitalOcean take a really long time to verify credentials (https://github.com/expressjs/timeout/issues/26)
req . clearTimeout ( ) ;
2018-01-31 18:37:05 -08:00
domains . add ( req . body . domain , req . body . zoneName || '' , req . body . provider , req . body . config , req . body . fallbackCertificate || null , req . body . tlsConfig || { provider : 'letsencrypt-prod' } , function ( error ) {
2017-11-20 22:53:14 +01:00
if ( error && error . reason === DomainError . ALREADY _EXISTS ) return next ( new HttpError ( 409 , error . message ) ) ;
2017-10-28 22:18:07 +02:00
if ( error && error . reason === DomainError . BAD _FIELD ) return next ( new HttpError ( 400 , error . message ) ) ;
if ( error && error . reason === DomainError . INVALID _PROVIDER ) return next ( new HttpError ( 400 , error . message ) ) ;
if ( error ) return next ( new HttpError ( 500 , error ) ) ;
next ( new HttpSuccess ( 201 , { domain : req . body . domain , config : req . body . config } ) ) ;
} ) ;
}
function get ( req , res , next ) {
assert . strictEqual ( typeof req . params . domain , 'string' ) ;
domains . get ( req . params . domain , function ( error , result ) {
if ( error && error . reason === DomainError . NOT _FOUND ) return next ( new HttpError ( 404 , error . message ) ) ;
if ( error ) return next ( new HttpError ( 500 , error ) ) ;
2018-01-31 18:22:33 -08:00
delete result . fallbackCertificate . key ; // do not return the 'key'. in caas, this is private
2017-10-28 22:18:07 +02:00
next ( new HttpSuccess ( 200 , result ) ) ;
} ) ;
}
function getAll ( req , res , next ) {
domains . getAll ( function ( error , result ) {
if ( error ) return next ( new HttpError ( 500 , error ) ) ;
next ( new HttpSuccess ( 200 , { domains : result } ) ) ;
} ) ;
}
function update ( req , res , next ) {
assert . strictEqual ( typeof req . params . domain , 'string' ) ;
assert . strictEqual ( typeof req . body , 'object' ) ;
2018-01-09 14:46:38 -08:00
if ( typeof req . body . provider !== 'string' ) return next ( new HttpError ( 400 , 'provider must be an object' ) ) ;
2017-10-28 22:18:07 +02:00
if ( typeof req . body . config !== 'object' ) return next ( new HttpError ( 400 , 'config must be an object' ) ) ;
2017-11-09 02:06:36 +01:00
if ( 'fallbackCertificate' in req . body && typeof req . body . fallbackCertificate !== 'object' ) return next ( new HttpError ( 400 , 'fallbackCertificate must be a object with cert and key strings' ) ) ;
2017-11-12 00:53:28 +01:00
if ( req . body . fallbackCertificate && ( ! req . body . fallbackCertificate . cert || typeof req . body . fallbackCertificate . cert !== 'string' ) ) return next ( new HttpError ( 400 , 'fallbackCertificate.cert must be a string' ) ) ;
if ( req . body . fallbackCertificate && ( ! req . body . fallbackCertificate . key || typeof req . body . fallbackCertificate . key !== 'string' ) ) return next ( new HttpError ( 400 , 'fallbackCertificate.key must be a string' ) ) ;
2018-01-31 17:42:26 +01:00
if ( 'tlsConfig' in req . body && typeof req . body . tlsConfig !== 'object' ) return next ( new HttpError ( 400 , 'tlsConfig must be a object with a provider string property' ) ) ;
if ( req . body . tlsConfig && ( ! req . body . tlsConfig . provider || typeof req . body . tlsConfig . provider !== 'string' ) ) return next ( new HttpError ( 400 , 'tlsConfig.provider must be a string' ) ) ;
2017-10-28 22:18:07 +02:00
2018-03-08 09:27:56 -08:00
// some DNS providers like DigitalOcean take a really long time to verify credentials (https://github.com/expressjs/timeout/issues/26)
req . clearTimeout ( ) ;
2018-01-31 18:37:05 -08:00
domains . update ( req . params . domain , req . body . provider , req . body . config , req . body . fallbackCertificate || null , req . body . tlsConfig || { provider : 'letsencrypt-prod' } , function ( error ) {
2017-10-28 22:18:07 +02:00
if ( error && error . reason === DomainError . NOT _FOUND ) return next ( new HttpError ( 404 , error . message ) ) ;
2017-11-07 02:29:28 +01:00
if ( error && error . reason === DomainError . BAD _FIELD ) return next ( new HttpError ( 400 , error . message ) ) ;
if ( error && error . reason === DomainError . INVALID _PROVIDER ) return next ( new HttpError ( 400 , error . message ) ) ;
2017-10-28 22:18:07 +02:00
if ( error ) return next ( new HttpError ( 500 , error ) ) ;
next ( new HttpSuccess ( 204 , { } ) ) ;
} ) ;
}
function del ( req , res , next ) {
assert . strictEqual ( typeof req . params . domain , 'string' ) ;
domains . del ( req . params . domain , function ( error ) {
if ( error && error . reason === DomainError . NOT _FOUND ) return next ( new HttpError ( 404 , error . message ) ) ;
2018-03-07 10:45:03 +01:00
if ( error && error . reason === DomainError . IN _USE ) return next ( new HttpError ( 409 , 'Domain is still in use. Remove all apps using this domain' ) ) ;
2017-10-28 22:18:07 +02:00
if ( error ) return next ( new HttpError ( 500 , error ) ) ;
2018-01-25 10:46:15 -08:00
next ( new HttpSuccess ( 204 ) ) ;
2017-10-28 22:18:07 +02:00
} ) ;
}
2018-01-01 19:19:07 -08:00
function setAdmin ( req , res , next ) {
assert . strictEqual ( typeof req . params . domain , 'string' ) ;
domains . setAdmin ( req . params . domain . toLowerCase ( ) , function ( error ) {
if ( error && error . reason === DomainError . NOT _FOUND ) return next ( new HttpError ( 404 , error . message ) ) ;
if ( error ) return next ( new HttpError ( 500 , error ) ) ;
next ( new HttpSuccess ( 202 , { } ) ) ;
} ) ;
}