Esempio n. 1
0
def login(data):
    try:
        return current_app.fauna_client.query(
            q.let(
                {
                    'response':
                    q.login(
                        q.match(q.index('unique_account_username_type'),
                                [data.get('username'), 'EMAIL']),
                        {'password': data.get('password')}),
                    'user':
                    q.select_with_default(
                        ['data', 'user'],
                        q.get(q.select(['instance'], q.var('response'))), None)
                }, {
                    'data': {
                        'token':
                        q.select('secret', q.var('response')),
                        'user':
                        q.if_(
                            q.is_ref(q.var('user')),
                            q.select(['data', 'alias'], q.get(q.var('user'))),
                            None)
                    }
                }))
    except Exception as e:
        print(e)
Esempio n. 2
0
  def test_login_logout(self):
    instance_ref = self.client.query(
      query.create(self.collection_ref, {"credentials": {"password": "******"}}))["ref"]
    secret = self.client.query(
      query.login(instance_ref, {"password": "******"}))["secret"]
    instance_client = self.client.new_session_client(secret=secret)

    self.assertEqual(instance_client.query(
      query.select("ref", query.get(Ref("self", Ref("widgets", Native.COLLECTIONS))))), instance_ref)

    self.assertTrue(instance_client.query(query.logout(True)))
Esempio n. 3
0
    def test_identity_has_identity(self):
        instance_ref = self.client.query(
            query.create(self.collection_ref,
                         {"credentials": {
                             "password": "******"
                         }}))["ref"]
        secret = self.client.query(
            query.login(instance_ref, {"password": "******"}))["secret"]
        instance_client = self.client.new_session_client(secret=secret)

        self.assertTrue(instance_client.query(query.has_identity()))
        self.assertEqual(instance_client.query(query.identity()), instance_ref)
    def test_has_current_token(self):
        instance_ref = self._q(
            query.create(self.collection_ref,
                         {"credentials": {
                             "password": "******"
                         }}))["ref"]
        secret = self._q(query.login(instance_ref,
                                     {"password": "******"}))["secret"]
        instance_client = self.client.new_session_client(secret=secret)

        self.assertTrue(instance_client.query(query.has_current_token()))
        self.assertFalse(self._q(query.has_current_token()))
def login():

    body = request.json
    client = FaunaClient(secret=FAUNA_SECRET)

    try:
        result = client.query(
            q.login(q.match(q.index("Users_by_username"), body["username"]),
                    {"password": body["password"]}))

        return {"secret": result['secret']}

    except faunadb.errors.BadRequest as exception:
        error = exception.errors[0]
        return {"code": error.code, "description": error.description}, 401
Esempio n. 6
0
def login():
    if "user_secret" in session:
        return redirect(url_for("dashboard"))

    if request.method == "POST":
        email = request.form.get("email").strip().lower()
        password = request.form.get("password")

        try:
            result = client.query(
                q.login(q.match(q.index("users_by_email"), email),
                        {"password": password}))
        except BadRequest as e:
            flash(
                "You have supplied invalid login credentials, please try again!",
                "danger")
            return redirect(url_for("login"))

        session["user_secret"] = result["secret"]
        return redirect(url_for("dashboard"))

    return render_template("login.html")
 def test_login(self):
     self.assertJson(
         query.login(query.ref(query.collection("widget"), "1"),
                     {"password": "******"}),
         '{"login":{"id":"1","ref":{"collection":"widget"}},"params":{"object":{"password":"******"}}}'
     )
Esempio n. 8
0
  def test_typecheckfns(self):
    coll = query.collection("typecheck_coll")
    db = query.database("typecheck_db")
    fn = query.function("typecheck_fn")
    index = query.index("typecheck_index")
    self.admin_client.query(query.create_collection({"name": "typecheck_coll"}))
    self.admin_client.query(query.create_index(
        {"name": "typecheck_index", "source": coll, "active": True}))
    doc = self.admin_client.query(query.create(
        coll, {"data": {}, "credentials": {"password": "******"}}))
    self.admin_client.query(query.create_database({"name": "typecheck_db"}))
    function = self._q(query.create_function(
        {"name": "typecheck_fn", "body": query.query(query.lambda_("x", query.now()))}))

    key = self.admin_client.query(
        query.create_key({"database": db, "role": "admin"}))
    token = self._q(query.login(doc["ref"], {"password": "******"}))
    credentials = self._q(query.select(['data', 0], query.paginate(query.credentials())))
    role = self.admin_client.query(query.create_role(
        {"name": "typecheck_role", "membership": [], "privileges": []}))

    values = [
        None,
        bytearray([12,3,4,5]),
        credentials,
        90,
        3.14,
        True,
        query.to_date(query.now()),
        query.date("1970-01-01"),
        query.now(),
        query.epoch(1, "second"),
        query.time("1970-01-01T00:00:00Z"),
        {"x": 10},
        query.get(doc["ref"]),
        query.paginate(query.collections()),
        [1, 2, 3],
        "a string",
        coll,
        query.collections(),
        query.match(index),
        query.union(query.match(index)),
        doc["ref"],
        query.get(doc["ref"]),
        index,
        db,
        coll,
        token["ref"],
        role["ref"],
        key["ref"],
        function["ref"],
        query.get(function["ref"]),
        query.query(query.lambda_("x", query.var("x"))),
    ]
    pairs = [
      ["array", query.is_array],
      ["object", query.is_object],
      ["string", query.is_string],
      ["null", query.is_null],
      ["number", query.is_number],
      ["bytes", query.is_bytes],
      ["date", query.is_date],
      ["timestamp", query.is_timestamp],
      ["set", query.is_set],
      ["ref", query.is_ref],
      ["boolean", query.is_boolean],
      ["double", query.is_double],
      ["integer", query.is_integer],
      ["database", query.is_database],
      ["index", query.is_index],
      ["collection", query.is_collection],
      ["token", query.is_token],
      ["function", query.is_function],
      ["collection", query.is_collection],
      ["role", query.is_role],
      ["credentials", query.is_credentials],
      ["key", query.is_key],
    ]
    expected = {
      "array":       1,
      "boolean":     1,
      "bytes":       1,
      "collection":  3,
      "credentials": 1,
      "database":    1,
      "date":        2,
      "double":      1,
      "function":    2,
      "integer":     1,
      "index":       1,
      "key":         1,
      "null":        1,
      "number":      2,
      "object":      5,
      "ref":         11,
      "role":        1,
      "set":         3,
      "string":      1,
      "timestamp":   3,
      "token":       1,
    }

    q = []
    for p in pairs:
      d = dict()
      d[p[0]] = query.count(query.filter_(query.lambda_("v", p[1](query.var("v"))), query.var("vals")))
      q.append(d)

    actual = self._q(query.let({"vals": values}, query.merge({}, q)))
    self.assertEqual(actual, expected)
Esempio n. 9
0
def login(email, password):
    return fauna.query(
        q.login(q.match(q.index('user_by_email'), email),
                {"password": password}))