Skip to content

Instantly share code, notes, and snippets.

@numbleroot
Created March 21, 2015 16:22
Show Gist options
  • Save numbleroot/fa43608b6f67ccd59008 to your computer and use it in GitHub Desktop.
Save numbleroot/fa43608b6f67ccd59008 to your computer and use it in GitHub Desktop.
Freitagsrunde Gatekeeper session script
#!/usr/bin/env python
# ~*~ coding: utf-8
# Gatekeeper unlock script.
# Retrieve your authentication session via command line.
# Based on: https://gist.github.com/Nighoo/4dee529926a7c8e9090c by Nighoo
import urllib.request
import urllib.parse
import urllib.error
import keyring
# Configure these values to your needs.
# frundeUserName = the user name you'd type into gatekeeper.freitagsrunde.org.
# frundeKeyringName = under which object your login values are stored in GNOME keyring.
# frundeKeyringUser = your identifier for this specific Freitagsrunden password.
frundeUserName = 'user'
frundeKeyringName = 'Freitagsrunde'
frundeKeyringUser = 'intern'
# Check the provided GNOME keyring credentials.
# If they aren't available, abort.
def tryCredentials():
found = keyring.get_password(frundeKeyringName, frundeKeyringUser)
if found is not None:
print("Found the provided credentials in your keyring. Proceeding...")
else:
print("The credentials you provided couldn't be found in your default GNOME keyring. Please add them.")
exit(1)
# Parse the 32 characters long form element from given request answer.
def getToken(ret, targetform):
start = ret.find(targetform)
begin_token = start + ret[start:].find(b'value=\'') + len('value=\'')
token = ret[begin_token:ret[begin_token:].find(b'\'') + begin_token]
if len(token) != 32:
print("Authentication token hadn't have the correct length. Aborting.")
exit(1)
else:
print("Received token: ", token)
return token
def main():
# Check if your credentials are available.
tryCredentials()
# Initialize the gatekeeper connection.
cookieHandler = urllib.request.HTTPCookieProcessor()
opener = urllib.request.build_opener(urllib.request.HTTPSHandler(), cookieHandler)
ret = opener.open('https://gatekeeper.freitagsrunde.org/gatekeeper').read()
# Parse for authentication token.
token = getToken(ret, b'/login/')
# Prepare session request data.
data = { 'csrfmiddlewaretoken': token, 'username': frundeUserName, 'password': keyring.get_password(frundeKeyringName, frundeKeyringUser) }
data = urllib.parse.urlencode(data)
data = data.encode('utf-8')
req = urllib.request.Request('https://gatekeeper.freitagsrunde.org/login/', data)
req.add_header('Referer', 'https://gatekeeper.freitagsrunde.org/gatekeeper')
# If we are able to get an answer to the prepared request, we're logged in.
# Otherwise, print the error and abort.
try:
ret = opener.open(req).read()
print("Logged in!")
token = getToken(ret, b'/gatekeeper/unkeep/')
except urllib.error.URLError as e:
print("Couldn't login:\n%s" % str(e.reason))
exit(1)
# As we are logged in now, we can unlock our gatekeeper session for 24 hours.
data = { 'csrfmiddlewaretoken': token, 'action': 'Akzeptieren', 'duration': '24', 'redirect': '', 'submit': 'Freischalten' }
data = urllib.parse.urlencode(data)
data = data.encode('utf-8')
req = urllib.request.Request('https://gatekeeper.freitagsrunde.org/gatekeeper/unkeep/', data)
req.add_header('Referer', 'https://gatekeeper.freitagsrunde.org/gatekeeper')
# If the request has been submitted successfully, everything's done and we can exit.
# Otherwise, we encountered an error.
try:
opener.open(req)
print("Everything done. Happy internetzing.")
exit(0)
except urllib.error.URLError as e:
print("Couldn't unkeep:\n%s" % str(e.reason))
exit(1)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment