def _get_user_email(self, access_token): """Determine even private email, if the token has 'user:email' scope.""" http_client = AsyncHTTPClient() headers = _api_headers(access_token) next_page = "https://%s/user/emails" % (GITHUB_API) while next_page: req = HTTPRequest(next_page, method="GET", headers=headers) resp = yield http_client.fetch(req) resp_json = json.loads(resp.body.decode('utf8', 'replace')) next_page = next_page_from_links(resp) for entry in resp_json: if "email" in entry: if "primary" in entry and entry["primary"]: return entry["email"] return None
def _check_organization_whitelist(self, org, username, access_token): http_client = AsyncHTTPClient() headers = _api_headers(access_token) # Get all the members for organization 'org' # With empty scope (even if authenticated by an org member), this # will only yield public org members. You want 'read:org' in order # to be able to iterate through all members. next_page = "%s://%s/orgs/%s/members" % (GITHUB_PROTOCOL, GITHUB_API, org) while next_page: req = HTTPRequest(next_page, method="GET", headers=headers) resp = yield http_client.fetch(req) resp_json = json.loads(resp.body.decode('utf8', 'replace')) next_page = next_page_from_links(resp) for entry in resp_json: if username == entry['login']: return True return False
def _get_user_organizations(self, access_token): """Get list of orgs user is a member of. Requires 'read:org' token scope. """ http_client = AsyncHTTPClient() headers = _api_headers(access_token) next_page = "https://%s/user/orgs" % (GITHUB_API) orgmap = {} while next_page: req = HTTPRequest(next_page, method="GET", headers=headers) try: resp = yield http_client.fetch(req) except HTTPError: return None resp_json = json.loads(resp.body.decode('utf8', 'replace')) next_page = next_page_from_links(resp) for entry in resp_json: # This could result in non-unique groups, if the first 32 # characters of the group names are the same. normalized_group = entry["login"][:32] orgmap[normalized_group] = entry["id"] return orgmap