Skip to content

Commit

Permalink
refact: Change company credentials for authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
PMax5 committed Feb 16, 2024
1 parent b5554cd commit a825877
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
1 change: 1 addition & 0 deletions helpers/joi.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ let credentials = Joi.object().keys({
companyName: Joi.string(),
edition: Joi.string().required(),
iat: Joi.number(),
iss: Joi.string(),
participationDays: Joi.number().required(),
activities: Joi.array().items(Joi.string())
})
Expand Down
2 changes: 1 addition & 1 deletion helpers/pre.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports.duration = {
const beginDate = new Date(edition.begin).getTime()
const endDate = new Date(edition.end).getTime()

return new Date(endDate - beginDate).getUTCDate()
return new Date(endDate - beginDate).getUTCDate() - 1
},
assign: 'duration'
}
Expand Down
18 changes: 10 additions & 8 deletions routes/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = [
description: 'Check token validation',
handler: async (request, h) => {
try {
const credentials = request.auth.credentials
const credentials = request.auth.credentials.credentials

const link = await request.server.methods.link.find({
companyId: credentials.company,
Expand Down Expand Up @@ -74,7 +74,7 @@ module.exports = [

let confirmedReservation = await request.server.methods.reservation.getConfirmedReservations(edition)
let pendingReservation = await request.server.methods.reservation.getPendingReservations(edition)
return venue.getStandsAvailability(confirmedReservation, pendingReservation, duration)
return venue.getStandsAvailability(confirmedReservation, pendingReservation, duration)
} catch (err) {
logger.error({ info: request.info, error: err })
return Boom.boomify(err)
Expand Down Expand Up @@ -103,7 +103,7 @@ module.exports = [
]
],
handler: async (request, h) => {
const companyId = request.auth.credentials.company
const companyId = request.auth.credentials.credentials.company
const edition = request.pre.edition

let step
Expand Down Expand Up @@ -155,7 +155,7 @@ module.exports = [
]
],
handler: async (request, h) => {
const companyId = request.auth.credentials.company
const companyId = request.auth.credentials.credentials.company
const edition = request.pre.edition
const info = request.payload.info
const titles = request.payload.titles
Expand Down Expand Up @@ -219,7 +219,7 @@ module.exports = [
]
],
handler: async (request, h) => {
let companyId = request.auth.credentials.company
let companyId = request.auth.credentials.credentials.company
let stands = request.payload.stands
let edition = request.pre.edition
let link = request.pre.link
Expand Down Expand Up @@ -290,6 +290,8 @@ module.exports = [

request.server.methods.mailgun.sendNewReservation(receivers, reservation, link, venue)

logger.info(reservation)

return reservation.toJSON()
} catch (err) {
logger.error({ info: request.info, error: err })
Expand Down Expand Up @@ -321,7 +323,7 @@ module.exports = [
]
],
handler: async (request, h) => {
const companyId = request.auth.credentials.company
const companyId = request.auth.credentials.credentials.company
const edition = request.pre.edition
const file = request.pre.file

Expand Down Expand Up @@ -372,7 +374,7 @@ module.exports = [
]
],
handler: async (request, h) => {
let companyId = request.auth.credentials.company
let companyId = request.auth.credentials.credentials.company
let edition = request.pre.edition
let venue = request.pre.venue

Expand Down Expand Up @@ -410,7 +412,7 @@ module.exports = [
],
handler: async (request, h) => {
try {
const companyId = request.auth.credentials.company
const companyId = request.auth.credentials.credentials.company
const edition = request.pre.edition
const latest = request.query.latest

Expand Down
12 changes: 8 additions & 4 deletions routes/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ module.exports = [

const token = await request.server.methods.jwt.verify(link[0].token)

if (token && token.exp * 1000 - new Date().getTime() <= 0) {
if (token && token.credentials.exp * 1000 - new Date().getTime() <= 0) {
await request.server.methods.link.revoke(companyId, edition)
return Boom.resourceGone('Token expired')
}

return token === null
? Boom.resourceGone('Token expired')
: { expirationDate: new Date(token.exp * 1000).toJSON() }
: { expirationDate: new Date(token.credentials.exp * 1000).toJSON() }
} catch (err) {
logger.error({ info: request.info, error: err })
return Boom.boomify(err)
Expand Down Expand Up @@ -309,7 +309,7 @@ module.exports = [

let link = await request.server.methods.link.create(
companyId, company.name, edition,
member.mails.main, token, participationDays,
request.auth.credentials.user, token, participationDays,
activities, advertisementKind, companyEmail, workshop, presentation, lunchTalk
)

Expand Down Expand Up @@ -391,7 +391,11 @@ module.exports = [
const { expirationDate } = request.payload

try {
const token = await request.server.methods.jwt.generate(edition, companyId, expirationDate)
const token = await request.server.methods.jwt.generate({
edition: edition,
company: companyId,
exp: Math.floor(expirationDate / 1000)
})
const link = await request.server.methods.link.setToken(request.params, token)
return (link) ? link.toJSON() : Boom.badData('No link associated')
} catch (err) {
Expand Down

0 comments on commit a825877

Please sign in to comment.