Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict'
  2. const {
  3. unassigned_code_points,
  4. commonly_mapped_to_nothing,
  5. non_ASCII_space_characters,
  6. prohibited_characters,
  7. bidirectional_r_al,
  8. bidirectional_l,
  9. } = require('./lib/memory-code-points')
  10. module.exports = saslprep
  11. // 2.1. Mapping
  12. /**
  13. * non-ASCII space characters [StringPrep, C.1.2] that can be
  14. * mapped to SPACE (U+0020)
  15. */
  16. const mapping2space = non_ASCII_space_characters
  17. /**
  18. * the "commonly mapped to nothing" characters [StringPrep, B.1]
  19. * that can be mapped to nothing.
  20. */
  21. const mapping2nothing = commonly_mapped_to_nothing
  22. // utils
  23. const getCodePoint = character => character.codePointAt(0)
  24. const first = x => x[0]
  25. const last = x => x[x.length - 1]
  26. /**
  27. * SASLprep.
  28. * @param {string} input
  29. * @param {object} opts
  30. * @param {boolean} opts.allowUnassigned
  31. */
  32. function saslprep(input, opts = {}) {
  33. if (typeof input !== 'string') {
  34. throw new TypeError('Expected string.')
  35. }
  36. if (input.length === 0) {
  37. return ''
  38. }
  39. // 1. Map
  40. const mapped_input = input
  41. .split('')
  42. .map(getCodePoint)
  43. // 1.1 mapping to space
  44. .map(character => (mapping2space.get(character) ? 0x20 : character))
  45. // 1.2 mapping to nothing
  46. .filter(character => !mapping2nothing.get(character))
  47. // 2. Normalize
  48. const normalized_input = String.fromCodePoint(...mapped_input).normalize('NFKC')
  49. const normalized_map = normalized_input.split('').map(getCodePoint)
  50. // 3. Prohibit
  51. const hasProhibited = normalized_map.some(character =>
  52. prohibited_characters.get(character)
  53. )
  54. if (hasProhibited) {
  55. throw new Error(
  56. 'Prohibited character, see https://tools.ietf.org/html/rfc4013#section-2.3'
  57. )
  58. }
  59. // Unassigned Code Points
  60. if (opts.allowUnassigned !== true) {
  61. const hasUnassigned = normalized_map.some(character =>
  62. unassigned_code_points.get(character)
  63. )
  64. if (hasUnassigned) {
  65. throw new Error(
  66. 'Unassigned code point, see https://tools.ietf.org/html/rfc4013#section-2.5'
  67. )
  68. }
  69. }
  70. // 4. check bidi
  71. const hasBidiRAL = normalized_map
  72. .some((character) => bidirectional_r_al.get(character))
  73. const hasBidiL = normalized_map
  74. .some((character) => bidirectional_l.get(character))
  75. // 4.1 If a string contains any RandALCat character, the string MUST NOT
  76. // contain any LCat character.
  77. if (hasBidiRAL && hasBidiL) {
  78. throw new Error(
  79. 'String must not contain RandALCat and LCat at the same time,' +
  80. ' see https://tools.ietf.org/html/rfc3454#section-6'
  81. )
  82. }
  83. /**
  84. * 4.2 If a string contains any RandALCat character, a RandALCat
  85. * character MUST be the first character of the string, and a
  86. * RandALCat character MUST be the last character of the string.
  87. */
  88. const isFirstBidiRAL = bidirectional_r_al.get(getCodePoint(first(normalized_input)))
  89. const isLastBidiRAL = bidirectional_r_al.get(getCodePoint(last(normalized_input)))
  90. if (hasBidiRAL && !(isFirstBidiRAL && isLastBidiRAL)) {
  91. throw new Error(
  92. 'Bidirectional RandALCat character must be the first and the last' +
  93. ' character of the string, see https://tools.ietf.org/html/rfc3454#section-6'
  94. )
  95. }
  96. return normalized_input
  97. }