Skip to content

Commit 93a7bd6

Browse files
committed
Added get_credentials and get_crdential function for LDAP protocol
1 parent 30166dd commit 93a7bd6

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

nxc/protocols/ldap/database.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,39 @@ def add_credential(self, credtype, domain, username, password, pillaged_from=Non
165165
q_groups = Insert(self.GroupRelationsTable)
166166

167167
self.db_execute(q_groups, groups)
168+
169+
def is_credential_valid(self, credential_id):
170+
"""Check if this credential ID is valid."""
171+
q = select(self.UsersTable).filter(
172+
self.UsersTable.c.id == credential_id,
173+
self.UsersTable.c.password is not None,
174+
)
175+
results = self.db_execute(q).all()
176+
return len(results) > 0
177+
178+
def get_credentials(self, filter_term=None, cred_type=None):
179+
"""Return credentials from the database."""
180+
# if we're returning a single credential by ID
181+
if self.is_credential_valid(filter_term):
182+
q = select(self.UsersTable).filter(self.UsersTable.c.id == filter_term)
183+
elif cred_type:
184+
q = select(self.UsersTable).filter(self.UsersTable.c.credtype == cred_type)
185+
# if we're filtering by username
186+
elif filter_term and filter_term != "":
187+
like_term = func.lower(f"%{filter_term}%")
188+
q = select(self.UsersTable).filter(func.lower(self.UsersTable.c.username).like(like_term))
189+
# otherwise return all credentials
190+
else:
191+
q = select(self.UsersTable)
192+
193+
return self.db_execute(q).all()
194+
195+
def get_credential(self, cred_type, domain, username, password):
196+
q = select(self.UsersTable).filter(
197+
self.UsersTable.c.domain == domain,
198+
self.UsersTable.c.username == username,
199+
self.UsersTable.c.password == password,
200+
self.UsersTable.c.credtype == cred_type,
201+
)
202+
results = self.db_execute(q).first()
203+
return results.id

0 commit comments

Comments
 (0)