Skip to content
Snippets Groups Projects
Commit 7e31fa20 authored by Jakob Berger's avatar Jakob Berger :key2: Committed by jakob
Browse files

initial commit

parents
No related branches found
No related tags found
No related merge requests found
import urllib.request, urllib.parse, urllib.error
import json
class Gitlab:
def __init__(self, host, private_token, urltemplate='https://%s/api/v3/', token_type='private'):
self.apiurl = urltemplate % host
if token_type is 'private':
self.auth = {'PRIVATE-TOKEN': private_token}
elif token_type is 'oauth':
self.auth = {'AUTHORIZATION': 'Bearer %s' % private_token}
def post_data(self, url: str, post_data_dict: dict) -> object:
data = urllib.parse.urlencode(post_data_dict).encode("utf-8")
req = urllib.request.Request("%s/api/v3/%s" % (self.apiurl, url), data,
{'AUTHORIZATION': 'Bearer %s' % auth_token})
return json.loads(urllib.request.urlopen(req).read().decode("utf-8"))
def iterate_over_objects(self, url: str, page_filter_func: staticmethod, *args) -> list:
results = []
stop = False
page = ['yo']
page_no = 1
while page is not None and not stop:
request = urllib.request.Request("%s%s?per_page=100&page=%d" % (self.apiurl, url, page_no), None, self.auth)
json_text = urllib.request.urlopen(request).read().decode("utf-8")
page = json.loads(json_text)
stop = page_filter_func(page, results, *args)
page_no += 1
return results
def find_single_object(self, url, eval_single_object_func, *args):
def __find_single_object_abstract(page, results, *args):
for gitlabobject in page:
result = eval_single_object_func(gitlabobject, *args)
if result is True:
results.append(gitlabobject)
return True
return False
return self.iterate_over_objects(url, __find_single_object_abstract, *args)
def find_objects_by_attributes(self, url, attributes):
return self.iterate_over_objects(url, _filter_page_by_attribute, attributes)
def find_single_object_by_attributes(self, url, attributes):
return self.find_single_object(url, _object_matches_attributes, attributes)
def _object_matches_attributes(gitlabobject, attributes):
is_match = True
for attribute in attributes.items():
if gitlabobject[attribute[0]] is not attribute[1]:
is_match = False
break
return is_match
def _filter_page_by_attribute(page, results, attributes):
for gitlabobject in page:
if _object_matches_attributes(gitlabobject, attributes):
results.append(gitlabobject)
return False #return false to keep appending following pages
def _append_objects_to_results(page, results, *args):
for element in page:
results.append(element)
return False #return false to keep appending pages
\ No newline at end of file
ldap.py 0 → 100644
import ldap3
class LdapHandler:
def __init__(self, ldaphost: str, default_query: str = '', ssl: bool = False):
server = ldap3.Server(ldaphost, use_ssl = ssl, get_info=ldap3.ALL)
self.connection = ldap3.Connection(server, auto_bind=True)
self.default_query = default_query
def get_ldap_users(self, user_list, query, silent = False):
results = []
i = 1
for user in user_list:
user = user.strip()
if not silent:
print("%s (%d/%d)" % (user, i, len(results)))
search_no = self.ldap.search(query, ldap3.SCOPE_SUBTREE, "uid=%s" % user, None)
search_res = self.ldap.result(search_no, 0)
if search_res[0] == 100:
dn = search_res[1][0][0]
name = search_res[1][0][1]['cn'][0]
mail = search_res[1][0][1]['mail'][0]
location = search_res[1][0][1]['l'][0]
results.append({'dn': dn, 'username': user, 'name': name, 'mail': mail, 'location': location})
return results
def connect(ldaphost: str, default_query: str = '', ssl: bool = False) -> LdapHandler:
return LdapHandler(ldaphost, default_query, ssl)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment