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

xcas

v0.1.0

Published

Extended Central Authentication Service (CAS) client for Node.js

Downloads

9

Readme

cas

Central Authentication Service (CAS) client for Node.js

This module handles CAS authentication (with support for proxies and extended attributes), and can also transparently redirect a web page if needed. The ticket validation step is available as its own function for those who wish to handle things manually. Proxied requests can be made through a function call, or optionally through an HTTP request with custom headers. Single sign out is also supported with Express/Connect.

To start the login process manually, send your users to: https://cas_base_url/login?service=url_to_handle_ticket_validation. In the University of Waterloo example below, this url would be: https://cas.uwaterloo.ca/cas/login?service='my_service'.

Or if you are using standard HTTP req/res objects for a web page, you may use the provided authenticate() function to handle the redirection automatically.

It is also possible to use this as a standalone CAS proxy server.

Installation

via npm:

$ npm install cas

Usage

Setup:

    var CAS = require('cas');
    var cas = new CAS({
        base_url: 'https://cas.uwaterloo.ca/cas', 
        service: 'my_service',
        version: 2.0
    });

Using it in a login route:

    exports.cas_login = function(req, res) {
      var ticket = req.param('ticket');
      if (ticket) {
        cas.validate(ticket, function(err, status, username) {
          if (err) {
            // Handle the error
            res.send({error: err});
          } else {
            // Log the user in
            res.send({status: status, username: username});
          }
        });
      } else {
        res.redirect('/');
      }
    };

Using the auto redirect authentication:

    exports.cas_login = function(req, res) {
      cas.authenticate(req, res, function(err, status, username, extended) {
        if (err) {
          // Handle the error
          res.send({error: err});
        } else {
          // Log the user in 
          res.send({status: status, username: username, attributes: extended.attributes});
        }
      });    
    }

Longer example with CAS proxy (also see the wiki):

    var fs = require('fs');
    var http = require('http');

    // Initialize CAS
    var CAS = require('cas');
    var cas = new CAS({
        base_url: 'https://cas.uwaterloo.ca/cas',
        version: 2.0,
        
        proxy_server: true,
        proxy_server_port: 0, // disable inbound external proxy connections
        proxy_server_key: fs.readFileSync('/path/to/private_key.pem'),
        proxy_server_cert: fs.readFileSync('/path/to/ssl_cert.pem'),
        
        // CAS server will connect to this. It must be accessible on the
        // public internet.
        proxy_callback_host: 'my-public-domain.example.com',
        proxy_callback_port: 8989
    });
    
    // Main web server
    var server = http.createServer();
    server.addListener('request', function(req, res) {

        var ip = req.connection.remoteAddress
             || req.socket.remoteAddress 
             || req.connection.socket.remoteAddress;
        
        cas.authenticate(req, res, function(err, status, username, extended) {
            if (err) {
                res.end(err.message);
                return;
            }
            
            // At this point the user has been authenticated. In a real web
            // framework you would want to use sessions to track the info.
            
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write('<div style="border:solid 1px black; padding:1em; margin:1em;">');
            res.write('<p>Welcome ' + username + '. Your IP address is ' + ip + '.</p>');
            res.write('<p>You are here: <b>http://' + req.headers.host + req.url + '</b></p>');
            
            // CAS server should return a PGTIOU since we specified a proxy
            var pgtIOU = extended['PGTIOU'];
            if (pgtIOU) {
                res.write('<p>');
                res.write('Your PGTIOU for this session is: ' + pgtIOU + '<br/>');
                res.write('Your web framework should keep track of this if it wants to use CAS proxied services on your behalf.<br/>');
                res.write('</p>');
            
                // For testing the proxy function at "/proxyDirect"
                if (req.url.match(/proxyDirect/)) {
                    res.write('Starting direct proxy request...<br/>');
                    
                    // Fetch http://localhost:8080/test through the proxy on user's behalf
                    cas.proxiedRequest(pgtIOU, {
                        protocol: 'http:',
                        method: 'GET',
                        hostname: 'localhost',
                        port: 8080,
                        pathname: '/test'
                    }, function(err, proxyReq, proxyRes) {
                        // Just display the proxied results directly
                        proxyRes.on('data', function(chunk) {
                            res.write(chunk);
                        });
                        proxyRes.on('end', function() {
                            res.write('Proxy Done<br/>');
                            res.write('</div>');
                            res.end();
                        });
                    });
                    
                    return;
                } // end of "/proxyDirect"
            }
            
            // Normal requests, such as to "/test"
            res.write('Done<br/>');
            res.write('</div>');
            res.end();
        
        });
        
    });
    server.listen(8080);

License

(The MIT License)

Copyright (c) 2011 Casey Banner <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.