Skip to content

Commit

Permalink
Closes CTFd#21
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeKevin committed Mar 15, 2015
1 parent eced601 commit d09d0a9
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 11 deletions.
15 changes: 13 additions & 2 deletions CTFd/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def admin_config():
view_challenges_unregistered = None
prevent_registration = None

ctf_name = set_config("ctf_name", request.form.get('ctf_name', None))
mg_api_key = set_config("mg_api_key", request.form.get('mg_api_key', None))
do_api_key = set_config("do_api_key", request.form.get('do_api_key', None))

db_start = Config.query.filter_by(key='start').first()
Expand All @@ -84,6 +86,14 @@ def admin_config():
db.session.commit()
return redirect('/admin/config')

ctf_name = get_config('ctf_name')
if not ctf_name:
set_config('do_api_key', None)

mg_api_key = get_config('do_api_key')
if not mg_api_key:
set_config('do_api_key', None)

do_api_key = get_config('do_api_key')
if not do_api_key:
set_config('do_api_key', None)
Expand All @@ -107,8 +117,9 @@ def admin_config():
db.session.commit()
db.session.close()

return render_template('admin/config.html', start=start, end=end, view_challenges_unregistered=view_challenges_unregistered,
prevent_registration=prevent_registration, do_api_key=do_api_key)
return render_template('admin/config.html', ctf_name=ctf_name, start=start, end=end,
view_challenges_unregistered=view_challenges_unregistered,
prevent_registration=prevent_registration, do_api_key=do_api_key, mg_api_key=mg_api_key)

@app.route('/admin/pages', defaults={'route': None}, methods=['GET', 'POST'])
@app.route('/admin/pages/<route>', methods=['GET', 'POST'])
Expand Down
2 changes: 1 addition & 1 deletion CTFd/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
HOST = ".ctfd.io"
UPLOAD_FOLDER = os.path.normpath('static/uploads')

##### EMAIL #####
##### EMAIL (if not using Mailgun) #####
CTF_NAME = ''
MAIL_SERVER = ''
MAIL_PORT = 0
Expand Down
28 changes: 21 additions & 7 deletions CTFd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import hashlib
import digitalocean
import shutil
import requests


def init_utils(app):
Expand Down Expand Up @@ -147,18 +148,31 @@ def set_config(key, value):


def mailserver():
if app.config['MAIL_SERVER'] and app.config['MAIL_PORT'] and app.config['ADMINS']:
if get_config('mg_api_key') or (app.config['MAIL_SERVER'] and app.config['MAIL_PORT'] and app.config['ADMINS']):
return True
return False


def sendmail(addr, text):
try:
msg = Message("Message from {0}".format(app.config['CTF_NAME']), sender = app.config['ADMINS'][0], recipients = [addr])
msg.body = text
mail.send(msg)
return True
except:
if get_config('mg_api_key'):
ctf_name = get_config('ctf_name')
mg_api_key = get_config('mg_api_key')
return requests.post(
"https://api.mailgun.net/v2/mail"+app.config['HOST']+"/messages",
auth=("api", mg_api_key),
data={"from": "{} Admin <[email protected]>".format(ctf_name),
"to": [addr],
"subject": "Message from {0}".format(ctf_name),
"text": text})
elif app.config['MAIL_SERVER'] and app.config['MAIL_PORT'] and app.config['ADMINS']:
try:
msg = Message("Message from {0}".format(get_config('ctf_name')), sender=app.config['ADMINS'][0], recipients=[addr])
msg.body = text
mail.send(msg)
return True
except:
return False
else:
return False


Expand Down
4 changes: 4 additions & 0 deletions CTFd/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ def setup():

if not is_setup():
if request.method == 'POST':
ctf_name = request.form['ctf_name']
ctf_name = Config('ctf_name', ctf_name)

## Admin user
name = request.form['name']
email = request.form['email']
Expand All @@ -71,6 +74,7 @@ def setup():

setup = Config('setup', True)

db.session.add(ctf_name)
db.session.add(admin)
db.session.add(page)
db.session.add(start)
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ passlib==1.6.2
bcrypt
six==1.8.0
itsdangerous
python-digitalocean
python-digitalocean
requests
10 changes: 10 additions & 0 deletions templates/admin/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ <h1>Config</h1>
<form method="POST">
<input name='nonce' type='hidden' value="{{ nonce }}">

<div class="row">
<label for="start">CTF Name:</label>
<input id='ctf_name' name='ctf_name' type='text' placeholder="CTF Name" {% if ctf_name is defined and ctf_name != None %}value="{{ ctf_name }}"{% endif %}>
</div>

<div class="row">
<label for="start">Mailgun API Key:</label>
<input id='mg_api_key' name='mg_api_key' type='text' placeholder="Mailgun API Key" {% if mg_api_key is defined and mg_api_key != None %}value="{{ mg_api_key }}"{% endif %}>
</div>

<div class="row">
<label for="start">Digital Ocean API Key:</label>
<input id='do_api_key' name='do_api_key' type='text' placeholder="Digital Ocean API Key" {% if do_api_key is defined and do_api_key != None %}value="{{ do_api_key }}"{% endif %}>
Expand Down
3 changes: 3 additions & 0 deletions templates/setup.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ <h1>Setup</h1>
</div>

<form method="POST">
<strong>CTF Name</strong>
<input class="radius" type='text' name='ctf_name' placeholder='CTF Name'><br/>

<strong>Username</strong>
<input class="radius" type='text' name='name' placeholder='Name'><br/>

Expand Down

0 comments on commit d09d0a9

Please sign in to comment.