# Path: moonrock/models/users.py
# class User(BaseObject):
#     __tablename__ = 'users'
# 
#     id = Column(Integer,
#                 Sequence('user_id_seq'),
#                 primary_key=True)
#     userid = Column(String(20), unique=True)
#     username = Column(String(120), unique=True)
#     email = Column(String(120))
#     first_name = Column(String(120))
#     last_name = Column(String(120))
#     twitter = Column(String(120))
#     password = Column(String(120))
#     groups = Column(ArrayType)
# 
#     def __repr__(self):
#         return "<User(id=%d, username='%s')>" % (self.id, self.username)
# 
#     def __json__(self, request):
#         return dict(
#             id=self.id,
#             userid=self.userid,
#             first_name=self.first_name,
#             last_name=self.last_name,
#             twitter=self.twitter,
#             email=self.email
#         )


import jwt
from sqlalchemy.orm.exc import NoResultFound
from datetime import datetime, timedelta
from pyramid.authentication import CallbackAuthenticationPolicy
from pyramid.interfaces import IAuthenticationPolicy
from pyramid_sqlalchemy import Session
from zope.interface import implementer
from .models.users import User
        forgetting the token. Client is responsible for that..
        """
        return []

    def unauthenticated_userid(self, request):
        authorization = request.headers.get('Authorization')
        if not authorization:
            return None
        try:
            authmeth, encoded_token = authorization.split(' ', 1)
        except ValueError: # not enough values to unpack
            return None
        if authmeth.lower() != 'bearer':
            return None

        token = encoded_token
        try:
            payload = jwt.decode(token, self.token_secret)
        except jwt.DecodeError: # can't decode
            return None

        try:
            return payload['user']['_id']
        except KeyError:
            return None


def groupfinder(username, request):
    groups = []
    try:

# Path: moonrock/models/users.py
# class User(BaseObject):
#     __tablename__ = 'users'
# 
#     id = Column(Integer,
#                 Sequence('user_id_seq'),
#                 primary_key=True)
#     userid = Column(String(20), unique=True)
#     username = Column(String(120), unique=True)
#     email = Column(String(120))
#     first_name = Column(String(120))
#     last_name = Column(String(120))
#     twitter = Column(String(120))
#     password = Column(String(120))
#     groups = Column(ArrayType)
# 
#     def __repr__(self):
#         return "<User(id=%d, username='%s')>" % (self.id, self.username)
# 
#     def __json__(self, request):
#         return dict(
#             id=self.id,
#             userid=self.userid,
#             first_name=self.first_name,
#             last_name=self.last_name,
#             twitter=self.twitter,
#             email=self.email
#         )


# Path: moonrock/security.py
import jwt
from sqlalchemy.orm.exc import NoResultFound
from datetime import datetime, timedelta
from pyramid.authentication import CallbackAuthenticationPolicy
from pyramid.interfaces import IAuthenticationPolicy
from pyramid_sqlalchemy import Session
from zope.interface import implementer
from .models.users import User

        forgetting the token. Client is responsible for that..
        """
        return []

    def unauthenticated_userid(self, request):
        authorization = request.headers.get('Authorization')
        if not authorization:
            return None
        try:
            authmeth, encoded_token = authorization.split(' ', 1)
        except ValueError: # not enough values to unpack
            return None
        if authmeth.lower() != 'bearer':
            return None

        token = encoded_token
        try:
            payload = jwt.decode(token, self.token_secret)
        except jwt.DecodeError: # can't decode
            return None

        try:
            return payload['user']['_id']
        except KeyError:
            return None


def groupfinder(username, request):
    groups = []
    try:

        user = Session.query(User).filter(User.username == username).one()