@@ -155,8 +155,25 @@ def lookup(self, token):
155
155
logger .error ("package jwcrypto not found, are you sure you've installed it correctly?" )
156
156
return None
157
157
158
- class TokenRedis ():
159
- """
158
+
159
+ class TokenRedis (BasePlugin ):
160
+ """Token plugin based on the Redis in-memory data store.
161
+
162
+ The token source is in the format:
163
+
164
+ host[:port[:db[:password]]]
165
+
166
+ where port and password are optional.
167
+
168
+ If your redis server is using the default port (6379) then you can use:
169
+
170
+ my-redis-host
171
+
172
+ In case you need to authenticate with the redis server you will have to
173
+ specify also the port and db:
174
+
175
+ my-redis-host:6379:0:verysecretpass
176
+
160
177
The TokenRedis plugin expects the format of the data in a form of json.
161
178
162
179
Prepare data with:
@@ -173,17 +190,34 @@ class TokenRedis():
173
190
"""
174
191
def __init__ (self , src ):
175
192
try :
176
- # import those ahead of time so we provide error earlier
177
193
import redis
178
- self ._server , self ._port = src .split (":" )
194
+ except ImportError :
195
+ logger .error ("Unable to load redis module" )
196
+ sys .exit ()
197
+ # Default values
198
+ self ._port = 6379
199
+ self ._db = 0
200
+ self ._password = None
201
+ try :
202
+ fields = src .split (":" )
203
+ if len (fields ) == 1 :
204
+ self ._server = fields [0 ]
205
+ elif len (fields ) == 2 :
206
+ self ._server , self ._port = fields
207
+ elif len (fields ) == 3 :
208
+ self ._server , self ._port , self ._db = fields
209
+ elif len (fields ) == 4 :
210
+ self ._server , self ._port , self ._db , self ._password = fields
211
+ else :
212
+ raise ValueError
213
+ self ._port = int (self ._port )
214
+ self ._db = int (self ._db )
179
215
logger .info ("TokenRedis backend initilized (%s:%s)" %
180
216
(self ._server , self ._port ))
181
217
except ValueError :
182
- logger .error ("The provided --token-source='%s' is not in an expected format <host>:<port>" %
183
- src )
184
- sys .exit ()
185
- except ImportError :
186
- logger .error ("package redis not found, are you sure you've installed them correctly?" )
218
+ logger .error ("The provided --token-source='%s' is not in the "
219
+ "expected format <host>[:<port>[:<db>[:<password>]]]" %
220
+ src )
187
221
sys .exit ()
188
222
189
223
def lookup (self , token ):
@@ -194,7 +228,8 @@ def lookup(self, token):
194
228
sys .exit ()
195
229
196
230
logger .info ("resolving token '%s'" % token )
197
- client = redis .Redis (host = self ._server , port = self ._port )
231
+ client = redis .Redis (host = self ._server , port = self ._port ,
232
+ db = self ._db , password = self ._password )
198
233
stuff = client .get (token )
199
234
if stuff is None :
200
235
return None
0 commit comments