def update_profile(usertype): if request and request.method == "PUT": try: auth = request.headers.get('AUTHORIZATION', b'').split() j = JWTTokenUtil(auth[1]) uid = j.get_user_id() userObj = Users.get(uid) if userObj.userType != usertype: raise Exception("Please provide correct usertype !!!") body = None if request.data: body = json.loads(request.data) resp, err = ValidateRegistrationData(body, usertype, True) if err: res = GetResponseObject(err, 400) return res else: data = f"Empty request body !!!!" res = GetResponseObject(data, 400) log.error(err) return res user = Cognito(user_pool_id=COGNITO_USER_POOL_ID, client_id=COGNITO_APP_CLIENT_ID, user_pool_region=AWS_REGION) out, err = UpdateItem(uid, usertype, body=body, update=True) if err: raise Exception(err) data = "User profile updated successfully !!!" res = GetResponseObject(data, 200, True) return res except Exception as e: data = f"Error while updating user profile: {str(e)}" log.error(data) res = GetResponseObject(data) return res else: data = f"Invalid request method, method {request.method} not supported !!!" return GetResponseObject(data, 405)
def sign_in(): if request and request.method == "GET": resp, err = GetUserPasswordFromAuthHeader(request) if err: log.error(err) res = GetResponseObject(err, 400) return res username, password = resp[0], resp[1] try: user = Cognito(user_pool_id=COGNITO_USER_POOL_ID, \ client_id=COGNITO_APP_CLIENT_ID, \ user_pool_region=AWS_REGION, \ username=username) user.admin_authenticate(password=password) user_rec = user.get_user() uid = user_rec.sub usertype = user_rec._data['custom:usertype'] userObj = Users.get(uid) # userObj = Users.get(uid, usertype) out = SerializeUserObj(userObj) # out["usertype"] = usertype data = { # "idToken": user.id_token, "accessToken": user.access_token, # "refreshToken": user.refresh_token, "profile": out } res = GetResponseObject(data, 200, True) res.headers['HMS-TOKEN'] = "Bearer " + user.access_token # res.set_cookie(settings.COOKIE_NAME , user.access_token) return res except Exception as e: msg = f"Error while authenticating user {str(e)}" return GetResponseObject(msg) # return HttpResponseServerError(res) else: data = f"Invalid request method, method {request.method} not supported !!!" return GetResponseObject(data, 405)