Skip to content

Commit

Permalink
Make configurable the JWT access_token (#89)
Browse files Browse the repository at this point in the history
* Make configurable the JWT access_token

* ci

* Add other files

* job name

* Update crypto.js
  • Loading branch information
nicoloboschi authored Feb 6, 2023
1 parent 9cf3208 commit bc955ff
Show file tree
Hide file tree
Showing 10 changed files with 6,880 additions and 1,160 deletions.
26 changes: 23 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ on:
- created

jobs:
analysis:
name: static analysis
fe-checks:
name: Frontend checks
runs-on: ubuntu-latest
steps:
- name: Check out code
- name: Checkout
uses: actions/checkout@v1
with:
fetch-depth: 1
Expand All @@ -32,6 +32,26 @@ jobs:
npm install -g @vue/cli
npm run build
be-checks:
name: Backend checks
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
with:
fetch-depth: 1
path: go/src/github.com/datastax/pulsar-admin-console
- name: Set up node
uses: actions/setup-node@master
with:
node-version: '16'
check-latest: true
- name: Run unit tests
run: |
cd server
npm ci
npm test
docker:
name: docker
runs-on: ubuntu-latest
Expand Down
80 changes: 42 additions & 38 deletions README.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
"token_path": "",
"admin_token": "",
"token_secret": "",
"token_options": {
"private_key_path": "",
"claim": "sub",
"expires_in": "12h",
"algorithm": "HS256"
},
"ssl": {
"verify_certs": true,
"hostname_validation": true,
Expand Down
119 changes: 119 additions & 0 deletions server/__tests__/server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//
// Copyright 2021 DataStax, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//

'use strict';

const fs = require('fs');
const axios = require('axios');
const {start, shutdown, getServer, getConfig} = require('../server');
const defaultConfig = require("../../config/default.json");
const globalConf = require("../../config/default.json");
const jwt = require('jsonwebtoken');

jest.mock('../config.js', () => {
const {createLogger, format, transports} = require('winston');
const globalConf = require("../../config/default.json");
globalConf.server_config.port = 0
globalConf.auth_mode = "user"
globalConf.server_config.user_auth.username = "username1"
globalConf.server_config.user_auth.password = "password1"


return {
L: createLogger({
level: 'debug',
format: format.combine(format.timestamp(), format.colorize(), format.simple()),
transports: [
new transports.Console({})
]
}),
globalConf
}
});

let client

beforeEach(() => {
start()
client = axios.create({
baseURL: "http://localhost:" + getServer().address().port
})
})


test('generateJWTSymmetric', async () => {
return client.post('/api/v1/auth/token', {
username: "username1",
password: "password1"
})
.then(function (response) {
expect(response.status).toBe(200)
expect(response.data.access_token).not.toBeUndefined()
return new Promise((resolve, reject) => {
jwt.verify(response.data.access_token, "default-secret", {
algorithms: ['HS256'],
sub: "username1",
maxAge: "12h"
}, function (err, decoded) {
if (err) {
reject(err)
} else {
resolve()
}
})

})

})
});


test('generateJWTAsymmetric', async () => {
getConfig().globalConf.server_config.token_options = {
algorithm: "RS256",
expires_in: "1d",
claim: "user",
private_key_path: "__tests__/test-private-key-rsa-pkcs8.key",
}
return client.post('/api/v1/auth/token', {
username: "username1",
password: "password1"
})
.then(function (response) {
expect(response.status).toBe(200)
expect(response.data.access_token).not.toBeUndefined()
return new Promise((resolve, reject) => {
jwt.verify(response.data.access_token, fs.readFileSync("__tests__/test-private-key-rsa-pkcs8.pem"), {
algorithms: ['RS256'],
user: "username1",
maxAge: "1d"
}, function (err, decoded) {
if (err) {
reject(err)
} else {
resolve()
}
})

})

})
});

afterEach(() => {
shutdown()
})
Binary file added server/__tests__/test-private-key-rsa-pkcs8.key
Binary file not shown.
27 changes: 27 additions & 0 deletions server/__tests__/test-private-key-rsa-pkcs8.pem
Original file line number Diff line number Di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WQ78Lga
R0GTLwuxSn4RHoay0UnEKbK9/mxt1N9+e3BZs8MCgYBU6+2HeEioiOfagPz4S7Gb
kcuHyn+AXUdA+W8lmZMbCCH2RrHYo9RKRyXUbmFIOIWNt/tho13Notd+98Rbm9al
eygv1HVfKW0uuVf8Ue6YFhJi3vzSJuif3QOHQ7x4ZWUfIXg3AId3OKuqVN6J3byi
m2TVyfgwDLR3NR+Ig6vksQKBgQC/nPlGnhSuVCLTQql45nZyTmYh5L3oh6BrnBPa
v2BkH5lUTHLeArhq19Oif9fdpbgkbHsL/NHixGgJH+yk5WbAweOmLljHVJ2SIg8q
l6kfO7tYce7nFfRIAYCY21r1XtQb0W5c71LCIqo+b+zVkkNG/We5w8wDs0B8Q550
aCXR0wKBgQDrvvMcLzz5zwVXvUUeYAlkGYwJkOcP0FkUaCxrL5tuiSZgiC3OaZjs
RVOzizBEKJVIaLirkpbzNEDVY5xdXIdTSscWDqRwrTRJzkdXBiOsa3s7bbuVYm8o
zTCZ9sxlv1aKRbPhH0o6lBD/l0OCmR/8k5y09tQ/YROIgArnI7GDTQ==
-----END RSA PRIVATE KEY-----
40 changes: 40 additions & 0 deletions server/crypto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Copyright 2021 DataStax, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//

'use strict';
const crypto = require('crypto')
const fs = require('fs')

const privateKeyToPem = async (privateKeyFilePath) => {
const privateKey = fs.readFileSync(privateKeyFilePath)

if (privateKeyFilePath.endsWith(".pem")) {
return privateKey
}
return crypto.createPrivateKey({
key: privateKey,
format: 'der',
type: 'pkcs8'
}).export({
format: 'pem',
type: 'pkcs8'
}).toString()

}
module.exports = {
privateKeyToPem
};
Loading

0 comments on commit bc955ff

Please sign in to comment.