apply review feedback

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2017-11-08 22:26:09 +00:00
parent 6858b00ed0
commit 78772cc830
No known key found for this signature in database
GPG key ID: 3F879DA5AD802A5E
2 changed files with 25 additions and 16 deletions

View file

@ -26,6 +26,7 @@ import (
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/common/config"
"github.com/matrix-org/util"
"github.com/matrix-org/dendrite/clientapi/httputil"
)
type turnServerResponse struct {
@ -39,38 +40,35 @@ func RequestTurnServer(req *http.Request, device *authtypes.Device, cfg config.D
turnConfig := cfg.TURN
// TODO Guest Support
if len(turnConfig.URIs) == 0 /* || (isGuest && !turnConfig.AllowGuests) */ {
if len(turnConfig.URIs) == 0 || turnConfig.UserLifetime == "" {
return util.JSONResponse{
Code: 200,
JSON: struct{}{},
}
}
duration, err := time.ParseDuration(turnConfig.UserLifetime)
if err != nil {
util.GetLogger(req.Context()).WithError(err).Warn("Invalid configuration value turn.turn_user_lifetime")
return util.JSONResponse{
Code: 200,
JSON: struct{}{},
}
}
// Duration checked at startup, err not possible
duration, _ := time.ParseDuration(turnConfig.UserLifetime)
resp := turnServerResponse{
Username: turnConfig.Username,
Password: turnConfig.Password,
URIs: turnConfig.URIs,
TTL: int(duration.Seconds()),
}
if turnConfig.SharedSecret != "" {
expiry := time.Now().Add(duration).Unix()
resp.Username = fmt.Sprintf("%d:%s", expiry, device.UserID)
mac := hmac.New(sha1.New, []byte(turnConfig.SharedSecret))
mac.Write([]byte(resp.Username))
_, err := mac.Write([]byte(resp.Username))
if err != nil {
return httputil.LogThenError(req, err)
}
resp.Username = fmt.Sprintf("%d:%s", expiry, device.UserID)
resp.Password = base64.StdEncoding.EncodeToString(mac.Sum(nil))
} else if turnConfig.Username != "" && turnConfig.Password != "" {
// Already have turnConfig.Username and turnConfig.Password in resp
resp.Username = turnConfig.Username
resp.Password = turnConfig.Password
} else {
return util.JSONResponse{
Code: 200,

View file

@ -152,8 +152,9 @@ type Dendrite struct {
// TURN Server Config
TURN struct {
// TODO Guest Support
// Whether or not guests can request TURN credentials
AllowGuests bool `yaml:"turn_allow_guests"`
//AllowGuests bool `yaml:"turn_allow_guests"`
// How long the authorization should last
UserLifetime string `yaml:"turn_user_lifetime"`
// The list of TURN URIs to pass to clients
@ -360,10 +361,20 @@ func (config *Dendrite) check(monolithic bool) error {
}
}
checkValidDuration := func(key, value string) {
if _, err := time.ParseDuration(config.TURN.UserLifetime); err != nil {
problems = append(problems, fmt.Sprintf("invalid duration for config key %q: %s", key, value))
}
}
checkNotEmpty("matrix.server_name", string(config.Matrix.ServerName))
checkNotEmpty("matrix.private_key", string(config.Matrix.PrivateKeyPath))
checkNotZero("matrix.federation_certificates", int64(len(config.Matrix.FederationCertificatePaths)))
if config.TURN.UserLifetime != "" {
checkValidDuration("turn.turn_user_lifetime", config.TURN.UserLifetime)
}
checkNotEmpty("media.base_path", string(config.Media.BasePath))
checkPositive("media.max_file_size_bytes", int64(*config.Media.MaxFileSizeBytes))
checkPositive("media.max_thumbnail_generators", int64(config.Media.MaxThumbnailGenerators))