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

Convert modules to standarized ECMAScript #570

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
14 changes: 8 additions & 6 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
import express from 'express'
import session from 'express-session'
import hogan from 'hogan-express'
import Keycloak from 'keycloak-connect'
import path from 'node:path'
import url from 'node:url'

const Keycloak = require('keycloak-connect')
const hogan = require('hogan-express')
const express = require('express')
const session = require('express-session')

const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
const app = express()

const server = app.listen(3000, function () {
Expand All @@ -29,7 +31,7 @@ const server = app.listen(3000, function () {

// Register '.mustache' extension with The Mustache Express
app.set('view engine', 'html')
app.set('views', require('path').join(__dirname, '/view'))
app.set('views', path.join(__dirname, '/view'))
app.engine('html', hogan)

// A normal un-protected public URL.
Expand Down
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"description": "Example page that demonstrates available keycloak functionality",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node index.js"
},
Expand Down
File renamed without changes.
10 changes: 6 additions & 4 deletions guides/securing-apps/nodejs-adapter.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ In the root directory of your project create a file called `server.js` and add t

[source,javascript]
----
const session = require('express-session');
const Keycloak = require('keycloak-connect');
import session from 'express-session';
import Keycloak from 'keycloak-connect';

const memoryStore = new session.MemoryStore();
const keycloak = new Keycloak({ store: memoryStore });
Expand Down Expand Up @@ -135,7 +135,8 @@ server-side state for authentication, you need to initialize the
session store that `express-session` is using.
[source,javascript]
----
const session = require('express-session');
import session from 'express-session';

const memoryStore = new session.MemoryStore();

// Configure session
Expand Down Expand Up @@ -169,7 +170,8 @@ then require Express in our project as outlined below:

[source,javascript]
----
const express = require('express');
import express from 'express';

const app = express();
----

Expand Down
31 changes: 14 additions & 17 deletions keycloak.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@
* the License.
*/

const BearerStore = require('./stores/bearer-store')
const CookieStore = require('./stores/cookie-store')
const SessionStore = require('./stores/session-store')

const Config = require('./middleware/auth-utils/config')
const GrantManager = require('./middleware/auth-utils/grant-manager')
const Setup = require('./middleware/setup')
const Admin = require('./middleware/admin')
const Logout = require('./middleware/logout')
const PostAuth = require('./middleware/post-auth')
const GrantAttacher = require('./middleware/grant-attacher')
const Protect = require('./middleware/protect')
const Enforcer = require('./middleware/enforcer')
const CheckSso = require('./middleware/check-sso')
import Admin from './middleware/admin.js'
import Config from './middleware/auth-utils/config.js'
import GrantManager from './middleware/auth-utils/grant-manager.js'
import CheckSso from './middleware/check-sso.js'
import Enforcer from './middleware/enforcer.js'
import GrantAttacher from './middleware/grant-attacher.js'
import Logout from './middleware/logout.js'
import PostAuth from './middleware/post-auth.js'
import Protect from './middleware/protect.js'
import Setup from './middleware/setup.js'
import BearerStore from './stores/bearer-store.js'
import CookieStore from './stores/cookie-store.js'
import SessionStore from './stores/session-store.js'

/**
* Instantiate a Keycloak.
Expand Down Expand Up @@ -58,7 +57,7 @@ const CheckSso = require('./middleware/check-sso')
* @return {Keycloak} A constructed Keycloak object.
*
*/
function Keycloak (config, keycloakConfig) {
export default function Keycloak (config, keycloakConfig) {
// If keycloakConfig is null, Config() will search for `keycloak.json`.
this.config = new Config(keycloakConfig)

Expand Down Expand Up @@ -426,5 +425,3 @@ Keycloak.prototype.redirectToLogin = function (request) {
Keycloak.prototype.getConfig = function () {
return this.config
}

module.exports = Keycloak
8 changes: 3 additions & 5 deletions middleware/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
'use strict'

const Token = require('./auth-utils/token')
const Signature = require('./auth-utils/signature')
import Token from './auth-utils/token.js'
import Signature from './auth-utils/signature.js'

function Admin (keycloak, url) {
this._keycloak = keycloak
Expand Down Expand Up @@ -99,7 +97,7 @@ function adminNotBefore (request, response, keycloak) {
})
}

module.exports = function (keycloak, adminUrl) {
export default function adminMiddleware (keycloak, adminUrl) {
let url = adminUrl
if (url[url.length - 1] !== '/') {
url = url + '/'
Expand Down
11 changes: 3 additions & 8 deletions middleware/auth-utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict'

const path = require('path')
const fs = require('fs')
import path from 'node:path'
import fs from 'node:fs'

/**
* Construct a configuration object.
Expand All @@ -31,7 +28,7 @@ const fs = require('fs')
*
* @constructor
*/
function Config (config) {
export default function Config (config) {
if (!config) {
config = path.join(process.cwd(), 'keycloak.json')
}
Expand Down Expand Up @@ -168,5 +165,3 @@ Config.prototype.configure = function configure (config) {
*/
this.verifyTokenAudience = resolveValue(config['verify-token-audience'] || config.verifyTokenAudience || false)
}

module.exports = Config
22 changes: 9 additions & 13 deletions middleware/auth-utils/grant-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict'

const URL = require('url')
const http = require('http')
const https = require('https')
const crypto = require('crypto')
const querystring = require('querystring')
const Grant = require('./grant')
const Token = require('./token')
const Rotation = require('./rotation')
import URL from 'node:url'
import http from 'node:http'
import https from 'node:https'
import crypto from 'node:crypto'
import querystring from 'node:querystring'
import Grant from './grant.js'
import Token from './token.js'
import Rotation from './rotation.js'

/**
* Construct a grant manager.
Expand All @@ -31,7 +29,7 @@ const Rotation = require('./rotation')
*
* @constructor
*/
function GrantManager (config) {
export default function GrantManager (config) {
this.realmUrl = config.realmUrl
this.clientId = config.clientId
this.secret = config.secret
Expand Down Expand Up @@ -536,5 +534,3 @@ const fetch = (manager, handler, options, params) => {
req.end()
})
}

module.exports = GrantManager
5 changes: 1 addition & 4 deletions middleware/auth-utils/grant.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict'

/**
* Construct a new grant.
Expand All @@ -30,7 +29,7 @@
*
* @constructor
*/
function Grant (grant) {
export default function Grant (grant) {
this.update(grant)
}

Expand Down Expand Up @@ -80,5 +79,3 @@ Grant.prototype.isExpired = function isExpired () {
}
return this.access_token.isExpired()
}

module.exports = Grant
13 changes: 5 additions & 8 deletions middleware/auth-utils/rotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict'
const URL = require('url')
const http = require('http')
const https = require('https')
const jwkToPem = require('jwk-to-pem')
import URL from 'node:url'
import http from 'node:http'
import https from 'node:https'
import jwkToPem from 'jwk-to-pem'

/**
* Construct a Rotation instance
Expand All @@ -26,7 +25,7 @@ const jwkToPem = require('jwk-to-pem')
*
* @constructor
*/
function Rotation (config) {
export default function Rotation (config) {
this.realmUrl = config.realmUrl
this.minTimeBetweenJwksRequests = config.minTimeBetweenJwksRequests
this.jwks = []
Expand Down Expand Up @@ -93,5 +92,3 @@ const nodeify = (promise, cb) => {
if (typeof cb !== 'function') return promise
return promise.then((res) => cb(null, res)).catch((err) => cb(err))
}

module.exports = Rotation
10 changes: 3 additions & 7 deletions middleware/auth-utils/signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict'

const Rotation = require('./rotation')
const crypto = require('crypto')
import crypto from 'node:crypto'
import Rotation from './rotation.js'

/**
* Construct a signature.
Expand All @@ -25,7 +23,7 @@ const crypto = require('crypto')
*
* @constructor
*/
function Signature (config) {
export default function Signature (config) {
this.publicKey = config.publicKey
this.rotation = new Rotation(config)
}
Expand All @@ -51,5 +49,3 @@ Signature.prototype.verify = function verify (token, callback) {
})
})
}

module.exports = Signature
5 changes: 1 addition & 4 deletions middleware/auth-utils/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict'

/**
* Construct a token.
Expand All @@ -27,7 +26,7 @@
* @param {String} token The JSON Web Token formatted token string.
* @param {String} clientId Optional clientId if this is an `access_token`.
*/
function Token (token, clientId) {
export default function Token (token, clientId) {
this.token = token
this.clientId = clientId

Expand Down Expand Up @@ -185,5 +184,3 @@ Token.prototype.hasPermission = function hasPermission (resource, scope) {

return false
}

module.exports = Token
8 changes: 3 additions & 5 deletions middleware/check-sso.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
'use strict'

const UUID = require('./../uuid')
const URL = require('url')
import URL from 'node:url'
import UUID from './../uuid.js'

function forceCheckSSO (keycloak, request, response) {
const host = request.hostname
Expand All @@ -38,7 +36,7 @@ function forceCheckSSO (keycloak, request, response) {
response.redirect(checkSsoUrl)
}

module.exports = function (keycloak) {
export default function checkSsoMiddleware (keycloak) {
return function checkSso (request, response, next) {
if (request.kauth && request.kauth.grant) {
return next()
Expand Down
6 changes: 1 addition & 5 deletions middleware/enforcer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
'use strict'

function handlePermissions (permissions, callback) {
for (let i = 0; i < permissions.length; i++) {
const expected = permissions[i].split(':')
Expand Down Expand Up @@ -42,7 +40,7 @@ function handlePermissions (permissions, callback) {
*
* @constructor
*/
function Enforcer (keycloak, config) {
export default function Enforcer (keycloak, config) {
this.keycloak = keycloak
this.config = config || {}

Expand Down Expand Up @@ -155,5 +153,3 @@ Enforcer.prototype.enforce = function enforce (expectedPermissions) {
}
}
}

module.exports = Enforcer
4 changes: 1 addition & 3 deletions middleware/grant-attacher.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
'use strict'

module.exports = function (keycloak) {
export default function grantAttacherMiddleware (keycloak) {
return function grantAttacher (request, response, next) {
keycloak.getGrant(request, response)
.then(grant => {
Expand Down
6 changes: 2 additions & 4 deletions middleware/logout.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
'use strict'
import URL from 'node:url'

const URL = require('url')

module.exports = function (keycloak, logoutUrl) {
export default function logoutMiddleware (keycloak, logoutUrl) {
return function logout (request, response, next) {
const parsedRequest = URL.parse(request.url, true); // eslint-disable-line
if (parsedRequest.pathname !== logoutUrl) {
Expand Down
6 changes: 2 additions & 4 deletions middleware/post-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
'use strict'
import URL from 'node:url'

const URL = require('url')

module.exports = function (keycloak) {
export default function postAuthMiddleware (keycloak) {
return function postAuth (request, response, next) {
if (!request.query.auth_callback) {
return next()
Expand Down
Loading