Giter VIP home page Giter VIP logo

node-cas-sfu's Introduction

node-cas-sfu

node-cas-sfu is a CAS client for Node.js, tailored to work with Simon Fraser University's CAS implementation.

Features

node-cas-sfu supports CAS version 2 and should work with a vanilla CAS installation, though this has not been tested. It also supports SFU-specific extensions to CAS, such as:

  • the allow string
  • using non-SFU "Apache" accounts

Usage

var CAS = require('cas-sfu');
var cas = new CAS({
    service: 'http://www.sfu.ca/myapp',     // REQUIRED; CAS will redirect back to this URL after a successful authentication
    allow: '!basket-weaving',                        // OPTIONAL; comma-delimited string. Defaults to "sfu". See http://i.sfu.ca/MWkAlX for a full list of allow options
    userObject: 'AUTH_USER'                 // OPTIONAL; object where the CAS response will be stored. Defaults to 'authenticatedUser'
});

// validate a ticket:
cas.validate(ticket, function(err, loggedIn, casResponse) {
    if (loggedIn) {
        console.log("Hello, you are logged in as %s", casResponse.user);
    } else {
        console.log("You are not logged in.");
    }
});

Several options are provided as defaults in the module but these can be overridden (e.g. to authenticate against cas-test.sfu.ca instead of cas.sfu.ca):

defaults = {
        casHost: 'https://cas.sfu.ca',
        casBasePath: '/cgi-bin/WebObjects/cas.woa',
        loginPath: '/wa/login',
        logoutPath: '/wa/logout',
        validatePath: '/wa/servicevalidate',
        appLogoutPath: '/wa/applogout'
};

You can override any of these by providing your own values in the options object you pass to new CAS() or CAS#getMiddleware()

Once a user has logged in, you will have an object containing information about the user (essentially, a JSON representation of the XML document returned by CAS). This object will be in either req.authenticatedUser or req.session.authenticatedUser (where authenticatedUser is whatever name you provided to the userObject option when you initialized the CAS client; authenticatedUser is the default). For example, if you initialized CAS with allow: '!list-1,!list-2' and log in as user kipling, who is a member of "list-2", the authenticatedUser object will be:

{
    user: 'kipling',
    authtype: 'sfu',
    maillist: 'list-2',
}

Usage in Connect/Express

node-cas-sfu exposes a getMiddleware function to provide Express middleware:

var cas = require('cas-sfu');
var casauth = cas.getMiddleware({
    service: 'http://www.sfu.ca/myapp',
    allow: '!basket-weaving',
    userObject: 'AUTH_USER'
});

var loggedin = function(req, res, next) {
    if ((req.session && req.session.AUTH_USER) || req.AUTH_USER) {
        next();
        return;
    }
    req.session.referer = req.url;  // store the referrer so we can send them back there after logging in
    res.redirect('/login');
};

app.get('/secretstuff', loggedin, function(req, res) {
    res.send('Hello, ' + req.session.AUTH_USER.user);       // Hello, kipling
});

app.get('/login', casauth, function(req, res) {
    req.session.AUTH_USER.logindate = new Date();
    console.log(req.session.AUTH_USER);
    res.redirect(req.session.referer || '/');
});

You may find it useful to have the logged-in user's username appear in your Express log, similar to how AUTH_USER or REMOTE_USER appears in the default Apache common log. You can do this by defining a custom Express log token and a custom log format:

app.configure(function() {
    express.logger.token('user', function(req, res) {
        var user = '-';
        if (req.session && req.session.AUTH_USER) {
            user = req.session.AUTH_USER.user;
        }
        return user;
    });
    app.use(express.logger({format: ':remote-addr - :user [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"'}));
});

No user logged in:

127.0.0.1 - - [Thu, 19 Apr 2012 01:06:45 GMT] "GET /protected HTTP/1.1" 302 - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1096.1 Safari/536.6"

User logged in:

127.0.0.1 - kipling [Thu, 19 Apr 2012 01:06:59 GMT] "GET /favicon.ico HTTP/1.1" 404 - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1096.1 Safari/536.6"

Using non-SFU (aka Apache) accounts

SFU's implementation of CAS allows users to authenticate with made-up, non-SFU accounts. These are often referred to as "Apache accounts" as they are most commonly used in Apache .htpasswd files via the mod_auth_cas Apache module. node-cas-sfu also supports Apache accounts; you can use them by setting allow=apache and including an apacheUsers object containing username & password hash pairs:

var CAS = require('cas-sfu');
var cas = new CAS({
    service: 'http://www.sfu.ca/myapp',
    allow: 'apache',
    userObject: 'AUTH_USER',
    apacheUsers: {"myfakeuser": "ubjQPM.hh9Qj2"}
});

Passwords can be any of UNIX crypt, SHA1, Apache MD5 or even plain text (but really, don't do plain text). node-cas-sfu uses the pass module to validate hashes.

Tests

Run node test.js or npm test to run the tests. The test script will prompt you for a valid SFU username & password (not recorded or stored anywhere) and will use those credentials to log into CAS via the REST interface, obtain a Service Ticket and attempt to validate that ticket. It will also attempt the same using a made-up "Apache" account (myfakeuser:pencil69).

License

Copyright (C) 2012 Simon Fraser University

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.

node-cas-sfu's People

Contributors

grahamb avatar stuartf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

node-cas-sfu's Issues

centos 5.8 npm install failed

Error log:
gyp ERR! build error
gyp ERR! stack Error: make failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/usr/local/node/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:267:23)
gyp ERR! stack at ChildProcess.emit (events.js:110:17)
gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:1067:12)
gyp ERR! System Linux 2.6.18-308.el5
gyp ERR! command "node" "/usr/local/node/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /usr/local/ncode/nodejs2/node_modules/cas-sfu/node_modules/xml2json/node_modules/node-expat
gyp ERR! node -v v0.12.0
gyp ERR! node-gyp -v v1.0.2
gyp ERR! not ok
npm ERR! Linux 2.6.18-308.el5
npm ERR! argv "/usr/local/node/bin/node" "/usr/sbin/npm" "install" "[email protected]"
npm ERR! node v0.12.0
npm ERR! npm v2.5.1
npm ERR! code ELIFECYCLE

npm ERR! [email protected] install: node-gyp rebuild
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script 'node-gyp rebuild'.
npm ERR! This is most likely a problem with the node-expat package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node-gyp rebuild
npm ERR! You can get their info via:
npm ERR! npm owner ls node-expat
npm ERR! There is likely additional logging output above.

not able to install cas-sfu in Windows 7

Hi,

I am trying to start the server using 'node app | bunyan', when I do that, it throws the following error

E:\oae\Hilary>node app | bunyan
[2013-08-26T08:45:15.322Z] INFO: oae-modules/4780 on chinna-PC: Initialized mod
ule oae-logger
[2013-08-26T08:45:15.353Z] INFO: oae-modules/4780 on chinna-PC: Initialized mod
ule oae-telemetry
[2013-08-26T08:45:15.636Z] INFO: oae-modules/4780 on chinna-PC: Initialized mod
ule oae-tenants
[2013-08-26T08:45:16.347Z] INFO: oae-modules/4780 on chinna-PC: Initialized mod
ule oae-config
[2013-08-26T08:45:19.693Z] ERROR: oae/4780 on chinna-PC: An uncaught exception w
as raised to the application.
Error: Cannot find module 'cas-sfu'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object. (E:\oae\Hilary\node_modules\oae-authentication\lib
\strategies\cas\strategy.js:16:11)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object. (E:\oae\Hilary\node_modules\oae-authentication\lib
\strategies\cas\init.js:28:19)
[2013-08-26T08:45:22.330Z] ERROR: oae-cleaner/4780 on chinna-PC: Could not list
the files. (directory=C:\Users\CHINNA1.CHI\AppData\Local\Temp\oae)
Error: ENOENT, readdir 'C:\Users\CHINNA
1.CHI\AppData\Local\Temp\oae'
^CTerminate batch job (Y/N)?

E:\oae\Hilary>

So , when I try to install the module separately , it throws the following error. COULD YOU PLEASE HEMLP ON THAT? THANKS IN ADVANCE.

E:\oae\cas-sfu>npm install -d
npm info it worked if it ends with ok
npm info using [email protected]
npm info using [email protected]
npm info preinstall [email protected]
npm info trying registry request attempt 1 at 13:05:58
npm http GET https://registry.npmjs.org/xml2json/0.3.2
npm http 304 https://registry.npmjs.org/xml2json/0.3.2
npm info install [email protected] into E:\oae\cas-sfu
npm info installOne [email protected]
npm info E:\oae\cas-sfu\node_modules\xml2json unbuild
npm info preinstall [email protected]
npm info trying registry request attempt 1 at 13:06:02
npm http GET https://registry.npmjs.org/node-expat/2.0.0
npm http 304 https://registry.npmjs.org/node-expat/2.0.0
npm info install [email protected] into E:\oae\cas-sfu\node_modules\xml2json
npm info installOne [email protected]
npm info E:\oae\cas-sfu\node_modules\xml2json\node_modules\node-expat unbuild
npm info preinstall [email protected]
npm info build E:\oae\cas-sfu\node_modules\xml2json\node_modules\node-expat
npm info linkStuff [email protected]
npm info install [email protected]

[email protected] install E:\oae\cas-sfu\node_modules\xml2json\node_modules\nod
e-expat
node-gyp rebuild

E:\oae\cas-sfu\node_modules\xml2json\node_modules\node-expat>node "E:\nodejs\nod
e_modules\npm\bin\node-gyp-bin....\node_modules\node-gyp\bin\node-gyp.js" reb
uild
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | win32 | ia32
gyp ERR! configure error
gyp ERR! stack Error: spawn ENOENT
gyp ERR! stack at errnoException (child_process.js:980:11)
gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:771:
34)
gyp ERR! System Windows_NT 6.1.7600
gyp ERR! command "node" "E:\nodejs\node_modules\npm\node_modules\node-gyp
bin\node-gyp.js" "rebuild"
gyp ERR! cwd E:\oae\cas-sfu\node_modules\xml2json\node_modules\node-expat
gyp ERR! node -v v0.10.17
gyp ERR! node-gyp -v v0.10.9
gyp ERR! not ok
npm info [email protected] Failed to exec install script
npm info E:\oae\cas-sfu\node_modules\xml2json\node_modules\node-expat unbuild
npm info preuninstall [email protected]
npm info uninstall [email protected]
npm info postuninstall [email protected]
npm info E:\oae\cas-sfu\node_modules\xml2json unbuild
npm info preuninstall [email protected]
npm info uninstall [email protected]
npm info postuninstall [email protected]
npm ERR! weird error 1
npm ERR! not ok code 0

E:\oae\cas-sfu>
E:\oae\cas-sfu>npm install -d
npm info it worked if it ends with ok
npm info using [email protected]
npm info using [email protected]
npm info preinstall [email protected]
npm info trying registry request attempt 1 at 13:15:13
npm http GET https://registry.npmjs.org/xml2json/0.3.2
npm http 304 https://registry.npmjs.org/xml2json/0.3.2
npm info install [email protected] into E:\oae\cas-sfu
npm info installOne [email protected]
npm info E:\oae\cas-sfu\node_modules\xml2json unbuild
npm info preinstall [email protected]
npm info trying registry request attempt 1 at 13:15:17
npm http GET https://registry.npmjs.org/node-expat/2.0.0
npm http 304 https://registry.npmjs.org/node-expat/2.0.0
npm info install [email protected] into E:\oae\cas-sfu\node_modules\xml2json
npm info installOne [email protected]
npm info E:\oae\cas-sfu\node_modules\xml2json\node_modules\node-expat unbuild
npm info preinstall [email protected]
npm info build E:\oae\cas-sfu\node_modules\xml2json\node_modules\node-expat
npm info linkStuff [email protected]
npm info install [email protected]

[email protected] install E:\oae\cas-sfu\node_modules\xml2json\node_modules\nod
e-expat
node-gyp rebuild

E:\oae\cas-sfu\node_modules\xml2json\node_modules\node-expat>node "E:\nodejs\nod
e_modules\npm\bin\node-gyp-bin....\node_modules\node-gyp\bin\node-gyp.js" reb
uild
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | win32 | ia32
gyp ERR! configure error
gyp ERR! stack Error: spawn ENOENT
gyp ERR! stack at errnoException (child_process.js:980:11)
gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:771:
34)
gyp ERR! System Windows_NT 6.1.7600
gyp ERR! command "node" "E:\nodejs\node_modules\npm\node_modules\node-gyp
bin\node-gyp.js" "rebuild"
gyp ERR! cwd E:\oae\cas-sfu\node_modules\xml2json\node_modules\node-expat
gyp ERR! node -v v0.10.17
gyp ERR! node-gyp -v v0.10.9
gyp ERR! not ok
npm info [email protected] Failed to exec install script
npm info E:\oae\cas-sfu\node_modules\xml2json\node_modules\node-expat unbuild
npm info preuninstall [email protected]
npm info uninstall [email protected]
npm info postuninstall [email protected]
npm info E:\oae\cas-sfu\node_modules\xml2json unbuild
npm info preuninstall [email protected]
npm info uninstall [email protected]
npm info postuninstall [email protected]
npm ERR! weird error 1
npm ERR! not ok code 0

E:\oae\cas-sfu>

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.