homeassistant: Convert auth command to python
This commit is contained in:
parent
cfcdb4e935
commit
81252de145
3 changed files with 27 additions and 20 deletions
25
roles/homeassistant/files/auth-command.py
Executable file
25
roles/homeassistant/files/auth-command.py
Executable file
|
@ -0,0 +1,25 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
import requests
|
||||||
|
|
||||||
|
username = os.environ.get("username")
|
||||||
|
password = os.environ.get("password")
|
||||||
|
|
||||||
|
if username is None or password is None:
|
||||||
|
sys.exit(2)
|
||||||
|
if not re.search(r"^[a-z]+$", username):
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
|
resp = requests.post(
|
||||||
|
"https://id.foo.sh/authcheck",
|
||||||
|
json={"username": username, "password": password, "group": "foosh"},
|
||||||
|
)
|
||||||
|
if resp.status_code != 200:
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
|
print("name = {}".format(resp.json()["name"]))
|
||||||
|
print("group = system-users")
|
||||||
|
print("local_only = false")
|
|
@ -1,18 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
|
|
||||||
set -eu
|
|
||||||
|
|
||||||
umask 077
|
|
||||||
|
|
||||||
if [ -z "${username:-}" ] || [ -z "${password:-}" ]; then
|
|
||||||
exit 2
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$(echo "$username" | sed -r 's/^[a-z]+$/x/')" != "x" ]; then
|
|
||||||
exit 2
|
|
||||||
fi
|
|
||||||
|
|
||||||
curl -sf -X POST -H "Content-Type: application/json" -d @- \
|
|
||||||
https://id.foo.sh/authcheck <<EOF
|
|
||||||
{"username": "${username}", "password": "${password}", "group": "foosh"}
|
|
||||||
EOF
|
|
|
@ -98,8 +98,8 @@
|
||||||
|
|
||||||
- name: Copy authentication command
|
- name: Copy authentication command
|
||||||
ansible.builtin.copy:
|
ansible.builtin.copy:
|
||||||
dest: /srv/homeassistant/auth-command.sh
|
dest: /srv/homeassistant/auth-command.py
|
||||||
src: auth-command.sh
|
src: auth-command.py
|
||||||
mode: "0755"
|
mode: "0755"
|
||||||
owner: root
|
owner: root
|
||||||
group: "{{ ansible_wheel }}"
|
group: "{{ ansible_wheel }}"
|
||||||
|
|
Loading…
Add table
Reference in a new issue