authservice/asadduser.py

56 lines
1.2 KiB
Python
Raw Permalink Normal View History

2021-01-26 22:06:39 +01:00
#!/usr/bin/python
2021-09-11 23:32:25 +02:00
import psycopg2
2021-01-26 22:06:39 +01:00
from pbkdf2 import crypt
import argparse
import os
parser = argparse.ArgumentParser(description='asadduser')
parser.add_argument('--user', '-u',
help='Login',
required=True)
parser.add_argument('--password', '-p',
help='Password',
required=True)
parser.add_argument('--application', '-a',
help='Application',
required=True)
args = parser.parse_args()
user = args.user
password = args.password
application = args.application
2021-09-11 23:32:25 +02:00
DB_NAME = "authservice"
2021-01-26 22:06:39 +01:00
pwhash = crypt(password, iterations=100000)
conn = None
cur = None
try:
2021-09-11 23:32:25 +02:00
conn = psycopg2.connect(database = DB_NAME)
2021-01-26 22:06:39 +01:00
conn.autocommit = False
cur = conn.cursor()
cur.execute("""
2021-09-11 23:32:25 +02:00
INSERT INTO user_t (login, pwhash)
VALUES(%s, %s)
2021-01-27 10:57:54 +01:00
""", [user, pwhash])
2021-01-26 22:06:39 +01:00
cur.execute("""
2021-09-11 23:32:25 +02:00
INSERT INTO user_application_mapping_t (application,"user")
2021-01-26 22:06:39 +01:00
VALUES(
2021-09-11 23:32:25 +02:00
(SELECT id FROM application_t WHERE name = %s),
(SELECT id FROM user_t WHERE login = %s)
2021-01-26 22:06:39 +01:00
)
""", [application, user])
conn.commit()
finally:
if cur:
cur.close()
if conn:
conn.rollback()
conn.close()