code4nix/uri-signer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

code4nix/uri-signer

最新稳定版本:1.0.6

Composer 安装命令:

composer require code4nix/uri-signer

包简介

uri signer with expiration option

README 文档

README

Create and check signed URIs with expiration time support. This bundle is a further development of Symfony's URI Signer.

Installation

composer require code4nix/uri-signer

Usage

Create a signed URI

To create a signed URI with an expiration time of 1 day (default), you can use $this->uriSigner->sign($strUri). You can add a second parameter $intExpires to add a custom expiration time. E.g. call $this->uriSigner->sign($strUri, 600) to get a URI with an expiration time of 10 minutes.

Check the signed URI/request

To check the URI you can use $this->uriSigner->check($strUri).

Instead of building a URI you can call $this->uriSigner->checkRequest($request) method and pass a Symfony\Component\HttpFoundation\Request object to check the signature of its related URI. Both methods return boolean true if the URI passes validation, or boolean false if the URI is invalid or expired.

Error handling

Alternatively you can run the check() or checkRequest() methods with a second optional boolean parameter $this->uriSigner->check($strUri, true) or $this->uriSigner->checkRequest($request, true), which will raise exceptions if a URI cannot be verified (e.g. has been tampered, has expired or is a malformed URI).

  • MalformedUriException
  • InvalidSignatureException
  • ExpiredLinkException

Usage in your controller:

<?php

declare(strict_types=1);

namespace App\Controller;

use Code4Nix\UriSigner\Exception\ExpiredLinkException;
use Code4Nix\UriSigner\Exception\InvalidSignatureException;
use Code4Nix\UriSigner\Exception\MalformedUriException;
use Code4Nix\UriSigner\UriSigner;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/uri_signer_test", name="uri_signer_test")
 */
class UriSignerTestController extends AbstractController
{

    public function __construct(
        private readonly UriSigner $uriSigner,
    ) {
    }

    public function __invoke(Request $request): Response
    {
        // Sign a URI with an expiration time of 10 min
        $uri = 'https://foo.bar/test';

        //https://foo.bar/test?_hash=eyJoYXNoZWQiOiJqNXUxeE1NRnpTRU1yRnREc
        $signedUri = $this->uriSigner->sign($uri, 600);

        // For test usage we will create our request object manually.
        $request = Request::create(
            $signedUri,
        );

        $responseText = 'URI is valid.';

        // Use check($signedUri, true) or checkRequest($request, true)
        // If set to true the second boolean parameter will raise exceptions if a URI cannot be verified.
        try {
            $this->uriSigner->checkRequest($request, true); // or $this->uriSigner->check($signedUri, true);
        } catch (MalformedUriException $e) {
            $responseText = 'Malformed URI detected!';
        } catch (ExpiredLinkException $e) {
            $responseText = 'URI has expired!';
        } catch (InvalidSignatureException $e) {
            $responseText = 'Invalid signature detected! The URI could have been tampered.';
        } catch (\Exception $e) {
            $responseText = 'Oh, la, la! Something went wrong ;-(';
        }

        return new Response($responseText);
    }
}

Configuration:

The default expiration and the parameter are configurable:

# config/config.yaml
code4nix_uri_signer:
  parameter: '_signed' # default: '_hash'
  expiration: 20 # default: 86400 (1 day)

统计信息

  • 总下载量: 3.28k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2024-02-05