qpa-client/server/src/Auth/SessionManager.ts

90 lines
2.4 KiB
TypeScript
Raw Normal View History

2018-04-02 20:23:44 +02:00
// Free API to get location from IP: http://freegeoip.net/json/149.11.144.50
2018-05-11 10:12:56 +02:00
2019-03-08 15:58:35 +01:00
import {User} from "./User.entity"
import * as uuid from 'uuid/v4'
2018-05-05 22:11:43 +02:00
const randomstring = require('random-string')
2019-03-11 13:39:16 +01:00
import {PostOffice} from '../post_office'
import {domain} from '../config'
2019-03-08 15:58:35 +01:00
import {Session, SessionInvite} from "./Session.entity"
2018-05-04 19:53:16 +02:00
2018-09-30 12:33:59 +02:00
export class SessionAlreadyValidatedError extends Error {}
2018-09-30 09:36:17 +02:00
const generateHash = () => randomstring({
2018-05-11 10:12:56 +02:00
length: 48,
letters: true,
special: false
})
2018-05-04 19:53:16 +02:00
2019-03-11 13:39:16 +01:00
const generateUniqueInviteHash = async () => {
2019-03-08 15:58:35 +01:00
const hash = generateHash()
2019-03-11 13:39:16 +01:00
const existingSession = await SessionInvite.findOne({hash: hash})
2019-03-08 15:58:35 +01:00
if (existingSession) {
return generateUniqueInviteHash()
} else {
return hash
2018-04-02 20:23:44 +02:00
}
}
2019-03-08 15:58:35 +01:00
const generateUniqueSessionHash = () => {
const hash = generateHash()
const existingSession = Session.findOne({hash: hash})
if (existingSession) {
return generateUniqueInviteHash()
} else {
return hash
2018-05-11 10:12:56 +02:00
}
2019-03-08 15:58:35 +01:00
}
2018-05-11 10:12:56 +02:00
2019-03-11 13:39:16 +01:00
interface Dependencies {
sendEmail: PostOffice
}
2019-03-08 15:58:35 +01:00
export default class SessionManager {
2019-03-11 13:39:16 +01:00
sendEmail: PostOffice
constructor(deps: Dependencies) {
this.sendEmail = deps.sendEmail
}
2019-03-08 15:58:35 +01:00
inviteUser = async (user: User): Promise<SessionInvite> => {
const invite = new SessionInvite()
invite.user = user
invite.hash = await generateUniqueInviteHash()
await invite.save()
2018-05-04 19:53:16 +02:00
2018-05-19 13:43:35 +02:00
return new Promise(async (resolve: (SessionInvite) => void, reject)=>{
try {
2019-03-11 13:39:16 +01:00
await this.sendEmail({
2018-05-19 13:43:35 +02:00
to: user.email,
from: `signin@${domain}`,
2018-09-30 12:33:59 +02:00
text: `Follow this link to start a session: https://${domain}/login/${invite.hash}`,
2018-05-19 13:43:35 +02:00
subject: 'Invitation for session'
})
resolve(invite)
console.log(`Sent invitation to ${user.email}`)
} catch (e) {
reject(e)
console.error('Failed to send invitation email ', invite, e)
throw e
}
})
2018-05-11 10:12:56 +02:00
}
2018-05-04 19:53:16 +02:00
2019-03-08 15:58:35 +01:00
initiateSession = async (inviteHash: string): Promise<Session> => {
const sessionInvite: SessionInvite = await SessionInvite.findOne({hash: inviteHash})
2019-01-30 16:44:05 +01:00
if (!sessionInvite) {
throw new Error(`Could not find invite with hash ${inviteHash}`)
}
2018-05-11 10:12:56 +02:00
if (sessionInvite.timeValidated) {
2018-09-30 09:36:17 +02:00
throw new SessionAlreadyValidatedError()
2018-05-11 10:12:56 +02:00
}
2019-03-08 15:58:35 +01:00
const session = new Session()
session.user = sessionInvite.user
session.isValid = true
session.hash = generateUniqueSessionHash()
await session.save()
2018-06-03 12:33:51 +02:00
2019-03-08 15:58:35 +01:00
return session
2018-06-03 12:33:51 +02:00
}
2018-05-04 19:53:16 +02:00
}