-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
147 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import DatabaseService from "../services/database.service"; | ||
import EncryptService from "../services/encrypt.service"; | ||
import { Provider, Dependency } from "@ritley/decorators"; | ||
|
||
@Provider.factory | ||
@Dependency("database", DatabaseService) | ||
@Dependency("encrypt", EncryptService) | ||
export default class SessionModel { | ||
|
||
static SESSION_MS_EXPIRATION = 1800000; | ||
|
||
static newExpirationDate() { | ||
return Date.now() + this.SESSION_MS_EXPIRATION; | ||
} | ||
|
||
static buildSession({ uid, name, ...user }) { | ||
return { | ||
expiration: this.newExpirationDate(), | ||
userUid: uid | ||
} | ||
} | ||
|
||
static updateSession({ expiration, ...session }) { | ||
return { | ||
expiration: this.newExpirationDate(), | ||
...session | ||
} | ||
} | ||
|
||
static hasSessionExpired = session => session.expiration < Date.now(); | ||
|
||
validateCredentials({ mail, pass }) { | ||
return new Promise((resolve, reject) => { | ||
this.database | ||
.one("users", { mail }) | ||
.then(user => { | ||
if(user && this.encrypt.decode(user.pass) === pass) resolve(user); | ||
else reject(new SessionInvalidCredentialsError); | ||
}) | ||
}); | ||
} | ||
|
||
upsertSession(user) { | ||
return new Promise(resolve => this.database.exists("sessions", { userUid: user.uid }) | ||
.then(session => this.updateSession(session).then(resolve), | ||
() => this.createSession(user)).then(resolve)); | ||
} | ||
|
||
createSession(user) { | ||
return this.database.create("sessions", SessionModel.buildSession(user)); | ||
} | ||
|
||
updateSession(session) { | ||
return this.database.update("sessions", { uid: session.uid }, SessionModel.updateSession(session)) | ||
} | ||
|
||
revalidate(session) { | ||
if(SessionModel.hasSessionExpired(session)) { | ||
this.database.remove("sessions", session.uid); | ||
return Promise.reject(new SessionExpiredError); | ||
} else { | ||
return this.updateSession(session); | ||
} | ||
} | ||
|
||
sessionExists(criteria) { | ||
return new Promise((resolve, reject) => | ||
this.database.exists("sessions", criteria).then(resolve, () => reject(new SessionNotCreatedError))); | ||
} | ||
} | ||
|
||
export class SessionNotCreatedError extends Error { | ||
constructor() { | ||
super("you need to create a session to perform this action") | ||
} | ||
} | ||
|
||
export class SessionInvalidCredentialsError extends Error { | ||
constructor() { | ||
super("your credentials are invalid") | ||
} | ||
} | ||
|
||
export class SessionExpiredError extends Error { | ||
constructor() { | ||
super("your session has expired") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,38 @@ | ||
import { AbstractResource } from "@ritley/core"; | ||
import SessionModel, { SessionInvalidCredentialsError } from "../models/session.model"; | ||
|
||
import { | ||
Default, | ||
MethodNotAllowed, | ||
Created, | ||
Throws, | ||
Unauthorized, | ||
BadRequest, | ||
Dependency, | ||
ReqTransformBodyAsync | ||
} from "@ritley/decorators"; | ||
|
||
|
||
@Dependency("sessionModel", SessionModel) | ||
export default class SessionResource extends AbstractResource { | ||
@Default(MethodNotAllowed) get() {} | ||
@Default(MethodNotAllowed) put() {} | ||
@Default(MethodNotAllowed) delete() {} | ||
|
||
static URI = "/sessions"; | ||
|
||
constructor() { | ||
super("/sessions"); | ||
super(SessionResource.URI); | ||
} | ||
|
||
get(req, res) { | ||
res.statusCode = 200; | ||
res.end("Hello from sessions!"); | ||
@Throws(SyntaxError, BadRequest) | ||
@Throws(SessionInvalidCredentialsError, Unauthorized) | ||
@Default(Created) | ||
@ReqTransformBodyAsync | ||
async post(req) { | ||
const body = await req.body; | ||
const payload = body.toJSON(); | ||
const user = await this.sessionModel.validateCredentials(payload); | ||
return this.sessionModel.upsertSession(user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters