Skip to content

Commit

Permalink
Merge pull request #58 from credebl/27-supabase-integration
Browse files Browse the repository at this point in the history
Supabase integration: 27
  • Loading branch information
KulkarniShashank authored Aug 25, 2023
2 parents 7db3911 + 4e0de54 commit 205a95d
Show file tree
Hide file tree
Showing 29 changed files with 382 additions and 222 deletions.
6 changes: 4 additions & 2 deletions apps/api-gateway/src/authz/authz.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import { JwtStrategy } from './jwt.strategy';
import { MobileJwtStrategy } from './mobile-jwt.strategy';
import { Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { VerificationService } from '../verification/verification.service';
import { SocketGateway } from './socket.gateway';
import { SupabaseService } from '@credebl/supabase';
import { UserModule } from '../user/user.module';
import { UserService } from '../user/user.service';
import { VerificationService } from '../verification/verification.service';

//import { WebhookService } from "../../../platform-service/src/webhook/webhook.service";

Expand Down Expand Up @@ -46,7 +47,8 @@ import { UserService } from '../user/user.service';
ConnectionService,
AgentService,
CommonService,
UserService
UserService,
SupabaseService
],
exports: [
PassportModule,
Expand Down
31 changes: 5 additions & 26 deletions apps/api-gateway/src/authz/jwt.strategy.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
// src/authz/jwt.strategy.ts

import * as dotenv from 'dotenv';
import * as jwt from 'jsonwebtoken';

import { ExtractJwt, Strategy } from 'passport-jwt';
import { Injectable, Logger } from '@nestjs/common';

import { CommonConstants } from '@credebl/common/common.constant';
import { JwtPayload } from './jwt-payload.interface';
import { NotFoundException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { UserService } from '../user/user.service';
import { passportJwtSecret } from 'jwks-rsa';

dotenv.config();

Expand All @@ -23,33 +18,17 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
private readonly usersService: UserService
) {
super({

secretOrKeyProvider: (request, jwtToken, done) => {
const decodedToken = jwt.decode(jwtToken) as jwt.JwtPayload;
const audiance = decodedToken.iss.toString();
const jwtOptions = {
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: `${audiance}${CommonConstants.URL_KEYCLOAK_JWKS}`
};
const secretprovider = passportJwtSecret(jwtOptions);
let certkey;
secretprovider(request, jwtToken, async (err, data) => {
certkey = data;
done(null, certkey);
});
},
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
algorithms: ['RS256']
secretOrKey: process.env.SUPABASE_JWT_SECRET,
ignoreExpiration: false
});
}
async validate(payload: JwtPayload): Promise<object> {

const userDetails = await this.usersService.findUserByKeycloakId(payload?.sub);
const userDetails = await this.usersService.findUserinSupabase(payload.sub);

if (!userDetails.response) {
throw new NotFoundException('Keycloak user not found');
throw new NotFoundException('User not found');
}

return {
Expand Down
5 changes: 3 additions & 2 deletions apps/api-gateway/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,17 @@ export class UserService extends BaseService {
}
}

async findUserByKeycloakId(id: string): Promise<{ response: object }> {
async findUserinSupabase(id: string): Promise<{ response: object }> {
const payload = { id };

try {
return this.sendNats(this.serviceProxy, 'get-user-by-keycloak-id', payload);
return this.sendNats(this.serviceProxy, 'get-user-by-supabase', payload);
} catch (error) {
this.logger.error(`Error in get user:${JSON.stringify(error)}`);
}
}


async invitations(id: number, status: string, getAllInvitationsDto: GetAllInvitationsDto): Promise<{ response: object }> {
const {pageNumber, pageSize, search} = getAllInvitationsDto;
const payload = { id, status, pageNumber, pageSize, search };
Expand Down
2 changes: 1 addition & 1 deletion apps/user/interfaces/user.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface UserI {
isEmailVerified?: boolean,
clientId?: string,
clientSecret?: string,
keycloakUserId?: string,
supabaseUserId?: string,
userOrgRoles?: object
}

Expand Down
3 changes: 0 additions & 3 deletions apps/user/repositories/fido-user.repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as bcrypt from 'bcrypt';

import { Injectable, Logger, NotFoundException } from '@nestjs/common';
import { CreateUserDto } from '../dtos/create-user.dto';
import { InternalServerErrorException } from '@nestjs/common';
Expand Down Expand Up @@ -34,7 +32,6 @@ export class FidoUserRepository {
email: createUserDto.email,
firstName: createUserDto.firstName,
lastName: createUserDto.lastName,
password: await bcrypt.hash(createUserDto.password, 10),
verificationCode: verifyCode
}
});
Expand Down
28 changes: 11 additions & 17 deletions apps/user/repositories/user.repository.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable prefer-destructuring */

import * as bcrypt from 'bcrypt';
import { Injectable, Logger, NotFoundException } from '@nestjs/common';
import { UpdateUserProfile, UserEmailVerificationDto, UserI, userInfo } from '../interfaces/user.interface';

import { InternalServerErrorException } from '@nestjs/common';
import { PrismaService } from '@credebl/prisma-service';
import { UpdateUserProfile, UserEmailVerificationDto, UserI, userInfo } from '../interfaces/user.interface';
// eslint-disable-next-line camelcase
import { user } from '@prisma/client';
import { v4 as uuidv4 } from 'uuid';
Expand Down Expand Up @@ -136,23 +136,22 @@ export class UserRepository {
* @param id
* @returns User data
*/
async getUserByKeycloakId(id: string): Promise<object> {
async getUserBySupabaseId(id: string): Promise<object> {
try {
return this.prisma.user.findFirst({
where: {
keycloakUserId: id
supabaseUserId: id
},
select: {
id: true,
username: true,
password: false,
email: true,
firstName: true,
lastName: true,
isEmailVerified: true,
clientId: true,
clientSecret: true,
keycloakUserId: true,
supabaseUserId: true,
userOrgRoles: {
include: {
orgRole: true,
Expand Down Expand Up @@ -195,7 +194,6 @@ export class UserRepository {
select: {
id: true,
username: true,
password: false,
email: true,
firstName: true,
lastName: true,
Expand All @@ -204,7 +202,7 @@ export class UserRepository {
isEmailVerified: true,
clientId: true,
clientSecret: true,
keycloakUserId: true,
supabaseUserId: true,
userOrgRoles: {
include: {
orgRole: true,
Expand Down Expand Up @@ -241,7 +239,6 @@ export class UserRepository {
select: {
id: true,
username: true,
password: false,
email: true,
firstName: true,
lastName: true,
Expand Down Expand Up @@ -274,15 +271,15 @@ export class UserRepository {
* @returns Updates organization details
*/
// eslint-disable-next-line camelcase
async updateUserDetails(id: number, keycloakUserId: string): Promise<user> {
async updateUserDetails(id: number, supabaseUserId: string): Promise<user> {
try {
const updateUserDetails = await this.prisma.user.update({
where: {
id
},
data: {
isEmailVerified: true,
keycloakUserId
supabaseUserId
}
});
return updateUserDetails;
Expand All @@ -306,8 +303,7 @@ export class UserRepository {
},
data: {
firstName: userInfo.firstName,
lastName: userInfo.lastName,
password: await bcrypt.hash(userInfo.password, 10)
lastName: userInfo.lastName
}
});
return updateUserDetails;
Expand All @@ -333,14 +329,13 @@ export class UserRepository {
select: {
id: true,
username: true,
password: false,
email: true,
firstName: true,
lastName: true,
isEmailVerified: true,
clientId: true,
clientSecret: true,
keycloakUserId: true,
supabaseUserId: true,
userOrgRoles: {
where: {
...filterOptions
Expand Down Expand Up @@ -399,14 +394,13 @@ export class UserRepository {
select: {
id: true,
username: true,
password: false,
email: true,
firstName: true,
lastName: true,
isEmailVerified: true,
clientId: false,
clientSecret: false,
keycloakUserId: false
supabaseUserId: false
},
take: pageSize,
skip: (pageNumber - 1) * pageSize,
Expand Down
4 changes: 3 additions & 1 deletion apps/user/src/fido/fido.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { KeycloakUrlService } from '@credebl/keycloak-url';
import { OrgRolesRepository } from 'libs/org-roles/repositories';
import { OrgRolesService } from '@credebl/org-roles';
import { PrismaService } from '@credebl/prisma-service';
import { SupabaseService } from '@credebl/supabase';
import { UserActivityRepository } from 'libs/user-activity/repositories';
import { UserActivityService } from '@credebl/user-activity';
import { UserDevicesRepository } from '../../repositories/user-device.repository';
Expand All @@ -32,7 +33,7 @@ import { UserService } from '../user.service';
]),
HttpModule,
CommonModule
],
],
controllers: [FidoController],
providers: [
UserService,
Expand All @@ -41,6 +42,7 @@ import { UserService } from '../user.service';
UserRepository,
UserDevicesRepository,
ClientRegistrationService,
SupabaseService,
Logger,
KeycloakUrlService,
FidoUserRepository,
Expand Down
11 changes: 6 additions & 5 deletions apps/user/src/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ export class UserController {
return this.userService.updateUserProfile(payload.updateUserProfileDto);
}

@MessagePattern({ cmd: 'get-user-by-keycloak-id' })
async findByKeycloakId(payload: { id }): Promise<object> {
return this.userService.findByKeycloakId(payload);
@MessagePattern({ cmd: 'get-user-by-supabase' })
async findSupabaseUser(payload: { id }): Promise<object> {
return this.userService.findSupabaseUser(payload);
}


@MessagePattern({ cmd: 'get-user-by-mail' })
async findUserByEmail(payload: { email }): Promise<object> {
return this.userService.findUserByEmail(payload);
return this.userService.findUserByEmail(payload);
}

@MessagePattern({ cmd: 'get-org-invitations' })
Expand Down Expand Up @@ -106,7 +107,7 @@ export class UserController {
}
@MessagePattern({ cmd: 'add-user' })
async addUserDetailsInKeyCloak(payload: { userEmail: string, userInfo: userInfo }): Promise<string | object> {
return this.userService.createUserInKeyCloak(payload.userEmail, payload.userInfo);
return this.userService.createUserForToken(payload.userEmail, payload.userInfo);
}

// Fetch Users recent activities
Expand Down
4 changes: 3 additions & 1 deletion apps/user/src/user.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { FidoModule } from './fido/fido.module';
import { KeycloakUrlService } from '@credebl/keycloak-url';
import { OrgRolesRepository } from 'libs/org-roles/repositories';
import { PrismaService } from '@credebl/prisma-service';
import { SupabaseService } from '@credebl/supabase';
import { UserActivityRepository } from 'libs/user-activity/repositories';
import { UserActivityService } from '@credebl/user-activity';
import { UserController } from './user.controller';
Expand All @@ -30,14 +31,15 @@ import { UserService } from './user.service';
CommonModule,
FidoModule,
OrgRolesModule
],
],
controllers: [UserController],
providers: [
UserService,
UserRepository,
PrismaService,
Logger,
ClientRegistrationService,
SupabaseService,
KeycloakUrlService,
OrgRolesService,
UserOrgRolesService,
Expand Down
Loading

0 comments on commit 205a95d

Please sign in to comment.