Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typings #64

Merged
merged 3 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 5.1.0

-[#64](https://github.com/okta/okta-oidc-middleware/pull/64) Added type declarations

# 5.0.1

-[#60](https://github.com/okta/okta-oidc-middleware/pull/60) chore: dependency updates
Expand Down
27 changes: 23 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@
"auth"
],
"files": [
"src"
"src",
"types"
],
"main": "index.js",
"types": "types/index.d.ts",
"scripts": {
"prepare": "yarn build",
"build": "node scripts/build.js",
"clean": "rm -rf dist && rm -rf reports",
"banners": "node util/maintain-banners.mjs",
"lint": "eslint .",
"lint:report": "eslint -f checkstyle -o ./reports/lint/eslint-checkstyle-result.xml .",
"test": "yarn lint && yarn test:unit && yarn test:e2e && yarn test:integration",
"test": "yarn lint && yarn test:types && yarn test:unit && yarn test:e2e && yarn test:integration",
"pretest:e2e": "yarn kill:port",
"posttest:e2e": "yarn kill:port",
"test:e2e": "wdio run wdio.conf.js",
"test:integration": "scripts/tck.sh 0.4.0",
"test:types": "tsd",
"test:unit": "jest",
"start": "node test/e2e/harness/start-server.js",
"start:custom": "node test/e2e/harness/start-custom-login-server.js",
Expand All @@ -38,9 +41,11 @@
},
"license": "Apache-2.0",
"dependencies": {
"@types/csurf": "^1.11.2",
"@types/express": "^4.17.17",
"@okta/configuration-validation": "^0.4.1",
"body-parser": "^1.20.1",
"csurf": "^1.9.0",
"csurf": "^1.11.0",
"express": "^4.18.2",
"lodash": "^4.17.21",
"negotiator": "^0.6.1",
Expand Down Expand Up @@ -75,13 +80,27 @@
"server-destroy": "^1.0.1",
"shelljs": "0.8.5",
"supertest": "^6.3.3",
"wdio-wait-for": "^2.2.6"
"wdio-wait-for": "^2.2.6",
"tsd": "^0.25.0",
"typescript": "^4.1.5"
},
"resolutions": {
"webdriver-manager": "^12.1.4",
"strip-ansi": "^6.0.1",
"ua-parser-js": "^1.0.33",
"**/recursive-readdir/minimatch": "^3.1.2",
"**/globule/minimatch": "^3.1.2"
},
"tsd": {
"directory": "test/types",
"compilerOptions": {
"skipLibCheck": true,
"esModuleInterop": true,
"paths": {
"@okta/oidc-middleware": [
"."
]
}
}
}
}
5 changes: 5 additions & 0 deletions scripts/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ if ! yarn lint:report; then
exit ${TEST_FAILURE}
fi

if ! yarn test:types; then
echo "test types failed! Exiting..."
exit ${TEST_FAILURE}
fi

echo ${TEST_SUITE_TYPE} > ${TEST_SUITE_TYPE_FILE}
echo ${TEST_RESULT_FILE_DIR} > ${TEST_RESULT_FILE_DIR_FILE}
exit ${PUBLISH_TYPE_AND_RESULT_DIR_BUT_SUCCEED_IF_NO_RESULTS}
1 change: 1 addition & 0 deletions scripts/travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ if [ "${TRAVIS_EVENT_TYPE}" = "cron" ] ; then
else
# run the lint, unit tests
yarn lint
yarn test:types
yarn test:unit
fi
117 changes: 117 additions & 0 deletions test/types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { ExpressOIDC } from '../..';
import express, { Express, RequestHandler, ErrorRequestHandler } from 'express';
import {expectType, expectError, expectAssignable, expectNotAssignable} from 'tsd';


// Constructor with required options
expectError(new ExpressOIDC());
expectError(new ExpressOIDC({}));
const minimumConfig: ExpressOIDC.ConfigurationOptions = {
client_id: 'foo',
client_secret: 'foo',
issuer: 'https://okta.foo',
appBaseUrl: 'https://app.foo',
};
const oidc = new ExpressOIDC(minimumConfig);
expectType<ExpressOIDC>(oidc);

// Constructor with extended options
const extendedConfig: ExpressOIDC.ConfigurationOptions = {
...minimumConfig,
loginRedirectUri: 'http://localhost:8080/authorization-code/callback',
logoutRedirectUri: 'http://localhost:8080/',
scope: 'openid profile',
response_type: 'code',
maxClockSkew: 120,
timeout: 10000,
sessionKey: 'oidc:https://okta.foo',
testing: {
disableHttpsCheck: true
},
};
expectType<ExpressOIDC>(new ExpressOIDC(extendedConfig));

// Constructor with routes
const configWithRoutes: ExpressOIDC.ConfigurationOptions = {
...minimumConfig,
routes: {
login: {
path: '/different/login',
viewHandler: (req, res, _next) => {
// `req.csrfToken()` is available from 'csurf' package
res.render('login', {
csrfToken: req.csrfToken(),
baseUrl: 'https://okta.foo'
});
}
},
loginCallback: {
path: '/different/callback',
afterCallback: '/profile',
failureRedirect: '/error',
handler: ((req, _res, next) => {
// `req.userContext` is available by using 'passport' with 'openid-client' strategy
console.log('email: ', req.userContext!.userinfo.email);
console.log('scope: ', req.userContext!.tokens.scope);
next();
}) as RequestHandler,
},
logout: {
path: '/different/logout'
},
logoutCallback: {
path: '/different/logout-callback'
}
}
};

// Use router
const app: Express = express();
app.use(oidc.router);

// Ready event handler
oidc.on('ready', () => {
app.listen(3000, () => console.log('app started'));
});

// Error event handler
oidc.on('error', err => {
// An error occurred while setting up OIDC, during token revokation, or during post-logout handling
if (err instanceof ExpressOIDC.OIDCMiddlewareError) {
console.error(err.type, err.message);
} else {
console.error(err.message);
}
});

// OIDCMiddlewareError
const err = new ExpressOIDC.OIDCMiddlewareError('middlewareError', 'Your custom callback handler must request "next"');
expectType<ExpressOIDC.OIDCMiddlewareError>(err);
expectAssignable<Error>(err);
expectType<string>(err.message);
expectType<string>(err.type);
expectType<'OIDCMiddlewareError'>(err.name);

// ensureAuthenticated
app.get('/protected', oidc.ensureAuthenticated(), (_req, res) => {
res.send('Protected stuff');
});
// ensureAuthenticated with options
app.get('/protected', oidc.ensureAuthenticated({
redirectTo: '/login',
loginHint: '[email protected]'
}), (_req, res) => {
res.send('Protected stuff');
});

// forceLogoutAndRevoke
app.post('/forces-logout', oidc.forceLogoutAndRevoke());

// `req.userContext` is available by using 'passport' with 'openid-client' strategy
app.get('/', (req, res) => {
if (req.userContext) {
res.send(`Hello ${req.userContext.userinfo.sub}!`);
} else {
res.send('Hello World!');
}
});
Loading