New users and password changes should be applied, but updaging all users all the time would waste processing.
44 lines
1.2 KiB
Python
Executable file
44 lines
1.2 KiB
Python
Executable file
#! /bin/env python3
|
|
from datetime import datetime, timezone, timedelta
|
|
from requests import get, post, patch
|
|
from os import getenv
|
|
import logging
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
fingerpass = getenv('FINGER_PW')
|
|
|
|
gitauth = (getenv('GIT_USER'), getenv('GIT_PW'))
|
|
|
|
|
|
fingerurl = f'https://finger.stacken.kth.se/services/git/all'
|
|
query = {}
|
|
|
|
if True:
|
|
since = datetime.now(timezone.utc) - timedelta(minutes=30)
|
|
query['since'] = since.strftime("%Y-%m-%dT%T%Z")
|
|
|
|
response = get(fingerurl, params=query, auth=('git', fingerpass))
|
|
response.raise_for_status()
|
|
|
|
for (user, passwd) in response.json().items():
|
|
response = post('https://git.stacken.kth.se/api/v1/admin/users',
|
|
data={
|
|
"username": user,
|
|
"email": f"{user}@stacken.kth.se",
|
|
"password": passwd,
|
|
},
|
|
auth = gitauth
|
|
)
|
|
if response.status_code == 422:
|
|
patch(f'https://git.stacken.kth.se/api/v1/admin/users/{user}',
|
|
data = { "password": passwd },
|
|
auth = gitauth
|
|
).raise_for_status()
|
|
print(f"Updated password for {user}")
|
|
else:
|
|
response.raise_for_status()
|
|
print(f"Created user {user}")
|
|
|
|
|
|
|