schema.userdb.redis 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. I. The database
  2. Redis database for user authentication and peer permissions
  3. has the following schema:
  4. 1) For the long-term credentials there must be keys
  5. "turn/realm/<realm-name>/user/<username>/key" and the values must be
  6. the hmackeys which is an md5 hash of "<username>:<realm-name>:<password>"
  7. (See STUN RFC: https://tools.ietf.org/html/rfc5389#page-35).
  8. For example, for the user "gorst", realm "north.gov"
  9. and password "hero", there must be key "turn/realm/north.gov/user/gorst/key"
  10. and the value should be md5 hash of "gorst:north.gov:hero"
  11. which will result in "7da2270ccfa49786e0115366d3a3d14d".
  12. 2) For the shared secrets (REST API), several key/value pairs
  13. may be used (same as in SQL schema). The secrets are stored as members
  14. of an unordered set. The name of the set will be
  15. "turn/realm/<realm-name>/secret" and the value(s) will be the secret(s).
  16. For example, if we have secrets "hero1", "hero2" and "hero3",
  17. then we will have set "turn/realm/north.gov/secret" with values
  18. "hero1", "hero2" and "hero3". The turnserver will try to use the
  19. secrets in arbitrary order.
  20. 3) The "white" and "black" peer IP ranges are stored as unordered sets
  21. of the following names:
  22. "turn/realm/<realm>/allowed-peer-ip" and
  23. "turn/realm/<realm>/denied-peer-ip".
  24. The meaning of the keys is the same as the meaning of allowed-peer-ip and
  25. denied-peer-ip turnserver command-line option (with the addition of the realm option).
  26. The only difference is that the turnserver option values are "static"
  27. (they remain the same for the lifetime of the turnserver process) but
  28. the database records can be dynamically changed
  29. and they will be almost immediately "seen" by the turnserver process.
  30. 4) For the oAuth authentication, there is a hash structure with the key
  31. "turn/oauth/kid/<kid-value>". The kid structure fields are:
  32. ikm_key - (optional) base64-encoded key ("input keying material").
  33. timestamp - (optional) the timestamp (in seconds) when the key
  34. lifetime started.
  35. lifetime - (optional) the key lifetime in seconds; the default value
  36. is 0 - unlimited lifetime.
  37. as_rs_alg - oAuth token encryption algorithm; the valid values are
  38. "A256GCM", "A128GCM" (see
  39. http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40#section-5.1).
  40. The default value is "A256GCM".
  41. realm - optionally, a kid can be assigned to a realm that is different
  42. from the default realm.
  43. 5) admin users (over https interface) are maintained as keys of form:
  44. "turn/admin_user/<username> with hash members "password" and,
  45. optionally, "realm".
  46. II. Extra realms data in the database
  47. We can use more than one realm with the same instance of the TURN server.
  48. This is done in two ways:
  49. 1) through the third-party authentication option. An oAuth kid can be optionally
  50. assigned to a realm. When the user provides kid, and the database record
  51. for that kid contains a non-empty non-default realm, then the user is treated
  52. as belonging to that realm.
  53. 2) the ORIGIN mechanism - users with different ORIGINS
  54. are placed into different realms. The database includes information about the
  55. relationships between the ORIGIN and realms, and about the extra realms
  56. database numbers.
  57. The relationship between ORIGIN and realm is set as keys of form:
  58. "turn/origin/<origin>" with the realm-names as the value. Many different
  59. ORIGIN keys may have the same realm. If the ORIGIN value is not found in
  60. the database or the ORIGIN field is missed in the initial allocate
  61. request, then the default realm is assumed.
  62. III) Example of a Redis default user database setup.
  63. This example sets user database for:
  64. * long-term credentials with hashed passwords and
  65. with default realm "north.gov";
  66. * long-term credentials with open passwords and
  67. with default realm "north.gov";
  68. * TURN REST API with shared secrets "logen", etc;
  69. * Black and white IP peer lists used.
  70. * Information how to match ORIGIN field with extra
  71. realms (if used). If no origin match found
  72. or the ORIGIN field is absent in the ALLOCATE request then the default
  73. realm is used.
  74. * The realm performance parameters: "max_bps",
  75. "total_quota" and "user_quota" (same names as the turnserver
  76. configuration options, with the same meanings).
  77. * The oAuth data for the key with kid "oldempire" and key value
  78. "12345678901234567890123456789012", and default realm.
  79. * The admin user 'skarling', realm 'north.gov', with password 'hoodless';
  80. * The global admin user 'bayaz' with password 'magi';
  81. The shell command would be:
  82. $ redis-cli <<!
  83. SELECT 2
  84. AUTH turn
  85. set turn/realm/north.gov/user/ninefingers/key "bc807ee29df3c9ffa736523fb2c4e8ee"
  86. set turn/realm/north.gov/user/gorst/key "7da2270ccfa49786e0115366d3a3d14d"
  87. set turn/realm/crinna.org/user/whirrun/key "6972e85e51f36e53b0b61759c5a5219a"
  88. set turn/realm/crinna.org/user/stranger-come-knocking/key "d43cb678560259a1839bff61c19de15e"
  89. sadd turn/realm/north.gov/secret "logen" "bloody9"
  90. sadd turn/realm/crinna.org/secret "north" "library"
  91. set turn/realm/north.gov/max-bps 500000
  92. set turn/realm/north.gov/total-quota 12000
  93. set turn/realm/north.gov/user-quota 10000
  94. set turn/realm/crinna.org/max-bps 400000
  95. set turn/realm/crinna.org/total-quota 10000
  96. set turn/realm/crinna.org/user-quota 8000
  97. set turn/origin/http://crinna.org:80 crinna.org
  98. set turn/origin/https://bligh.edu:443 crinna.org
  99. sadd turn/realm/north.gov/allowed-peer-ip "172.17.13.200" "172.17.13.201"
  100. sadd turn/realm/crinna.org/allowed-peer-ip "172.17.13.202"
  101. sadd turn/realm/north.gov/denied-peer-ip "172.17.13.133-172.17.14.56" "172.17.17.133-172.17.19.56" "123::45"
  102. sadd turn/realm/crinna.org/denied-peer-ip "123::77"
  103. hmset turn/oauth/kid/oldempire ikm_key 'MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIK' as_rs_alg 'A256GCM'
  104. hmset turn/admin_user/skarling realm 'north.gov' password 'hoodless'
  105. hmset turn/admin_user/bayaz password 'magi'
  106. save
  107. !
  108. IV. Redis database configuration parameters
  109. TURN Server connects to the Redis and keeps the same connection during the
  110. TURN Server process lifetime. That means that we have to take care about that
  111. connection - it must not expire.
  112. You have to take care about Redis connection parameters, the timeout and the
  113. keepalive. The following settings must be in your Redis config file
  114. (/etc/redis.conf or /usr/local/etc/redis.conf):
  115. ..........
  116. timeout 0
  117. ..........
  118. tcp-keepalive 60
  119. ..........