Пример #1
0
def change_account(acc_id):
    account_ids = [acc.id for acc in request.user.accounts]
    try:
        user_info = jwt_generate_user_info(request.user, request.user.accounts[account_ids.index(acc_id)])
        jwt_token = jwt_auth.jwt_encode_callback(user_info)
        return _generate_repsonse(user_info, jwt_token.decode("utf8"))
    except ValueError:
        pass

    return make_response(json.dumps({"msg": "Not associated with account"}), 401)
Пример #2
0
 def test_return_200_if_jwt_token_valid(self):
     test_client = application.test_client()
     with application.app_context():
         jwt_token = jwt_auth.jwt_encode_callback(
             jwt_generate_user_info(self.user, self.account_dev))
         auth_header = {
             "Authorization": "JWT {}".format(jwt_token.decode('utf-8'))
         }
         r = test_client.get("/v2/apps", headers=auth_header)
         self.assertEqual(200, r.status_code)
Пример #3
0
 def test_jwt_return_401_if_when_account_does_not_exist(self):
     test_client = application.test_client()
     with application.app_context():
         jwt_token = jwt_auth.jwt_encode_callback(
             jwt_generate_user_info(self.user, Account(id=1024)))
         auth_header = {
             "Authorization": "JWT {}".format(jwt_token.decode('utf-8'))
         }
         r = test_client.get("/v2/apps", headers=auth_header)
         self.assertEqual(401, r.status_code)
         self.assertEqual("Account does not exist",
                          json.loads(r.data)['msg'])
Пример #4
0
 def test_jwt_populate_request_user_if_token_is_valid(self):
     with application.app_context(), application.test_client(
     ) as test_client:
         jwt_token = jwt_auth.jwt_encode_callback(
             jwt_generate_user_info(self.user, self.account_infra))
         auth_header = {
             "Authorization": "JWT {}".format(jwt_token.decode('utf-8'))
         }
         r = test_client.get("/v2/apps", headers=auth_header)
         self.assertEqual(200, r.status_code)
         self.assertEqual("*****@*****.**", request.user.tx_email)
         self.assertEqual(5, request.user.current_account.id)
Пример #5
0
 def test_jwt_return_401_if_user_is_not_linked_to_account(self):
     """
     If user tries to access account without being associated to this account
     """
     test_client = application.test_client()
     with application.app_context():
         jwt_token = jwt_auth.jwt_encode_callback(
             jwt_generate_user_info(self.user, self.account_with_no_user))
         auth_header = {
             "Authorization": "JWT {}".format(jwt_token.decode('utf-8'))
         }
         r = test_client.get("/v2/apps", headers=auth_header)
         self.assertEqual(401, r.status_code)
         self.assertEqual("Permission Denied to access this account",
                          json.loads(r.data)['msg'])
Пример #6
0
    def test_add_default_account_on_first_jwt_token(self):
        """
        Depois do processo de login, o token JWT conterá o account_id da conta padrão
        do usuário.
        """
        test_client = application.test_client()
        jwt = MagicMock()

        with application.app_context(), \
             patch.object(routes, "check_authentication_successful",
                          return_value={"email": self.user.tx_email}),\
                patch.object(routes.jwt_auth, "jwt_encode_callback") as jwt_auth_mock:
            response = test_client.get("/authenticate/google")

            jwt_auth_mock.assert_called_once_with(
                jwt_generate_user_info(self.user, self.user.accounts[0]))
Пример #7
0
    def test_jwt_auth_with_token_from_session_if_headers_not_present(self):
        """
        Se não encontrarmos o token JWT no header, olhamos na flask session procurando por ele.
        """
        test_client = application.test_client()

        with application.app_context(), \
             patch.object(routes, "check_authentication_successful",
                          return_value={"email": self.user.tx_email}):
            jwt_token = jwt_auth.jwt_encode_callback(
                jwt_generate_user_info(self.user, self.account_dev))

            with test_client.session_transaction() as flask_session:
                flask_session['jwt'] = jwt_token

            response = test_client.get("/v2/apps")
            self.assertEqual(200, response.status_code)
Пример #8
0
def authorized(resp):
    access_token = resp and resp.get('access_token')

    authentication_ok = check_authentication_successful(access_token)
    if not authentication_ok:
        return render_template("login-failed.html",
                               reason="Invalid OAuth2 code")

    user = _get_user_by_email(authentication_ok["email"])
    if not user:
        return render_template("login-failed.html", reason="User not found")

    if not user.accounts:
        return render_template("login-failed.html",
                               reason="No associated accounts")

    data = {}
    data["jwt"]: bytes = jwt_auth.jwt_encode_callback(
        jwt_generate_user_info(user, user.accounts[0]))

    session["jwt"] = data["jwt"] = data["jwt"].decode('utf-8')
    return redirect("{}?jwt={}".format(conf.REDIRECT_AFTER_LOGIN, data["jwt"]))
Пример #9
0
 def make_auth_header(self, user, account) -> Dict[str, str]:
     jwt_token = jwt_auth.jwt_encode_callback(
         jwt_generate_user_info(user, account))
     return {"Authorization": "JWT {}".format(jwt_token.decode('utf-8'))}
Пример #10
0
def me():
    return json.dumps(
        jwt_generate_user_info(request.user, request.user.current_account)
    )
Пример #11
0
 def generate_jwt_token_for_user(self, user, account):
     return jwt_auth.jwt_encode_callback(
         jwt_generate_user_info(user, account))