# Who Calls (g.req.auth)

> Read in your code who calls an API of API Maker - the API user, a person of your users table, a person of Google, Azure AD or AWS Cognito - with g.req.auth, in eight languages, and a login API which gets both tokens.

Source: https://docs.apimaker.dev/v4/examples/req/auth/auth.html

> **The code in every language**
>
> Each example is the inside of `main`, written in every [language](https://docs.apimaker.dev/v4/docs/languages/index.html) API Maker runs : pick yours in the tabs, and every page shows it from then on. The types a call takes (`GetAll`, `DeepItem`…) are those of `am` : `from am import GetAll` in Python, `use am::GetAll;` in Rust, `import am.GetAll;` in Java and Kotlin ; PHP and C# have them without an import, Go names them `am.GetAll`.

Every provider which checked a token of the call leaves what it found in `g.req.auth` : the application in `authAMUser`, the person in `authAMDB`, `authGoogle`, `authAzure`, `authAWS` or `authCustom`. The [sign-in providers](https://docs.apimaker.dev/v4/docs/authorization/sign-in-providers.html) page says how each one is set up.

## The API user

The application behind `x-am-authorization`, from the [token API](https://docs.apimaker.dev/v4/docs/apis-all/system-apis/system-generated-token-api.html).

**TypeScript**

```typescript
const apiUserAuth = g.req.auth.authAMUser; // the API user of the token of x-am-authorization
```

**Python**

```python
api_user_auth = g.req.auth.authAMUser  # the API user of the token of x-am-authorization
```

**PHP**

```php
$apiUserAuth = $g->req->auth->authAMUser; // the API user of the token of x-am-authorization
```

**C#**

```csharp
var apiUserAuth = g.Req.Auth.GetObject("authAMUser"); // the API user of the token of x-am-authorization
```

**Go**

```go
apiUserAuth := g.Req.Auth().Map("authAMUser") // the API user of the token of x-am-authorization
```

**Rust**

```rust
let api_user_auth = &g.req.auth()["authAMUser"]; // the API user of the token of x-am-authorization
```

**Java**

```java
JsonObject apiUserAuth = g.req.getAuth().getObject("authAMUser"); // the API user of the token of x-am-authorization
```

**Kotlin**

```kotlin
val apiUserAuth = g.req.auth.getObject("authAMUser") // the API user of the token of x-am-authorization
```

## A person of your users table

The person behind `x-am-user-authorization`, a row of the table of a [DB token generator](https://docs.apimaker.dev/v4/docs/authorization/AMDB.html).

**TypeScript**

```typescript
const databaseUserAuth = g.req.auth.authAMDB; // the row of the person, without its password
```

**Python**

```python
database_user_auth = g.req.auth.authAMDB  # the row of the person, without its password
```

**PHP**

```php
$databaseUserAuth = $g->req->auth->authAMDB; // the row of the person, without its password
```

**C#**

```csharp
var databaseUserAuth = g.Req.Auth.GetObject("authAMDB"); // the row of the person, without its password
```

**Go**

```go
databaseUserAuth := g.Req.Auth().Map("authAMDB") // the row of the person, without its password
```

**Rust**

```rust
let database_user_auth = &g.req.auth()["authAMDB"]; // the row of the person, without its password
```

**Java**

```java
JsonObject databaseUserAuth = g.req.getAuth().getObject("authAMDB"); // the row of the person, without its password
```

**Kotlin**

```kotlin
val databaseUserAuth = g.req.auth.getObject("authAMDB") // the row of the person, without its password
```

### A login API which gets both tokens

**TypeScript**

```typescript
import * as T from 'types';

// A login API (apiAccessType : IS_PUBLIC) : the token of the API user 'default' for the app, and the token of the
// person whose email and password the body brings, checked by the DB token generator 'users_tg'.
async function main(g: T.IAMGlobal) {
    const password = await g.sys.system.getSecret('common.apiUserPasswords.default');
    const [app, person] = await g.sys.system.getToken([
        { u: 'default', p: password },
        { name: 'users_tg', u: g.req.body.email, p: g.req.body.password },
    ]);
    return { amToken: app.token, amDbToken: person.token, refreshToken: person.refresh_token };
}
module.exports = main;
```

**Python**

```python
from am import G, GetToken


# A login API (apiAccessType : IS_PUBLIC) : the token of the API user 'default' for the app, and the token of the
# person whose email and password the body brings, checked by the DB token generator 'users_tg'.
async def main(g: G):
    password = await g.sys.system.get_secret('common.apiUserPasswords.default')
    app, person = await g.sys.system.get_tokens([
        GetToken('default', password),
        GetToken(g.req.body.email, g.req.body.password, name='users_tg'),
    ])
    return {'amToken': app.token, 'amDbToken': person.token, 'refreshToken': person.refresh_token}
```

**PHP**

```php
<?php
// A login API (apiAccessType : IS_PUBLIC) : the token of the API user 'default' for the app, and the token of the
// person whose email and password the body brings, checked by the DB token generator 'users_tg'.
function main(AmG $g)
{
    $password = $g->sys->system->getSecret('common.apiUserPasswords.default');
    [$app, $person] = $g->sys->system->getTokens([
        new GetToken('default', $password),
        new GetToken($g->req->body->email, $g->req->body->password, name: 'users_tg'),
    ]);
    return ['amToken' => $app->token, 'amDbToken' => $person->token, 'refreshToken' => $person->refresh_token];
}
```

**C#**

```csharp
// A login API (apiAccessType : IS_PUBLIC) : the token of the API user 'default' for the app, and the token of the
// person whose email and password the body brings, checked by the DB token generator 'users_tg'.
file static class Api
{
    public static async Task<object?> Main(G g)
    {
        var password = (await g.Sys.System.GetSecret("common.apiUserPasswords.default"))?.GetValue<string>();
        var tokens = await g.Sys.System.GetTokens([
            new GetToken("default", password),
            new GetToken(g.Req.Body.GetString("email"), g.Req.Body.GetString("password")) { Name = "users_tg" },
        ]);
        return new { amToken = tokens[0].Token, amDbToken = tokens[1].Token, refreshToken = tokens[1].RefreshToken };
    }
}
```

**Go**

```go
// A login API (apiAccessType : IS_PUBLIC) : the token of the API user 'default' for the app, and the token of the
// person whose email and password the body brings, checked by the DB token generator 'users_tg'.
package code

import "apimaker/am"

func Main(g *am.G) (any, error) {
	secret, err := g.Sys.System.GetSecret("common.apiUserPasswords.default")
	if err != nil {
		return nil, err
	}
	password, _ := secret.(string)
	body := g.Req.Body()
	tokens, err := g.Sys.System.GetTokens([]am.GetToken{
		{U: "default", P: password},
		{Name: "users_tg", U: body.String("email"), P: body.String("password")},
	})
	if err != nil {
		return nil, err
	}
	return am.M{"amToken": tokens[0].Token, "amDbToken": tokens[1].Token, "refreshToken": tokens[1].RefreshToken}, nil
}
```

**Rust**

```rust
// A login API (apiAccessType : IS_PUBLIC) : the token of the API user 'default' for the app, and the token of the
// person whose email and password the body brings, checked by the DB token generator 'users_tg'.
use am::{Errors, G, GetToken, Value, json};

pub fn main(g: &mut G) -> Result<Value, Errors> {
    let password = g.sys.system.get_secret("common.apiUserPasswords.default", None)?;
    let body = g.req.body();
    let tokens = g.sys.system.get_tokens(vec![
        GetToken::new("default", password.as_str().unwrap_or_default()),
        GetToken::new(body["email"].as_str().unwrap_or_default(), body["password"].as_str().unwrap_or_default()).name("users_tg"),
    ])?;
    Ok(json!({ "amToken": tokens[0].token, "amDbToken": tokens[1].token, "refreshToken": tokens[1].refresh_token }))
}
```

**Java**

```java
// A login API (apiAccessType : IS_PUBLIC) : the token of the API user 'default' for the app, and the token of the
// person whose email and password the body brings, checked by the DB token generator 'users_tg'.
import am.G;
import am.GetToken;
import am.JsonObject;
import am.TokenResponse;
import java.util.List;

class Api {
    static Object main(G g) {
        String password = (String) g.sys.system.getSecret("common.apiUserPasswords.default");
        JsonObject body = g.req.getBody();
        List<TokenResponse> tokens = g.sys.system.getTokens(List.of(
            new GetToken("default", password),
            new GetToken(body.getString("email"), body.getString("password")).name("users_tg")));
        return JsonObject.of("amToken", tokens.get(0).getToken(), "amDbToken", tokens.get(1).getToken(), "refreshToken", tokens.get(1).getRefreshToken());
    }
}
```

**Kotlin**

```kotlin
// A login API (apiAccessType : IS_PUBLIC) : the token of the API user 'default' for the app, and the token of the
// person whose email and password the body brings, checked by the DB token generator 'users_tg'.
import am.G
import am.GetToken

fun main(g: G): Any? {
    val password = g.sys.system.getSecret("common.apiUserPasswords.default") as String
    val body = g.req.body
    val (app, person) = g.sys.system.getTokens(
        listOf(
            GetToken("default", password),
            GetToken(body.getString("email"), body.getString("password")).name("users_tg"),
        ),
    )
    return mapOf("amToken" to app.token, "amDbToken" to person.token, "refreshToken" to person.refreshToken)
}
```

- The token of a person, a row of the table of the token generator `users_tg`, from the [token API](https://docs.apimaker.dev/v4/docs/apis-all/system-apis/system-generated-token-api.html#the-token-of-a-person) :

```json
{
    "name": "users_tg",
    "u": "USER_NAME",
    "p": "PASSWORD"
}
```

## Google, Azure AD and AWS Cognito

The opened token of the provider, with the columns its `groupsDataSource` selected from your table.

### Google

**TypeScript**

```typescript
const googleAuth = g.req.auth.authGoogle; // the opened token of x-google-authorization
```

**Python**

```python
google_auth = g.req.auth.authGoogle  # the opened token of x-google-authorization
```

**PHP**

```php
$googleAuth = $g->req->auth->authGoogle; // the opened token of x-google-authorization
```

**C#**

```csharp
var googleAuth = g.Req.Auth.GetObject("authGoogle"); // the opened token of x-google-authorization
```

**Go**

```go
googleAuth := g.Req.Auth().Map("authGoogle") // the opened token of x-google-authorization
```

**Rust**

```rust
let google_auth = &g.req.auth()["authGoogle"]; // the opened token of x-google-authorization
```

**Java**

```java
JsonObject googleAuth = g.req.getAuth().getObject("authGoogle"); // the opened token of x-google-authorization
```

**Kotlin**

```kotlin
val googleAuth = g.req.auth.getObject("authGoogle") // the opened token of x-google-authorization
```

### Azure AD

**TypeScript**

```typescript
const azureAuth = g.req.auth.authAzure; // the opened token of x-azure-authorization
```

**Python**

```python
azure_auth = g.req.auth.authAzure  # the opened token of x-azure-authorization
```

**PHP**

```php
$azureAuth = $g->req->auth->authAzure; // the opened token of x-azure-authorization
```

**C#**

```csharp
var azureAuth = g.Req.Auth.GetObject("authAzure"); // the opened token of x-azure-authorization
```

**Go**

```go
azureAuth := g.Req.Auth().Map("authAzure") // the opened token of x-azure-authorization
```

**Rust**

```rust
let azure_auth = &g.req.auth()["authAzure"]; // the opened token of x-azure-authorization
```

**Java**

```java
JsonObject azureAuth = g.req.getAuth().getObject("authAzure"); // the opened token of x-azure-authorization
```

**Kotlin**

```kotlin
val azureAuth = g.req.auth.getObject("authAzure") // the opened token of x-azure-authorization
```

### AWS Cognito

**TypeScript**

```typescript
const awsAuth = g.req.auth.authAWS; // the opened token of x-aws-authorization
```

**Python**

```python
aws_auth = g.req.auth.authAWS  # the opened token of x-aws-authorization
```

**PHP**

```php
$awsAuth = $g->req->auth->authAWS; // the opened token of x-aws-authorization
```

**C#**

```csharp
var awsAuth = g.Req.Auth.GetObject("authAWS"); // the opened token of x-aws-authorization
```

**Go**

```go
awsAuth := g.Req.Auth().Map("authAWS") // the opened token of x-aws-authorization
```

**Rust**

```rust
let aws_auth = &g.req.auth()["authAWS"]; // the opened token of x-aws-authorization
```

**Java**

```java
JsonObject awsAuth = g.req.getAuth().getObject("authAWS"); // the opened token of x-aws-authorization
```

**Kotlin**

```kotlin
val awsAuth = g.req.auth.getObject("authAWS") // the opened token of x-aws-authorization
```

## A provider of your own

The result of your token validator, in `g.req.auth.authCustom` : see [the custom provider example](https://docs.apimaker.dev/v4/examples/req/auth/authCustom.html).

## Related

- [Sign-in providers](https://docs.apimaker.dev/v4/docs/authorization/sign-in-providers.html) · [The request : g.req](https://docs.apimaker.dev/v4/examples/req/req.html) · [The global object g](https://docs.apimaker.dev/v4/docs/pre-defined-terms/global-object-g.html)
