Disallowing non-alphanumeric usernames

This commit is contained in:
James Barnsley
2017-02-17 21:36:31 +13:00
parent 16d057cd17
commit 639bcbcbce
4 changed files with 18 additions and 10 deletions

View File

@ -1,11 +1,14 @@
import tornado.ioloop, tornado.web, tornado.websocket, tornado.template
import logging, uuid, subprocess, pykka
import random, string, logging, uuid, subprocess, pykka
from datetime import datetime
from tornado.escape import json_encode, json_decode
logger = logging.getLogger(__name__)
# generate random string
def generateGuid(length):
return ''.join(random.choice(string.lowercase) for i in range(length))
# digest a protocol header into it's id/name parts
def digest_protocol( protocol ):
@ -26,9 +29,9 @@ def digest_protocol( protocol ):
# invalid, so just create a default connection, and auto-generate an ID
except:
clientid = str(uuid.uuid4().hex)
connectionid = str(uuid.uuid4().hex)
username = str(uuid.uuid4().hex)
clientid = generateGuid(12)
connectionid = generateGuid(12)
username = 'Anonymous'
generated = True
# construct our protocol object, and return

View File

@ -68,8 +68,8 @@ export let sizedImages = function( images ){
return sizes;
}
export let generateGuid = function(){
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
export let generateGuid = function(format = 'xxxxxxxxxxxx'){
return format.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});

View File

@ -90,11 +90,12 @@ const PusherMiddleware = (function(){
var state = store.getState();
var connection = {
clientid: Math.random().toString(36).substr(2, 9),
clientid: helpers.generateGuid(),
connectionid: helpers.generateGuid(),
username: Math.random().toString(36).substr(2, 9)
username: 'Anonymous'
}
if( state.pusher.username ) connection.username = state.pusher.username;
connection.username = connection.username.replace(/\W/g, '')
socket = new WebSocket(
'ws://'+state.mopidy.host+':'+state.mopidy.port+'/iris/ws',

View File

@ -51,6 +51,10 @@ class Settings extends React.Component{
return false;
}
handleUsernameChange(username){
this.setState({pusher_username: username.replace(/\W/g, '')})
}
renderConnectionStatus(service){
if( this.props[service].connected ){
return (
@ -151,7 +155,7 @@ class Settings extends React.Component{
<div className="input">
<input
type="text"
onChange={ e => this.setState({ pusher_username: e.target.value }) }
onChange={e => this.handleUsernameChange(e.target.value)}
onBlur={ e => this.props.pusherActions.setUsername(this.state.pusher_username) }
value={ this.state.pusher_username } />
</div>