# Path: auth/constants.py
# AUTH_ALLOWED = 1
# AUTH_DENIED = 0
# AUTH_ERROR = -1
# AUTH_VALIDATION = 5
# AUTH_VALIDATION_FAILED = 6
# STAGE_COUNTERS = 'counters'
# STAGE_LOGIN = 'login'
# STAGE_LOGOUT = 'logout'
# ROLES = {
#     u'super-admin': u'Super Admin',
#     u'network-admin': u'Network Admin',
#     u'gateway-admin': u'Gateway Admin',
#     u'service': u'Service',
# }
# STATUS_ICONS = {
#     'active': 'bolt',
#     'archived': 'trash',
#     'blocked': 'thumb-down',
#     'ended': 'flag',
#     'expired': 'circle-x',
#     'new': 'file',
# }
# ACTIONS = {
#     'vouchers': [
#         'extend',
#         'block',
#         'unblock',
#         'archive',
#     ],
# }
# 
# Path: auth/graphs.py
# def available_actions(status, interface):
#     if status in states:
#         result = {}
#         for action, defn in six.iteritems(actions):
#             if action in states[status] and defn['interface'] == interface:
#                 result[action] = defn
#         return result
# 
#     return {}


import base64
import datetime
import json
import re
import string
import flask
from auth import constants
from auth.graphs import available_actions
from flask import current_app
from flask_security import UserMixin, RoleMixin, current_user, SQLAlchemyUserDatastore
from flask_sqlalchemy import SQLAlchemy
from random import choice
from sqlalchemy import event
from sqlalchemy.engine import Engine
from sqlalchemy.orm import backref
from sqlalchemy.schema import UniqueConstraint
    outgoing = db.Column(db.BigInteger)

    gateway_id = db.Column(db.Unicode(20), db.ForeignKey('gateways.id', onupdate='cascade'), nullable=False)
    gateway = db.relationship(Gateway, backref=backref('auths', lazy='dynamic'))

    voucher_id = db.Column(db.Integer, db.ForeignKey('vouchers.id', onupdate='cascade'))
    voucher = db.relationship(Voucher, backref=backref('auths', lazy='dynamic'))

    status = db.Column(db.Integer)
    messages = db.Column(db.Text)
    created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)

    def matches_voucher(self, voucher):
        return self.gateway_id == voucher.gateway_id and self.mac == voucher.mac and self.ip == voucher.ip

    def process_request(self):
        if self.token is None:
            return (constants.AUTH_DENIED, 'No connection token provided')

        voucher = Voucher.query.filter_by(token=self.token).first()

        if voucher is None:
            return (constants.AUTH_DENIED, 'Requested token not found: %s' % self.token)
        else:
            self.voucher_id = voucher.id

        if voucher.ip is None:
            voucher.ip = flask.request.args.get('ip')

        if voucher.status in ['archived', 'blocked', 'ended', 'expired']:

# Path: auth/constants.py
# AUTH_ALLOWED = 1
# AUTH_DENIED = 0
# AUTH_ERROR = -1
# AUTH_VALIDATION = 5
# AUTH_VALIDATION_FAILED = 6
# STAGE_COUNTERS = 'counters'
# STAGE_LOGIN = 'login'
# STAGE_LOGOUT = 'logout'
# ROLES = {
#     u'super-admin': u'Super Admin',
#     u'network-admin': u'Network Admin',
#     u'gateway-admin': u'Gateway Admin',
#     u'service': u'Service',
# }
# STATUS_ICONS = {
#     'active': 'bolt',
#     'archived': 'trash',
#     'blocked': 'thumb-down',
#     'ended': 'flag',
#     'expired': 'circle-x',
#     'new': 'file',
# }
# ACTIONS = {
#     'vouchers': [
#         'extend',
#         'block',
#         'unblock',
#         'archive',
#     ],
# }
# 
# Path: auth/graphs.py
# def available_actions(status, interface):
#     if status in states:
#         result = {}
#         for action, defn in six.iteritems(actions):
#             if action in states[status] and defn['interface'] == interface:
#                 result[action] = defn
#         return result
# 
#     return {}


# Path: auth/models.py
import base64
import datetime
import json
import re
import string
import flask
from auth import constants
from auth.graphs import available_actions
from flask import current_app
from flask_security import UserMixin, RoleMixin, current_user, SQLAlchemyUserDatastore
from flask_sqlalchemy import SQLAlchemy
from random import choice
from sqlalchemy import event
from sqlalchemy.engine import Engine
from sqlalchemy.orm import backref
from sqlalchemy.schema import UniqueConstraint

    outgoing = db.Column(db.BigInteger)

    gateway_id = db.Column(db.Unicode(20), db.ForeignKey('gateways.id', onupdate='cascade'), nullable=False)
    gateway = db.relationship(Gateway, backref=backref('auths', lazy='dynamic'))

    voucher_id = db.Column(db.Integer, db.ForeignKey('vouchers.id', onupdate='cascade'))
    voucher = db.relationship(Voucher, backref=backref('auths', lazy='dynamic'))

    status = db.Column(db.Integer)
    messages = db.Column(db.Text)
    created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)

    def matches_voucher(self, voucher):
        return self.gateway_id == voucher.gateway_id and self.mac == voucher.mac and self.ip == voucher.ip

    def process_request(self):
        if self.token is None:
            return (constants.AUTH_DENIED, 'No connection token provided')

        voucher = Voucher.query.filter_by(token=self.token).first()

        if voucher is None:
            return (constants.AUTH_DENIED, 'Requested token not found: %s' % self.token)
        else:
            self.voucher_id = voucher.id

        if voucher.ip is None:
            voucher.ip = flask.request.args.get('ip')

        if voucher.status in ['archived', 'blocked', 'ended', 'expired']:

            return (constants.AUTH_DENIED, 'Requested token is the wrong status: %s' % self.token)