Skip to content

Core\Api\SchemeType

The kind of security scheme that an #[Core\Api\Scheme] declares. There are five kinds. They are the five that OpenAPI 3.1 lists.

You write a case as the type of a scheme. The type decides which other fields the scheme needs:

| Case | Needs | Optional | |---|---|---| | ApiKey | in, parameter | | | Http | scheme, such as bearer or basic | format | | MutualTls | nothing | | | OAuth2 | flows | | | OpenIdConnect | url | |

A missing field does not compile. A field that the type does not take does not compile either.

nvs build --openapi writes each scheme to the API document. The case becomes the scheme's type in the way OpenAPI writes it, for example apiKey or mutualTLS.

Good to know: a scheme only describes your API to its clients. It does not check a request. #[Core\Access] decides who may call a route.

Examples

The five kinds and what each needs

<?nvs
// Shows the five kinds of security scheme, and the fields each kind needs.
class Kinds
{
// Returns the text that the API document writes as the scheme's `type`.
public static function documentName(Core\Api\SchemeType $type): string
{
return match ($type) {
Core\Api\SchemeType::ApiKey => 'apiKey',
Core\Api\SchemeType::Http => 'http',
Core\Api\SchemeType::MutualTls => 'mutualTLS',
Core\Api\SchemeType::OAuth2 => 'oauth2',
Core\Api\SchemeType::OpenIdConnect => 'openIdConnect',
};
}
// Returns the fields that a scheme of this kind needs, next to `name` and `type`.
public static function needs(Core\Api\SchemeType $type): string
{
return match ($type) {
Core\Api\SchemeType::ApiKey => 'in, parameter',
Core\Api\SchemeType::Http => 'scheme (format is optional)',
Core\Api\SchemeType::MutualTls => 'no other field',
Core\Api\SchemeType::OAuth2 => 'flows',
Core\Api\SchemeType::OpenIdConnect => 'url',
};
}
}
array<Core\Api\SchemeType> $everything = [
Core\Api\SchemeType::ApiKey,
Core\Api\SchemeType::Http,
Core\Api\SchemeType::MutualTls,
Core\Api\SchemeType::OAuth2,
Core\Api\SchemeType::OpenIdConnect,
];
foreach ($everything as Core\Api\SchemeType $type) {
echo Kinds::documentName($type), ': ', Kinds::needs($type), "\n";
}
Output
apiKey: in, parameter
http: scheme (format is optional)
mutualTLS: no other field
oauth2: flows
openIdConnect: url

Declaring a scheme of each kind

<?nvs
// Declares one security scheme of each kind, and a route that names two of them.
// Each `#[Core\Api\Scheme]` adds one scheme to the generated API document.
// The `type` decides which other fields the scheme needs.
#[Core\Api\Scheme(name: 'key', type: Core\Api\SchemeType::ApiKey, in: Core\Api\KeyIn::Header, parameter: 'X-Api-Key')]
#[Core\Api\Scheme(name: 'bearer', type: Core\Api\SchemeType::Http, scheme: 'bearer', format: 'JWT')]
#[Core\Api\Scheme(name: 'certificate', type: Core\Api\SchemeType::MutualTls)]
#[Core\Api\Scheme(
name: 'login',
type: Core\Api\SchemeType::OAuth2,
flows: {
clientCredentials: {
tokenUrl: 'https://example.com/oauth/token',
scopes: ['orders:read' => 'Read orders'],
},
},
)]
#[Core\Api\Scheme(name: 'openid', type: Core\Api\SchemeType::OpenIdConnect, url: 'https://example.com/.well-known/openid-configuration')]
class Auth
{}
class Orders
{
/// Show one order.
#[Core\Route(path: '/orders/{id}', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
// A client may use the key or the bearer token. Every name here must be a declared scheme.
#[Core\Api(security: ['key', 'bearer'])]
public function show(uint $id): string
{
return 'order ' . ($id as string);
}
}
// A scheme only describes the API. It does not check anything when the route runs.
Orders $orders = new Orders();
// This prints `order 42`.
echo $orders->show(42), "\n";
Output
order 42