Skip to content

Commit

Permalink
Merge pull request #12 from ThreeDify/dev
Browse files Browse the repository at this point in the history
Dev => Master
  • Loading branch information
silwalanish authored Nov 13, 2020
2 parents 50867aa + b6c1fb4 commit 49c37db
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 40 deletions.
10 changes: 10 additions & 0 deletions src/domain/AccessTokenPayload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface AccessTokenPayloadData {
id: number;
random: Buffer;
}

export interface AccessTokenPayload {
data: AccessTokenPayloadData;
}

export default AccessTokenPayload;
25 changes: 8 additions & 17 deletions src/services/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import Debug, { Debugger } from 'debug';

import userService from './users';
import User from '../models/User';
import tokenService from './tokens';
import Token from '../models/Token';
import { NewUser } from '../domain/NewUser';
import { hash, compare } from '../utils/hash';
import { verifyTokenSign } from '../utils/tokens';
import { verifyAndDecodeAccessToken } from '../utils/tokens';
import AccessTokenPayload from '../domain/AccessTokenPayload';
import { LoginCredential, TokenCredential } from '../domain/login';

const debug: Debugger = Debug('threedify:services:auth');
Expand Down Expand Up @@ -67,27 +66,19 @@ export async function authenticate(
return;
}

debug('Check if token is not revoked.');
const token: Token | undefined = await tokenService.fetchTokenByAccessToken(
debug('Check if the access token is valid.');
const payload = verifyAndDecodeAccessToken(
tokenCred.accessToken
);
) as AccessTokenPayload;

if (token) {
if (payload) {
debug('Check if user exists.');
const user: User | undefined = await userService.fetchUserById(
token.userId,
{
withPassword: true,
}
payload.data.id
);

if (user) {
debug('Check if the tokens are valid.');
const [isAccessTokenValid, _] = verifyTokenSign(tokenCred, user);

if (isAccessTokenValid) {
return user;
}
return user;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export async function refreshTokens(

if (isRefreshTokenValid) {
debug('Generate new access token.');
const refreshedAccessToken = generateAccessToken();
const refreshedAccessToken = generateAccessToken(user);

debug('Update access token in database.');
await Token.query()
Expand Down
9 changes: 9 additions & 0 deletions src/utils/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ export function verify(token: string, secret: string): boolean {
}
}

export function decode(token: string, secret: string): boolean | any {
try {
return jwt.verify(token, secret);
} catch (err) {
return false;
}
}

export default {
sign,
verify,
decode,
};
39 changes: 17 additions & 22 deletions src/utils/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ import jwt from './jwt';
import config from '../config';
import User from '../models/User';
import { TokenCredential } from '../domain/login';
import AccessTokenPayload from '../domain/AccessTokenPayload';

const debug: Debugger = Debug('threedify:utils:tokens');

export function generateAccessToken(): string {
export function generateAccessToken(user: User): string {
debug('Generating access token.');

return jwt.sign(
{ data: crypto.randomBytes(256) },
{ data: { id: user.id, random: crypto.randomBytes(256) } },
config.accessTokenSecret,
config.accessTokenConfig
);
}

export function generateTokens(user: User): TokenCredential {
const accessToken: string = generateAccessToken();
const accessToken: string = generateAccessToken(user);

debug('Generating refresh token.');
const refreshToken: string = jwt.sign(
Expand Down Expand Up @@ -55,35 +56,29 @@ export function verifyTokenSign(tokens: TokenCredential, user: User) {
config.refreshTokenSecret + user.password
);
}

return [isAccessTokenValid, isRefreshTokenValid];
}

export function refresh(refreshToken: string): TokenCredential | boolean {
debug('Check if refresh token exists.');
if (!refreshToken) {
return false;
}

debug('Verifing refresh token.');
const isRefreshTokenValid: boolean = jwt.verify(
refreshToken,
config.refreshTokenSecret
);
export function verifyAndDecodeAccessToken(
accessToken: string
): boolean | AccessTokenPayload {
debug('Check if access token exists.');
if (accessToken) {
debug('Verifing and decoding access token.');
let isAccessTokenValid: boolean | AccessTokenPayload = jwt.decode(
accessToken,
config.accessTokenSecret
);

if (isRefreshTokenValid) {
return {
refreshToken,
accessToken: generateAccessToken(),
};
return isAccessTokenValid;
}

return false;
}

export default {
generateAccessToken,
generateTokens,
verifyTokenSign,
refresh,
generateAccessToken,
verifyAndDecodeAccessToken,
};

0 comments on commit 49c37db

Please sign in to comment.