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

vz-jwt-authentication

v0.0.14

Published

Module extending the jwt-simple token verification.

Downloads

3

Readme

#vz-jwt-authentication

Package used for validating and refreshing JWT tokens when they only have 1/3 of their lifetime left. It will make sure that all the calls made will be authenticated, and creates 2 routes '/vz-jwt-auth/check/:returnUrl' and '/vz-jwt-auth/initialize'. The check route allows for checking if a user is still logged in and if not, it will return a 401 response with a Location header to which the client needs to redirect. The location will consist of the ssoUrl present in the config of the package (corresponding to the set NODE_ENV) to which a returnUrl will be appended. The returnUrl will consist of the clientAppProtocol://clientAppHostName[:clientAppPortNumber]/#parsed req.params.returnUrl The initialize route, provides the xsrf token and the user name if the token validation is successfull.

Install

$npm install vz-jwt-authentication

Usage

Express Router:

var jwtauth = require('vz-jwt-authentication'),
    express  = require('express'),
    app      = express();     
jwtauth.initialize(app, 'clientAppProtocol','clientAppHostName',['clientAppPortNumber');

Angular App:

 $rootScope.$on('$stateChangeStart', function (event, toState, toParams) {
                if(toState.name !='initialState') {
                    authentication.do(toState.name);
                }
            });
            

Authentication Service:

 define (['./services'], function(module){
         module.factory('authentication', function($http){
         function authenticate(returnUrl) {
             $http.get('/Vz-Jwt-Auth/Check/'+ returnUrl).then(function(response){
                 console.log('Authentication succeeded for user \'' + response.data.user.username + '\'');
             });
         }
         return { do: authenticate};
     });
 });

Get XSRF Token and User Name Service:

define(['./services'], function(module) {
module.factory('getXsrfTokenAndUser', function($http, $window){
    this.initialize = function (){
        $http.get('/Vz-Jwt-Auth/Initialize').then(function(response){
                $window.localStorage['user'] = response.data.user;
                $window.localStorage['xsrfToken'] = response.data.xsrfToken;
            });
    };
    return this;
});
});

Interceptors:

define([
    './interceptors',
    'lodash'
], function (module, _) {
    module.factory('JWTInterceptor', function ($q, $window) {
            return {
                request: function (request) {
                    if ($window.localStorage['xsrfToken']) {
                        request.headers.xsrfToken = $window.localStorage['xsrfToken'];
                    }
                    return request || $q.when(request);

                },
                response: function (response) {
                    if (response.status === 200 && response.data.user) {
                        $window.localStorage['user'] = response.data.user;
                    }
                    return response || $q.when(response);
                },
                responseError: function (rejection) {
                    var bodyArray = ["401 - xsrf attack.", "401 - Missing token.", "401 - Expired token.", "401 - Invalid token."];
                    if (rejection.status === 401 && bodyArray.indexOf(rejection.data) > -1) {
                        //check for
                        console.log("Response Error 401", rejection);
                        $window.location.href = rejection.headers('Location');
                    }
                    return $q.reject(rejection);
                }
            }
        }
    )
});

Prerequisites

The module checks the request.cookies.jwtToken it also verifies that the xsrfToken provided in the request header is the same as the xsrfToken in the decoded jwt token. To validate the signature, it expects a certificate called PublicKey.cer in the root folder of your application. The certificate needs to contain a base64 encoded public key. If the validation of the token fails, the middleware will return 401 response with the following content: "401 - Missing token.", "401 - Invalid token.", "401 - Expired token.", "401 - xsrf attack."