fix token Redis import

this is a standard conexion to redis server with TokenRedis class
This commit is contained in:
Alex Bernal 2019-05-19 21:33:11 -05:00 committed by GitHub
parent 70911c6d6e
commit d9050dc55a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 57 additions and 19 deletions

View File

@ -2,6 +2,7 @@ from __future__ import print_function
import os import os
import sys import sys
class BasePlugin(object): class BasePlugin(object):
def __init__(self, src): def __init__(self, src):
self.source = src self.source = src
@ -34,7 +35,8 @@ class ReadOnlyTokenFile(BasePlugin):
tok, target = line.split(': ') tok, target = line.split(': ')
self._targets[tok] = target.strip().rsplit(':', 1) self._targets[tok] = target.strip().rsplit(':', 1)
except ValueError: except ValueError:
print >>sys.stderr, "Syntax error in %s on line %d" % (self.source, index) print >>sys.stderr, "Syntax error in %s on line %d" % (
self.source, index)
index += 1 index += 1
def lookup(self, token): def lookup(self, token):
@ -112,7 +114,7 @@ class JWTTokenApi(BasePlugin):
key.import_from_pem(key_data) key.import_from_pem(key_data)
except: except:
try: try:
key.import_key(k=key_data.decode('utf-8'),kty='oct') key.import_key(k=key_data.decode('utf-8'), kty='oct')
except: except:
print('Failed to correctly parse key data!', file=sys.stderr) print('Failed to correctly parse key data!', file=sys.stderr)
return None return None
@ -132,25 +134,61 @@ class JWTTokenApi(BasePlugin):
print("Failed to parse token: %s" % str(e), file=sys.stderr) print("Failed to parse token: %s" % str(e), file=sys.stderr)
return None return None
except ImportError as e: except ImportError as e:
print("package jwcrypto not found, are you sure you've installed it correctly?", file=sys.stderr) print(
"package jwcrypto not found, are you sure you've installed it correctly?", file=sys.stderr)
return None return None
import sys
if sys.version_info >= (2, 7): class TokenRedis(BasePlugin):
import redis """ Token plugin for Redis DataBase
import simplejson The Redis tokens are accepted with the struct ->
class TokenRedis(object): E.g: redis set value
def __init__(self, src): host={"ip":"localhost", "port": "5901"}
self._server, self._port = src.split(":") json_host=json.dumps(host)
redis.set('token', json_host)
...
Start websockify with this args -> e.g
websockify --web=/path/to/noVNC --token-plugin=TokenRedis --token-source=[IP]:[PORT] [LISTEN_PORT]
"""
def lookup(self, token): def __init__(self, src):
client = redis.Redis(host=self._server,port=self._port) self._server, self._port = src.split(":")
stuff = client.get(token)
if stuff is None: def lookup(self, token):
try:
import simplejson as json
except ImportError:
import json
try:
import redis
try:
client = redis.Redis(host=self._server, port=self._port)
stuff = client.get(token)
if stuff is None:
return None
try:
host = json.loads(stuff.decode('utf-8'))
# Removing uni-code chars
import ast
data = ast.literal_eval(
json.dumps([host["ip"], host["port"]]))
return data
except ValueError:
print("Decoding JSON has failed {0}".format(
file=sys.stderr))
return None
except (redis.exceptions.ConnectionError,
redis.exceptions.BusyLoadingError):
print("Redis conexion error {0}".format(file=sys.stderr))
return None return None
else:
combo = simplejson.loads(stuff.decode("utf-8")) except ImportError:
pair = combo["host"] print("The package redis, not found,{0} ".format(file=sys.stderr))
return pair.split(':') return None