Skip to content

Commit

Permalink
Merge pull request #568 from credebl/fix-org-service-issues
Browse files Browse the repository at this point in the history
fix: org client validations & org create validations
  • Loading branch information
nishad-ayanworks authored Mar 5, 2024
2 parents bcb42b4 + 70a7526 commit 103bc39
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
14 changes: 14 additions & 0 deletions apps/organization/repositories/organization.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ export class OrganizationRepository {
}
}


async checkOrganizationSlugExist(orgSlug: string): Promise<organisation> {
try {
return this.prisma.organisation.findUnique({
where: {
orgSlug
}
});
} catch (error) {
this.logger.error(`error in checkOrganizationSlugExist: ${JSON.stringify(error)}`);
throw error;
}
}

/**
*
* @Body createOrgDtp
Expand Down
36 changes: 22 additions & 14 deletions apps/organization/src/organization.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable prefer-destructuring */
import { organisation, user } from '@prisma/client';
import { Injectable, Logger, ConflictException, InternalServerErrorException, HttpException, BadRequestException, ForbiddenException } from '@nestjs/common';
import { Injectable, Logger, ConflictException, InternalServerErrorException, HttpException, BadRequestException, ForbiddenException, UnauthorizedException } from '@nestjs/common';
import { PrismaService } from '@credebl/prisma-service';
import { CommonService } from '@credebl/common';
import { OrganizationRepository } from '../repositories/organization.repository';
Expand Down Expand Up @@ -63,6 +63,13 @@ export class OrganizationService {
}

const orgSlug = this.createOrgSlug(createOrgDto.name);

const isOrgSlugExist = await this.organizationRepository.checkOrganizationSlugExist(orgSlug);

if (isOrgSlugExist) {
throw new ConflictException(ResponseMessages.organisation.error.exists);
}

createOrgDto.orgSlug = orgSlug;
createOrgDto.createdBy = userId;
createOrgDto.lastChangedBy = userId;
Expand Down Expand Up @@ -353,31 +360,32 @@ export class OrganizationService {
}

async clientLoginCredentails(clientCredentials: IClientCredentials): Promise<IAccessTokenData> {

const {clientId, clientSecret} = clientCredentials;
return this.authenticateClientKeycloak(clientId, clientSecret);
}
const {clientId, clientSecret} = clientCredentials;
return this.authenticateClientKeycloak(clientId, clientSecret);
}


async authenticateClientKeycloak(clientId: string, clientSecret: string): Promise<IAccessTokenData> {

try {
const payload = new ClientCredentialTokenPayloadDto();
// eslint-disable-next-line camelcase
payload.client_id = clientId;
// eslint-disable-next-line camelcase
payload.client_secret = clientSecret;
payload.scope = 'email profile';

const payload = new ClientCredentialTokenPayloadDto();
// eslint-disable-next-line camelcase
payload.client_id = clientId;
// eslint-disable-next-line camelcase
payload.client_secret = clientSecret;
payload.scope = 'email profile';

try {
const mgmtTokenResponse = await this.clientRegistrationService.getToken(payload);
return mgmtTokenResponse;
} catch (error) {
throw new UnauthorizedException(ResponseMessages.organisation.error.invalidClient);
}

} catch (error) {
this.logger.error(`Error in authenticateClientKeycloak : ${JSON.stringify(error)}`);
throw new RpcException(error.response ? error.response : error);
}

}

/**
Expand Down
2 changes: 1 addition & 1 deletion apps/user/src/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ export class UserService {
tokenResponse.isRegisteredToSupabase = false;
return tokenResponse;
} catch (error) {
throw new UnauthorizedException(error?.message);
throw new UnauthorizedException(ResponseMessages.user.error.invalidCredentials);
}

} else {
Expand Down
3 changes: 2 additions & 1 deletion libs/common/src/response-messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ export const ResponseMessages = {
invalidInvitationId:'Invalid format for invitation id',
ecosystemIdIsRequired:'ecosystemId is required',
roleNotMatch: 'User does not have access',
orgDoesNotMatch: 'Organization does not match'
orgDoesNotMatch: 'Organization does not match',
invalidClient: 'Invalid client credentials'
}
},

Expand Down

0 comments on commit 103bc39

Please sign in to comment.