Tokens

Tokens( )
Tokens( )
Tokens( )
Tokens( )
tokens( )
Tokens( )

Description

The Tokens function provides access to the system collection that stores access tokens, created with the Login function. This is useful for:

  • Listing currently existing tokens

  • Creating tokens without credentials

  • Indexing tokens so that you can query tokens based on relationships with other documents.

If you have a reference to a specific token, acquired with Login, you can add custom fields to the token’s document using Update or Replace. Access tokens can be invalidated by calling Logout.

Due to implementation details, Paginate(Tokens()) does not list existing tokens. See the example below.

Parameters

None.

Returns

None.

Examples

  1. List existing tokens

    The following query lists any existing tokens:

    Not available in this language yet.
    Not available in this language yet.
    System.out.println(
        client.query(
            Paginate(Documents(Tokens()))
        ).get());
    client.query(
      q.Paginate(q.Documents(q.Tokens()))
    )
    .then((ret) => console.log(ret))
    Not available in this language yet.
    println(Await.result(
      client.query(
        Paginate(Documents(Tokens()))
      ),
      5.seconds
    ))
    {data: [ref(id = "255391018093904394", collection = ref(id = "tokens"))]}
    { data: [ Ref(Tokens(), "255391018093904394") ] }
    {data: [ref(id = "255391018093904394", collection = ref(id = "tokens"))]}
  2. Use Tokens() in an index

    The following query creates an index on the internal tokens collection, so that tokens associated with a specific service can be queried:

    Value result = await client.Query(
      CreateIndex(
        Obj(
          "name", "tokens_by_instance",
          "permissions", Obj("read", "public"),
          "source", Tokens(),
          "terms", Arr(Obj("field", "instance")),
          "values", Arr(Obj("field", Arr("data", "name")))
        )
      )
    );
    Console.WriteLine(
      JsonConvert.SerializeObject(result, Formatting.Indented)
      .Replace("\\n", "\n")
    );
    result, _ := client.Query(
      f.CreateIndex(
        f.Obj{
          "name": "tokens_by_instance",
          "permissions": f.Obj{"read": "public"},
          "source": f.Tokens(),
          "terms": f.Arr{f.Obj{"field": "instance"}},
          "values": f.Arr{f.Obj{"field": f.Arr{"data", "name"}}}}))
    fmt.Println(result)
    System.out.println(
        client.query(
            CreateIndex(
                Obj(
                  "name", Value("tokens_by_instance"),
                  "permissions", Obj("read", Value("public")),
                  "source", Tokens(),
                  "terms", Arr(Obj("field", Value("instance"))),
                  "values", Arr(
                      Obj("field", Arr(Value("data"), Value("name")))
                  )
                )
            )
        ).get());
    client.query(
      q.CreateIndex({
        name: 'tokens_by_instance',
        permissions: { read: 'public' },
        source: q.Tokens(),
        terms: [ { field: 'instance' } ],
        values: [ { field: ['data', 'name'] } ],
      })
    )
    .then((ret) => console.log(ret))
    print(client.query(
      q.create_index({
        "name": "tokens_by_instance",
        "permissions": { "read": "public" },
        "source": q.tokens(),
        "terms": [ { "field": "instance" } ],
        "values": [ { "field": ["data", "name"] } ]
      })
    ))
    println(Await.result(
      client.query(
        CreateIndex(
          Obj(
            "name" -> "tokens_by_instance",
            "permissions" -> Obj("read" -> "public"),
            "source" -> Tokens(),
            "terms" -> Arr(Obj("field" -> "instance")),
            "values" -> Arr(Obj("field" -> Arr("data", "name")))
          )
        )
      ),
      5.seconds
    ))
    {
      "object": {
        "ref": {
          "@ref": {
            "id": "tokens_by_instance",
            "collection": {
              "@ref": {
                "id": "indexes"
              }
            }
          }
        },
        "ts": 1576104034680000,
        "active": false,
        "serialized": true,
        "name": "tokens_by_instance",
        "permissions": {
          "object": {
            "read": "public"
          }
        },
        "source": {
          "@ref": {
            "id": "tokens"
          }
        },
        "terms": [
          {
            "object": {
              "field": "instance"
            }
          }
        ],
        "values": [
          {
            "object": {
              "field": [
                "data",
                "name"
              ]
            }
          }
        ],
        "partitions": 1
      }
    }
    map[active:false name:tokens_by_instance partitions:1 permissions:map[read:public] ref:{tokens_by_instance 0xc0001680c0 0xc0001680c0 <nil>} serialized:true source:0x15306c0 terms:[map[field:instance]] ts:1576104347010000 values:[map[field:[data name]]]]
    {ref: ref(id = "tokens_by_instance", collection = ref(id = "indexes")), ts: 1576104514660000, active: false, serialized: true, name: "tokens_by_instance", permissions: {read: "public"}, source: ref(id = "tokens"), terms: [{field: "instance"}], values: [{field: ["data", "name"]}], partitions: 1}
    { ref: Index("tokens_by_instance"),
      ts: 1576104922970000,
      active: false,
      serialized: true,
      name: 'tokens_by_instance',
      permissions: { read: 'public' },
      source: Tokens(),
      terms: [ { field: 'instance' } ],
      values: [ { field: [Array] } ],
      partitions: 1 }
    {'ref': Ref(id=tokens_by_instance, collection=Ref(id=indexes)), 'ts': 1576105173880000, 'active': False, 'serialized': True, 'name': 'tokens_by_instance', 'permissions': {'read': 'public'}, 'source': Ref(id=tokens), 'terms': [{'field': 'instance'}], 'values': [{'field': ['data', 'name']}], 'partitions': 1}
    {name: "tokens_by_instance", source: ref(id = "tokens"), ts: 1576105489780000, permissions: {read: "public"}, ref: ref(id = "tokens_by_instance", collection = ref(id = "indexes")), values: [{field: ["data", "name"]}], partitions: 1, terms: [{field: "instance"}], serialized: true, active: false}
  3. Modify a token

    The following query generates a token by calling Login():

    Value result = await client.Query(
      Login(
        Match(Index("users_by_email"), "alice@site.example"),
        Obj("password", "new password")
      )
    );
    Console.WriteLine(
      JsonConvert.SerializeObject(result, Formatting.Indented)
      .Replace("\\n", "\n")
    );
    result, _ := client.Query(
      f.Login(
        f.MatchTerm(f.Index("users_by_email"), "alice@site.example"),
        f.Obj{ "password": "new password" }))
    fmt.Println(result)
    System.out.println(
        client.query(
            Login(
                Match(Index("users_by_email"), Value("alice@site.example")),
                Obj("password", Value("new password"))
            )
        ).get();
    client.query(
      q.Login(
        q.Match(q.Index('users_by_email'), 'alice@site.example'),
        { password: 'new password' },
      )
    )
    .then((ret) => console.log(ret))
    print(client.query(
      q.login(
        q.match(q.index("users_by_email"), "alice@site.example"),
        { "password": "new password" },
      )
    ))
    println(Await.result(
      client.query(
        Login(
          Match(Index("users_by_email"), "alice@site.example"),
          Obj("password" -> "new password")
        )
      ),
      5.seconds
    ))
    {"object":{"ref":{"@ref":{"id":"251585618429084160","collection":{"@ref":{"id":"tokens"}}}},"ts":1576189592680000,"instance":{"@ref":{"id":"251407645221585408","collection":{"@ref":{"id":"users","collection":{"@ref":{"id":"collections"}}}}}},"secret":"fnEDfc_JeRACAAN9IwrU8AIASmsjen_3spEt5f79gcSiPf3IywI"}}
    map[instance:{251407645221585408 0xc000146360 0xc000146360 <nil>} ref:{251509693463134720 0xc0001461b0 0xc0001461b0 <nil>} secret:fnEDfYq70HACAAN9IwrU8AIALfJiahL_J_KcpDgUjW0K_n_TvFY ts:1576117185010000]
    {ref: ref(id = "251501214465786368", collection = ref(id = "tokens")), ts: 1576109098790000, instance: ref(id = "251407645221585408", collection = ref(id = "users", collection = ref(id = "collections"))), secret: "fnEDfYMFpNACAAN9IwrU8AIAci6OvrScKEoSVSpbfZq7_LfCpeA"}
    { ref: Ref(Tokens(), "251500495731950080"),
      ts: 1576108413380000,
      instance: Ref(Collection("users"), "251407645221585408"),
      secret: 'fnEDfYJeTPACAAN9IwrU8AIAItH5Pfj5cqbybb_JmqNOncUKI14' }
    {'ref': Ref(id=251502661795316224, collection=Ref(id=tokens)), 'ts': 1576110479070000, 'instance': Ref(id=251407645221585408, collection=Ref(id=users, collection=Ref(id=collections))), 'secret': 'fnEDfYRWoGACAAN9IwrU8AIAFwnfIh4vM_28LzJqFIP_45o8X2Q'}
    {ref: ref(id = "251502310512919040", collection = ref(id = "tokens")), ts: 1576110144060000, instance: ref(id = "251407645221585408", collection = ref(id = "users", collection = ref(id = "collections"))), secret: "fnEDfYQE1lACAAN9IwrU8AIAaicAc8rxWsP_rUoTiLjYK00NRI4"}

    In the following query, we use the token’s reference, from the previous query, to update the token with some metadata:

    Value result = await client.Query(
      Update(
        Ref(Tokens(), "251585618429084160"),
        Obj("data", Obj("meta", "data"))
      )
    );
    Console.WriteLine(
      JsonConvert.SerializeObject(result, Formatting.Indented)
      .Replace("\\n", "\n")
    );
    result, err := client.Query(
      f.Update(
        f.Ref(f.Tokens(), "251509693463134720"),
        f.Obj{ "data": f.Obj{ "meta": "data" }}))
    fmt.Println(result)
    System.out.println(
        client.query(
            Update(
                Ref(Tokens(), "251501214465786368"),
                Obj("data", Obj("meta", Value("data")))
            )
        ).get());
    client.query(
      q.Update(
        q.Ref(q.Tokens(), '251500495731950080'),
        { data: { meta: 'data' } }
      )
    )
    .then((ret) => console.log(ret))
    print(client.query(
      q.update(
        q.ref(q.tokens, "251502661795316224")
        { "data": { "meta": "data" } }
      )
    ))
    println(Await.result(
      client.query(
        Update(
          Ref(Tokens(), "251502310512919040"),
          Obj("data" -> Obj("meta" -> "data"))
        )
      ),
      5.seconds
    ))
    {"object":{"ref":{"@ref":{"id":"251585618429084160","collection":{"@ref":{"id":"tokens"}}}},"ts":1576189592760000,"instance":{"@ref":{"id":"251407645221585408","collection":{"@ref":{"id":"users","collection":{"@ref":{"id":"collections"}}}}}},"data":{"object":{"meta":"data"}}}}
    map[data:map[meta:data] instance:{251407645221585408 0xc0000c1590 0xc0000c1590 <nil>} ref:{251509693463134720 0xc0000c13e0 0xc0000c13e0 <nil>} ts:1576117185040000]
    {ref: ref(id = "251501214465786368", collection = ref(id = "tokens")), ts: 1576109098880000, instance: ref(id = "251407645221585408", collection = ref(id = "users", collection = ref(id = "collections"))), data: {meta: "data"}}
    { ref: Ref(Tokens(), "251500495731950080"),
      ts: 1576108413460000,
      instance: Ref(Collection("users"), "251407645221585408"),
      data: { meta: 'data' } }
    {'ref': Ref(id=251502661795316224, collection=Ref(id=tokens)), 'ts': 1576110479090000, 'instance': Ref(id=251407645221585408, collection=Ref(id=users, collection=Ref(id=collections))), 'data': {'meta': 'data'}}
    {ref: ref(id = "251502310512919040", collection = ref(id = "tokens")), ts: 1576110144160000, instance: ref(id = "251407645221585408", collection = ref(id = "users", collection = ref(id = "collections"))), data: {meta: "data"}}

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!