-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
89 lines (69 loc) · 2.4 KB
/
server.py
File metadata and controls
89 lines (69 loc) · 2.4 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from github import Github, GithubException
import uuid
import os
import base64
from flask import Flask, request, render_template
import boto3
GITHUB_USERNAME = os.environ["GITHUB_USERNAME"]
GITHUB_PASSWORD = os.environ["GITHUB_PASSWORD"]
g = Github(GITHUB_USERNAME, GITHUB_PASSWORD)
dynamodb = boto3.resource('dynamodb', region_name='us-west-1')
table = dynamodb.Table('CandidateAuth')
app = Flask(__name__)
@app.route('/')
def hello():
return render_template("index.html")
@app.route('/challenge', methods = ["POST", "GET"])
def challenge():
keys = ["name", "uuid", "github_username"]
if any([key not in request.form for key in keys]):
return render_template("index.html")
candidate_name = request.form["name"]
github_username = request.form["github_username"]
user_id = request.form["uuid"]
try:
_validate_user_id(user_id)
except Exception as e:
error_message = str(e)
return render_template("error.html", error=error_message)
user = g.get_user()
repo_name = str(uuid.uuid4()) + "-" + candidate_name
repo = user.create_repo(repo_name, private=True)
repo_contents_path = "repo_contents"
files = os.listdir(repo_contents_path)
for file in files:
full_path = os.path.join(repo_contents_path, file)
with open(full_path, 'r') as f:
content = f.read()
repo.create_file(file, 'Adding file: ' + file, content)
try:
repo.add_to_collaborators(github_username, "push")
except GithubException as e:
repo.delete()
error_message = "Authorization failed due to invalid Github username!"
return render_template("error.html", error=error_message)
# repo.add_to_collaborators(github_username, "push")
return
def _validate_user_id(user_id):
response = table.get_item(
Key={
"uuid": user_id,
}
)
if "Item" not in response:
raise Exception("Authorization failed due to invalid UUID! UUID not recognized.")
item = response["Item"]
if item["status"]:
raise Exception("Authorization failed due to invalid UUID! UUID already used.")
def _invalidate_user_id(user_id):
table.update_item(
Key={
'uuid': user_id,
},
UpdateExpression='SET status = :val1',
ExpressionAttributeValues={
':val1': True
}
)
if __name__ == '__main__':
app.run()