Exemplo n.º 1
0
 def post(self):
     try:
         args = parser.parse_args()
         if (args['device'] is None) or (args['login'] is None) or (
                 args['password'] is None):
             return {'success': False, 'errno': ERRNO_FIELDS_ABSENT}, 400
         pass_md5 = hashlib.md5(
             args['password'].encode('utf-8')).hexdigest()
         user = User.query.filter(User.login == args['login'],
                                  User.password == pass_md5).one_or_none()
         if user is None:
             log.info("Invalid login/password")
             return {'success': False, 'errno': ERRNO_INVALID_PASSWORD}, 403
         new_token = Token(token=str(uuid.uuid4()),
                           user_id=user.id,
                           device=args['device'])
         if args['expires'] is not None:
             new_token.expires_at = datetime.fromtimestamp(args['expires'] /
                                                           1000.0)
         db.session.add(new_token)
         db.session.commit()
         log.info("Created new token: %s" % new_token.token)
         return {'token': new_token.token}
     except Exception as e:
         db.session.rollback()
         log.exception(e)
         return {'success': False, 'errno': ERRNO_INTERNAL_UNKNOWN}, 500
Exemplo n.º 2
0
 def put(self):
     try:
         args = parser.parse_args()
         if (args['device'] is None) or (args['login'] is None) or (
                 args['password'] is None):
             return {'success': False, 'errno': ERRNO_FIELDS_ABSENT}, 400
         pass_md5 = hashlib.md5(
             args['password'].encode('utf-8')).hexdigest()
         new_user = User(login=args['login'], password=pass_md5)
         db.session.add(new_user)
         new_token = Token(token=str(uuid.uuid4()),
                           user=new_user,
                           device=args['device'])
         if args['expires'] is not None:
             new_token.expires_at = datetime.fromtimestamp(args['expires'] /
                                                           1000.0)
         db.session.add(new_token)
         db.session.commit()
         return {'token': new_token.token}
     except Exception as e:
         db.session.rollback()
         log.error(e)
         return {'success': False, 'errno': ERRNO_INTERNAL_UNKNOWN}, 500