Skip to content
This repository has been archived by the owner on May 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #21 from RodolfoSilva/feature/add-option-to-custom…
Browse files Browse the repository at this point in the history
…ize-password-change

Change password with custom logic
  • Loading branch information
RodolfoSilva authored Jul 24, 2020
2 parents 1f8fd3a + c4463a5 commit 02e9493
Show file tree
Hide file tree
Showing 6 changed files with 558 additions and 875 deletions.
8 changes: 5 additions & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
version: '3.6'
services:
postgres:
image: postgres:10.5
image: postgres:alpine
restart: always
environment:
POSTGRES_PASSWORD: postgres
volumes:
- db_data:/var/lib/postgresql/data
graphql-engine:
image: hasura/graphql-engine:v1.0.0-beta.6
image: hasura/graphql-engine:v1.3.0
ports:
- "8080:8080"
depends_on:
Expand All @@ -18,7 +20,7 @@ services:
- ./wait-for:/wait-for
environment:
# database url to connect
HASURA_GRAPHQL_DATABASE_URL: postgres://postgres:@postgres:5432/postgres
HASURA_GRAPHQL_DATABASE_URL: postgres://postgres:postgres@postgres:5432/postgres
# enable the console served by server
HASURA_GRAPHQL_ENABLE_CONSOLE: 'true' # set "false" to disable console
HASURA_GRAPHQL_AUTH_HOOK: http://hasura-auth:4000/hook
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/uni/hasura/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test('createUser calls fetch with the right args and returns the user id', async
},
};

fetch.mockReturnValue(
(fetch as any).mockReturnValue(
Promise.resolve(new Response(JSON.stringify(serverResponse))),
);

Expand Down
4 changes: 2 additions & 2 deletions src/auth-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as vars from './vars';

export const generateJwtAccessToken = (payload: any) => {
const jwtOptions: jwt.SignOptions = {
algorithm: vars.jwtAlgorithm,
algorithm: vars.jwtAlgorithm as jwt.Algorithm,
expiresIn: vars.jwtTokenExpiresIn,
};

Expand All @@ -14,7 +14,7 @@ export const generateJwtAccessToken = (payload: any) => {

export const generateJwtRefreshToken = (payload: any) => {
const jwtOptions: jwt.SignOptions = {
algorithm: vars.jwtAlgorithm,
algorithm: vars.jwtAlgorithm as jwt.Algorithm,
expiresIn: vars.jwtRefreshTokenExpiresIn,
};

Expand Down
10 changes: 10 additions & 0 deletions src/custom-logic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { User } from './hasura';
import { Request } from 'express';

export function isUserAllowedToChangePassword(
currentUserId: string,
user: User,
req: Request,
) {
return false;
}
25 changes: 13 additions & 12 deletions src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getUserByRefreshToken,
getUserByCredentials,
} from './hasura';
import { isUserAllowedToChangePassword } from './custom-logic';

const getRole = (req: Request) =>
getIn(req, `headers["${vars.hasuraHeaderPrefix}role"]`, '');
Expand Down Expand Up @@ -69,7 +70,7 @@ const checkUserCanDoRegistration = (req: Request): boolean => {

const checkUserIsPartOfStaffOrIsTheCurrentUser = (
req: Request,
userId: string,
user: User,
): boolean => {
if (isAdmin(req)) {
return true;
Expand All @@ -79,20 +80,20 @@ const checkUserIsPartOfStaffOrIsTheCurrentUser = (
.split(',')
.map((role: string) => role.trim());

if (getIntersection(roles, ['admin']).length >= 1) {
const currentUserId = getCurrentUserId(req);

if (
getIntersection(roles, ['admin']).length >= 1 ||
isUserAllowedToChangePassword(currentUserId, user, req)
) {
return true;
}

if (!isAuthenticated(req)) {
return false;
}

try {
const currentUserId = getCurrentUserId(req);
return currentUserId === userId;
} catch (e) {
return false;
}
return currentUserId === user.id;
};

const resolvers = {
Expand Down Expand Up @@ -163,16 +164,16 @@ const resolvers = {
};
},
async auth_change_password(_, { user_id, new_password }, ctx) {
if (!checkUserIsPartOfStaffOrIsTheCurrentUser(ctx.req, user_id)) {
throw new Error('Forbidden');
}

const user: User | undefined = await getUserById(user_id);

if (!user) {
throw new Error('Unable to find user');
}

if (!(await checkUserIsPartOfStaffOrIsTheCurrentUser(ctx.req, user))) {
throw new Error('Forbidden');
}

const result = await changeUserPassword(user, new_password);

return {
Expand Down
Loading

0 comments on commit 02e9493

Please sign in to comment.