CurrentToken

CurrentToken()
CurrentToken()
CurrentToken()
CurrentToken()
current_token()
CurrentToken()
CurrentToken()

Description

The CurrentToken function returns information about the active authentication:

  • When a token is used, a Reference to the token document is returned.

  • When a key is used, and the key exists in the current database (and not in a parent database), a Reference to the key document is returned.

  • When authentication is performed by an identity provider, an object containing the active JWT’s claims is returned.

For all other cases, CurrentToken emits an error. These cases include:

  • Authentication uses the "root" Fauna secret.

  • Authentication uses a key that has the database field set.

  • Authentication uses a key that exists in a parent database, for example, when using a scoped key.

Parameters

None.

Returns

For tokens, and keys defined in the current database, a Reference to the associated token/key.

For JWTs, an object containing the claims from the active JWT.

For connections using the "root" Fauna key, or a key that has the database field defined, an error is returned.

Examples

  1. The following query runs in the context of a Fauna token:

    try
    {
        Value result = await client.Query(
            CurrentToken()
        );
    
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    RefV(id = "1", collection = RefV(id = "tokens"))
    result, err := client.Query(
    	f.CurrentToken())
    
    if err != nil {
    	fmt.Fprintln(os.Stderr, err)
    } else {
    	fmt.Println(result)
    }
    {1 0xc000200210 0xc000200210 <nil>}
    System.out.println(
        client.query(
            CurrentToken()
        ).get());
    ref(id = "1", collection = ref(id = "tokens"))
    client.query(
      q.CurrentToken()
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error('Error: %s', err))
    Ref(Tokens(), "1")
    result = client.query(
      q.current_token()
    )
    print(result)
    Ref(id=1, collection=Ref(id=tokens))
    try {
      println(Await.result(
        client.query(
          CurrentToken()
        ),
        5.seconds
      ))
    } catch {
      case unknown: Throwable => println("Error: " + unknown.getMessage())
    }
    ref(id = "1", collection = ref(id = "tokens"))
    CurrentToken()
    Ref(Tokens(), "1")
  2. The following query runs in the context of a Fauna key:

    try
    {
        Value result = await client.Query(
            CurrentToken()
        );
    
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    RefV(id = "1", collection = RefV(id = "keys"))
    result, err := client.Query(
    	f.CurrentToken())
    
    if err != nil {
    	fmt.Fprintln(os.Stderr, err)
    } else {
    	fmt.Println(result)
    }
    {1 0xc000109650 0xc000109650 <nil>}
    System.out.println(
        client.query(
            CurrentToken()
        ).get());
    ref(id = "1", collection = ref(id = "keys"))
    client.query(
      q.CurrentToken()
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error('Error: %s', err))
    Ref(Keys(), "1")
    result = client.query(
      q.current_token()
    )
    print(result)
    Ref(id=1, collection=Ref(id=keys))
    try {
      println(Await.result(
        client.query(
          CurrentToken()
        ),
        5.seconds
      ))
    } catch {
      case unknown: Throwable => println("Error: " + unknown.getMessage())
    }
    ref(id = "1", collection = ref(id = "keys"))
    CurrentToken()
    Ref(Keys(), "1")
  3. The following query runs in the context of a Fauna key defined in a parent database:

    try
    {
        Value result = await client.Query(
            CurrentToken()
        );
    
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    ERROR: invalid token: Token metadata is not accessible.
    result, err := client.Query(
    	f.CurrentToken())
    
    if err != nil {
    	fmt.Fprintln(os.Stderr, err)
    } else {
    	fmt.Println(result)
    }
    Response error 400. Errors: [](invalid token): Token metadata is not accessible., details: []
    try {
        System.out.println(
            client.query(
                CurrentToken()
            ).get());
    } catch (Exception e) {
        System.out.println("ERROR " + e.getMessage());
    }
    ERROR com.faunadb.client.errors.BadRequestException: invalid token: Token metadata is not accessible.
    client.query(
      q.CurrentToken()
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error('Error: %s', err))
    Error: [BadRequest: invalid token] {
      description: 'Token metadata is not accessible.',
      requestResult: [RequestResult]
    }
    try:
      result = client.query(
        q.current_token()
      )
      print(result)
    except:
      print("Error: ", sys.exc_info()[0], sys.exc_info()[1])
    Error:  <class 'faunadb.errors.BadRequest'> Token metadata is not accessible.
    try {
      val result = client.query(
        CurrentToken()
      ).recover {
        case error: Any => "Error: " + error.getMessage()
      }
      println(Await.result(result, 5.seconds))
    } catch {
      case unknown: Throwable => println("Error: " + unknown.getMessage())
    }
    Error: invalid token: Token metadata is not accessible.
    Not available in this language, yet.
  4. The following query runs in the context of a JWT token:

    try
    {
        Value result = await client.Query(
            CurrentToken()
        );
    
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    ObjectV(iss: StringV(https://dev--nozpv3z.us.auth0.com/),sub: StringV(8cYrAhHUTtqr9eSliRmdrGryJhYbcPQA@clients),aud: StringV(https://db.fauna.com/db/yo8yrjgkcydye),iat: LongV(1617396332),exp: LongV(1617482732),azp: StringV(8cYrAhHUTtqr9eSliRmdrGryJhYbcPQA),gty: StringV(client-credentials))
    result, err := client.Query(
    	f.CurrentToken())
    
    if err != nil {
    	fmt.Fprintln(os.Stderr, err)
    } else {
    	fmt.Println(result)
    }
    map[aud:https://db.fauna.com/db/yo8yrjgkcydye azp:8cYrAhHUTtqr9eSliRmdrGryJhYbcPQA exp:1617728575 gty:client-credentials iat:1617642175 iss:https://dev--nozpv3z.us.auth0.com/ sub:8cYrAhHUTtqr9eSliRmdrGryJhYbcPQA@clients]
    System.out.println(
        client.query(
            CurrentToken()
        ).get());
    {iss: "https://dev--nozpv3z.us.auth0.com/", sub: "l6vJS8QvHC2LmiGRaOTiE16givuufR22@clients", aud: "https://db.fauna.com/db/yxxf5x9w1ybyn", iat: 1611084659, exp: 1611171059, azp: "l6vJS8QvHC2LmiGRaOTiE16givuufR22", gty: "client-credentials"}
    client.query(
      q.CurrentToken()
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error('Error: %s', err))
    {
      iss: 'https://dev--nozpv3z.us.auth0.com/',
      sub: 'l6vJS8QvHC2LmiGRaOTiE16givuufR22@clients',
      aud: 'https://db.fauna.com/db/yxxf5x9w1ybyn',
      iat: 1611084659,
      exp: 1611171059,
      azp: 'l6vJS8QvHC2LmiGRaOTiE16givuufR22',
      gty: 'client-credentials'
    }
    result = client.query(
      q.current_token()
    )
    print(result)
    {'iss': 'https://dev--nozpv3z.us.auth0.com/', 'sub': '8cYrAhHUTtqr9eSliRmdrGryJhYbcPQA@clients', 'aud': 'https://db.fauna.com/db/yo8yrjgkcydye', 'iat': 1617997880, 'exp': 1618084280, 'azp': '8cYrAhHUTtqr9eSliRmdrGryJhYbcPQA', 'gty': 'client-credentials'}
    try {
      println(Await.result(
        client.query(
          CurrentToken()
        ),
        5.seconds
      ))
    } catch {
      case unknown: Throwable => println("Error: " + unknown.getMessage())
    }
    {exp: 1611171059, iat: 1611084659, sub: "l6vJS8QvHC2LmiGRaOTiE16givuufR22@clients", azp: "l6vJS8QvHC2LmiGRaOTiE16givuufR22", aud: "https://db.fauna.com/db/yxxf5x9w1ybyn", gty: "client-credentials", iss: "https://dev--nozpv3z.us.auth0.com/"}
    CurrentToken()
    {
      iss: 'https://dev--nozpv3z.us.auth0.com/',
      sub: '8cYrAhHUTtqr9eSliRmdrGryJhYbcPQA@clients',
      aud: 'https://db.fauna.com/db/yo8yrjgkcydye',
      iat: 1624306581,
      exp: 1624392981,
      azp: '8cYrAhHUTtqr9eSliRmdrGryJhYbcPQA',
      gty: 'client-credentials'
    }

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!