Skip to content
Open
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
50 changes: 44 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion services/service-core/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
/dist
/node_modules

.env
.local.env

# Logs
logs
*.log
Expand Down
5 changes: 3 additions & 2 deletions services/service-core/nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
}
"deleteOutDir": true,
"assets": [{"include": "**/*.yaml", "outDir": "./dist"}]
}
}
5 changes: 4 additions & 1 deletion services/service-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@
"@apollo/server": "^4.9.1",
"@nestjs/apollo": "^12.0.7",
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.0.0",
"@nestjs/config": "^3.1.1",
"@nestjs/core": "^10.0.0",
"@nestjs/graphql": "^12.0.8",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/typeorm": "^10.0.0",
"apollo-server-express": "^3.12.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"dotenv": "^16.3.1",
"graphql": "^16.8.0",
"highlight.js": "^11.8.0",
"js-yaml": "^4.1.0",
"joi": "^17.11.0",
"jsonwebtoken": "^9.0.2",
"keycloak-connect": "^21.1.2",
Expand All @@ -49,6 +51,7 @@
"@nestjs/testing": "^10.0.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/js-yaml": "^4.0.8",
"@types/jsonwebtoken": "^9.0.3",
"@types/node": "^20.3.1",
"@types/supertest": "^2.0.12",
Expand Down
56 changes: 31 additions & 25 deletions services/service-core/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,54 @@ import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { DatabaseModule } from './database/database.module';
import { User } from './database/users/users.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { ConfigModule } from '@nestjs/config';
import { KeycloakConnectModule } from 'nest-keycloak-connect';
import { KeycloakAuthGuard } from './auth/keycloak.guard';
import { CustomConfigService } from './config/config.service';

@Module({
imports: [
TypeOrmModule.forRootAsync({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: '.local.env'
isGlobal: true
})
],
useFactory: (configService: ConfigService) => ({
type: 'postgres',
host: configService.get('DB_HOST'),
port: Number(configService.get('DB_PORT')),
username: configService.get('DB_USERNAME'),
password: configService.get('DB_PASSWORD'),
database: configService.get('DB_DATABASE'),
entities: [User],
synchronize: true
}),
inject: [ConfigService]
useFactory: () => {
const configService = new CustomConfigService();
const { database } = configService.get();
return {
type: 'postgres',
host: database.DB_HOST,
port: Number(database.DB_PORT),
username: database.DB_USERNAME,
password: database.DB_PASSWORD,
database: database.DB_DATABASE,
entities: [User],
synchronize: true
};
}
}),
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
autoSchemaFile: join(process.cwd(), 'src/schema.gql')
}),

KeycloakConnectModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
authServerUrl: configService.get('KEYCLOAK_SERVER_URL'),
realm: 'humanitech',
resource: 'nest-application',
secret: configService.get('KEYCLOAK_SECRET'),
'public-client': true,
verifyTokenAudience: true,
'confidential-port': 0
}),
inject: [ConfigService]
useFactory: async () => {
const configService = new CustomConfigService();
const { keycloak } = configService.get();

return {
authServerUrl: keycloak.authServerUrl,
realm: keycloak.realm,
resource: keycloak.nestClientId,
secret: keycloak.ADMIN_CLIENT_SECRET,
'public-client': true,
verifyTokenAudience: true,
'confidential-port': 0
};
}
}),

DatabaseModule,
Expand Down
30 changes: 0 additions & 30 deletions services/service-core/src/auth/config.ts

This file was deleted.

32 changes: 21 additions & 11 deletions services/service-core/src/auth/keycloak.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@
import { Injectable } from '@nestjs/common';
import { verify } from 'jsonwebtoken';
import axios from 'axios';
import { ConfigService } from '@nestjs/config';
import { Config } from './config';
import { CustomConfigService } from '../config/config.service';
import { UpdateUser } from 'src/graphql/users/users.entity';
import { userInputValidator } from './keycloak.validator';

@Injectable()
export class KeycloakService {
constructor(private readonly configService: ConfigService) {}
constructor(private readonly configService: CustomConfigService) {}
private readonly config = this.configService.get();

async getPublicKey() {
const { realmUrl } = this.config.keycloak;
try {
const response = await axios.get(Config.realmUrl);
const response = await axios.get(realmUrl);
const publicKey = response.data.public_key;
return `-----BEGIN PUBLIC KEY-----\n${publicKey}\n-----END PUBLIC KEY-----`;
} catch (error) {
Expand All @@ -33,18 +34,26 @@ export class KeycloakService {
}

async getAdminToken() {
const {
realmUrl,
grantType,
clientId,
KEYCLOAK_ADMIN,
KEYCLOAK_ADMIN_PASSWORD,
ADMIN_CLIENT_SECRET
} = this.config.keycloak;
const params = new URLSearchParams({
username: this.configService.get(Config.keycloakAdmin),
password: this.configService.get(Config.keycloakAdminPassword),
grant_type: Config.grantType,
client_id: Config.clientId,
client_secret: this.configService.get(Config.adminClientSecret)
username: KEYCLOAK_ADMIN,
password: KEYCLOAK_ADMIN_PASSWORD,
grant_type: grantType,
client_id: clientId,
client_secret: ADMIN_CLIENT_SECRET
});

const requestBody = params.toString();
try {
const getTokenData = await fetch(
`${Config.realmUrl}/protocol/openid-connect/token`,
`${realmUrl}/protocol/openid-connect/token`,
{
method: 'POST',
headers: {
Expand Down Expand Up @@ -85,13 +94,14 @@ export class KeycloakService {
}

async editUser(id: string, userInput: UpdateUser) {
const { adminUrl } = this.config.keycloak;
const accessToken = await this.getAdminToken();
const { error } = userInputValidator.validate(userInput);
if (error) {
throw new Error(error.details[0].message);
}

const updateUser = await fetch(`${Config.adminUrl}/users/${id}`, {
const updateUser = await fetch(`${adminUrl}/users/${id}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Expand Down
46 changes: 0 additions & 46 deletions services/service-core/src/auth/test/config.spec.ts

This file was deleted.

Loading