Skip to content

Commit

Permalink
- Add redis to docker-compose
Browse files Browse the repository at this point in the history
- Streamline env management and reorganize files
- Add [email protected] to keyring
  • Loading branch information
Harsh14901 committed Jan 5, 2022
1 parent 4e3d1d5 commit 5542748
Show file tree
Hide file tree
Showing 18 changed files with 74 additions and 59 deletions.
Binary file modified .env.secret
Binary file not shown.
Binary file modified .gitsecret/keys/pubring.kbx
Binary file not shown.
Binary file modified .gitsecret/keys/pubring.kbx~
Binary file not shown.
2 changes: 1 addition & 1 deletion .gitsecret/paths/mapping.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.env:9c1a52e44745107c32d34055320ee62f4572a83a96453b4d38495940c79ab9a3
.env:1074cf24f051cdd8cbbdf11b4e1c8f2ed88736319a8c0771476e55c77e239a78
src/config/private.pem:040731b01f84c8b9119367982872acb3d046cdf67f21566e18cfa337c130c1d4
src/config/public.pem:2a545e85b82c860d6185deff6f81a8d478b7d21eba2062dceae84d2cee03211d
16 changes: 15 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ version: '3.4'

services:
database:
image: '${REGISTRY_NAME}mongo:latest'
image: '${REGISTRY_NAME}mongo:4.2-bionic'
volumes:
- casidb:/data/db
networks:
- 'internal'
restart: 'unless-stopped'
redis:
image: '${REGISTRY_NAME}redis:6.2-alpine'
restart: always
networks:
- 'internal'
ports:
- '6379:6379'
env_file:
- './.env'
command: redis-server --loglevel warning --requirepass ${REDIS_PASS}
volumes:
- casi_redis:/data
CASI:
build: .
image: '${REGISTRY_NAME}devclubiitd/casi:0.1'
Expand All @@ -30,9 +42,11 @@ services:
- MONGODB_URI_LOCAL
depends_on:
- database
- redis

volumes:
casidb:
casi_redis:

networks:
reverseproxy:
Expand Down
13 changes: 13 additions & 0 deletions src/config/axios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as keys from './keys';

const HttpsProxyAgent = require('https-proxy-agent');

const axiosDefaultConfig = {
proxy: false,
httpsAgent: !keys.isDev
? new HttpsProxyAgent('http://devclub.iitd.ac.in:3128')
: null,
};
const axios = require('axios').create(axiosDefaultConfig);

export default axios;
5 changes: 5 additions & 0 deletions src/config/keys.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const fs = require('fs');
const path = require('path');

require('dotenv').config({
path: `${__dirname}/../../.env`,
});

export const expTime = 60 * 20;
export const rememberTime = 60 * 60 * 24 * 2;
export const reqExpTime = 60;
Expand All @@ -19,6 +23,7 @@ export const accountExists =
'An account is already linked with that account, Please try linking another one.';

export const noRedirectState = 'xyz';
export const isDev = process.env.NODE_ENV === 'DEV';

// Role to Privilege
export const r2p = {
Expand Down
Binary file modified src/config/private.pem.secret
Binary file not shown.
Binary file modified src/config/public.pem.secret
Binary file not shown.
16 changes: 16 additions & 0 deletions src/config/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import redis from 'redis';
import { isDev } from './keys';

const redisURl = isDev
? 'redis://127.0.0.1:6379'
: `redis://:${process.env.REDIS_PASS}@redis:6379`;
console.log('Redis url: ', redisURl);
const rtokens = redis.createClient({
url: redisURl,
});

rtokens.on('error', (err) => {
console.log(err);
});

export default rtokens;
9 changes: 0 additions & 9 deletions src/data/resourceToken.js

This file was deleted.

25 changes: 8 additions & 17 deletions src/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express from 'express';
import { verify, decode } from 'jsonwebtoken';
import bcrypt from 'bcryptjs';
import util from 'util';
import rtoken from '../data/resourceToken';
import rtoken from '../config/redis';
import * as keys from '../config/keys';
import {
verifyToken,
Expand All @@ -24,19 +24,10 @@ import {
noRedirectState,
} from '../config/keys';
import { Client, User } from '../models/user';
import axios from '../config/axios';

const router = express.Router();
const passport = require('passport');
const HttpsProxyAgent = require('https-proxy-agent');

const axiosDefaultConfig = {
proxy: false,
httpsAgent:
process.env.NODE_ENV !== 'DEV'
? new HttpsProxyAgent('http://devclub.iitd.ac.in:3128')
: null,
};
const axios = require('axios').create(axiosDefaultConfig);
const qs = require('qs');
// post route to check validity of tokens, clients will hit this route.
router.post('/refresh-token', async (req, res) => {
Expand Down Expand Up @@ -69,10 +60,10 @@ router.get('/email/verify/token', async (req, res) => {
} catch (error) {
console.log(error);
res.clearCookie(accessTokenName, {
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
domain: !keys.isDev ? 'devclub.in' : null,
});
res.clearCookie(refreshTokenName, {
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
domain: !keys.isDev ? 'devclub.in' : null,
});
res.render('account_verified', { error: true });
}
Expand Down Expand Up @@ -130,10 +121,10 @@ router.get('/password/reset/token', async (req, res) => {
} catch (error) {
console.log(error);
res.clearCookie(accessTokenName, {
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
domain: !keys.isDev ? 'devclub.in' : null,
});
res.clearCookie(refreshTokenName, {
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
domain: !keys.isDev ? 'devclub.in' : null,
});
res.render('login', {
message: 'Invalid Token. Please try resetting your password again',
Expand Down Expand Up @@ -348,8 +339,8 @@ router.get('/clientVerify', async (req, res) => {
const token = createJWTCookie(user, res, refreshTokenName);
res.cookie('_rememberme', token, {
httpOnly: false,
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
secure: process.env.NODE_ENV !== 'DEV',
domain: !keys.isDev ? 'devclub.in' : null,
secure: !keys.isDev,
});
return res.status(200).json({
err: false,
Expand Down
8 changes: 4 additions & 4 deletions src/routes/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable import/named */
import express from 'express';
import { verifyToken, getUserPrivilege } from '../utils/utils';
import { accessTokenName, refreshTokenName } from '../config/keys';
import { accessTokenName, isDev, refreshTokenName } from '../config/keys';
import settingsRoutes from './settings';
import { Client, SocialAccount, User } from '../models/user';

Expand All @@ -28,10 +28,10 @@ router.post('/', async (req, res) => {
router.post('/logout', (req, res) => {
try {
res.clearCookie(accessTokenName, {
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
domain: !isDev ? 'devclub.in' : null,
});
res.clearCookie(refreshTokenName, {
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
domain: !isDev ? 'devclub.in' : null,
});
return res.json({
err: false,
Expand Down Expand Up @@ -85,7 +85,7 @@ router.post('/delete', async (req, res) => {
await user.remove();

res.clearCookie(accessTokenName, {
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
domain: !isDev ? 'devclub.in' : null,
});
return res.redirect('/');
} catch (error) {
Expand Down
5 changes: 2 additions & 3 deletions src/routes/settings.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable import/named */
import bcrypt from 'bcryptjs';
import { SocialAccount } from '../models/user';
import { accessTokenName } from '../config/keys';
import { accessTokenName, isDev } from '../config/keys';
import { createJWTCookie, verifyToken } from '../utils/utils';

const router = require('express').Router();
Expand Down Expand Up @@ -101,8 +101,7 @@ router.post('/', async (req, res) => {
} else {
// If the validation was successful, update the user and create a new JWT for the updated credentials
res.clearCookie(accessTokenName, {
domain:
process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
domain: !isDev ? 'devclub.in' : null,
});
await createJWTCookie(user, res);
res.render('settings', { messages });
Expand Down
6 changes: 3 additions & 3 deletions src/routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
sendVerificationEmail,
addRoles,
} from '../utils/utils';
import { refreshTokenName } from '../config/keys';
import { isDev, refreshTokenName } from '../config/keys';
import { User } from '../models/user';

const router = express.Router();
Expand Down Expand Up @@ -124,7 +124,7 @@ router.post('/register', async (req, res) => {
username,
email,
password,
isverified: process.env.NODE_ENV === 'DEV',
isverified: isDev,
});

// encrypt the password using bcrypt
Expand All @@ -142,7 +142,7 @@ router.post('/register', async (req, res) => {

addRoles(user);

if (process.env.NODE_ENV !== 'DEV') {
if (!isDev) {
sendVerificationEmail(user);
}

Expand Down
6 changes: 1 addition & 5 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import * as keys from './config/keys';

import { socialAuthenticate, linkSocial } from './utils/utils';

require('dotenv').config({
path: `${__dirname}/../.env`,
});

const app = express();

const passport = require('passport');
Expand Down Expand Up @@ -192,7 +188,7 @@ app.use('/profile', profile);
app.use('/client', client);
app.use('/api', api);

if (process.env.NODE_ENV === 'DEV') {
if (keys.isDev) {
app.use('/test', tests);
}
app.get('/privacy-policy', (req, res) => {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from 'express';
import util from 'util';
import rtoken from '../data/resourceToken';
import rtoken from '../config/redis';
import { makeid } from '../utils/utils';

const router = express.Router();
Expand Down
20 changes: 5 additions & 15 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,7 @@
import jwt, { verify } from 'jsonwebtoken';
import * as keys from '../config/keys';
import { User, SocialAccount, Role } from '../models/user';

const HttpsProxyAgent = require('https-proxy-agent');

const axiosDefaultConfig = {
proxy: false,
httpsAgent:
process.env.NODE_ENV !== 'DEV'
? new HttpsProxyAgent('http://devclub.iitd.ac.in:3128')
: null,
};
const axios = require('axios').create(axiosDefaultConfig);
import axios from '../config/axios';

const getUserPrivilege = (user) => {
let privilege = 0;
Expand Down Expand Up @@ -67,10 +57,10 @@ const createJWTCookie = (user, res, tokenName = keys.accessTokenName) => {
// set the cookie with token with the same age as that of token
res.cookie(tokenName, token, {
maxAge: exp * 1000, // in milli seconds
secure: process.env.NODE_ENV !== 'DEV', // set to true if you are using https
secure: !keys.isDev, // set to true if you are using https
httpOnly: true,
sameSite: 'lax',
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
domain: !keys.isDev ? 'devclub.in' : null,
});
return token;
};
Expand Down Expand Up @@ -120,10 +110,10 @@ const verifyToken = async (
// I wasn't able to verify the token as it was invalid
// clear the tokens
res.clearCookie(keys.accessTokenName, {
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
domain: !keys.isDev ? 'devclub.in' : null,
});
res.clearCookie(keys.refreshTokenName, {
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
domain: !keys.isDev ? 'devclub.in' : null,
});
throw err;
}
Expand Down

0 comments on commit 5542748

Please sign in to comment.