summaryrefslogtreecommitdiff
path: root/ui/app/lib/validate.js
blob: 4e672e8f80145ac89e54cf62e96d77c8a9c8dbca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// https://github.com/dropbox/zxcvbn

var DOMAIN_RE = /^((?:(?:(?:\w[\.\-\+]?)*)\w)+)((?:(?:(?:\w[\.\-\+]?){0,62})\w)+)\.(\w{2,6})$/
var USER_RE   = /^[a-z0-9_-]{1,200}$/
var USER_INT_RE = /^[\.@a-z0-9_-]*$/

//
// Validations returns an error message or false if no errors
//

class Validations {

  domain(input) {
    if (!input.match(DOMAIN_RE)) {
      return "Not a domain name"
    } else {
      return null
    }
  }

  usernameInteractive(input) {
    if (!input.match(USER_INT_RE)) {
      return "Username contains an invalid character"
    }
    return false
  }

  username(input) {
    if (!input.match(USER_INT_RE)) {
      return "Username contains an invalid character"
    }
    if (!input.match('@')) {
      return "Username must be in the form username@domain"
    }
    let parts      = input.split('@')
    let userpart   = parts[0]
    let domainpart = parts[1]
    if (!userpart.match(USER_RE)) {
      return "Username contains an invalid character"
    } else if (!domainpart.match(DOMAIN_RE)) {
      return "Username must include a valid domain name."
    }
    return false
  }

  passwordStrength(passwd) {
    if (typeof(zxcvbn) == 'function') {
      // zxcvbn performs very slow on long strings, so we cap
      // the calculation at 30 characters
      return zxcvbn(passwd.substring(0,30))
    } else {
      return null
    }
  }

  //
  // loads the zxcvbn library. because this library is big, we don't load it
  // every time, just when needed.
  //
  // this is the webpack way to do this:
  //
  //    require.ensure([], function () {
  //      var zxcvbn = require('zxcvbn');
  //    });
  //
  // that works, but requires that we also process the original coffeescript
  // source if we want to avoid warning messages.
  //
  loadPasswdLib(onload) {
    var id = "zxcvbn-script"
    if (!document.getElementById(id)) {
      var script = document.createElement('script')
      script.id = id
      script.onload = onload
      script.src = './js/zxcvbn.js'
      document.getElementsByTagName('script')[0].appendChild(script)
    }
  }
}

var Validate = new Validations()
export default Validate