GithubHelp home page GithubHelp logo

kamerk22 / adonis-url-signer Goto Github PK

View Code? Open in Web Editor NEW
11.0 5.0 2.0 98 KB

Create and validate signed URLs with a limited lifetime in AdonisJs ๐Ÿ”

License: MIT License

JavaScript 100.00%
adonisjs adonisjs-framework pacakge mail security url-signature nodejs

adonis-url-signer's Introduction

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.

adonis-url-signer's People

Contributors

dependabot[bot] avatar jericopulvera avatar kamerk22 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

adonis-url-signer's Issues

Expiration time measure unit.

Hello, i've implemented this tool in order to set a time expiration when a user requests a reset password link and seems to work just fine.

But i would like the URL lasted only 5 minutes instead.

Do i have to change the code or is it possible to pass a decimal number like :
.17
.08
or so?

Thanks in advance and also thanks for this tool.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.