Skip to content

Commit

Permalink
Merge pull request #107 from GAIA-X4PLC-AAD/feature/41-API-Export-key…
Browse files Browse the repository at this point in the history
…cloak-config-into-a-keycloak.json

API: Export keycloak config into a keycloak.json (remove hard coded config)
  • Loading branch information
devbysp authored Oct 8, 2024
2 parents 229df53 + 7da2f6b commit 610b949
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 24 deletions.
4 changes: 3 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# React Naming Convention REACT_APP_ https://create-react-app.dev/docs/adding-custom-environment-variables/
REACT_APP_REALM_NAME=gaia-x
REACT_APP_EDGE_API_URI=https://portal.gxfs.dev/api
REACT_APP_FEDERATED_CATALOGUE_API_URL=https://fc-keycloak.gxfs.gx4fm.org/
REACT_APP_FEDERATED_CATALOGUE_API_URL=https://fc-server.gxfs.gx4fm.org
REACT_APP_CLIENT_ID=federated-catalogue
REACT_APP_KEYCLOAK_API_URL=https://fc-keycloak.gxfs.gx4fm.org/
REACT_APP_SD_WIZARD_API=https://sd-creation-wizard-api.gxfs.gx4fm.org
WATCHPACK_POLLING=true

25 changes: 22 additions & 3 deletions config-overrides.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
// Overrides create-react-app webpack configs without ejecting
// https://github.com/timarney/react-app-rewired
const path = require('path');

const { useBabelRc, override } = require('customize-cra');
module.exports = override(useBabelRc());

module.exports = override(
// Use Babel config from .babelrc
useBabelRc(),

// Add Webpack alias for Keycloak configuration based on the environment
(config) => {
const isProduction = process.env.NODE_ENV === 'production';

// Define the path to the correct keycloak-config based on the environment
config.resolve.alias['keycloak-config'] = path.resolve(
__dirname,
isProduction
? 'src/keycloak-config.prod.json' // Production environment
: 'src/keycloak-config.json' // Development environment
);

// Return the modified Webpack config
return config;
}
);
34 changes: 17 additions & 17 deletions src/context/AuthContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import axios from 'axios';
import Keycloak, { KeycloakConfig, KeycloakInitOptions } from 'keycloak-js';
import React, { createContext, useEffect, useState, useMemo } from 'react';

const keycloakConfig: KeycloakConfig = {
realm: 'gaia-x',
clientId: 'portal',
url: 'https://fc-keycloak.gxfs.gx4fm.org/',
};

const keycloak = new Keycloak(keycloakConfig);
import keycloakConfig from 'keycloak-config';
import Keycloak, { KeycloakInitOptions } from 'keycloak-js';
import React, { createContext, useEffect, useMemo, useState } from 'react';

const getDotEnvKeycloakApiUrl = (): string => {
if (!process.env.REACT_APP_KEYCLOAK_API_URL) {
throw new Error('REACT_APP_KEYCLOAK_API_URL is not defined');
}
return process.env.REACT_APP_KEYCLOAK_API_URL;
}

const keycloakInitOptions: KeycloakInitOptions = {
onLoad: 'check-sso',
checkLoginIframe: false,
pkceMethod: 'S256',
};
const keycloak = new Keycloak({
...keycloakConfig.config,
url: getDotEnvKeycloakApiUrl()
});
const initOptions = keycloakConfig.initOptions as KeycloakInitOptions;

export interface AuthContextType {
isAuthenticated: boolean;
Expand All @@ -31,7 +31,7 @@ const defaultAuthContextValues: AuthContextType = {
token: '',
login: () => Promise.resolve(),
logout: () => Promise.resolve(),
hasRole: (role: string) => false,
hasRole: (_role: string) => false,
redirectPath: null,
setRedirectPath: () => {},
};
Expand All @@ -54,7 +54,7 @@ const AuthContextProvider: React.FC<AuthContextProviderProps> = ({
// Initialise Keycloak
useEffect(() => {
keycloak
.init(keycloakInitOptions)
.init(initOptions)
.then((authenticated) => {
setIsAuthenticated(authenticated);
if (authenticated) {
Expand Down
11 changes: 11 additions & 0 deletions src/keycloak-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"config": {
"realm": "gaia-x",
"clientId": "portal"
},
"initOptions": {
"onLoad": "check-sso",
"checkLoginIframe": false,
"pkceMethod": "S256"
}
}
11 changes: 11 additions & 0 deletions src/keycloak-config.prod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"config": {
"realm": "gaia-x",
"clientId": "portal"
},
"initOptions": {
"onLoad": "check-sso",
"checkLoginIframe": false,
"pkceMethod": "S256"
}
}
5 changes: 4 additions & 1 deletion src/services/cypherQueryApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ const getHeaders = () => {
}

function getEndpoint() {
const serverUrl: string = 'https://fc-server.gxfs.gx4fm.org';
if (!process.env.REACT_APP_FEDERATED_CATALOGUE_API_URL) {
throw new Error('REACT_APP_FEDERATED_CATALOGUE_API_URL is not defined');
}
const serverUrl = process.env.REACT_APP_FEDERATED_CATALOGUE_API_URL;
return serverUrl + '/query';
}

Expand Down
11 changes: 9 additions & 2 deletions src/services/schemaApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import axios from 'axios';

import { ShapesAndOntologiesInput } from '../types/ontologies.model';

const SERVER_BASE_URL: string = 'https://fc-server.gxfs.gx4fm.org';
if (!process.env.REACT_APP_FEDERATED_CATALOGUE_API_URL) {
throw new Error('REACT_APP_FEDERATED_CATALOGUE_API_URL is not defined');
}
const SERVER_BASE_URL: string = process.env.REACT_APP_FEDERATED_CATALOGUE_API_URL;

export const fetchAllSchemas = async (): Promise<ShapesAndOntologiesInput> => {
const endpoint = SERVER_BASE_URL + '/schemas';
Expand Down Expand Up @@ -40,7 +43,11 @@ export const getConvertedFile = async (id: string) => {
const formData = new FormData();
formData.append('file', blob, 'shaclFile.shacl');

const endpoint = 'https://sd-creation-wizard-api.gxfs.gx4fm.org/convertFile';
if (!process.env.REACT_APP_SD_WIZARD_API) {
throw new Error('REACT_APP_SD_WIZARD_API is not defined');
}

const endpoint = process.env.REACT_APP_SD_WIZARD_API + '/convertFile';
const response = await axios.post(endpoint, formData, {
headers: {
'Content-Type': 'multipart/form-data'
Expand Down

0 comments on commit 610b949

Please sign in to comment.