BVB Source Codes

parse-server Show Config.js Source code

Return Download parse-server: download Config.js Source code - Download parse-server Source code - Type:.js
  1. // A Config object provides information about how a specific app is
  2. // configured.
  3. // mount is the URL for the root of the API; includes http, domain, etc.
  4.  
  5. import AppCache from './cache';
  6. import SchemaCache from './Controllers/SchemaCache';
  7. import DatabaseController from './Controllers/DatabaseController';
  8.  
  9. function removeTrailingSlash(str) {
  10.   if (!str) {
  11.     return str;
  12.   }
  13.   if (str.endsWith("/")) {
  14.     str = str.substr(0, str.length - 1);
  15.   }
  16.   return str;
  17. }
  18.  
  19. export class Config {
  20.   constructor(applicationId: string, mount: string) {
  21.     const cacheInfo = AppCache.get(applicationId);
  22.     if (!cacheInfo) {
  23.       return;
  24.     }
  25.  
  26.     this.applicationId = applicationId;
  27.     this.jsonLogs = cacheInfo.jsonLogs;
  28.     this.masterKey = cacheInfo.masterKey;
  29.     this.clientKey = cacheInfo.clientKey;
  30.     this.javascriptKey = cacheInfo.javascriptKey;
  31.     this.dotNetKey = cacheInfo.dotNetKey;
  32.     this.restAPIKey = cacheInfo.restAPIKey;
  33.     this.webhookKey = cacheInfo.webhookKey;
  34.     this.fileKey = cacheInfo.fileKey;
  35.     this.allowClientClassCreation = cacheInfo.allowClientClassCreation;
  36.     this.userSensitiveFields = cacheInfo.userSensitiveFields;
  37.  
  38.     // Create a new DatabaseController per request
  39.     if (cacheInfo.databaseController) {
  40.       const schemaCache = new SchemaCache(cacheInfo.cacheController, cacheInfo.schemaCacheTTL, cacheInfo.enableSingleSchemaCache);
  41.       this.database = new DatabaseController(cacheInfo.databaseController.adapter, schemaCache);
  42.     }
  43.  
  44.     this.schemaCacheTTL = cacheInfo.schemaCacheTTL;
  45.     this.enableSingleSchemaCache = cacheInfo.enableSingleSchemaCache;
  46.  
  47.     this.serverURL = cacheInfo.serverURL;
  48.     this.publicServerURL = removeTrailingSlash(cacheInfo.publicServerURL);
  49.     this.verifyUserEmails = cacheInfo.verifyUserEmails;
  50.     this.preventLoginWithUnverifiedEmail = cacheInfo.preventLoginWithUnverifiedEmail;
  51.     this.emailVerifyTokenValidityDuration = cacheInfo.emailVerifyTokenValidityDuration;
  52.     this.accountLockout = cacheInfo.accountLockout;
  53.     this.passwordPolicy = cacheInfo.passwordPolicy;
  54.     this.appName = cacheInfo.appName;
  55.  
  56.     this.analyticsController = cacheInfo.analyticsController;
  57.     this.cacheController = cacheInfo.cacheController;
  58.     this.hooksController = cacheInfo.hooksController;
  59.     this.filesController = cacheInfo.filesController;
  60.     this.pushController = cacheInfo.pushController;
  61.     this.pushControllerQueue = cacheInfo.pushControllerQueue;
  62.     this.pushWorker = cacheInfo.pushWorker;
  63.     this.hasPushSupport = cacheInfo.hasPushSupport;
  64.     this.hasPushScheduledSupport = cacheInfo.hasPushScheduledSupport;
  65.     this.loggerController = cacheInfo.loggerController;
  66.     this.userController = cacheInfo.userController;
  67.     this.authDataManager = cacheInfo.authDataManager;
  68.     this.customPages = cacheInfo.customPages || {};
  69.     this.mount = removeTrailingSlash(mount);
  70.     this.liveQueryController = cacheInfo.liveQueryController;
  71.     this.sessionLength = cacheInfo.sessionLength;
  72.     this.expireInactiveSessions = cacheInfo.expireInactiveSessions;
  73.     this.generateSessionExpiresAt = this.generateSessionExpiresAt.bind(this);
  74.     this.generateEmailVerifyTokenExpiresAt = this.generateEmailVerifyTokenExpiresAt.bind(this);
  75.     this.revokeSessionOnPasswordReset = cacheInfo.revokeSessionOnPasswordReset;
  76.   }
  77.  
  78.   static validate({
  79.     verifyUserEmails,
  80.     userController,
  81.     appName,
  82.     publicServerURL,
  83.     revokeSessionOnPasswordReset,
  84.     expireInactiveSessions,
  85.     sessionLength,
  86.     emailVerifyTokenValidityDuration,
  87.     accountLockout,
  88.     passwordPolicy
  89.   }) {
  90.     const emailAdapter = userController.adapter;
  91.     if (verifyUserEmails) {
  92.       this.validateEmailConfiguration({emailAdapter, appName, publicServerURL, emailVerifyTokenValidityDuration});
  93.     }
  94.  
  95.     this.validateAccountLockoutPolicy(accountLockout);
  96.  
  97.     this.validatePasswordPolicy(passwordPolicy);
  98.  
  99.     if (typeof revokeSessionOnPasswordReset !== 'boolean') {
  100.       throw 'revokeSessionOnPasswordReset must be a boolean value';
  101.     }
  102.  
  103.     if (publicServerURL) {
  104.       if (!publicServerURL.startsWith("http://") && !publicServerURL.startsWith("https://")) {
  105.         throw "publicServerURL should be a valid HTTPS URL starting with https://"
  106.       }
  107.     }
  108.  
  109.     this.validateSessionConfiguration(sessionLength, expireInactiveSessions);
  110.   }
  111.  
  112.   static validateAccountLockoutPolicy(accountLockout) {
  113.     if (accountLockout) {
  114.       if (typeof accountLockout.duration !== 'number' || accountLockout.duration <= 0 || accountLockout.duration > 99999) {
  115.         throw 'Account lockout duration should be greater than 0 and less than 100000';
  116.       }
  117.  
  118.       if (!Number.isInteger(accountLockout.threshold) || accountLockout.threshold < 1 || accountLockout.threshold > 999) {
  119.         throw 'Account lockout threshold should be an integer greater than 0 and less than 1000';
  120.       }
  121.     }
  122.   }
  123.  
  124.   static validatePasswordPolicy(passwordPolicy) {
  125.     if (passwordPolicy) {
  126.       if (passwordPolicy.maxPasswordAge !== undefined && (typeof passwordPolicy.maxPasswordAge !== 'number' || passwordPolicy.maxPasswordAge < 0)) {
  127.         throw 'passwordPolicy.maxPasswordAge must be a positive number';
  128.       }
  129.  
  130.       if (passwordPolicy.resetTokenValidityDuration !== undefined && (typeof passwordPolicy.resetTokenValidityDuration !== 'number' || passwordPolicy.resetTokenValidityDuration <= 0)) {
  131.         throw 'passwordPolicy.resetTokenValidityDuration must be a positive number';
  132.       }
  133.  
  134.       if(passwordPolicy.validatorPattern){
  135.         if(typeof(passwordPolicy.validatorPattern) === 'string') {
  136.           passwordPolicy.validatorPattern = new RegExp(passwordPolicy.validatorPattern);
  137.         }
  138.         else if(!(passwordPolicy.validatorPattern instanceof RegExp)){
  139.           throw 'passwordPolicy.validatorPattern must be a regex string or RegExp object.';
  140.         }
  141.       }
  142.  
  143.  
  144.       if(passwordPolicy.validatorCallback && typeof passwordPolicy.validatorCallback !== 'function') {
  145.         throw 'passwordPolicy.validatorCallback must be a function.';
  146.       }
  147.  
  148.       if(passwordPolicy.doNotAllowUsername && typeof passwordPolicy.doNotAllowUsername !== 'boolean') {
  149.         throw 'passwordPolicy.doNotAllowUsername must be a boolean value.';
  150.       }
  151.  
  152.       if (passwordPolicy.maxPasswordHistory && (!Number.isInteger(passwordPolicy.maxPasswordHistory) || passwordPolicy.maxPasswordHistory <= 0 || passwordPolicy.maxPasswordHistory > 20)) {
  153.         throw 'passwordPolicy.maxPasswordHistory must be an integer ranging 0 - 20';
  154.       }
  155.     }
  156.   }
  157.  
  158.   // if the passwordPolicy.validatorPattern is configured then setup a callback to process the pattern
  159.   static setupPasswordValidator(passwordPolicy) {
  160.     if (passwordPolicy && passwordPolicy.validatorPattern) {
  161.       passwordPolicy.patternValidator = (value) => {
  162.         return passwordPolicy.validatorPattern.test(value);
  163.       }
  164.     }
  165.   }
  166.  
  167.   static validateEmailConfiguration({emailAdapter, appName, publicServerURL, emailVerifyTokenValidityDuration}) {
  168.     if (!emailAdapter) {
  169.       throw 'An emailAdapter is required for e-mail verification and password resets.';
  170.     }
  171.     if (typeof appName !== 'string') {
  172.       throw 'An app name is required for e-mail verification and password resets.';
  173.     }
  174.     if (typeof publicServerURL !== 'string') {
  175.       throw 'A public server url is required for e-mail verification and password resets.';
  176.     }
  177.     if (emailVerifyTokenValidityDuration) {
  178.       if (isNaN(emailVerifyTokenValidityDuration)) {
  179.         throw 'Email verify token validity duration must be a valid number.';
  180.       } else if (emailVerifyTokenValidityDuration <= 0) {
  181.         throw 'Email verify token validity duration must be a value greater than 0.'
  182.       }
  183.     }
  184.   }
  185.  
  186.   get mount() {
  187.     var mount = this._mount;
  188.     if (this.publicServerURL) {
  189.       mount = this.publicServerURL;
  190.     }
  191.     return mount;
  192.   }
  193.  
  194.   set mount(newValue) {
  195.     this._mount = newValue;
  196.   }
  197.  
  198.   static validateSessionConfiguration(sessionLength, expireInactiveSessions) {
  199.     if (expireInactiveSessions) {
  200.       if (isNaN(sessionLength)) {
  201.         throw 'Session length must be a valid number.';
  202.       }
  203.       else if (sessionLength <= 0) {
  204.         throw 'Session length must be a value greater than 0.'
  205.       }
  206.     }
  207.   }
  208.  
  209.   generateEmailVerifyTokenExpiresAt() {
  210.     if (!this.verifyUserEmails || !this.emailVerifyTokenValidityDuration) {
  211.       return undefined;
  212.     }
  213.     var now = new Date();
  214.     return new Date(now.getTime() + (this.emailVerifyTokenValidityDuration * 1000));
  215.   }
  216.  
  217.   generatePasswordResetTokenExpiresAt() {
  218.     if (!this.passwordPolicy || !this.passwordPolicy.resetTokenValidityDuration) {
  219.       return undefined;
  220.     }
  221.     const now = new Date();
  222.     return new Date(now.getTime() + (this.passwordPolicy.resetTokenValidityDuration * 1000));
  223.   }
  224.  
  225.   generateSessionExpiresAt() {
  226.     if (!this.expireInactiveSessions) {
  227.       return undefined;
  228.     }
  229.     var now = new Date();
  230.     return new Date(now.getTime() + (this.sessionLength * 1000));
  231.   }
  232.  
  233.   get invalidLinkURL() {
  234.     return this.customPages.invalidLink || `${this.publicServerURL}/apps/invalid_link.html`;
  235.   }
  236.  
  237.   get invalidVerificationLinkURL() {
  238.     return this.customPages.invalidVerificationLink || `${this.publicServerURL}/apps/invalid_verification_link.html`;
  239.   }
  240.  
  241.   get linkSendSuccessURL() {
  242.     return this.customPages.linkSendSuccess || `${this.publicServerURL}/apps/link_send_success.html`
  243.   }
  244.  
  245.   get linkSendFailURL() {
  246.     return this.customPages.linkSendFail || `${this.publicServerURL}/apps/link_send_fail.html`
  247.   }
  248.  
  249.   get verifyEmailSuccessURL() {
  250.     return this.customPages.verifyEmailSuccess || `${this.publicServerURL}/apps/verify_email_success.html`;
  251.   }
  252.  
  253.   get choosePasswordURL() {
  254.     return this.customPages.choosePassword || `${this.publicServerURL}/apps/choose_password`;
  255.   }
  256.  
  257.   get requestResetPasswordURL() {
  258.     return `${this.publicServerURL}/apps/${this.applicationId}/request_password_reset`;
  259.   }
  260.  
  261.   get passwordResetSuccessURL() {
  262.     return this.customPages.passwordResetSuccess || `${this.publicServerURL}/apps/password_reset_success.html`;
  263.   }
  264.  
  265.   get parseFrameURL() {
  266.     return this.customPages.parseFrameURL;
  267.   }
  268.  
  269.   get verifyEmailURL() {
  270.     return `${this.publicServerURL}/apps/${this.applicationId}/verify_email`;
  271.   }
  272. }
  273.  
  274. export default Config;
  275. module.exports = Config;
  276.  
downloadConfig.js Source code - Download parse-server Source code
Related Source Codes/Software:
react-boilerplate - 2017-06-07
webtorrent - Streaming torrent client for the web ... 2017-06-06
machine-learning-for-software-engineers - A complete daily plan for studying to become a mac... 2017-06-06
upterm - A terminal emulator for the 21st century. 2017-06-06
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
storybook - 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top