-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CGI middleware for requests to People API (#346)
* Add CGI wrapper for People API requests * Add CGI handler to dev server * Point Auth to new People API endpoint * Deploy cgi-bin * Condense print statements in CGI script Co-authored-by: Nate Foss <[email protected]>
- Loading branch information
Showing
7 changed files
with
125 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,3 +108,6 @@ public | |
|
||
# TernJS port file | ||
.tern-port | ||
|
||
# Credentials for the MIT People API | ||
credentials.ini |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/python3 | ||
from cgi import FieldStorage | ||
from configparser import ConfigParser | ||
from json import dumps, loads | ||
from os import path | ||
from requests import get | ||
|
||
args = FieldStorage() | ||
|
||
error = 'Status: 400 Bad Request\n' | ||
header = 'Content-Type: application/json\n\n' | ||
|
||
if 'kerb' not in args: | ||
output = {"error": "kerb was not specified"} | ||
header = error + header | ||
else: | ||
c = ConfigParser() | ||
try: | ||
with open(path.join(path.dirname(path.realpath(__file__)), 'credentials.ini')) as fp: | ||
c.read_file(fp) | ||
response = get('https://mit-people-v3.cloudhub.io/people/v3/people/{0}'.format(args['kerb'].value), headers={'client_id': c['Credentials']['ID'], 'client_secret': c['Credentials']['Secret']}) | ||
if response.status_code != 200: | ||
header = error + header | ||
output = {"error": "could not get user data"} | ||
else: | ||
data = loads(response.text) | ||
if data['item']['affiliations'][0]['type'] != "student": | ||
header = error + header | ||
output = {"error": "user is not a student"} | ||
else: | ||
year = data['item']['affiliations'][0]['classYear'] | ||
if year == "G": | ||
header = error + header | ||
output = {"error": "user is a graduate student (currently unhandled)"} | ||
else: | ||
year = int(year) | ||
year = year - 1 | ||
output = {"year": year} | ||
except Exception: | ||
header = error + header | ||
output = {"error": "could not read credentials"} | ||
|
||
print(header) | ||
print(dumps(output)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters