Skip to content

Commit

Permalink
Classifier: Remove the faulty session id code (#6499)
Browse files Browse the repository at this point in the history
* Remove the faulty session id code
  • Loading branch information
goplayoutside3 authored Dec 10, 2024
1 parent ad8d5be commit d051d9c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 90 deletions.
2 changes: 1 addition & 1 deletion packages/lib-classifier/src/store/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const authorization = getBearerToken(store.authClient)

## sessionUtils

Get the current session ID to include with classification metadata. A new session ID is returned if more than five minutes have passed since the last call to `sessionUtils.getSessionID()`. Sessions are stored in browser session storage, so unique to individual tabs.
Get the current session ID to include with classification metadata. Sessions are stored in browser session storage, so unique to individual tabs.

```js
const sessionID = sessionUtils.getSessionID()
Expand Down
28 changes: 5 additions & 23 deletions packages/lib-classifier/src/store/utils/session.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,23 @@
import hash from 'hash.js'

const storage = window.sessionStorage || window.localStorage
const storage = window?.sessionStorage

const sessionUtils = {
fiveMinutesFromNow () {
const d = new Date()
d.setMinutes(d.getMinutes() + 5)
return d
},

generateSessionID () {
const sha2 = hash.sha256()
const id = sha2.update(`${Math.random() * 10000}${Date.now()}${Math.random() * 1000}`).digest('hex')
const ttl = this.fiveMinutesFromNow()
const stored = { id, ttl }
const stored = { id }
try {
storage.setItem('session_id', JSON.stringify(stored))
storage?.setItem('session_id', JSON.stringify(stored))
} catch (e) {
console.error(e)
}
return stored
},

getSessionID () {
const stored = (storage.getItem('session_id')) ? JSON.parse(storage.getItem('session_id')) : this.generateSessionID()
let { id, ttl } = stored

if (ttl < Date.now()) {
id = this.generateSessionID()
} else {
ttl = this.fiveMinutesFromNow()
}
try {
storage.setItem('session_id', JSON.stringify({ id, ttl }))
} catch (e) {
console.error(e)
}
const stored = (storage?.getItem('session_id')) ? JSON.parse(storage?.getItem('session_id')) : this.generateSessionID()
const { id } = stored
return id
}
}
Expand Down
68 changes: 2 additions & 66 deletions packages/lib-classifier/src/store/utils/session.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,14 @@ import hash from 'hash.js'
import sessionUtils from './session'

describe('Store utils > sessionUtils', function () {
describe('fiveMinutesFromNow', function () {
it('should return a Date object', function () {
const time = sessionUtils.fiveMinutesFromNow()
expect(time).to.be.an.instanceof(Date)
})

it('should call the Date object getMinutes method', function () {
const getMinutesSpy = sinon.spy(Date.prototype, 'getMinutes')
sessionUtils.fiveMinutesFromNow()
expect(getMinutesSpy).to.have.been.called()
getMinutesSpy.restore()
})

it('should call the Date object setMinutes method', function () {
const setMinutesSpy = sinon.spy(Date.prototype, 'setMinutes')
sessionUtils.fiveMinutesFromNow()
expect(setMinutesSpy).to.have.been.called()
setMinutesSpy.restore()
})
})

describe('getSessionID', function () {
let generateSessionIDSpy
let fiveMinutesFromNowSpy
beforeEach(function () {
generateSessionIDSpy = sinon.spy(sessionUtils, 'generateSessionID')
fiveMinutesFromNowSpy = sinon.spy(sessionUtils, 'fiveMinutesFromNow')
})

afterEach(function () {
generateSessionIDSpy.restore()
fiveMinutesFromNowSpy.restore()
sessionStorage.removeItem('session_id')
})

Expand All @@ -48,43 +24,12 @@ describe('Store utils > sessionUtils', function () {
expect(generateSessionIDSpy).to.have.been.called()
})

it('it should retrieve id from session or local storage if it exists', function () {
it('it should retrieve id from session if it exists', function () {
const stored = { id: 'foobar', ttl: (Date.now() + 50000) }
sessionStorage.setItem('session_id', JSON.stringify(stored))
sessionUtils.getSessionID()
expect(generateSessionIDSpy).to.have.not.been.called()
})

it('should call fiveMinutesFromNow if the ttl property is greater than Date.now()', function () {
sessionUtils.getSessionID()
expect(fiveMinutesFromNowSpy).to.have.been.called()
})

it('should update sessionStorage', function () {
sessionUtils.getSessionID()
const stored = JSON.parse(sessionStorage.getItem('session_id'))

// Dates are a second off from each other in ISO format,
// but equal when starting as Date objects and converted to Date strings
// Unsure of the minor discrepency
expect(new Date(stored.ttl).toString()).to.equal(fiveMinutesFromNowSpy.returnValues[0].toString())
})

describe('when the stored token has expired', function () {
before(function () {
const stored = { id: 'foobar', ttl: (Date.now() - 1) }
sessionStorage.setItem('session_id', JSON.stringify(stored))
})

after(function () {
sessionStorage.removeItem('session_id')
})

it('should generate a new session ID', function () {
sessionUtils.getSessionID()
expect(generateSessionIDSpy).to.have.been.calledOnce()
})
})
})

describe('generateSessionID', function () {
Expand Down Expand Up @@ -113,20 +58,11 @@ describe('Store utils > sessionUtils', function () {
dateSpy.restore()
})

it('should call fiveMinutesFromNow', function () {
const fiveMinutesFromNowSpy = sinon.spy(sessionUtils, 'fiveMinutesFromNow')
sessionUtils.generateSessionID()
expect(fiveMinutesFromNowSpy).to.have.been.called()
fiveMinutesFromNowSpy.restore()
})

it('should return an object with the generated id and the result of calling fiveMinutesFromNow', function () {
it('should return an object with the generated id', function () {
const result = sessionUtils.generateSessionID()
expect(result).to.be.an('object')
expect(result).to.have.property('id')
expect(result.id).to.be.a('string')
expect(result).to.have.property('ttl')
expect(result.ttl).to.be.an.instanceof(Date)
})

it('should store the stringified generated id and Date object in session storage', function () {
Expand Down

0 comments on commit d051d9c

Please sign in to comment.