npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

adonis-url-signer

v1.0.2

Published

URL signing provider for Adonis.js

Downloads

12

Readme

Create secured URLs with a limited lifetime in AdonisJs

JavaScript Style Guide Build Status Coverage Status

This library allows you to easily create "signed" URLs. These URLs have a "signature" hash appended as query string so we can easily verify that it's not manipulated.

For example, you might use signed URLs to implement "password reset link" which is valid for couple of hours only.

Install

adonis install adonis-url-signer

Setup

Register the urlSigner provider in start/app.js:

const providers = [
    ...
    'adonis-url-signer/providers/UrlSignerProvider'
]

Register aliases of provider in start/app.js:

const aliases = {
    ...
    UrlSigner: 'Adonis/UrlSigner'
}

Config

Configuration is defined inside a file config/urlSigner.js

const Env = use('Env')

module.exports = {
    signatureKey: Env.getOrFail('APP_KEY'),
    defaultExpirationTimeInHour: 24,
    options: {
        expires: 'expires',
        signature: 'signature'
    }
}

Usage

To create a signed URL, use the sign method of the UrlSigner.

const UrlSigner = use("UrlSigner");

return UrlSigner.sign("http://secured.domain/sign", { user: 1 })

// http://secured.domain/sign/?user=1&signature=xxxxxxxxxxx

You can also generate a temporary signed URL that expires, you may use the temporarySign method. You need to pass expiration in hour:

const UrlSigner = use("UrlSigner");

return UrlSigner.temporarySign('http://secured.domain/tempSigner', 24, { user : 1 });

// http://secured.domain/sign/?expiry=1553106418&user=1&signature=xxxxxxxxxxx

Validating Incoming Request

To verify incoming request has valid signature, you should call isValidSign method.

const UrlSigner = use("UrlSigner");

Route.get("/sign", ({ request }) => {
    const url = request.protocol() + "://" + request.header("host") + request.originalUrl();
    if (!UrlSigner.isValidSign(url)) {
        // 403 Forbridden
    }
    ...
});

Alternative you can also use signed middleware to validate incoming request. Register named middleware in start/kernel.js:

const namedMiddleware = {
    ...
    signed: 'Adonis/UrlSigner/Signed'
}

After registing middleware in you kernel, you may use that middleware in you route. If incoming request does not have a valid signature, the middleware will automatically throw an error 403 Forbridden: Access Denied:

const UrlSigner = use("UrlSigner");

Route.get("/sign", ({ request }) => {
    // ...
}).middleware("signed");

Tests

Tests are written using japa. Run the following commands to run tests.

npm run test:local

# report coverage
npm run test

# on windows
npm run test:win

Credits

Support

Having trouble? Open an issue!

License

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