Skip to content

Commit

Permalink
trying new auth method of using uuid instead of ip address. ip addres…
Browse files Browse the repository at this point in the history
…s is causing issues. Updated axios to automatically put the uuid header. Added util to remove user tokens
  • Loading branch information
subnub committed Dec 8, 2020
1 parent 025bef8 commit 39b22ee
Show file tree
Hide file tree
Showing 18 changed files with 156 additions and 62 deletions.
4 changes: 2 additions & 2 deletions backend/controllers/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ class FileController {

const user = req.user;

const ipAddress = req.clientIp;
const currentUUID = req.headers.uuid as string;

const streamVideoAccessToken = await user.generateAuthTokenStreamVideo(ipAddress);
const streamVideoAccessToken = await user.generateAuthTokenStreamVideo(currentUUID);

createStreamVideoCookie(res, streamVideoAccessToken);

Expand Down
23 changes: 14 additions & 9 deletions backend/controllers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ class UserController {
try {

const body = req.body;
const ipAddress = req.clientIp;

const currentUUID = req.headers.uuid as string;

const {user, accessToken, refreshToken} = await UserProvider.login(body, ipAddress);
const {user, accessToken, refreshToken} = await UserProvider.login(body, currentUUID);

createLoginCookie(res, accessToken, refreshToken);

Expand All @@ -79,7 +80,9 @@ class UserController {

if (!user) throw new NotFoundError("User Not Found");

const {accessToken, refreshToken} = await user.generateAuthToken(req.clientIp);
const currentUUID = req.headers.uuid as string;

const {accessToken, refreshToken} = await user.generateAuthToken(currentUUID);

if (!accessToken || !refreshToken) throw new InternalServerError("User/Access/Refresh Token Missing");

Expand Down Expand Up @@ -153,9 +156,9 @@ class UserController {

try {

const ipAddress = req.clientIp;
const currentUUID = req.headers.uuid as string;

const {user, accessToken, refreshToken} = await UserProvider.create(req.body, ipAddress);
const {user, accessToken, refreshToken} = await UserProvider.create(req.body, currentUUID);

createLoginCookie(res, accessToken, refreshToken);

Expand All @@ -182,9 +185,10 @@ class UserController {
const oldPassword = req.body.oldPassword;
const newPassword = req.body.newPassword;
const oldRefreshToken = req.cookies["refresh-token"];
const ipAddress = req.clientIp;

const currentUUID = req.headers.uuid as string;

const {accessToken, refreshToken} = await UserProvider.changePassword(userID, oldPassword, newPassword, oldRefreshToken, ipAddress);
const {accessToken, refreshToken} = await UserProvider.changePassword(userID, oldPassword, newPassword, oldRefreshToken, currentUUID);

createLoginCookie(res, accessToken, refreshToken);

Expand Down Expand Up @@ -247,11 +251,12 @@ class UserController {
try {

const verifyToken = req.body.emailToken;
const ipAddress = req.clientIp;

const currentUUID = req.headers.uuid as string;

const user = await UserProvider.verifyEmail(verifyToken);

const {accessToken, refreshToken} = await user.generateAuthToken(ipAddress);
const {accessToken, refreshToken} = await user.generateAuthToken(currentUUID);

createLoginCookie(res, accessToken, refreshToken);

Expand Down
10 changes: 6 additions & 4 deletions backend/controllers/userGoogle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ class UserGoogleController {

const user = req.user;
const code = req.body.code;
const ipAddress = req.clientIp;

const currentUUID = req.headers.uuid as string;

const {accessToken, refreshToken} = await UserProviderGoogle.addGoogleStorage(user, code, ipAddress);
const {accessToken, refreshToken} = await UserProviderGoogle.addGoogleStorage(user, code, currentUUID);

createLoginCookie(res, accessToken, refreshToken);

Expand All @@ -74,9 +75,10 @@ class UserGoogleController {
try {

const user = req.user
const ipAddress = req.clientIp;

const {accessToken, refreshToken} = await UserProviderGoogle.removeGoogleStorage(user, ipAddress);
const currentUUID = req.headers.uuid as string;

const {accessToken, refreshToken} = await UserProviderGoogle.removeGoogleStorage(user, currentUUID);

createLoginCookie(res, accessToken, refreshToken);

Expand Down
9 changes: 5 additions & 4 deletions backend/controllers/userPersonal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ class UserPersonalController {

const user = req.user;
const s3Data = req.body;
const ipAddress = req.clientIp;

const currentUUID = req.headers.uuid as string;

const {accessToken, refreshToken} = await UserProviderPersonal.addS3Storage(user, s3Data, ipAddress);
const {accessToken, refreshToken} = await UserProviderPersonal.addS3Storage(user, s3Data, currentUUID);

createLoginCookie(res, accessToken, refreshToken);

Expand All @@ -63,9 +64,9 @@ class UserPersonalController {
try {

const user = req.user;
const ipAddress = req.clientIp;
const currentUUID = req.headers.uuid as string;

const {accessToken, refreshToken} = await UserProviderPersonal.removeS3Storage(user, ipAddress);
const {accessToken, refreshToken} = await UserProviderPersonal.removeS3Storage(user, currentUUID);

createLoginCookie(res, accessToken, refreshToken);

Expand Down
2 changes: 2 additions & 0 deletions backend/cookies/createCookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import {Response } from "express";
import env from "../enviroment/env";

const maxAgeAccess = 60 * 1000 * 20;
//const maxAgeAccess = 1000;
const maxAgeRefresh = 60 * 1000 * 60 * 24 * 30;
//const maxAgeRefresh = 1000;
const maxAgeStreamVideo = 60 * 1000 * 60 * 24;

const secureCookies = env.secureCookies ? env.secureCookies === "true" ? true : false : false;
Expand Down
2 changes: 1 addition & 1 deletion backend/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const auth = async(req: RequestType, res: Response, next: NextFunction) => {

try {

console.log("auth up address", req.clientIp, req.ip);
console.log("auth uuid", req.headers.uuid);

// console.log(req.headers);

Expand Down
4 changes: 2 additions & 2 deletions backend/middleware/authNoEmailVerificication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ const auth = async(req: RequestType, res: Response, next: NextFunction) => {
if (!user) throw new Error("No User");

if (!user.emailVerified && !env.disableEmailVerification) {
const ipAddress = req.clientIp;
user = await userUpdateCheck(res, user._id, ipAddress);
const currentUUID = req.headers.uuid as string;
user = await userUpdateCheck(res, user._id, currentUUID);
}

req.user = user;
Expand Down
14 changes: 8 additions & 6 deletions backend/middleware/authRefresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@ type jwtType = {
time: number
}

const removeOldTokens = async(userID: string, ipAddress: string | undefined, oldTime: number) => {
const removeOldTokens = async(userID: string, uuid: string | undefined, oldTime: number) => {

try {

console.log("removing token with IP address", ipAddress);
console.log("removing token with uuid address", uuid);

const minusTime = oldTime - (1000 * 60 * 60);
//const minusTime = oldTime - (1000);

ipAddress = ipAddress ? ipAddress : "";
uuid = uuid ? uuid : "unknown";

if (ipAddress === "") return;
if (uuid === "unknown") return;

await User.updateOne({_id: userID}, {$pull: {tokens: {ipAddress, time: {$lt: minusTime}}}})
await User.updateOne({_id: userID}, {$pull: {tokens: {uuid, time: {$lt: minusTime}}}})

} catch (e) {
console.log("cannot remove old tokens", e);
Expand All @@ -40,6 +41,7 @@ const authRefresh = async(req: RequestType, res: Response, next: NextFunction) =
try {

const refreshToken = req.cookies["refresh-token"];
const currentUUID = req.headers.uuid as string;

if (!refreshToken) throw new Error("No Refresh Token");

Expand All @@ -63,7 +65,7 @@ const authRefresh = async(req: RequestType, res: Response, next: NextFunction) =
if (currentEncryptedToken === encryptedToken) {

tokenFound = true;
removeOldTokens(user._id, req.clientIp, time);
removeOldTokens(user._id, currentUUID, time);
break;
}
}
Expand Down
11 changes: 6 additions & 5 deletions backend/middleware/authStreamVideo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ type jwtType = {
time: number
}

const removeOldTokens = async(userID: string, ipAddress: string | undefined, oldTime: number) => {
const removeOldTokens = async(userID: string, uuid: string | undefined, oldTime: number) => {

try {

const minusTime = oldTime - (60 * 1000 * 60 * 24);

ipAddress = ipAddress ? ipAddress : "";
uuid = uuid ? uuid : "unknown";

if (ipAddress === "") return;
if (uuid === "unknown") return;

await User.updateOne({_id: userID}, {$pull: {tempTokens: {ipAddress, time: {$lt: minusTime}}}})
await User.updateOne({_id: userID}, {$pull: {tempTokens: {uuid, time: {$lt: minusTime}}}})

} catch (e) {
console.log("cannot remove old tokens", e);
Expand All @@ -40,6 +40,7 @@ const authStreamVideo = async(req: RequestType, res: Response, next: NextFunctio
try {

const accessTokenStreamVideo = req.cookies["video-access-token"];
const currentUUID = req.headers.uuid as string;

if (!accessTokenStreamVideo) throw new Error("No Access Token");

Expand All @@ -63,7 +64,7 @@ const authStreamVideo = async(req: RequestType, res: Response, next: NextFunctio
if (currentEncryptedToken === encryptedToken) {

tokenFound = true;
removeOldTokens(user._id, req.clientIp, time);
removeOldTokens(user._id, currentUUID, time);
break;
}
}
Expand Down
20 changes: 10 additions & 10 deletions backend/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const userSchema = new mongoose.Schema({
type: String,
required: true
},
ipAddress: {
uuid: {
type: String,
required: true,
},
Expand All @@ -52,7 +52,7 @@ const userSchema = new mongoose.Schema({
type: String,
required: true
},
ipAddress: {
uuid: {
type: String,
required: true,
},
Expand Down Expand Up @@ -188,8 +188,8 @@ export interface UserInterface extends Document {
encryptToken: (tempToken: any, key: any, publicKey: any) => any;
decryptToken: (encryptedToken: any, key: any, publicKey: any) => any;
findByCreds: (email: string, password: string) => Promise<UserInterface>;
generateAuthToken: (ipAddress: string | undefined) => Promise<{accessToken: string, refreshToken: string}>
generateAuthTokenStreamVideo: (ipAddress: string | undefined) => Promise<string>
generateAuthToken: (uuid: string | undefined) => Promise<{accessToken: string, refreshToken: string}>
generateAuthTokenStreamVideo: (uuid: string | undefined) => Promise<string>
generateEncryptionKeys: () => Promise<void>;
changeEncryptionKey: (randomKey: Buffer) => Promise<void>;
generateEmailVerifyToken: () => Promise<string>;
Expand Down Expand Up @@ -251,7 +251,7 @@ userSchema.methods.toJSON = function() {
return userObject;
}

userSchema.methods.generateAuthTokenStreamVideo = async function(ipAddress: string | undefined) {
userSchema.methods.generateAuthTokenStreamVideo = async function(uuid: string | undefined) {

const iv = crypto.randomBytes(16);

Expand All @@ -266,14 +266,14 @@ userSchema.methods.generateAuthTokenStreamVideo = async function(ipAddress: stri

const encryptedToken = user.encryptToken(accessTokenStreamVideo, encryptionKey, iv);

ipAddress = ipAddress ? ipAddress : "";
uuid = uuid ? uuid : "unknown";

await User.updateOne({_id: user._id}, {$push: {"tempTokens": {token: encryptedToken, ipAddress, time}}});
await User.updateOne({_id: user._id}, {$push: {"tempTokens": {token: encryptedToken, uuid, time}}});

return accessTokenStreamVideo;
}

userSchema.methods.generateAuthToken = async function(ipAddress: string | undefined) {
userSchema.methods.generateAuthToken = async function(uuid: string | undefined) {

const iv = crypto.randomBytes(16);

Expand All @@ -293,9 +293,9 @@ userSchema.methods.generateAuthToken = async function(ipAddress: string | undefi

//user.tokens = user.tokens.concat({token: encryptedToken});

ipAddress = ipAddress ? ipAddress : "";
uuid = uuid ? uuid : "unknown";

await User.updateOne({_id: user._id}, {$push: {"tokens": {token: encryptedToken, ipAddress, time}}})
await User.updateOne({_id: user._id}, {$push: {"tokens": {token: encryptedToken, uuid, time}}})

// console.log("saving user")
// console.log("user saved")
Expand Down
8 changes: 4 additions & 4 deletions backend/services/UserGoogle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class UserGoogleService {
return url;
}

addGoogleStorage = async(user: UserInterface, code: string, ipAddress: string | undefined) => {
addGoogleStorage = async(user: UserInterface, code: string, uuid: string | undefined) => {

const redirectURL = env.remoteURL + "/add-google-account";

Expand All @@ -51,7 +51,7 @@ class UserGoogleService {

user.encryptDriveTokenData(token);

const {accessToken, refreshToken} = await user.generateAuthToken(ipAddress);
const {accessToken, refreshToken} = await user.generateAuthToken(uuid);

resolve({accessToken, refreshToken});

Expand All @@ -62,14 +62,14 @@ class UserGoogleService {
})
}

removeGoogleStorage = async(user: UserInterface, ipAddress: string | undefined) => {
removeGoogleStorage = async(user: UserInterface, uuid: string | undefined) => {

user.googleDriveEnabled = undefined;
user.googleDriveData = undefined;

await user.save();

return await user.generateAuthToken(ipAddress);
return await user.generateAuthToken(uuid);
}
}

Expand Down
8 changes: 4 additions & 4 deletions backend/services/UserPersonalService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class UserPeronsalService {

}

addS3Storage = async(user: UserInterface, s3Data: any, ipAddress: string | undefined) => {
addS3Storage = async(user: UserInterface, s3Data: any, uuid: string | undefined) => {

const {id, key, bucket} = s3Data;

Expand All @@ -29,10 +29,10 @@ class UserPeronsalService {

await user.save();

return await user.generateAuthToken(ipAddress);
return await user.generateAuthToken(uuid);
}

removeS3Storage = async(user: UserInterface, ipAddress: string | undefined) => {
removeS3Storage = async(user: UserInterface, uuid: string | undefined) => {

const date = new Date();

Expand All @@ -43,7 +43,7 @@ class UserPeronsalService {

await user.save();

return await user.generateAuthToken(ipAddress);
return await user.generateAuthToken(uuid);
}

downloadPersonalFileList = async(user: userAccessType) => {
Expand Down
Loading

0 comments on commit 39b22ee

Please sign in to comment.