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

Encrypt password before saving to a file #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions spec/api/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Jira Api', function () {
before(function () {
ConfigData = {
username: 'test',
password: 'test',
password: '6c1c88c2d6c9307924631245',
host: 'test.domain.com',
protocol: 'https',
port: '',
Expand All @@ -30,7 +30,7 @@ describe('Jira Api', function () {

it('It should be initialized with Jira Client', function () {
expect(JiraApi.client).to.not.equal('undefined')
expect(JiraApi.client.username).to.be.equal(ConfigData.username)
expect(JiraApi.client.apiVersion).to.be.equal(ConfigData.apiVersion)
})

describe('Issue', function () {
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('Jira Api', function () {
})
})
})

describe('User issues', function () {
it('It should return all issues for current user', function () {
JiraApi.client.get = sinon.stub().returns(Promise.resolve({
Expand Down
27 changes: 17 additions & 10 deletions spec/api/client_spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Client = require('../../src/api/client')
const EncryptDecrypter = require('../../src/cli/encrypt_decrypter')
const request = require('request')
const expect = require('chai').expect

Expand All @@ -8,8 +9,8 @@ describe('Jira client', function () {

before(function () {
configData = {
username: 'test',
password: 'test',
username: 'testname',
password: '6c1c88c2d6c9307924631245',
host: 'test.domain.com',
protocol: 'https',
port: '80',
Expand All @@ -24,23 +25,29 @@ describe('Jira client', function () {
})

it('It should set default properties for api requests', function () {
expect(JiraClient.username).to.be.equal(configData.username)
expect(JiraClient.password).to.be.equal(configData.password)
expect(JiraClient.protocol).to.be.equal(configData.protocol)
expect(JiraClient.host).to.be.equal(configData.host)
expect(JiraClient.port).to.be.equal('')
expect(JiraClient.options).to.deep.equal({
auth: {
'user': configData.username,
'pass': EncryptDecrypter.decrypt(configData.password)
},
json: true
})
expect(JiraClient.apiVersion).to.be.equal(configData.apiVersion)
expect(JiraClient.domainData).to.deep.equal({
protocol: configData.protocol,
hostname: configData.host,
port: ''
})
})
})

describe('With missing config information', function () {
it('It should throw exception when config object does not have all request fields', function () {
it('It should throw exception when config object does not have all request fields', function () {
const configData = {
username: 'test',
password: 'test'
password: '6c1c88c2d6c9307924631245'
}
expect((configData) => (Client.createClientWith(configData))).to.throw(Error)
})
})
})

13 changes: 6 additions & 7 deletions spec/cli/config_spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@

const fs = require('fs')
const config = require('../../src/cli/config')
const EncryptDecrypter = require('../../src/cli/encrypt_decrypter')

const expect = require('chai').expect

describe('Config', function () {
let Config, expected

beforeEach(function () {
Config = config.createConfigWith('config_test.json')
expected = {
Expand All @@ -27,21 +29,18 @@ describe('Config', function () {
})

it('It should save config information', function () {

Config.save(expected)

const actual = JSON.parse(fs.readFileSync(Config.configFilename))

expect(expected.username).to.be.equal(actual.username)
expect(expected.apiVersion).to.be.equal(actual.apiVersion)
})

it('It should render saved config information', function () {

const actual = Config.detail()

expect(expected.username).to.be.equal(actual.username)
expect(expected.password).to.be.equal(actual.password)
const actual = Config.detail()
console.log(EncryptDecrypter.decrypt(expected.password))
expect(EncryptDecrypter.encrypt(expected.password)).to.be.equal(actual.password)
expect(expected.host).to.be.equal(actual.host)
expect(expected.protocol).to.be.equal(actual.protocol)
expect(expected.port).to.be.equal(actual.port)
Expand Down
3 changes: 2 additions & 1 deletion src/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ class Api {
if (response.total === 0) {
throw new Error('There are no worklogs for this issue')
}

return response.worklogs
.filter(worklog => worklog.author.name === this.client.username)
.filter(worklog => worklog.author.name === this.client.options.auth.user)
.sort((worklogA, worklogB) => {
return +(worklogA.id < worklogB.id) || +(worklogA.id === worklogB.id) - 1
})
Expand Down
19 changes: 7 additions & 12 deletions src/api/client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const url = require('url')
const request = require('request')
const EncryptDecrypter = require('../cli/encrypt_decrypter')
const MAX_RESULTS = 20
const PORT = '80'

Expand All @@ -8,26 +9,20 @@ class Client {
if (!Config || !hasValidConfig(Config)) {
throw new Error('Missing Config attribute')
}

this.username = Config.username
this.password = Config.password
this.apiVersion = Config.apiVersion
this.protocol = Config.protocol
this.host = Config.host
this.port = Config.port === PORT ? '' : Config.port
this.maxResult = Config.max_results || MAX_RESULTS
this.options = {
auth: {
'user': this.username,
'pass': this.password
'user': Config.username,
'pass': EncryptDecrypter.decrypt(Config.password)
},
json: true
}
this.domainData = {
protocol: this.protocol,
hostname: this.host,
port: this.port
protocol: Config.protocol,
hostname: Config.host,
port: Config.port === PORT ? '' : Config.port
}
this.maxResult = Config.max_results || MAX_RESULTS
}

static createClientWith (ConfigData) {
Expand Down
6 changes: 5 additions & 1 deletion src/cli/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path')
const mkdirp = require('mkdirp')
const userhome = require('user-home')
const setupConfig = require('../cli/setup')
const EncryptDecrypter = require('./encrypt_decrypter')

class Config {
constructor (fileName) {
Expand All @@ -23,8 +24,11 @@ class Config {
}

save (configData) {
let data = Object.assign({}, configData)
data.password = EncryptDecrypter.encrypt(configData.password)

createFolder(this.configFilename)
fs.writeFileSync(this.configFilename, JSON.stringify(configData), 'utf8')
fs.writeFileSync(this.configFilename, JSON.stringify(data), 'utf8')

setupConfig.switchConfig(this.filename)
}
Expand Down
17 changes: 17 additions & 0 deletions src/cli/encrypt_decrypter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const crypto = require('crypto')
const algorithm = 'aes-256-ctr'
const key = '7b{7Y8R#>8UPheVs2%Yx{Dxwh9MAVzwvV'

class EncryptDecrypter {
encrypt (text) {
let cipher = crypto.createCipher(algorithm, key)
return cipher.update(text, 'utf8', 'hex') + cipher.final('hex')
}

decrypt (text) {
let decipher = crypto.createDecipher(algorithm, key)
return decipher.update(text, 'hex', 'utf8') + decipher.final('utf8')
}
}

module.exports = new EncryptDecrypter()