Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: org client validations & org create validations #568

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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