Skip to content

Commit

Permalink
#13: Adding a script for generating Nginx authentication for a game
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeiP committed May 16, 2019
1 parent d58cda8 commit b39b0ff
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 15 deletions.
58 changes: 58 additions & 0 deletions scripts/generate_passwords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from shutil import copyfile
import string, random, crypt
import MySQLdb

def find_check_ids_by_name(checks, cursor):
# execute SQL query using execute() method.
cursor.execute("SELECT id FROM kipa_tehtava WHERE sarja_id IN (26, 27, 28, 29, 30) AND nimi IN ('%s')" % "', '".join(checks))
data = cursor.fetchall()

res = []
for row in data:
for val in row:
res.append(str(val))

return res

prefix = 'llhk19-'
baseurl = '/kipa/Leon_lenkki_ja_Hilkan_kilpa_2019/'
accounts = {
"actionfactory": ["Action_Factory"],
"yo": ["Tehtavan 1 nimi", "Tehtavan 2 nimi"]
}
templatefile = '/srv/django/kipa/passwords-llhk19'
path = '/srv/django/kipa/auth_files/llhk19/'
letters = string.ascii_letters

access_config = ''

db = MySQLdb.connect(user="USER", passwd="PASSWORD", host="localhost", db="DATABASE")
cursor = db.cursor()

print(accounts)
for name, checks in accounts.items():
username = prefix + name
pwdfile = path + name
password = ''.join(random.choice(letters) for i in range(10))
pwhash = crypt.crypt(password)
copyfile(templatefile, pwdfile)
f = open(pwdfile, 'a')
print(username + ':' + pwhash, file=f)
print(username + ';' + password)

ids = find_check_ids_by_name(checks, cursor)
for id in ids:
access_config = access_config + """
location {baseurl}syota/tehtava/{id} {{
auth_basic "Vain {name}-rastin tulosten syottajille";
auth_basic_user_file "{pwdfile}";
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/kipa.sock;
}}
""".format(baseurl=baseurl, name=name.capitalize(), id=id, pwdfile=pwdfile)

c = open(path + 'access.conf', 'w')
print(access_config, file=c)

db.close()
11 changes: 0 additions & 11 deletions web/settings.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import os

import time
try :
fin = open("templates/version.html", "r")
revision_line=fin.readline() ;
fin.close()
fout = open("templates/version.html", "w")
fout.write( revision_line )
fout.write( "{% comment %} " +time.strftime('%X %x') +" {% endcomment %}" ) # Add clock to force svn commit
fout.close()
except: pass

hakemisto=os.path.normpath(os.path.dirname(__file__))
tarkistus= os.getcwd()

Expand Down
1 change: 0 additions & 1 deletion web/templates/tupa/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
<a href="/kipa/{{kisa_nimi}}/" class="capitalize">{{kisa_nimi|alaviiva_pois}}</a> {%if taakse.url and taakse.title%}&raquo; <a href="{{taakse.url}}">{{taakse.title|alaviiva_pois}}</a> {% endif %}&raquo; {{heading|alaviiva_pois}}
{% endif %}

<div id="version">{% include "version.html" %}</div>
</div>

<div id="main" class="clearfix">
Expand Down
2 changes: 1 addition & 1 deletion web/templates/tupa/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ <h1>Kisat</h1>
</div><!-- /main -->
</div><!-- /wrap -->

<div id="footer">Kipa {% include "version.html" %} &nbsp;</div>
<div id="footer">Kipa</div>

</body>
</html>
2 changes: 0 additions & 2 deletions web/templates/version.html

This file was deleted.

0 comments on commit b39b0ff

Please sign in to comment.