Exemple #1
0
 def post(self):
     # get the post data
     post_data = request.get_json()
     if post_data is None:
         return CommonResponseObject.fail_response(
             'Please provide required data', status.HTTP_403_FORBIDDEN)
     user = User.get_user_by_email(post_data.get('email'))
     if user:
         # return response to inform that user already existed
         return CommonResponseObject.register_user_exist()
     # if user does not exist, try to create new user and store to the database
     # initialize new user object with information from the request
     try:
         user = self.__check_register_json_data(post_data)
         if not isinstance(user, User):
             return user
         # insert the user
         db.session.add(user)
         db.session.commit()
         token = ConfirmationToken.generate_confirmation_token(user.email)
         confirm_url = url_for('auth.confirm_api',
                               token=token,
                               _external=True)
         html = "<p>Welcome! Thanks for signing up. Please follow this link to activate your account:</p><p><a href=" + confirm_url + ">{{ Activate}}</a></p><br><p>Cheers!</p>"
         subject = "Scloud Service Email Confirmation"
         Mail.send(user.email, subject, html)
         # generate the auth token
         # return response with auth token
         return CommonResponseObject.success_resp_with_mess(
             'Register succesfully, please confirm your email which is sent to your email address'
         )
     except Exception as e:
         # database exception, cannot store user information
         print(e)
         return CommonResponseObject.register_exception()
 def post(self):
     post_data = request.get_json()
     root_mac_address = post_data.get('root_mac_address')
     mac_address = post_data.get('mac_address')
     if not post_data or not mac_address or not root_mac_address:
         return CommonResponseObject.fail_response(
             'Please provide your mac_address and the mac_address of device for root changing',
             status.HTTP_412_PRECONDITION_FAILED)
     auth_token = RequestUtils.get_access_token(request)
     user_id = User.decode_auth_token(auth_token)
     device = DeviceList.get_device_by_user_id_and_mac(user_id,mac_address)
     if not device:
         return CommonResponseObject.fail_response(
             'Your provided mac_address is unathorized',
             status.HTTP_401_UNAUTHORIZED)
     root_device = DeviceList.get_device_by_user_id_and_mac(user_id,root_mac_address)
     if not root_device:
         return CommonResponseObject.fail_response(
             'Your provided mac address of root device is invalid',
             status.HTTP_404_NOT_FOUND)
     if not root_device.root:
         return CommonResponseObject.fail_response(
             'Your provided mac address of root device is unauthorized',
             status.HTTP_401_UNAUTHORIZED)
     try:
         device.root = True
         root_device.root = False
         db.session.save()
         db.session.commit()
         return CommonResponseObject.success_resp_with_mess(
             'Your root device is changed successfully')
     except Exception:
         return CommonResponseObject.fail_response(
             'Some errors occurred')
 def post(self):
     post_data = request.get_json()
     if not post_data or not post_data.get('mac_address'):
         return CommonResponseObject.fail_response(
             'Please provide your mac_address for deauthorization',
             status.HTTP_412_PRECONDITION_FAILED)
     auth_token = RequestUtils.get_access_token(request)
     user_id = User.decode_auth_token(auth_token)
     mac_address = post_data.get('mac_address')
     device = DeviceList.get_device_by_user_id_and_mac(user_id, mac_address)
     if not device:
         return CommonResponseObject.fail_response(
             'Invalid authentication token',status.HTTP_401_UNAUTHORIZED)
     if device.root:
         return CommonResponseObject.fail_response(
             'You are not able to deauthorize without decoding your files to raw',
             status.HTTP_403_FORBIDDEN)
     try:
         db.session.delete(device)
         db.session.commit()
         return CommonResponseObject.success_resp_with_mess(
             'Your device is no longer authorized')
     except Exception:
         return CommonResponseObject.fail_response(
             'Some errors occured, please try again')
Exemple #4
0
 def post(self):
     auth_token = RequestUtils.get_access_token(request)
     if auth_token:
         resp = User.decode_auth_token(auth_token)
         if not isinstance(resp, str):
             return CommonResponseObject.success_resp_with_mess(
                 'Token is still available')
         return CommonResponseObject.fail_response(
             message=resp, error_code=status.HTTP_401_UNAUTHORIZED)
Exemple #5
0
 def get(self, token):
     try:
         email = ConfirmationToken.confirm_token(token)
     except:
         return CommonResponseObject.fail_response(
             'The token is invalid or expired', status.HTTP_404_NOT_FOUND)
     user = User.query.filter_by(email=email).first_or_404()
     if user.is_confirmed:
         return CommonResponseObject.fail_response(
             'The user is confirmed, please login',
             status.HTTP_202_ACCEPTED)
     user.is_confirmed = True
     db.session.add(user)
     db.session.commit()
     return CommonResponseObject.success_resp_with_mess(
         'The user is successfully confirmed')
Exemple #6
0
 def post(self):
     post_data = request.get_json()
     if post_data is None:
         return CommonResponseObject.fail_response(
             'Please provde required data', status.HTTP_403_FORBIDDEN)
     auth_token = RequestUtils.get_access_token(
         request)  #Return authentication token
     user_id = User.decode_auth_token(auth_token)
     user = User.query.filter_by(id=user_id).first()
     if not user:
         return CommonResponseObject.fail_response(
             'User does not exist, please try again',
             status.HTTP_404_NOT_FOUND)
     isChanged = False
     password = post_data.get('password')
     if password:
         user.password = password
         isChanged = True
     bday = post_data.get('birthday')
     if bday:
         isChanged = True
         user.birthday = datetime.datetime.strptime(bday, "%d/%m/%Y")
     job = post_data.get('job')
     if job:
         user.job = job
         isChanged = True
     fullname = post_data.get('fullname')
     if fullname:
         user.fullname = fullname
         isChanged = True
     country = post_data.get('country')
     if country:
         user.country = country
         isChanged = True
     if isChanged:
         db.session.save()
         db.session.commit()
     return CommonResponseObject.success_resp_with_mess(
         'Your information is updated successfully')