Skip to content

Relative key file path cannot be loadedΒ #52

@ricklambrechts

Description

@ricklambrechts

File can not be loaded when using a relative path.

I would suggest to add base_path() around $path when the $path does not start with /.

$keys[] = JWKFactory::createFromKeyFile($path);

Related code in other projects:

<?php

declare(strict_types=1);

namespace App\Attributes;

use Attribute;
use Illuminate\Container\Attributes\Config;
use Illuminate\Container\Container;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Container\ContextualAttribute;
use Illuminate\Support\Str;
use Jose\Component\KeyManagement\JWKFactory;

#[Attribute(Attribute::TARGET_PARAMETER)]
class JWKFromConfig implements ContextualAttribute
{
    /**
     * Create a new attribute instance.
     */
    public function __construct(public string $key, public mixed $default = null)
    {
    }

    /**
     * Resolve the configuration value.
     *
     * @param self $attribute
     * @param Container $container
     * @return mixed
     * @throws BindingResolutionException
     */
    public static function resolve(self $attribute, Container $container): mixed
    {
        $privateKeyPath = Config::resolve(new Config($attribute->key, $attribute->default), $container);
        if (empty($privateKeyPath)) {
            throw new BindingResolutionException('Configuration value not found for key: ' . $attribute->key);
        }

        $absolutePrivateKeyPath = Str::startsWith($privateKeyPath, DIRECTORY_SEPARATOR) ?
            $privateKeyPath :
            base_path($privateKeyPath);

        if (!file_exists($absolutePrivateKeyPath)) {
            throw new BindingResolutionException('Private key file not found at path: ' . $privateKeyPath);
        }

        return JWKFactory::createFromKeyFile($absolutePrivateKeyPath);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions