Skip to content

Commit

Permalink
remove the htaccess files on production if they already exist, #148
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Sep 18, 2019
1 parent 8fb47dd commit 8bf28a5
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions js/common/writePhetioHtaccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,35 @@ module.exports = async function writePhetioHtaccess( passwordProtectPath, latest

const simPackage = isProductionDeploy ? JSON.parse( fs.readFileSync( `../${latestOption.simName}/package.json` ) ) : null;

const wrapperHtaccessPath = 'wrappers/.htaccess';
const wrapperHtaccessFullPath = `${passwordProtectPath}/${wrapperHtaccessPath}`;
const rootHtaccessPath = '.htaccess';
const rootHtaccessFullPath = `${passwordProtectPath}/${rootHtaccessPath}`;

// Only skip htaccess creation if in production deploy when the "allowPublicAccess" flag is present
if ( !( simPackage && simPackage.phet && simPackage.phet[ 'phet-io' ] && simPackage.phet[ 'phet-io' ].allowPublicAccess ) ) {
// If we are allowing public access, make sure that the htaccess files don't already exist locally already. This can
// occur when an rc is published (first) during a production deploy.
if ( simPackage && simPackage.phet && simPackage.phet[ 'phet-io' ] && simPackage.phet[ 'phet-io' ].allowPublicAccess ) {
try {
await fs.removeFileSync( wrapperHtaccessFullPath );
}
catch( e ) { /* don't worry about it */ }
try {
await fs.removeFileSync( rootHtaccessFullPath );
}
catch( e ) { /* don't worry about it */ }
}
else {
try {
const passwordProtectWrapperContents = 'AuthType Basic\n' +
'AuthName "PhET-iO Password Protected Area"\n' +
'AuthUserFile ' + authFilepath + '\n' +
'Require valid-user\n';

// Write a file to add authentication to the ./wrappers directory
let filePath = 'wrappers/.htaccess';
await writeFile( `${passwordProtectPath}/${filePath}`, passwordProtectWrapperContents );
await writeFile( wrapperHtaccessFullPath, passwordProtectWrapperContents );
if ( devVersionPath ) {
await devScp( `${passwordProtectPath}/${filePath}`, `${devVersionPath}/phet-io/${filePath}` );
await devScp( wrapperHtaccessFullPath, `${devVersionPath}/phet-io/${wrapperHtaccessPath}` );
}

const phetioPackage = JSON.parse( fs.readFileSync( '../phet-io/package.json' ) );
Expand All @@ -81,10 +97,9 @@ module.exports = async function writePhetioHtaccess( passwordProtectPath, latest
const passwordProtectIndexContents = '<FilesMatch "index.*">\n'
+ passwordProtectWrapperContents
+ '</FilesMatch>\n';
filePath = '.htaccess';
await writeFile( `${passwordProtectPath}/${filePath}`, passwordProtectIndexContents );
await writeFile( rootHtaccessFullPath, passwordProtectIndexContents );
if ( devVersionPath ) {
await devScp( `${passwordProtectPath}/${filePath}`, `${devVersionPath}/phet-io/${filePath}` );
await devScp( rootHtaccessFullPath, `${devVersionPath}/phet-io/${rootHtaccessPath}` );
}
}
winston.debug( 'phetio authentication htaccess written' );
Expand Down

0 comments on commit 8bf28a5

Please sign in to comment.