@anonymous/

ConsciousBlondElectricity

Python

No description

fork
loading
Files
  • main.py
main.py
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
def authenticate(realm, authid, details):
  print("WAMP-Anonymous dynamic authenticator invoked: realm='{}', authid='{}'".format(realm, authid))
  pprint(details)

  principal = {
    u'role': u'anonymous'
  }

  # Is it best way to response error?
  # if (details is None) or (not 'Bearer token' in details['authextra']):
  if (details is None) :
    principal[u'extra'] = {
        u'error': u"Access denied: No Bearer token in authexta"
    }

    return principal


  token = details['authextra']['Bearer token'];

  print("BEFORE")

  try:
    payload = jwt.decode(token, JWT_SECRET)
  except Exception as e:
    # todo: Log in case of system error

    print(e);

    principal[u'extra'] = {
        u'error': e
    }

    return principal

  print("AFTER")

  print("payload {}".format(payload))


  # Connect to an existing database
  db = psycopg2.connect(u"dbname=crypter user=crypter password=password")

  # Open a cursor to perform database operations
  cursor = db.cursor(cursor_factory=psycopg2.extras.DictCursor)

  # Query the database and obtain data as Python objects
  cursor.execute('SELECT * FROM "user" WHERE uuid=%(uuid)s;', { 'uuid': payload[u'uuid'] })

  user = cursor.fetchone()

  cursor.close()
  db.close()


  if not user:
    principal[u'extra'] = {
        u'error': u"Access denied: No such user"
    }

    return principal

  if payload[u'hash'] == user[u'hash']:

    principal = {
        u'role': u'user'
    }
  else:
    principal[u'extra'] = {
        u'error': u"Access denied: Wrong token"
    }

  return principal