Skip to content

Commit 13dd07a

Browse files
committed
Convert modules to standarized ECMAScript
Closes keycloak#569 Signed-off-by: Jon Koops <[email protected]>
1 parent a3adfdb commit 13dd07a

40 files changed

+198
-292
lines changed

example/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
* License for the specific language governing permissions and limitations under
1414
* the License.
1515
*/
16+
import express from 'express'
17+
import session from 'express-session'
18+
import hogan from 'hogan-express'
19+
import Keycloak from 'keycloak-connect'
20+
import path from 'node:path'
21+
import url from 'node:url'
1622

17-
const Keycloak = require('keycloak-connect')
18-
const hogan = require('hogan-express')
19-
const express = require('express')
20-
const session = require('express-session')
21-
23+
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
2224
const app = express()
2325

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

3032
// Register '.mustache' extension with The Mustache Express
3133
app.set('view engine', 'html')
32-
app.set('views', require('path').join(__dirname, '/view'))
34+
app.set('views', path.join(__dirname, '/view'))
3335
app.engine('html', hogan)
3436

3537
// A normal un-protected public URL.

example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.1.0",
44
"description": "Example page that demonstrates available keycloak functionality",
55
"main": "index.js",
6+
"type": "module",
67
"scripts": {
78
"start": "node index.js"
89
},
File renamed without changes.

guides/securing-apps/nodejs-adapter.adoc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ In the root directory of your project create a file called `server.js` and add t
6363
6464
[source,javascript]
6565
----
66-
const session = require('express-session');
67-
const Keycloak = require('keycloak-connect');
66+
import session from 'express-session';
67+
import Keycloak from 'keycloak-connect';
6868
6969
const memoryStore = new session.MemoryStore();
7070
const keycloak = new Keycloak({ store: memoryStore });
@@ -135,7 +135,8 @@ server-side state for authentication, you need to initialize the
135135
session store that `express-session` is using.
136136
[source,javascript]
137137
----
138-
const session = require('express-session');
138+
import session from 'express-session';
139+
139140
const memoryStore = new session.MemoryStore();
140141
141142
// Configure session
@@ -169,7 +170,8 @@ then require Express in our project as outlined below:
169170
170171
[source,javascript]
171172
----
172-
const express = require('express');
173+
import express from 'express';
174+
173175
const app = express();
174176
----
175177

keycloak.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,19 @@
1414
* the License.
1515
*/
1616

17-
const BearerStore = require('./stores/bearer-store')
18-
const CookieStore = require('./stores/cookie-store')
19-
const SessionStore = require('./stores/session-store')
20-
21-
const Config = require('./middleware/auth-utils/config')
22-
const GrantManager = require('./middleware/auth-utils/grant-manager')
23-
const Setup = require('./middleware/setup')
24-
const Admin = require('./middleware/admin')
25-
const Logout = require('./middleware/logout')
26-
const PostAuth = require('./middleware/post-auth')
27-
const GrantAttacher = require('./middleware/grant-attacher')
28-
const Protect = require('./middleware/protect')
29-
const Enforcer = require('./middleware/enforcer')
30-
const CheckSso = require('./middleware/check-sso')
17+
import Admin from './middleware/admin.js'
18+
import Config from './middleware/auth-utils/config.js'
19+
import GrantManager from './middleware/auth-utils/grant-manager.js'
20+
import CheckSso from './middleware/check-sso.js'
21+
import Enforcer from './middleware/enforcer.js'
22+
import GrantAttacher from './middleware/grant-attacher.js'
23+
import Logout from './middleware/logout.js'
24+
import PostAuth from './middleware/post-auth.js'
25+
import Protect from './middleware/protect.js'
26+
import Setup from './middleware/setup.js'
27+
import BearerStore from './stores/bearer-store.js'
28+
import CookieStore from './stores/cookie-store.js'
29+
import SessionStore from './stores/session-store.js'
3130

3231
/**
3332
* Instantiate a Keycloak.
@@ -58,7 +57,7 @@ const CheckSso = require('./middleware/check-sso')
5857
* @return {Keycloak} A constructed Keycloak object.
5958
*
6059
*/
61-
function Keycloak (config, keycloakConfig) {
60+
export default function Keycloak (config, keycloakConfig) {
6261
// If keycloakConfig is null, Config() will search for `keycloak.json`.
6362
this.config = new Config(keycloakConfig)
6463

@@ -426,5 +425,3 @@ Keycloak.prototype.redirectToLogin = function (request) {
426425
Keycloak.prototype.getConfig = function () {
427426
return this.config
428427
}
429-
430-
module.exports = Keycloak

middleware/admin.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
* License for the specific language governing permissions and limitations under
1414
* the License.
1515
*/
16-
'use strict'
17-
18-
const Token = require('./auth-utils/token')
19-
const Signature = require('./auth-utils/signature')
16+
import Token from './auth-utils/token.js'
17+
import Signature from './auth-utils/signature.js'
2018

2119
function Admin (keycloak, url) {
2220
this._keycloak = keycloak
@@ -99,7 +97,7 @@ function adminNotBefore (request, response, keycloak) {
9997
})
10098
}
10199

102-
module.exports = function (keycloak, adminUrl) {
100+
export default function adminMiddleware (keycloak, adminUrl) {
103101
let url = adminUrl
104102
if (url[url.length - 1] !== '/') {
105103
url = url + '/'

middleware/auth-utils/config.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
17-
'use strict'
18-
19-
const path = require('path')
20-
const fs = require('fs')
16+
import path from 'node:path'
17+
import fs from 'node:fs'
2118

2219
/**
2320
* Construct a configuration object.
@@ -31,7 +28,7 @@ const fs = require('fs')
3128
*
3229
* @constructor
3330
*/
34-
function Config (config) {
31+
export default function Config (config) {
3532
if (!config) {
3633
config = path.join(process.cwd(), 'keycloak.json')
3734
}
@@ -168,5 +165,3 @@ Config.prototype.configure = function configure (config) {
168165
*/
169166
this.verifyTokenAudience = resolveValue(config['verify-token-audience'] || config.verifyTokenAudience || false)
170167
}
171-
172-
module.exports = Config

middleware/auth-utils/grant-manager.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,14 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
'use strict'
17-
18-
const URL = require('url')
19-
const http = require('http')
20-
const https = require('https')
21-
const crypto = require('crypto')
22-
const querystring = require('querystring')
23-
const Grant = require('./grant')
24-
const Token = require('./token')
25-
const Rotation = require('./rotation')
16+
import URL from 'node:url'
17+
import http from 'node:http'
18+
import https from 'node:https'
19+
import crypto from 'node:crypto'
20+
import querystring from 'node:querystring'
21+
import Grant from './grant.js'
22+
import Token from './token.js'
23+
import Rotation from './rotation.js'
2624

2725
/**
2826
* Construct a grant manager.
@@ -31,7 +29,7 @@ const Rotation = require('./rotation')
3129
*
3230
* @constructor
3331
*/
34-
function GrantManager (config) {
32+
export default function GrantManager (config) {
3533
this.realmUrl = config.realmUrl
3634
this.clientId = config.clientId
3735
this.secret = config.secret
@@ -536,5 +534,3 @@ const fetch = (manager, handler, options, params) => {
536534
req.end()
537535
})
538536
}
539-
540-
module.exports = GrantManager

middleware/auth-utils/grant.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
'use strict'
1716

1817
/**
1918
* Construct a new grant.
@@ -30,7 +29,7 @@
3029
*
3130
* @constructor
3231
*/
33-
function Grant (grant) {
32+
export default function Grant (grant) {
3433
this.update(grant)
3534
}
3635

@@ -80,5 +79,3 @@ Grant.prototype.isExpired = function isExpired () {
8079
}
8180
return this.access_token.isExpired()
8281
}
83-
84-
module.exports = Grant

middleware/auth-utils/rotation.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
'use strict'
17-
const URL = require('url')
18-
const http = require('http')
19-
const https = require('https')
20-
const jwkToPem = require('jwk-to-pem')
16+
import URL from 'node:url'
17+
import http from 'node:http'
18+
import https from 'node:https'
19+
import jwkToPem from 'jwk-to-pem'
2120

2221
/**
2322
* Construct a Rotation instance
@@ -26,7 +25,7 @@ const jwkToPem = require('jwk-to-pem')
2625
*
2726
* @constructor
2827
*/
29-
function Rotation (config) {
28+
export default function Rotation (config) {
3029
this.realmUrl = config.realmUrl
3130
this.minTimeBetweenJwksRequests = config.minTimeBetweenJwksRequests
3231
this.jwks = []
@@ -93,5 +92,3 @@ const nodeify = (promise, cb) => {
9392
if (typeof cb !== 'function') return promise
9493
return promise.then((res) => cb(null, res)).catch((err) => cb(err))
9594
}
96-
97-
module.exports = Rotation

0 commit comments

Comments
 (0)