Scripts to setup shell and install base packages
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.5 KiB

  1. #! /bin/bash
  2. IFS="
  3. "
  4. SSO_HOST="example.com/api/v3"
  5. SSO_TOKEN="token"
  6. LDAP_HOST="localhost"
  7. LDAP_USERDN="uid=user,ou=people,dc=example,dc=com"
  8. LDAP_PASS='pass'
  9. LDAP_BASE="ou=people,dc=example,dc=com"
  10. DIRECTORY="/usr/local/src/authentik_ldap_sync"
  11. deleted_users="$DIRECTORY/deleted_users"
  12. deleted_pklist="$DIRECTORY/user_pklist"
  13. authentik_users="$DIRECTORY/authentik_users"
  14. ldap_search() {
  15. ldapsearch -D $LDAP_USERDN -w$LDAP_PASS -b $LDAP_BASE -h $LDAP_HOST $*
  16. }
  17. if [ ! -d "$DIRECTORY" ]; then
  18. mkdir $DIRECTORY
  19. fi
  20. if [ ! -f "$deleted_pklist" ]; then
  21. touch $deleted_pklist
  22. else
  23. rm $deleted_pklist
  24. fi
  25. ldap_search 'uid' | grep -E '^uid:' | cut -d' ' -f2 | sort > $DIRECTORY/ldap_users
  26. curl -X GET "https://$SSO_HOST/core/users/?page_size=1000" \
  27. -H "accept: application/json"\
  28. -H "authorization: Bearer $SSO_TOKEN" | jq 'del(.results[].groups_obj)' | jq '.results[].attributes.ldap_uniq' | sed -e '/null/d' -e 's/"//g' | sort > $authentik_users
  29. curl -X GET "https://$SSO_HOST/core/users/?page_size=1000" \
  30. -H "accept: application/json"\
  31. -H "authorization: Bearer $SSO_TOKEN" | jq '.results[] | "\(.pk) \(.username)"' | sed 's/"//g' > $DIRECTORY/pk_username
  32. diff $DIRECTORY/ldap_users $authentik_users |grep '^>'|awk '{print $2}' > $deleted_users
  33. for user in `cat $deleted_users`
  34. do
  35. grep " $user$" $DIRECTORY/pk_username >> $deleted_pklist
  36. done
  37. for user_pk in `cat $deleted_pklist`
  38. do
  39. PK=`echo $user_pk | cut -d' ' -f1`
  40. curl -X DELETE "https://$SSO_HOST/core/users/$PK/" \
  41. -H "accept: application/json" \
  42. -H "authorization: Bearer $SSO_TOKEN"
  43. done