Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Add profile filter for passport (#140)
Browse files Browse the repository at this point in the history
* Add profile filter for passport

* Fix tests and clone user
  • Loading branch information
wtrocki authored Oct 2, 2017
1 parent 3879e3a commit 1843eeb
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 13 deletions.
3 changes: 3 additions & 0 deletions cloud/passportauth/example/UserRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ const userSeedData = [
* A sample user service implementation
*/
export class SampleUserService implements UserService {
public getProfile(user: any) {
return user;
}
public validatePassword(user: any, password: string) {
return user.password === password;
}
Expand Down
4 changes: 1 addition & 3 deletions cloud/passportauth/src/auth/DefaultStrategies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ export const webStrategy = (userRepo: UserRepository, userService: UserService)
return (loginId: string, password: string, done: (error: Error | null, user: any) => any) => {
const callback = (err?: Error, user?: any) => {
if (user && userService.validatePassword(user, password)) {
return done(null, user);
return done(null, userService.getProfile(user));
}

return err ? done(err, false) : done(null, false);
};

userRepo.getUserByLogin(loginId, callback);
};
};
Expand Down
3 changes: 2 additions & 1 deletion cloud/passportauth/src/auth/PassportAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ export class PassportAuth implements EndpointSecurity {
if (user && userService.validatePassword(user, req.body.password)) {
const payload = req.body.username;
const token = jwt.sign(payload, secret);
return res.status(200).json({ 'token': token, 'profile': user });
const profile = userService.getProfile(user);
return res.status(200).json({ 'token': token, 'profile': profile });
}
return res.status(401).send();
};
Expand Down
9 changes: 8 additions & 1 deletion cloud/passportauth/src/user/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ export interface UserService {
* @param role - The role required to access a resource
* @returns {boolean} - Returns true/false if the user is authorized to access a resource
*/
hasResourceRole(user: any, role: string|undefined): boolean;
hasResourceRole(user: any, role: string | undefined): boolean;

/**
* Returns profile data for the user
* Function should be used to filter out data that should not be returned to the client.
* For example password hash.
*/
getProfile(user: any): any;
}

export default UserService;
5 changes: 4 additions & 1 deletion cloud/passportauth/test/mocks/MockUserRepo.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { UserRepository, UserService } from '../../src/index';

export const mockUserService: UserService = {
getProfile(user: any) {
return user;
},
validatePassword(user: any, password: string) {
return user.password === password;
},
hasResourceRole(user: any, roleRequired: string|undefined) {
hasResourceRole(user: any, roleRequired: string | undefined) {
if (roleRequired) {
return user.roles.indexOf(roleRequired) > -1;
} else {
Expand Down
13 changes: 6 additions & 7 deletions demo/server/src/modules/passport-auth/DemoUserRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,21 @@ export const users: any[] = require('./users.json');
* Note: This implementation is only for demo purposes.
*/
export class SampleUserService implements UserService {
// Map user object
public getLoginId(user: any) {
return user.username;
}

public validatePassword(user: any, password: string) {
return user.password === password;
}

public hasResourceRole(user: any, role: string|undefined) {
public hasResourceRole(user: any, role: string | undefined) {
if (role) {
return user.roles.indexOf(role) > -1;
} else {
return true;
}
}
public getProfile(user: any) {
const profileData = _.cloneDeep(user);
delete profileData.password;
return profileData;
}
}

/**
Expand Down

0 comments on commit 1843eeb

Please sign in to comment.