feat(microsoft-entra-id): new Microsoft Entra ID integration

14 commands (Microsoft Graph users): user get/list/create/update/delete,
account disable/enable, revoke sign-in sessions, reset password, assign
manager, and group/manager/auth-method reads. Azure AD OAuth 2.0
client-credentials, stdlib-only.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Guillaume BOURGEOIS
2026-07-11 22:22:46 +02:00
parent 334ecac83e
commit 1033388518
15 changed files with 1151 additions and 0 deletions
@@ -0,0 +1,70 @@
import json, os, sys, urllib.parse, urllib.request, urllib.error
API = "https://graph.microsoft.com/v1.0"
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _token():
cfg = _cfg()
data = urllib.parse.urlencode({
"grant_type": "client_credentials",
"client_id": str(cfg.get("client_id") or ""),
"client_secret": str(cfg.get("client_secret") or ""),
"scope": "https://graph.microsoft.com/.default",
}).encode("utf-8")
url = "https://login.microsoftonline.com/" + str(cfg.get("tenant_id") or "") + "/oauth2/v2.0/token"
req = urllib.request.Request(url, data=data,
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"},
method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
tok = json.loads(r.read())
if not tok.get("access_token"):
raise Exception("Token request failed: " + json.dumps(tok))
return tok["access_token"]
def request(method, path, params=None, body=None, extra_headers=None, token=None):
url = API + path
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
if q:
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
data = json.dumps(body).encode("utf-8") if body is not None else None
headers = {"Accept": "application/json", "Authorization": "Bearer " + (token or _token())}
if data is not None:
headers["Content-Type"] = "application/json"
if extra_headers:
headers.update(extra_headers)
req = urllib.request.Request(url, data=data, headers=headers, method=method)
with urllib.request.urlopen(req, timeout=90) as r:
raw = r.read()
return json.loads(raw) if raw else {}
def main():
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
user_id = inputs.get("user_id")
if not user_id:
raise Exception("user_id is required")
manager_id = inputs.get("manager_id")
if not manager_id:
raise Exception("manager_id is required")
q = lambda v: urllib.parse.quote(str(v), safe="")
body = {"@odata.id": "https://graph.microsoft.com/v1.0/users/" + q(manager_id)}
resp = request("PUT", "/users/" + q(user_id) + "/manager/$ref", body=body)
if not resp:
resp = {"ok": True, "user_id": user_id, "manager_id": manager_id}
print(json.dumps(resp))
try:
main()
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
@@ -0,0 +1,92 @@
import json, os, sys, urllib.parse, urllib.request, urllib.error
API = "https://graph.microsoft.com/v1.0"
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _token():
cfg = _cfg()
data = urllib.parse.urlencode({
"grant_type": "client_credentials",
"client_id": str(cfg.get("client_id") or ""),
"client_secret": str(cfg.get("client_secret") or ""),
"scope": "https://graph.microsoft.com/.default",
}).encode("utf-8")
url = "https://login.microsoftonline.com/" + str(cfg.get("tenant_id") or "") + "/oauth2/v2.0/token"
req = urllib.request.Request(url, data=data,
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"},
method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
tok = json.loads(r.read())
if not tok.get("access_token"):
raise Exception("Token request failed: " + json.dumps(tok))
return tok["access_token"]
def request(method, path, params=None, body=None, extra_headers=None, token=None):
url = API + path
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
if q:
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
data = json.dumps(body).encode("utf-8") if body is not None else None
headers = {"Accept": "application/json", "Authorization": "Bearer " + (token or _token())}
if data is not None:
headers["Content-Type"] = "application/json"
if extra_headers:
headers.update(extra_headers)
req = urllib.request.Request(url, data=data, headers=headers, method=method)
with urllib.request.urlopen(req, timeout=90) as r:
raw = r.read()
return json.loads(raw) if raw else {}
def main():
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
user_principal_name = inputs.get("user_principal_name")
if not user_principal_name:
raise Exception("user_principal_name is required")
display_name = inputs.get("display_name")
if not display_name:
raise Exception("display_name is required")
password = inputs.get("password")
if not password:
raise Exception("password is required")
mail_nickname = inputs.get("mail_nickname") or str(user_principal_name).split("@")[0]
account_enabled = inputs.get("account_enabled", True)
force_change_password = inputs.get("force_change_password", True)
body = {
"accountEnabled": bool(account_enabled),
"displayName": display_name,
"userPrincipalName": user_principal_name,
"mailNickname": mail_nickname,
"passwordProfile": {
"password": password,
"forceChangePasswordNextSignIn": bool(force_change_password),
},
}
if inputs.get("given_name"):
body["givenName"] = inputs.get("given_name")
if inputs.get("surname"):
body["surname"] = inputs.get("surname")
if inputs.get("job_title"):
body["jobTitle"] = inputs.get("job_title")
if inputs.get("department"):
body["department"] = inputs.get("department")
resp = request("POST", "/users", body=body)
print(json.dumps(resp))
try:
main()
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
@@ -0,0 +1,66 @@
import json, os, sys, urllib.parse, urllib.request, urllib.error
API = "https://graph.microsoft.com/v1.0"
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _token():
cfg = _cfg()
data = urllib.parse.urlencode({
"grant_type": "client_credentials",
"client_id": str(cfg.get("client_id") or ""),
"client_secret": str(cfg.get("client_secret") or ""),
"scope": "https://graph.microsoft.com/.default",
}).encode("utf-8")
url = "https://login.microsoftonline.com/" + str(cfg.get("tenant_id") or "") + "/oauth2/v2.0/token"
req = urllib.request.Request(url, data=data,
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"},
method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
tok = json.loads(r.read())
if not tok.get("access_token"):
raise Exception("Token request failed: " + json.dumps(tok))
return tok["access_token"]
def request(method, path, params=None, body=None, extra_headers=None, token=None):
url = API + path
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
if q:
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
data = json.dumps(body).encode("utf-8") if body is not None else None
headers = {"Accept": "application/json", "Authorization": "Bearer " + (token or _token())}
if data is not None:
headers["Content-Type"] = "application/json"
if extra_headers:
headers.update(extra_headers)
req = urllib.request.Request(url, data=data, headers=headers, method=method)
with urllib.request.urlopen(req, timeout=90) as r:
raw = r.read()
return json.loads(raw) if raw else {}
def main():
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
user_id = inputs.get("user_id")
if not user_id:
raise Exception("user_id is required")
q = lambda v: urllib.parse.quote(str(v), safe="")
resp = request("DELETE", "/users/" + q(user_id))
if not resp:
resp = {"ok": True, "user_id": user_id}
print(json.dumps(resp))
try:
main()
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
@@ -0,0 +1,66 @@
import json, os, sys, urllib.parse, urllib.request, urllib.error
API = "https://graph.microsoft.com/v1.0"
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _token():
cfg = _cfg()
data = urllib.parse.urlencode({
"grant_type": "client_credentials",
"client_id": str(cfg.get("client_id") or ""),
"client_secret": str(cfg.get("client_secret") or ""),
"scope": "https://graph.microsoft.com/.default",
}).encode("utf-8")
url = "https://login.microsoftonline.com/" + str(cfg.get("tenant_id") or "") + "/oauth2/v2.0/token"
req = urllib.request.Request(url, data=data,
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"},
method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
tok = json.loads(r.read())
if not tok.get("access_token"):
raise Exception("Token request failed: " + json.dumps(tok))
return tok["access_token"]
def request(method, path, params=None, body=None, extra_headers=None, token=None):
url = API + path
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
if q:
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
data = json.dumps(body).encode("utf-8") if body is not None else None
headers = {"Accept": "application/json", "Authorization": "Bearer " + (token or _token())}
if data is not None:
headers["Content-Type"] = "application/json"
if extra_headers:
headers.update(extra_headers)
req = urllib.request.Request(url, data=data, headers=headers, method=method)
with urllib.request.urlopen(req, timeout=90) as r:
raw = r.read()
return json.loads(raw) if raw else {}
def main():
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
user_id = inputs.get("user_id")
if not user_id:
raise Exception("user_id is required")
q = lambda v: urllib.parse.quote(str(v), safe="")
resp = request("PATCH", "/users/" + q(user_id), body={"accountEnabled": False})
if not resp:
resp = {"ok": True, "user_id": user_id, "account_enabled": False}
print(json.dumps(resp))
try:
main()
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
@@ -0,0 +1,66 @@
import json, os, sys, urllib.parse, urllib.request, urllib.error
API = "https://graph.microsoft.com/v1.0"
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _token():
cfg = _cfg()
data = urllib.parse.urlencode({
"grant_type": "client_credentials",
"client_id": str(cfg.get("client_id") or ""),
"client_secret": str(cfg.get("client_secret") or ""),
"scope": "https://graph.microsoft.com/.default",
}).encode("utf-8")
url = "https://login.microsoftonline.com/" + str(cfg.get("tenant_id") or "") + "/oauth2/v2.0/token"
req = urllib.request.Request(url, data=data,
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"},
method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
tok = json.loads(r.read())
if not tok.get("access_token"):
raise Exception("Token request failed: " + json.dumps(tok))
return tok["access_token"]
def request(method, path, params=None, body=None, extra_headers=None, token=None):
url = API + path
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
if q:
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
data = json.dumps(body).encode("utf-8") if body is not None else None
headers = {"Accept": "application/json", "Authorization": "Bearer " + (token or _token())}
if data is not None:
headers["Content-Type"] = "application/json"
if extra_headers:
headers.update(extra_headers)
req = urllib.request.Request(url, data=data, headers=headers, method=method)
with urllib.request.urlopen(req, timeout=90) as r:
raw = r.read()
return json.loads(raw) if raw else {}
def main():
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
user_id = inputs.get("user_id")
if not user_id:
raise Exception("user_id is required")
q = lambda v: urllib.parse.quote(str(v), safe="")
resp = request("PATCH", "/users/" + q(user_id), body={"accountEnabled": True})
if not resp:
resp = {"ok": True, "user_id": user_id, "account_enabled": True}
print(json.dumps(resp))
try:
main()
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
@@ -0,0 +1,64 @@
import json, os, sys, urllib.parse, urllib.request, urllib.error
API = "https://graph.microsoft.com/v1.0"
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _token():
cfg = _cfg()
data = urllib.parse.urlencode({
"grant_type": "client_credentials",
"client_id": str(cfg.get("client_id") or ""),
"client_secret": str(cfg.get("client_secret") or ""),
"scope": "https://graph.microsoft.com/.default",
}).encode("utf-8")
url = "https://login.microsoftonline.com/" + str(cfg.get("tenant_id") or "") + "/oauth2/v2.0/token"
req = urllib.request.Request(url, data=data,
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"},
method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
tok = json.loads(r.read())
if not tok.get("access_token"):
raise Exception("Token request failed: " + json.dumps(tok))
return tok["access_token"]
def request(method, path, params=None, body=None, extra_headers=None, token=None):
url = API + path
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
if q:
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
data = json.dumps(body).encode("utf-8") if body is not None else None
headers = {"Accept": "application/json", "Authorization": "Bearer " + (token or _token())}
if data is not None:
headers["Content-Type"] = "application/json"
if extra_headers:
headers.update(extra_headers)
req = urllib.request.Request(url, data=data, headers=headers, method=method)
with urllib.request.urlopen(req, timeout=90) as r:
raw = r.read()
return json.loads(raw) if raw else {}
def main():
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
user_id = inputs.get("user_id")
if not user_id:
raise Exception("user_id is required")
q = lambda v: urllib.parse.quote(str(v), safe="")
resp = request("GET", "/users/" + q(user_id) + "/authentication/methods")
print(json.dumps(resp))
try:
main()
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
@@ -0,0 +1,64 @@
import json, os, sys, urllib.parse, urllib.request, urllib.error
API = "https://graph.microsoft.com/v1.0"
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _token():
cfg = _cfg()
data = urllib.parse.urlencode({
"grant_type": "client_credentials",
"client_id": str(cfg.get("client_id") or ""),
"client_secret": str(cfg.get("client_secret") or ""),
"scope": "https://graph.microsoft.com/.default",
}).encode("utf-8")
url = "https://login.microsoftonline.com/" + str(cfg.get("tenant_id") or "") + "/oauth2/v2.0/token"
req = urllib.request.Request(url, data=data,
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"},
method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
tok = json.loads(r.read())
if not tok.get("access_token"):
raise Exception("Token request failed: " + json.dumps(tok))
return tok["access_token"]
def request(method, path, params=None, body=None, extra_headers=None, token=None):
url = API + path
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
if q:
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
data = json.dumps(body).encode("utf-8") if body is not None else None
headers = {"Accept": "application/json", "Authorization": "Bearer " + (token or _token())}
if data is not None:
headers["Content-Type"] = "application/json"
if extra_headers:
headers.update(extra_headers)
req = urllib.request.Request(url, data=data, headers=headers, method=method)
with urllib.request.urlopen(req, timeout=90) as r:
raw = r.read()
return json.loads(raw) if raw else {}
def main():
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
user_id = inputs.get("user_id")
if not user_id:
raise Exception("user_id is required")
q = lambda v: urllib.parse.quote(str(v), safe="")
resp = request("GET", "/users/" + q(user_id))
print(json.dumps(resp))
try:
main()
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
@@ -0,0 +1,65 @@
import json, os, sys, urllib.parse, urllib.request, urllib.error
API = "https://graph.microsoft.com/v1.0"
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _token():
cfg = _cfg()
data = urllib.parse.urlencode({
"grant_type": "client_credentials",
"client_id": str(cfg.get("client_id") or ""),
"client_secret": str(cfg.get("client_secret") or ""),
"scope": "https://graph.microsoft.com/.default",
}).encode("utf-8")
url = "https://login.microsoftonline.com/" + str(cfg.get("tenant_id") or "") + "/oauth2/v2.0/token"
req = urllib.request.Request(url, data=data,
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"},
method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
tok = json.loads(r.read())
if not tok.get("access_token"):
raise Exception("Token request failed: " + json.dumps(tok))
return tok["access_token"]
def request(method, path, params=None, body=None, extra_headers=None, token=None):
url = API + path
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
if q:
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
data = json.dumps(body).encode("utf-8") if body is not None else None
headers = {"Accept": "application/json", "Authorization": "Bearer " + (token or _token())}
if data is not None:
headers["Content-Type"] = "application/json"
if extra_headers:
headers.update(extra_headers)
req = urllib.request.Request(url, data=data, headers=headers, method=method)
with urllib.request.urlopen(req, timeout=90) as r:
raw = r.read()
return json.loads(raw) if raw else {}
def main():
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
user_id = inputs.get("user_id")
if not user_id:
raise Exception("user_id is required")
limit = inputs.get("limit", 50)
q = lambda v: urllib.parse.quote(str(v), safe="")
resp = request("GET", "/users/" + q(user_id) + "/memberOf", params={"$top": limit})
print(json.dumps(resp))
try:
main()
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
@@ -0,0 +1,64 @@
import json, os, sys, urllib.parse, urllib.request, urllib.error
API = "https://graph.microsoft.com/v1.0"
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _token():
cfg = _cfg()
data = urllib.parse.urlencode({
"grant_type": "client_credentials",
"client_id": str(cfg.get("client_id") or ""),
"client_secret": str(cfg.get("client_secret") or ""),
"scope": "https://graph.microsoft.com/.default",
}).encode("utf-8")
url = "https://login.microsoftonline.com/" + str(cfg.get("tenant_id") or "") + "/oauth2/v2.0/token"
req = urllib.request.Request(url, data=data,
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"},
method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
tok = json.loads(r.read())
if not tok.get("access_token"):
raise Exception("Token request failed: " + json.dumps(tok))
return tok["access_token"]
def request(method, path, params=None, body=None, extra_headers=None, token=None):
url = API + path
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
if q:
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
data = json.dumps(body).encode("utf-8") if body is not None else None
headers = {"Accept": "application/json", "Authorization": "Bearer " + (token or _token())}
if data is not None:
headers["Content-Type"] = "application/json"
if extra_headers:
headers.update(extra_headers)
req = urllib.request.Request(url, data=data, headers=headers, method=method)
with urllib.request.urlopen(req, timeout=90) as r:
raw = r.read()
return json.loads(raw) if raw else {}
def main():
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
user_id = inputs.get("user_id")
if not user_id:
raise Exception("user_id is required")
q = lambda v: urllib.parse.quote(str(v), safe="")
resp = request("GET", "/users/" + q(user_id) + "/manager")
print(json.dumps(resp))
try:
main()
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
@@ -0,0 +1,72 @@
import json, os, sys, urllib.parse, urllib.request, urllib.error
API = "https://graph.microsoft.com/v1.0"
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _token():
cfg = _cfg()
data = urllib.parse.urlencode({
"grant_type": "client_credentials",
"client_id": str(cfg.get("client_id") or ""),
"client_secret": str(cfg.get("client_secret") or ""),
"scope": "https://graph.microsoft.com/.default",
}).encode("utf-8")
url = "https://login.microsoftonline.com/" + str(cfg.get("tenant_id") or "") + "/oauth2/v2.0/token"
req = urllib.request.Request(url, data=data,
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"},
method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
tok = json.loads(r.read())
if not tok.get("access_token"):
raise Exception("Token request failed: " + json.dumps(tok))
return tok["access_token"]
def request(method, path, params=None, body=None, extra_headers=None, token=None):
url = API + path
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
if q:
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
data = json.dumps(body).encode("utf-8") if body is not None else None
headers = {"Accept": "application/json", "Authorization": "Bearer " + (token or _token())}
if data is not None:
headers["Content-Type"] = "application/json"
if extra_headers:
headers.update(extra_headers)
req = urllib.request.Request(url, data=data, headers=headers, method=method)
with urllib.request.urlopen(req, timeout=90) as r:
raw = r.read()
return json.loads(raw) if raw else {}
def main():
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
filt = inputs.get("filter")
search = inputs.get("search")
limit = inputs.get("limit", 50)
params = {"$top": limit}
extra_headers = None
if filt:
params["$filter"] = filt
extra_headers = {"ConsistencyLevel": "eventual"}
if search:
params["$search"] = "\"" + str(search) + "\""
extra_headers = {"ConsistencyLevel": "eventual"}
resp = request("GET", "/users", params=params, extra_headers=extra_headers)
print(json.dumps(resp))
try:
main()
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
@@ -0,0 +1,76 @@
import json, os, sys, urllib.parse, urllib.request, urllib.error
API = "https://graph.microsoft.com/v1.0"
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _token():
cfg = _cfg()
data = urllib.parse.urlencode({
"grant_type": "client_credentials",
"client_id": str(cfg.get("client_id") or ""),
"client_secret": str(cfg.get("client_secret") or ""),
"scope": "https://graph.microsoft.com/.default",
}).encode("utf-8")
url = "https://login.microsoftonline.com/" + str(cfg.get("tenant_id") or "") + "/oauth2/v2.0/token"
req = urllib.request.Request(url, data=data,
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"},
method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
tok = json.loads(r.read())
if not tok.get("access_token"):
raise Exception("Token request failed: " + json.dumps(tok))
return tok["access_token"]
def request(method, path, params=None, body=None, extra_headers=None, token=None):
url = API + path
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
if q:
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
data = json.dumps(body).encode("utf-8") if body is not None else None
headers = {"Accept": "application/json", "Authorization": "Bearer " + (token or _token())}
if data is not None:
headers["Content-Type"] = "application/json"
if extra_headers:
headers.update(extra_headers)
req = urllib.request.Request(url, data=data, headers=headers, method=method)
with urllib.request.urlopen(req, timeout=90) as r:
raw = r.read()
return json.loads(raw) if raw else {}
def main():
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
user_id = inputs.get("user_id")
if not user_id:
raise Exception("user_id is required")
password = inputs.get("password")
if not password:
raise Exception("password is required")
force_change = inputs.get("force_change", True)
q = lambda v: urllib.parse.quote(str(v), safe="")
body = {
"passwordProfile": {
"password": password,
"forceChangePasswordNextSignIn": bool(force_change),
}
}
resp = request("PATCH", "/users/" + q(user_id), body=body)
if not resp:
resp = {"ok": True, "user_id": user_id}
print(json.dumps(resp))
try:
main()
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
@@ -0,0 +1,64 @@
import json, os, sys, urllib.parse, urllib.request, urllib.error
API = "https://graph.microsoft.com/v1.0"
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _token():
cfg = _cfg()
data = urllib.parse.urlencode({
"grant_type": "client_credentials",
"client_id": str(cfg.get("client_id") or ""),
"client_secret": str(cfg.get("client_secret") or ""),
"scope": "https://graph.microsoft.com/.default",
}).encode("utf-8")
url = "https://login.microsoftonline.com/" + str(cfg.get("tenant_id") or "") + "/oauth2/v2.0/token"
req = urllib.request.Request(url, data=data,
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"},
method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
tok = json.loads(r.read())
if not tok.get("access_token"):
raise Exception("Token request failed: " + json.dumps(tok))
return tok["access_token"]
def request(method, path, params=None, body=None, extra_headers=None, token=None):
url = API + path
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
if q:
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
data = json.dumps(body).encode("utf-8") if body is not None else None
headers = {"Accept": "application/json", "Authorization": "Bearer " + (token or _token())}
if data is not None:
headers["Content-Type"] = "application/json"
if extra_headers:
headers.update(extra_headers)
req = urllib.request.Request(url, data=data, headers=headers, method=method)
with urllib.request.urlopen(req, timeout=90) as r:
raw = r.read()
return json.loads(raw) if raw else {}
def main():
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
user_id = inputs.get("user_id")
if not user_id:
raise Exception("user_id is required")
q = lambda v: urllib.parse.quote(str(v), safe="")
resp = request("POST", "/users/" + q(user_id) + "/revokeSignInSessions")
print(json.dumps(resp))
try:
main()
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
@@ -0,0 +1,60 @@
import json, os, sys, urllib.parse, urllib.request, urllib.error
API = "https://graph.microsoft.com/v1.0"
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _token():
cfg = _cfg()
data = urllib.parse.urlencode({
"grant_type": "client_credentials",
"client_id": str(cfg.get("client_id") or ""),
"client_secret": str(cfg.get("client_secret") or ""),
"scope": "https://graph.microsoft.com/.default",
}).encode("utf-8")
url = "https://login.microsoftonline.com/" + str(cfg.get("tenant_id") or "") + "/oauth2/v2.0/token"
req = urllib.request.Request(url, data=data,
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"},
method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
tok = json.loads(r.read())
if not tok.get("access_token"):
raise Exception("Token request failed: " + json.dumps(tok))
return tok["access_token"]
def request(method, path, params=None, body=None, extra_headers=None, token=None):
url = API + path
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
if q:
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
data = json.dumps(body).encode("utf-8") if body is not None else None
headers = {"Accept": "application/json", "Authorization": "Bearer " + (token or _token())}
if data is not None:
headers["Content-Type"] = "application/json"
if extra_headers:
headers.update(extra_headers)
req = urllib.request.Request(url, data=data, headers=headers, method=method)
with urllib.request.urlopen(req, timeout=90) as r:
raw = r.read()
return json.loads(raw) if raw else {}
def main():
resp = request("GET", "/organization", params={"$select": "id,displayName", "$top": 1})
if "value" not in resp:
raise Exception("unexpected response")
print(json.dumps({"ok": True}))
try:
main()
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
@@ -0,0 +1,90 @@
import json, os, sys, urllib.parse, urllib.request, urllib.error
API = "https://graph.microsoft.com/v1.0"
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _token():
cfg = _cfg()
data = urllib.parse.urlencode({
"grant_type": "client_credentials",
"client_id": str(cfg.get("client_id") or ""),
"client_secret": str(cfg.get("client_secret") or ""),
"scope": "https://graph.microsoft.com/.default",
}).encode("utf-8")
url = "https://login.microsoftonline.com/" + str(cfg.get("tenant_id") or "") + "/oauth2/v2.0/token"
req = urllib.request.Request(url, data=data,
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"},
method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
tok = json.loads(r.read())
if not tok.get("access_token"):
raise Exception("Token request failed: " + json.dumps(tok))
return tok["access_token"]
def request(method, path, params=None, body=None, extra_headers=None, token=None):
url = API + path
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
if q:
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
data = json.dumps(body).encode("utf-8") if body is not None else None
headers = {"Accept": "application/json", "Authorization": "Bearer " + (token or _token())}
if data is not None:
headers["Content-Type"] = "application/json"
if extra_headers:
headers.update(extra_headers)
req = urllib.request.Request(url, data=data, headers=headers, method=method)
with urllib.request.urlopen(req, timeout=90) as r:
raw = r.read()
return json.loads(raw) if raw else {}
def main():
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
user_id = inputs.get("user_id")
if not user_id:
raise Exception("user_id is required")
q = lambda v: urllib.parse.quote(str(v), safe="")
body = {}
if inputs.get("display_name"):
body["displayName"] = inputs.get("display_name")
if inputs.get("given_name"):
body["givenName"] = inputs.get("given_name")
if inputs.get("surname"):
body["surname"] = inputs.get("surname")
if inputs.get("job_title"):
body["jobTitle"] = inputs.get("job_title")
if inputs.get("department"):
body["department"] = inputs.get("department")
if inputs.get("mobile_phone"):
body["mobilePhone"] = inputs.get("mobile_phone")
fields_json = inputs.get("fields_json")
if fields_json:
extra = json.loads(fields_json)
if not isinstance(extra, dict):
raise Exception("fields_json must be a JSON object")
body.update(extra)
if not body:
raise Exception("nothing to update")
resp = request("PATCH", "/users/" + q(user_id), body=body)
if not resp:
resp = {"ok": True, "user_id": user_id}
print(json.dumps(resp))
try:
main()
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)