Voyti

Voyti

User management, authentication & authorization

REST API

The JSON REST API for user management ships as a separate package, yiirocks/voyti-api. It adds the voyti-routes-api config group, Bearer-token authentication on top of core's IdentityAdapter, and two console commands for issuing and revoking API tokens. It reuses core's models, services, and administratorPermissionName permission check.

Installation

Create an issue →

Routes are not auto-registered. Pull the voyti-routes-api config group into your router and mount it at whatever prefix you like:

use Yiisoft\Config\Config;
use Yiisoft\Definitions\DynamicReference;
use Yiisoft\Router\Group;
use Yiisoft\Router\RouteCollection;
use Yiisoft\Router\RouteCollectionInterface;
use Yiisoft\Router\RouteCollector;

/** @var Config $config */

return [
    RouteCollectionInterface::class => [
        'class' => RouteCollection::class,
        '__construct()' => [
            'collector' => DynamicReference::to(
                static fn() => (new RouteCollector())
                    ->addRoute(
                        Group::create('/user/api/')
                            ->routes(...$config->get('voyti-routes-api')),
                    )
            ),
        ],
    ],
];

The group wraps itself with its own middleware (JsonDataResponseMiddleware, plus ApiTokenAuthenticationMiddleware and AccessRuleMiddleware on the v1/ routes), so nothing else needs wiring.

Configuration

apiTokenLifespan int
0
API token lifetime in seconds. 0 disables expiry entirely (tokens never expire). Enforced when resolving a Bearer token.
// config/params.php
return [
    'yiirocks/voyti-api' => [
        'apiTokenLifespan' => 3600,
    ],
];

Authentication

Requests authenticate with an Authorization: Bearer <token> header, not the web session/CSRF cookie. ApiTokenAuthenticationMiddleware resolves the token to a user for that request only and returns 401 when the header is missing or the token is invalid or expired. AccessRuleMiddleware then enforces administratorPermissionName as usual, so API tokens only grant what that permission grants.

Managing tokens

The package registers two console commands under yiisoft/yii-console:

Command Description
voyti:api-token:generate Generate a REST API access token for a user (printed once)
voyti:api-token:revoke Revoke all REST API access tokens for a user

Endpoints

Route name Method Path Purpose
voyti/api-openapiGETopenapi.jsonOpenAPI 3.1 spec (JSON). Public, so tooling (Swagger UI, codegen) can fetch it without a Bearer token.
voyti/api-v1-users-indexGETv1/usersList users
voyti/api-v1-users-viewGETv1/users/{id}View a user
voyti/api-v1-users-createPOSTv1/usersCreate a user
voyti/api-v1-users-updatePATCHv1/users/{id}Update a user
voyti/api-v1-users-deleteDELETEv1/users/{id}Delete a user