Merge pull request #1147 from syncthing/fix-1118

Generate a random API key on initial setup (fixes #1118)
This commit is contained in:
Audrius Butkevicius 2014-12-29 13:28:38 +00:00
commit b13ae17a47
4 changed files with 26 additions and 11 deletions

View File

@ -104,15 +104,12 @@ function decimals(val, num) {
return decs; return decs;
} }
function randomString(len, bits) { function randomString(len) {
bits = bits || 36; var i, result = '', chars = '01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-';
var outStr = "", for (i = 0; i < len; i++) {
newStr; result += chars[Math.round(Math.random() * (chars.length - 1))];
while (outStr.length < len) {
newStr = Math.random().toString(bits).slice(2);
outStr += newStr.slice(0, Math.min(newStr.length, (len - outStr.length)));
} }
return outStr.toLowerCase(); return result;
} }
function isEmptyObject(obj) { function isEmptyObject(obj) {

View File

@ -994,7 +994,7 @@ angular.module('syncthing.core')
}; };
$scope.setAPIKey = function (cfg) { $scope.setAPIKey = function (cfg) {
cfg.APIKey = randomString(30, 32); cfg.APIKey = randomString(32);
}; };
$scope.showURPreview = function () { $scope.showURPreview = function () {

File diff suppressed because one or more lines are too long

View File

@ -20,6 +20,7 @@ import (
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"io" "io"
"math/rand"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -369,6 +370,10 @@ func (cfg *Configuration) prepare(myID protocol.DeviceID) {
cfg.Options.ListenAddress = uniqueStrings(cfg.Options.ListenAddress) cfg.Options.ListenAddress = uniqueStrings(cfg.Options.ListenAddress)
cfg.Options.GlobalAnnServers = uniqueStrings(cfg.Options.GlobalAnnServers) cfg.Options.GlobalAnnServers = uniqueStrings(cfg.Options.GlobalAnnServers)
if cfg.GUI.APIKey == "" {
cfg.GUI.APIKey = randomString(32)
}
} }
// ChangeRequiresRestart returns true if updating the configuration requires a // ChangeRequiresRestart returns true if updating the configuration requires a
@ -674,3 +679,16 @@ func (l FolderDeviceConfigurationList) Swap(a, b int) {
func (l FolderDeviceConfigurationList) Len() int { func (l FolderDeviceConfigurationList) Len() int {
return len(l) return len(l)
} }
// randomCharset contains the characters that can make up a randomString().
const randomCharset = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-"
// randomString returns a string of random characters (taken from
// randomCharset) of the specified length.
func randomString(l int) string {
bs := make([]byte, l)
for i := range bs {
bs[i] = randomCharset[rand.Intn(len(randomCharset))]
}
return string(bs)
}