PageRenderTime 66ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/app/scripts/lib/validate.js

https://gitlab.com/wamburu/fxa-content-server
JavaScript | 110 lines | 53 code | 19 blank | 38 comment | 10 complexity | d46cffce6d5132b6e64752017b710f6c MD5 | raw file
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. // Do some validation.
  5. define([
  6. 'lib/constants'
  7. ], function (Constants) {
  8. 'use strict';
  9. // taken from the fxa-auth-server
  10. var HEX_STRING = /^(?:[a-fA-F0-9]{2})+$/;
  11. return {
  12. /**
  13. * Check if an email address is valid
  14. *
  15. * @return true if email is valid, false otw.
  16. */
  17. isEmailValid: function (email) {
  18. if (typeof email !== 'string') {
  19. return false;
  20. }
  21. var parts = email.split('@');
  22. var localLength = parts[0] && parts[0].length;
  23. var domainLength = parts[1] && parts[1].length;
  24. // Original regexp from:
  25. // http://blog.gerv.net/2011/05/html5_email_address_regexp/
  26. // Modified to remove the length checks, which are done later.
  27. // IETF spec: http://tools.ietf.org/html/rfc5321#section-4.5.3.1.1
  28. // NOTE: this does *NOT* allow internationalized domain names.
  29. return (/^[\w.!#$%&'*+\-\/=?\^`{|}~]+@[a-z\d][a-z\d\-]*(?:\.[a-z\d][a-z\d\-]*)*$/i).test(email) &&
  30. // total email allwed to be 256 bytes long
  31. email.length <= 256 &&
  32. // local side only allowed to be 64 bytes long
  33. 1 <= localLength && localLength <= 64 &&
  34. // domain side allowed to be up to 255 bytes long which
  35. // doesn't make much sense unless the local side has 0 length;
  36. 1 <= domainLength && domainLength <= 255;
  37. },
  38. /**
  39. * Check if an email verification code is valid
  40. */
  41. isCodeValid: function (code) {
  42. if (typeof code !== 'string') {
  43. return false;
  44. }
  45. // codes are fixed length hex strings.
  46. return code.length === Constants.CODE_LENGTH &&
  47. HEX_STRING.test(code);
  48. },
  49. /**
  50. * Check if an OAuth code is valid
  51. */
  52. isOAuthCodeValid: function (code) {
  53. if (typeof code !== 'string') {
  54. return false;
  55. }
  56. // codes are fixed length hex strings.
  57. return code.length === Constants.OAUTH_CODE_LENGTH &&
  58. HEX_STRING.test(code);
  59. },
  60. /**
  61. * Check if a verification token is valid
  62. */
  63. isTokenValid: function (token) {
  64. if (typeof token !== 'string') {
  65. return false;
  66. }
  67. // tokens are variable length hex strings.
  68. return HEX_STRING.test(token);
  69. },
  70. /**
  71. * Check if a verification uid is valid
  72. */
  73. isUidValid: function (uid) {
  74. if (typeof uid !== 'string') {
  75. return false;
  76. }
  77. // uids are fixed length hex strings.
  78. return uid.length === Constants.UID_LENGTH &&
  79. HEX_STRING.test(uid);
  80. },
  81. /**
  82. * Check if a password is valid
  83. */
  84. isPasswordValid: function (password) {
  85. if (typeof password !== 'string') {
  86. return false;
  87. }
  88. return password.length >= Constants.PASSWORD_MIN_LENGTH;
  89. }
  90. };
  91. });