@@ -42,7 +42,7 @@ class MongoDBAuthenticationBackend(object):
42
42
43
43
_collection_name = 'users'
44
44
_indexes = [
45
- ( 'username' , pymongo .ASCENDING )
45
+ pymongo . IndexModel ([( 'username' , pymongo .ASCENDING )], unique = True )
46
46
]
47
47
_hash_function = hashlib .sha256
48
48
@@ -53,12 +53,17 @@ def __init__(self, db_host='localhost', db_port=27017, db_name='st2auth', db_use
53
53
self ._db_port = db_port
54
54
self ._db_username = db_username
55
55
self ._db_password = db_password
56
+ self ._db_indexes_created = False
56
57
57
- self ._client = MongoClient (host = self ._db_host , port = self ._db_port , tz_aware = True )
58
- self ._db = self ._client [db_name ]
59
-
58
+ kwargs = {}
60
59
if self ._db_username :
61
- self ._db .authenticate (name = self ._db_username , password = self ._db_password )
60
+ kwargs ["username" ] = self ._db_username
61
+ kwargs ["password" ] = self ._db_password
62
+
63
+ self ._client = MongoClient (
64
+ host = self ._db_host , port = self ._db_port , tz_aware = True , ** kwargs
65
+ )
66
+ self ._db = self ._client [db_name ]
62
67
63
68
self ._collection = self ._db [self ._collection_name ]
64
69
self ._ensure_indexes ()
@@ -87,4 +92,7 @@ def get_user(self, username):
87
92
pass
88
93
89
94
def _ensure_indexes (self ):
90
- self ._collection .ensure_index (self ._indexes , unique = True )
95
+ if self ._db_indexes_created :
96
+ return
97
+ self ._collection .create_indexes (self ._indexes )
98
+ self ._db_indexes_created = True
0 commit comments