Beispiel #1
0
 def post(self):
     user = self.current_user
     desc = self.get_argument("description")
     value = "%040x" % random.randrange(16 ** 40)
     # TODO: Retry on duplicate token value (peewee.IntegrityError)?
     Token.create(user=user, value=value, desc=desc)
     self.redirect(self.reverse_url("web:settings"))
Beispiel #2
0
 def post(self):
     user = self.current_user
     desc = self.get_argument("description")
     value = "%040x" % random.randrange(16**40)
     # TODO: Retry on duplicate token value (peewee.IntegrityError)?
     Token.create(user=user, value=value, desc=desc)
     self.redirect(self.reverse_url("web:settings"))
Beispiel #3
0
def create_token():
    # Get the parameters
    params = request.get_json()
    paramsCode = params.get('code')
    try:
        code = MobileCode.get(MobileCode.mobile_code == paramsCode)
        if code.mobile_code == paramsCode:
            generatedToken = binascii.b2a_hex(os.urandom(16))
            currentTime = int(time.time())
            expirationDate = currentTime + 3600
            token = Token.create(token=generatedToken,
                                 user_id=code.user_id,
                                 created_at=currentTime,
                                 updated_at=currentTime,
                                 expiration_date=expirationDate)
            token.save()
            isDeleted = code.delete_instance()
            tokenData = model_to_dict(token)
            return jsonify({
                'response':
                'Token well created for our user, and code well deleted',
                'token': tokenData,
                'code_deleted': isDeleted
            }), 201
        return str('Code given did not match to code generated')
    except Exception as identifier:
        return jsonify({
            'error':
            'Code not found {message}'.format(message=identifier.message)
        }), 404
Beispiel #4
0
def login_validate_code():
    try:
        # Get params from request
        params = request.get_json()

        # Build attributes with sent params
        login_username = params.get('username')
        login_code = params.get('code')

        print 'login username : '******'login code : ', login_code

        # Check if username is given
        if username == None:
            return 'Missing username.'

        # Check if code is given
        if login_code == None:
            return 'Missing code.'
        else:

            # Fetch user data, depending of given login ussername
            user = User.get(User.username == login_username)

            # Fetch code data, depending of given login code
            code = CodeLogin.get(CodeLogin.code == login_code)

            # Check if user is deleted
            if user.is_deleted == True:
                return "This user has been deleted."
            else:

                # Check if user's code match with given login code
                if user.id != code.user_id:
                    return "Code doesn't match."
                else:

                    # User successfully logged, so build a new token
                    user_id = user.id
                    login_token = generate_token(16)

                    print 'user_id : ', user_id
                    print 'token : ', login_token

                    # Create and save new token
                    token_query = Token.create(user_id=user_id,
                                               login_token=login_token)
                    token_query.save()
                    token = model_to_dict(token_query)

                    # Send newly created token to browser in JSON
                    return jsonify({'new_token': token}), 201

    # Catch error if request fail
    except Exception as error:
        print error

        # Send error to browser
        return jsonify({'error': 'Not found'}), 404
Beispiel #5
0
def login():
    params = request.form # Dict
    email = params['email']
    pwd = md5(params['password']).hexdigest()
    try:
        user = User.get(User.email == email)
        if user.password == pwd:
            gifs = list(Gif.select().dicts())
            users = list(User.select().dicts())
            try:
                token = Token.get(Token.user_id == user.id)
                return render_template('index.html', page='user', user=user, gifs=gifs, users=users, token='0'), 201
            except:
                token_created = md5(str(user.id) + user.email).hexdigest()
                query = Token.create(token=token_created, user_id=user.id)
                query.save()
                return render_template('index.html', page='user', user=user, gifs=gifs, users=users, token='1'), 201
        else:
            return render_template('index.html', page='home', login='******'), 201
    except:
        return render_template('index.html', page='home', login='******'), 201
def create_token(token, user_id):
    token = Token.create(value=token, user_id=user_id)
    token.save()
    return token