Skip to content

Commit

Permalink
Add typescript test file
Browse files Browse the repository at this point in the history
  • Loading branch information
vesse committed Jun 13, 2017
1 parent 7b2f35b commit 407e03e
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ npm-debug.log
.directory
.idea/
out
tmp
test/typescript/*.js*
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,31 @@
"dependencies": {
"@types/node": "^7.0.23",
"@types/passport": "^0.3.3",
"ldapauth-fork": "^4.0.0",
"ldapauth-fork": "^4.0.1",
"passport-strategy": "^1.0.0"
},
"devDependencies": {
"@types/basic-auth": "^1.1.1",
"@types/bunyan": "^1.8.0",
"@types/express": "^4.0.35",
"basic-auth": "^1.1.0",
"body-parser": "^1.17.2",
"bunyan": "^1.8.10",
"chai": "^4.0.1",
"eslint": "^3.19.0",
"eslint": "^4.0.0",
"express": "^4.15.3",
"ldapjs": "^1.0.1",
"mocha": "^3.4.2",
"passport": "^0.3.2",
"supertest": "^3.0.0",
"typescript": "^2.3.4",
"watch": "^1.0.2"
},
"scripts": {
"prepublish": "npm run lint",
"lint": "eslint ./lib",
"lint:watch": "watch 'npm run lint' ./lib --wait 0.5",
"test": "NODE_PATH=lib mocha --reporter spec test/*-test.js"
"test": "NODE_PATH=lib mocha --reporter spec test/*-test.js",
"test:typescript": "cd test/typescript && tsc"
}
}
91 changes: 91 additions & 0 deletions test/typescript/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Just a test file that should compile properly with tsc
*/
import * as Passport from 'passport';
import * as LdapStrategy from '../../';
import * as Express from 'express';
import { Request } from 'express';
import * as Logger from 'bunyan';
import * as BasicAuth from 'basic-auth';

const app = Express();

interface User {
dn: string;
foo?: boolean;
}

const user: User = { dn: 'test' };

const log = new Logger({
name: 'ldap',
component: 'client',
stream: process.stderr,
level: 'trace'
});

const options: LdapStrategy.Options = {
server: {
url: 'ldap://ldap.forumsys.com:389',
bindDN: 'cn=read-only-admin,dc=example,dc=com',
bindCredentials: 'password',
searchBase: 'dc=example,dc=com',
searchFilter: '(uid={{username}})',
log: log,
cache: true,
includeRaw: true,
groupSearchFilter: '(member={{dn}})',
groupSearchBase: 'dc=example,dc=com'
},
credentialsLookup: BasicAuth
}

const optionsAsFunction: LdapStrategy.OptionsFunction = (req: Request, callback: LdapStrategy.OptionsFunctionCallback) => {
callback(null, options);
}

const regularCallback: LdapStrategy.VerifyCallback = (user: User, callback: LdapStrategy.VerifyDoneCallback) => {
if (user.foo) {
callback(new Error('Foo user is an error'), null, { message: 'Foo user' });
} else if (!user) {
callback(null, false, { message: 'No user' });
} else {
callback(null, user);
}
}

const reqCallback: LdapStrategy.VerifyCallbackWithRequest = (req: Request, user: User, callback: LdapStrategy.VerifyDoneCallback) => {
if (user.foo) {
callback(new Error('Foo user is an error'), null, { message: 'Foo user' });
} else if (!user) {
callback(null, false, { message: 'No user' });
} else {
callback(null, user);
}
}

const credentialsLookup: LdapStrategy.CredentialsLookup = (req: Request): LdapStrategy.CredentialsLookupResult => ({
user: 'username',
pass: 'password'
});

Passport.serializeUser((user: User, done) => done(null, user.dn));
Passport.deserializeUser((dn, done) => done(null, user));

Passport.use(new LdapStrategy(options, regularCallback));
Passport.use('withreq', new LdapStrategy(options, reqCallback));
Passport.use('dynopts', new LdapStrategy(optionsAsFunction));

const authOpts: LdapStrategy.AuthenticateOptions = {
badRequestMessage: 'Bad request you did there'
}

app.post('/login', Passport.authenticate('ldapauth', authOpts));

app.post('/login', (req, res, next) => {
Passport.authenticate('ldapauth', (err: Error, user: User) => {
req.logIn(user, (err: Error) => {
res.send({ok: 1});
})
})(req, res, next);
});

0 comments on commit 407e03e

Please sign in to comment.