# IdentityInterface

> IdentityInterface is the interface that should be implemented by a class providing identity information.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [web](/docs/api/yii/web/index.md) › IdentityInterface

| interface | `yii\web\IdentityInterface` |
| --- | --- |
| Since | 2.0 |
| Source | [`framework/web/IdentityInterface.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/web/IdentityInterface.php#L59) |

IdentityInterface is the interface that should be implemented by a class providing identity information.

This interface can typically be implemented by a user model class. For example, the following
code shows how to implement this interface by a User ActiveRecord class:

```
class User extends ActiveRecord implements IdentityInterface
{
    public static function findIdentity($id)
    {
        return static::findOne($id);
    }

    public static function findIdentityByAccessToken($token, $type = null)
    {
        return static::findOne(['access_token' => $token]);
    }

    public function getId()
    {
        return $this->id;
    }

    public function getAuthKey()
    {
        return $this->authKey;
    }

    public function validateAuthKey($authKey)
    {
        return $this->authKey === $authKey;
    }
}
```

In some situations not all of these methods are required to be implemented.
For example, if your application is a pure stateless RESTful application,
you would only need to implement [findIdentityByAccessToken()](/docs/api/yii/web/identityinterface/index.md#method-findIdentityByAccessToken)
and [getId()](/docs/api/yii/web/identityinterface/index.md#method-getId) while leaving all other methods with an empty body.
Or if your application uses session only authentication, you would need to implement all the methods
except [findIdentityByAccessToken()](/docs/api/yii/web/identityinterface/index.md#method-findIdentityByAccessToken).

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`findIdentity()`](#method-findIdentity) static abstract | Finds an identity by the given ID. | `IdentityInterface` |
| [`findIdentityByAccessToken()`](#method-findIdentityByAccessToken) static abstract | Finds an identity by the given token. | `IdentityInterface` |
| [`getAuthKey()`](#method-getAuthKey) abstract | Returns a key that can be used to check the validity of a given identity ID. | `IdentityInterface` |
| [`getId()`](#method-getId) abstract | Returns an ID that can uniquely identify a user identity. | `IdentityInterface` |
| [`validateAuthKey()`](#method-validateAuthKey) abstract | Validates the given auth key. | `IdentityInterface` |

## Method details

### findIdentity() <span id="method-findIdentity"></span>

```php
abstract public static function findIdentity(string|int $id): yii\web\IdentityInterface|null
```

Finds an identity by the given ID.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$id` | `string\|int` | The ID to be looked for |

**Returns**

[`IdentityInterface`](/docs/api/yii/web/identityinterface/index.md)`|null`: The identity object that matches the given ID. Null should be returned if such an identity cannot be found or the identity is not in an active state (disabled, deleted, etc.)

### findIdentityByAccessToken() <span id="method-findIdentityByAccessToken"></span>

```php
abstract public static function findIdentityByAccessToken(mixed $token, mixed $type = null): yii\web\IdentityInterface|null
```

Finds an identity by the given token.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$token` | `mixed` | The token to be looked for |
| `$type` | `mixed` | The type of the token. The value of this parameter depends on the implementation. For example, [`HttpBearerAuth`](/docs/api/yii/filters/auth/httpbearerauth/index.md) will set this parameter to be `yii\filters\auth\HttpBearerAuth`. |

**Returns**

[`IdentityInterface`](/docs/api/yii/web/identityinterface/index.md)`|null`: The identity object that matches the given token. Null should be returned if such an identity cannot be found or the identity is not in an active state (disabled, deleted, etc.)

### getAuthKey() <span id="method-getAuthKey"></span>

```php
abstract public function getAuthKey(): string|null
```

Returns a key that can be used to check the validity of a given identity ID.

The key should be unique for each individual user, and should be persistent
so that it can be used to check the validity of the user identity.

The space of such keys should be big enough to defeat potential identity attacks.

The returned key is used to validate session and auto-login (if [`User::$enableAutoLogin`](/docs/api/yii/web/user/index.md#property-enableAutoLogin) is enabled).

Make sure to invalidate earlier issued authKeys when you implement force user logout, password change and
other scenarios, that require forceful access revocation for old sessions.

**Returns**

`string|null`: A key that is used to check the validity of a given identity ID.

**See also**

- [`validateAuthKey()`](/docs/api/yii/web/identityinterface/index.md#method-validateAuthKey)

### getId() <span id="method-getId"></span>

```php
abstract public function getId(): string|int
```

Returns an ID that can uniquely identify a user identity.

**Returns**

`string|int`: An ID that uniquely identifies a user identity.

### validateAuthKey() <span id="method-validateAuthKey"></span>

```php
abstract public function validateAuthKey(string $authKey): bool|null
```

Validates the given auth key.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$authKey` | `string` | The given auth key |

**Returns**

`bool|null`: Whether the given auth key is valid.

**See also**

- [`getAuthKey()`](/docs/api/yii/web/identityinterface/index.md#method-getAuthKey)