-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathssoUsernameByOrg-simple.py
More file actions
74 lines (64 loc) · 1.68 KB
/
Copy pathssoUsernameByOrg-simple.py
File metadata and controls
74 lines (64 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#! /usr/bin/python3
#
# Pre-reqs:
# pip3 install -r requirements.txt
#
# Usage: python3 ssoUsernameByOrg-simple github-organization-name
#
# Returns CSV of GithubUsername,netid@illinois.edu
#
# Set GITHUB_TOKEN environment variable (or .env file) to your personal access token
#
# This script is left around as a simple example, but only works for the first 100
# users of an organization. For Organizations with more users, see the
# ssoUsernameByOrg.py script that does pagenation.
#
from github import Github
import requests
import json
import argparse
import os
from pprint import pprint
from dotenv import load_dotenv
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('org')
args = arg_parser.parse_args()
load_dotenv()
token = os.getenv('GITHUB_TOKEN', '...')
g = Github(token)
query = """
query($organization: String!) {
organization(login: $organization) {
samlIdentityProvider {
ssoUrl,
externalIdentities(first: 100) {
edges {
node {
guid,
samlIdentity {
nameId
}
user {
login
}
}
}
}
}
}
}"""
variables = {
'organization': args.org,
}
headers = {
'authorization': 'bearer ' + token,
'content-type': 'application/json'
}
r = requests.post('https://api.github.com/graphql',
json={'query': query, 'variables': variables}, headers=headers)
#pprint(r.json())
out_j = r.json()
for node in out_j["data"]["organization"]["samlIdentityProvider"]["externalIdentities"]["edges"]:
username = node["node"]["user"]["login"]
samlId = node["node"]["samlIdentity"]["nameId"]
print("{},{}".format(username, samlId))