# Path: moonrock/security.py
# def create_jwt_token(user, token_secret):
#     payload = dict(
#         iat=datetime.now(),
#         exp=datetime.now() + timedelta(days=14),
#         user=dict(
#             _id=user.id,
#             username=user.username,
#             first_name=user.first_name,
#             last_name=user.last_name,
#             twitter=user.twitter))
#     bytes_token = jwt.encode(payload, token_secret)
#     token = bytes_token.decode('utf-8')
#     return token
# 
# 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
#         )


from urllib.parse import parse_qsl
from urllib.parse import urlencode
from pyramid.httpexceptions import HTTPFound, HTTPUnauthorized
from pyramid_sqlalchemy import Session
from requests_oauthlib import OAuth1
from rest_toolkit import resource
from .security import create_jwt_token
from .models.users import User
import requests




@resource('/api/auth/')
class AuthResource:
    request_token_url = 'https://api.twitter.com/oauth/request_token'
    access_token_url = 'https://api.twitter.com/oauth/access_token'
    authenticate_url = 'https://api.twitter.com/oauth/authenticate'

    def __init__(self, request):
        self.request = request
        settings = request.registry.settings
        self.consumer_key = settings.get('TWITTER_CONSUMER_KEY')
        self.client_secret = settings.get('TWITTER_CONSUMER_SECRET')
        self.callback_uri = settings.get('TWITTER_CALLBACK_URL')
        self.token_secret = settings.get('TOKEN_SECRET')


@AuthResource.controller(name='login', method='POST')
def login_resource(resource, request):
    settings = request.registry.settings
    token_secret = settings.get('TOKEN_SECRET')
    username = request.json_body.get('username')
    password = request.json_body.get('password')

    user = Session.query(User).filter(User.username == username).one()
    if user:
        user_password = user.password
        if user_password and user_password == password:

# Path: moonrock/security.py
# def create_jwt_token(user, token_secret):
#     payload = dict(
#         iat=datetime.now(),
#         exp=datetime.now() + timedelta(days=14),
#         user=dict(
#             _id=user.id,
#             username=user.username,
#             first_name=user.first_name,
#             last_name=user.last_name,
#             twitter=user.twitter))
#     bytes_token = jwt.encode(payload, token_secret)
#     token = bytes_token.decode('utf-8')
#     return token
# 
# 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/auth.py
from urllib.parse import parse_qsl
from urllib.parse import urlencode
from pyramid.httpexceptions import HTTPFound, HTTPUnauthorized
from pyramid_sqlalchemy import Session
from requests_oauthlib import OAuth1
from rest_toolkit import resource
from .security import create_jwt_token
from .models.users import User
import requests





@resource('/api/auth/')
class AuthResource:
    request_token_url = 'https://api.twitter.com/oauth/request_token'
    access_token_url = 'https://api.twitter.com/oauth/access_token'
    authenticate_url = 'https://api.twitter.com/oauth/authenticate'

    def __init__(self, request):
        self.request = request
        settings = request.registry.settings
        self.consumer_key = settings.get('TWITTER_CONSUMER_KEY')
        self.client_secret = settings.get('TWITTER_CONSUMER_SECRET')
        self.callback_uri = settings.get('TWITTER_CALLBACK_URL')
        self.token_secret = settings.get('TOKEN_SECRET')


@AuthResource.controller(name='login', method='POST')
def login_resource(resource, request):
    settings = request.registry.settings
    token_secret = settings.get('TOKEN_SECRET')
    username = request.json_body.get('username')
    password = request.json_body.get('password')

    user = Session.query(User).filter(User.username == username).one()
    if user:
        user_password = user.password
        if user_password and user_password == password:

            token = create_jwt_token(user, token_secret)