Create and validate signed URLs with a limited lifetime

Overview

Create secured URLs with a limited lifetime

Latest Version on Packagist Software License Build Status Quality Score Total Downloads

This package can create URLs with a limited lifetime. This is done by adding an expiration date and a signature to the URL.

$urlSigner = new MD5UrlSigner('randomkey');

$urlSigner->sign('https://myapp.com', 30);

// => The generated url will be valid for 30 days

This will output an URL that looks like https://myapp.com/?expires=xxxx&signature=xxxx.

Imagine mailing this URL out to the users of your application. When a user clicks on a signed URL your application can validate it with:

$urlSigner->validate('https://myapp.com/?expires=xxxx&signature=xxxx');

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

All postcards are published on our website.

Installation

The package can installed via Composer:

composer require spatie/url-signer

Usage

A signer-object can sign URLs and validate signed URLs. A secret key is used to generate signatures.

use Spatie\UrlSigner\MD5UrlSigner;

$urlSigner = new MD5UrlSigner('mysecretkey');

Generating URLs

Signed URLs can be generated by providing a regular URL and an expiration date to the sign method.

$expirationDate = (new DateTime)->modify('10 days');

$urlSigner->sign('https://myapp.com', $expirationDate);

// => The generated url will be valid for 10 days

If an integer is provided as expiration date, the url will be valid for that amount of days.

$urlSigner->sign('https://myapp.com', 30);

// => The generated url will be valid for 30 days

Validating URLs

To validate a signed URL, simply call the validate() method. This will return a boolean.

$urlSigner->validate('https://myapp.com/?expires=1439223344&signature=2d42f65bd023362c6b61f7432705d811');

// => true

$urlSigner->validate('https://myapp.com/?expires=1439223344&signature=2d42f65bd0-INVALID-23362c6b61f7432705d811');

// => false

Writing custom signers

This packages provides a signer that uses md5 to generate signature. You can create your own signer by implementing the Spatie\UrlSigner\UrlSigner-interface. If you let your signer extend Spatie\UrlSigner\BaseUrlSigner you'll only need to provide the createSignature-method.

Tests

The tests can be run with:

$ vendor/bin/phpspec run

Integrations

To get started quickly in Laravel you can use the spatie/laravel-url-signer package.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.

Credits

About Spatie

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.

Comments
  • I don't think this is secure

    I don't think this is secure

    Please, guys, pull this goodie off of the github. It is horribly insecure.

    1. Once your secret is leaked, you are pwned... If you think your secret will never leak then think again.

    2. It is not very hard to reverse engineer the secret anyway.

    I used to do the stuff like this as well, but always gave me creeps. At last I resorted to generating random tokens and storing them in database along with their expiration, it was not that hard.

    opened by josefsabl 8
  • CloudFront UrlSigner

    CloudFront UrlSigner

    Could you be interested into a UrlSigner based on the CloudFront PHP SDK? I'll make one and I can do a PR if you like so. I'd then make a PR also on the laravel-url-signer to allow the choice between the two UrlSigners

    opened by IlCallo 3
  • Always needed to be instantiated?

    Always needed to be instantiated?

    Hi I just recently used this library, but I want to ask something, is it always needed to be instantiated or created new object just to use this?

    What if we want to use the Signer in code A, and the validate in code B, repeat the code all the way?

    And I have checked the Laravel URL Signer which have :: or static access object. why don't this URL-Signer has the same?

    Thank You

    opened by codetensor 3
  • Validating URL from another server

    Validating URL from another server

    I'm using Spatie Media Library for my images and am generating private and public URLs using Spatie URL Signer (Laravel package). This is working perfectly as long as everything is on the same server.

    Recently I've started thinking about integrating a load balancer, which means I'll have to host my images on another server. Instead of using Amazon I've decided to go with DigitalOcean's block storage.

    Now my problem: how do I validate a URL on server B that was generated on Server A?

    Server B is my media server. To keep things light I'm working with the non-Laravel version of URL Signer on server B.

    I've tried using the same keys on both servers (something really simple for testing), but that's not working.

    SERVER A (Laravel)
    config/laravel-url-signer.php
        'signatureKey' => '123123123',
    
    SERVER B (PHP)
    use Spatie\UrlSigner\MD5UrlSigner;
    $urlSigner = new MD5UrlSigner('123123123');
    

    No matter what I do the validation just keeps returning false. Is this possible?

    opened by timgavin 3
  • Use hash_equals for hash comparison to mitigate timing attacks

    Use hash_equals for hash comparison to mitigate timing attacks

    hash_equals should be used to compare hashes instead of basic string comparison.

    It is built into PHP since 5.6, as this project supports >=5.5.0 I added indigophp/hash-compat as a polyfill library.

    All the tests still pass, I'm sorry not adding new ones but I didn't see how for this change.

    opened by kronthto 3
  • Add missing abstract method in the BaseUrlSigner

    Add missing abstract method in the BaseUrlSigner

    The class expects child classes to define a createSignature method with the expected API, but it does not enforce that through an abstract method, which is the right way to enforce that.

    opened by stof 2
  • Remove league/url dependency

    Remove league/url dependency

    The league/url package is deprecated in favor of league/uri. The latter requires php extensions to be installed which I'd rather avoid, so some refactoring would be required. Solutions: an alternative url package or a local implementation.

    revisit for v2 
    opened by sebastiandedeyne 2
  • use stronger signature algo?

    use stronger signature algo?

    just stumbled over this lib yesterday and it feels interessting to me.

    I am wondering why there is only a md5 based signature and whether this fact is "good enough" to protect a url from beeing fake-signed or similar?

    would it make sense to use a 'more modern' crypto algo to sign the urls?

    opened by staabm 1
  • DateTimeImmutable support

    DateTimeImmutable support

    Currently, the signer requires using either an integer or a DateTime for the expiration. Is there any reason not to support using a DateTimeImmutable for the date ?

    opened by stof 1
  • Use PHP8 version of league/uri-components

    Use PHP8 version of league/uri-components

    Use the newly tagged version (https://github.com/thephpleague/uri-components/releases/tag/2.3.0) of league/uri-components which supports PHP-8, instead of using the dev-master dependency. Using the dev-master dependency was giving conflicts when the minimum-stability does not allow this.

    opened by arjanwestdorp 1
  • Replace league/url with league/uri & league/uri-components

    Replace league/url with league/uri & league/uri-components

    league/url has been abandoned since 2015-09-23 and this PR replaces it with its successor: league/uri and its components package league/uri-components.

    The package change requires the minimum PHP version to be set to 7.2. PHPUnit was also updated to the last version that supports PHP7.2.

    I know there was concern about requiring new extensions (https://github.com/spatie/url-signer/issues/1) by making the switch, however these are not hard dependencies. If you don't use the league/uri i18n URI processing feature (ext-intl) or don't want to create a data URI from a filepath (ext-fileinfo) then there are no issues – an exception is thrown should you use one of these features and don't have the required extension.

    All existing unit tests pass without any modification.

    Please let me know if you require any changes to get this PR merged.

    Thanks!

    opened by jakejackson1 1
Releases(2.0.0)
Owner
Spatie
We create open source, digital products and courses for the developer community
Spatie
Mail sending module for Mezzio and Laminas MVC with support for file attachment and template email composition

This module provides an easy and flexible way to send emails from Mezzio and Laminas MVC applications (formerly known as Zend Expressive and Zend MVC). It allows you to pre-configure emails and transports, and then send those emails at runtime.

null 82 Jan 16, 2022
Queue, preview and and send emails stored in the database.

Codeigniter4 email queue Queue, preview and and send emails stored in the database. This package provides an interface for creating emails on the fly

null 3 Apr 12, 2022
Fetch is a library for reading email and attachments, primarily using the POP and IMAP protocols

Fetch Fetch is a library for reading email and attachments, primarily using the POP and IMAP protocols. Installing N.b. A note on Ubuntu 14.04 (probab

Tedious Developments 501 Jan 4, 2023
📧 Handy email creation and transfer library for PHP with both text and MIME-compliant support.

?? Handy email creation and transfer library for PHP with both text and MIME-compliant support.

Nette Foundation 401 Dec 22, 2022
SendPortal - Open-source self-hosted email marketing, subscriber and list management, email campaigns and more

SendPortal includes subscriber and list management, email campaigns, message tracking, reports and multiple workspaces/domains in a modern, flexible and scalable application.

Mettle 1.2k Jan 4, 2023
Cypht: Lightweight Open Source webmail written in PHP and JavaScript

All your E-mail, from all your accounts, in one place. Cypht is not your father's webmail. Unless you are one of my daughters, in which case it is your father's webmail. Cypht is like a news reader, but for E-mail. Cypht does not replace your existing accounts - it combines them into one. And it's also a news reader.

Jason Munro 773 Dec 30, 2022
Provides a clean and simple way to configure the WordPress-bundled PHPMailer library, allowing you to quickly get started sending mail through a local or cloud based service of your choice

WP PHPMailer provides a clean and simple way to configure the WordPress-bundled PHPMailer library, allowing you to quickly get started sending mail through a local or cloud based service of your choice.

Itineris Limited 61 Dec 6, 2022
Omnisend: Ecommerce Email Marketing and SMS Platform

Omnisend Omnisend: Ecommerce Email Marketing and SMS Platform Version v1.x Support all PHP Version >=5.6 v2.x Support all PHP Version >=7.0 Installati

Hung Nguyen 3 Jan 6, 2022
Laravel IMAP is an easy way to integrate both the native php-imap module and an extended custom imap protocol into your Laravel app.

Laravel IMAP is an easy way to integrate both the native php-imap module and an extended custom imap protocol into your Laravel app. This enables your app to not only respond to new emails but also allows it to read and parse existing mails and much more.

null 530 Jan 6, 2023
EMAIL, PASSWORD AND USERNAME GENERATOR

Email-Generator EMAIL, PASSWORD AND USERNAME GENERATOR Install on desktop : Install XAMPP Added environment variable system path => C:\xampp\php downl

Alex 2 Jan 8, 2022
Stampie is a simple API Wrapper for different email providers such as Postmark and SendGrid.

Stampie Stampie is a simple API Wrapper for different email providers such as Postmark and SendGrid. It is very easy to use and to integrate into your

Stampie 289 Dec 5, 2022
PHPMailer – A full-featured email creation and transfer class for PHP

PHPMailer – A full-featured email creation and transfer class for PHP Features Probably the world's most popular code for sending email from PHP! Used

PHPMailer 19.1k Jan 2, 2023
Laravel mailer which will catch all the sent emails and show them on an application view.

Laravel Web Mailer This package contains a web mailer which will catch all the sent emails. Then, you can view it visiting the route /web-inbox. The e

Creagia 54 Dec 16, 2022
A ready-to-use PHP script for sending Emails with an HTML Template will use a Gmail account as the sender and you will not need any email server. Powered by PHPMailer.

Gmail Email Sender by PHP A ready-to-use PHP script for sending Emails with an HTML Template will use a Gmail account as the sender and you will not n

Max Base 4 Oct 29, 2022
Create and validate signed URLs with a limited lifetime

This package can create URLs with a limited lifetime. This is done by adding an expiration date and a signature to the URL.

Spatie 349 Dec 31, 2022
Create CloudFront signed URLs in Laravel 6+

Easy to use Laravel 6+ wrapper around the official AWS PHP SDK which allows to sign URLs to access Private Content through CloudFront CDN

Dreamonkey S.r.l. 45 Dec 31, 2022
Michael Pratt 307 Dec 23, 2022
Laravel package a helper to Generate the QR code and signed it for ZATCA E-invoicing

Laravel ZATCA E-invoicing Introduction Laravel package a helper to Generate the QR code and signed it for ZATCA E-invoicing Installation To get the la

Ayman Alaiwah 8 Aug 17, 2022
laminas-memory manages data in an environment with limited memory

Memory objects (memory containers) are generated by the memory manager, and transparently swapped/loaded when required.

Laminas Project 5 Jul 26, 2022
zend-memory manages data in an environment with limited memory

Memory objects (memory containers) are generated by the memory manager, and transparently swapped/loaded when required.

Zend Framework 16 Aug 29, 2020