Skip to content

Commit

Permalink
fixes for keycloak and aut and registation for migrant app
Browse files Browse the repository at this point in the history
  • Loading branch information
gioppoluca committed Jun 30, 2024
1 parent b3ed1cc commit 1db8950
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 124 deletions.
50 changes: 27 additions & 23 deletions application/micado-backend/src/application.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
// ---------- ADD IMPORTS -------------
import {AuthenticationComponent} from '@loopback/authentication';
//import { JWTAuthenticationComponent} from '@loopback/authentication-jwt';
//import {AuthenticationComponent, Strategies} from 'loopback4-authentication';
import {BootMixin} from '@loopback/boot';
import {ApplicationConfig} from '@loopback/core';
import {RepositoryMixin} from '@loopback/repository';
import {RestApplication} from '@loopback/rest';
import {CrudRestComponent} from '@loopback/rest-crud';
import { AuthenticationComponent } from '@loopback/authentication';
import { BootMixin } from '@loopback/boot';
import { ApplicationConfig } from '@loopback/core';
import { RepositoryMixin } from '@loopback/repository';
import { RestApplication } from '@loopback/rest';
import { CrudRestComponent } from '@loopback/rest-crud';
import {
RestExplorerBindings,
RestExplorerComponent
} from '@loopback/rest-explorer';
import {ServiceMixin} from '@loopback/service-proxy';
import { ServiceMixin } from '@loopback/service-proxy';
import multer from 'multer';
import path from 'path';
import {MySequence} from './sequence';
import {FILE_UPLOAD_SERVICE, STORAGE_DIRECTORY} from './services/file-upload-service.service';
//import { KeycloakVerifyProvider } from './modules/keycloak-verify.provider';
import {registerAuthenticationStrategy} from '@loopback/authentication';
import {MicadoAuthenticationStrategy} from './modules/micado-strategy'
import { MySequence } from './sequence';
import { FILE_UPLOAD_SERVICE, STORAGE_DIRECTORY } from './services/file-upload-service.service';
import { registerAuthenticationStrategy } from '@loopback/authentication';
import { MicadoAuthenticationStrategy } from './modules/micado-strategy'


/**
* The main application class for the Micado Backend application.
* This class sets up the application configuration, including:
* - CORS settings
* - Custom sequence handler
* - Static file serving
* - REST explorer configuration
* - File upload configuration
* - Controller and component registration
* - Authentication system setup
*/
export class MicadoBackendApplication extends BootMixin(
ServiceMixin(RepositoryMixin(RestApplication)),
) {
Expand All @@ -27,12 +36,12 @@ export class MicadoBackendApplication extends BootMixin(
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
preflightContinue: true,
allowedHeaders:['Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'Authorization', 'Access-Control-Allow-Origin'],
allowedHeaders: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'Authorization', 'Access-Control-Allow-Origin'],
optionsSuccessStatus: 204,
maxAge: 86400,
credentials: false,
}

}) {
super(options);

Expand Down Expand Up @@ -62,20 +71,15 @@ export class MicadoBackendApplication extends BootMixin(
},
};
this.component(CrudRestComponent);
// ------ ADD SNIPPET AT THE BOTTOM ---------
// Mount authentication system
this.component(AuthenticationComponent);
registerAuthenticationStrategy(this, MicadoAuthenticationStrategy);
// Mount jwt component
// this.component(JWTAuthenticationComponent);
// Customize authentication verify handlers
//this.bind(Strategies.Passport.KEYCLOAK_VERIFIER).toProvider( KeycloakVerifyProvider, );
}

/**
* Configure `multer` options for file upload
*/
protected configureFileUpload (destination?: string) {
protected configureFileUpload(destination?: string) {
// Upload files to `dist/.sandbox` by default
destination = destination ?? path.join(__dirname, '../.sandbox');
this.bind(STORAGE_DIRECTORY).to(destination);
Expand Down
2 changes: 0 additions & 2 deletions application/micado-backend/src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ export class UserController {
content: {
'application/json': {
schema: getModelSchemaRef(User, {
title: 'NewUser',

}),
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ export class UseroneController {
content: {
'application/json': {
schema: getModelSchemaRef(Userone, {
title: 'NewUser',

}),
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
{
"template": {
"method": "POST",
"url": "https://{baseurl}/auth/admin/realms/{realm}/users",
"url": "https://{baseurl}/admin/realms/{realm}/users",
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand Down Expand Up @@ -92,7 +92,7 @@
{
"template": {
"method": "POST",
"url": "https://{baseurl}/auth/admin/realms/{realm}/users",
"url": "https://{baseurl}/admin/realms/{realm}/users",
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand Down Expand Up @@ -203,7 +203,7 @@
{
"template": {
"method": "PUT",
"url": "https://{baseurl}/auth/admin/realms/{realm}/users/{userid}",
"url": "https://{baseurl}/admin/realms/{realm}/users/{userid}",
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand Down Expand Up @@ -236,7 +236,7 @@
{
"template": {
"method": "POST",
"url": "https://{baseurl}/auth/admin/realms/{realm}/groups",
"url": "https://{baseurl}/admin/realms/{realm}/groups",
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand All @@ -262,7 +262,7 @@
{
"template": {
"method": "PUT",
"url": "https://{baseurl}/auth/admin/realms/{realm}/users/{userId}/groups/{groupId}",
"url": "https://{baseurl}/admin/realms/{realm}/users/{userId}/groups/{groupId}",
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand All @@ -286,7 +286,7 @@
{
"template": {
"method": "GET",
"url": "https://{baseurl}/auth/admin/realms/{realm}/clients/{clientId}/roles",
"url": "https://{baseurl}/admin/realms/{realm}/clients/{clientId}/roles",
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand All @@ -309,7 +309,7 @@
{
"template": {
"method": "GET",
"url": "https://{baseurl}/auth/admin/realms/{realm}/groups/{groupId}/members",
"url": "https://{baseurl}/admin/realms/{realm}/groups/{groupId}/members",
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand All @@ -332,7 +332,7 @@
{
"template": {
"method": "GET",
"url": "https://{baseurl}/auth/admin/realms/{realm}/groups",
"url": "https://{baseurl}/admin/realms/{realm}/groups",
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand All @@ -354,7 +354,7 @@
{
"template": {
"method": "GET",
"url": "https://{baseurl}/auth/admin/realms/{realm}/users/{id}",
"url": "https://{baseurl}/admin/realms/{realm}/users/{id}",
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand All @@ -377,7 +377,7 @@
{
"template": {
"method": "GET",
"url": "https://{baseurl}/auth/admin/realms/{realm}/users",
"url": "https://{baseurl}/admin/realms/{realm}/users",
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand All @@ -399,7 +399,7 @@
{
"template": {
"method": "GET",
"url": "https://{baseurl}/auth/admin/realms/{realm}/groups",
"url": "https://{baseurl}/admin/realms/{realm}/groups",
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand All @@ -421,7 +421,7 @@
{
"template": {
"method": "GET",
"url": "https://{baseurl}/auth/admin/realms/{realm}/roles",
"url": "https://{baseurl}/admin/realms/{realm}/roles",
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand All @@ -443,7 +443,7 @@
{
"template": {
"method": "GET",
"url": "https://{baseurl}/auth/admin/realms/{realm}/clients?clientId={clientId}",
"url": "https://{baseurl}/admin/realms/{realm}/clients?clientId={clientId}",
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand All @@ -466,7 +466,7 @@
{
"template": {
"method": "GET",
"url": "https://{baseurl}/auth/admin/realms/{realm}/clients/{clientId}/roles?name={roleName}",
"url": "https://{baseurl}/admin/realms/{realm}/clients/{clientId}/roles?name={roleName}",
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand All @@ -490,7 +490,7 @@
{
"template": {
"method": "POST",
"url": "https://{baseurl}/auth/admin/realms/{realm}/users/{userid}/role-mappings/realm",
"url": "https://{baseurl}/admin/realms/{realm}/users/{userid}/role-mappings/realm",
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand All @@ -515,7 +515,7 @@
{
"template": {
"method": "DELETE",
"url": "https://{baseurl}/auth/admin/realms/{realm}/users/{userid}/role-mappings/realm",
"url": "https://{baseurl}/admin/realms/{realm}/users/{userid}/role-mappings/realm",
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand All @@ -540,7 +540,7 @@
{
"template": {
"method": "GET",
"url": "https://{baseurl}/auth/admin/realms/{realm}/users/{userid}/role-mappings/realm",
"url": "https://{baseurl}/admin/realms/{realm}/users/{userid}/role-mappings/realm",
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand All @@ -563,7 +563,7 @@
{
"template": {
"method": "GET",
"url": "https://{baseurl}/auth/admin/realms/{realm}/users?username={username}",
"url": "https://{baseurl}/admin/realms/{realm}/users?username={username}",
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand Down
2 changes: 1 addition & 1 deletion application/micado-backend/src/models/user.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {UserPreferences} from './user-preferences.model';
import {UserConsent} from './user-consent.model';
import {Tenant} from './tenant.model';

@model()
@model({ settings: { forceId: false,idInjection: false, postgresql: {schema: 'micadoapp', table: 'user'} } })
export class User extends Entity {
@property({
type: 'string',
Expand Down
13 changes: 7 additions & 6 deletions application/micado-backend/src/models/userone.model.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {Entity, model, property, hasMany, hasOne} from '@loopback/repository';
import {IndividualInterventionPlan} from './individual-intervention-plan.model';
import {UserPictures} from './user-pictures.model';
import {UserPreferences} from './user-preferences.model';
import {UserConsent} from './user-consent.model';
import { Entity, model, property, hasMany, hasOne } from '@loopback/repository';

@model()

@model({ settings: { forceId: false,idInjection: false, postgresql: {schema: 'micadoapp', table: 'user'} } })
export class Userone extends Entity {
@property({
type: 'string',
Expand All @@ -24,6 +21,10 @@ export class Userone extends Entity {
})
group?: string;


constructor(data?: Partial<Userone>) {
super(data);
}
}

export interface UseroneRelations {
Expand Down
35 changes: 5 additions & 30 deletions application/micado-backend/src/modules/micado-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ export class MicadoAuthenticationStrategy implements AuthenticationStrategy {
// const userProfile = this.userService.convertToUserProfile(user);
// const up = this.userService.convertToUserProfile(null)
console.log('we are in authenticate of micadoauthstrategy')
//console.log(request.headers)
//console.log(request.headers.authorization)
if (request.headers.authorization) {

const tokenparts: any = request.headers.authorization?.split(' ')
Expand All @@ -40,7 +38,7 @@ export class MicadoAuthenticationStrategy implements AuthenticationStrategy {
var iss_array = iss.split("/");
var realm = iss_array[iss_array.length - 1]
console.log(realm)
console.log('prima di keycloak')
console.log('calling keycloak')
console.log('https://' + process.env.IDENTITY_HOSTNAME + '/realms/' + realm + '/protocol/openid-connect/userinfo')

const axios = require('axios').default;
Expand All @@ -52,50 +50,27 @@ export class MicadoAuthenticationStrategy implements AuthenticationStrategy {

}
).then(function (response: any) {
console.log('response')
console.log(response)
if (response.status != 200) {
console.log('error in response')
console.log(response)
return Promise.reject(undefined)
//return undefined
}
else {
let uu = new AuthUser({ username: 'pippo' })
let uu = new AuthUser({ id: response.data.sub, email: response.data.email, username: response.data.preferred_username, firstName: response.data.given_name, lastName: response.data.family_name })
return Promise.resolve(uu);
}
})
.catch(function (error: any) {
console.log('error in catch')
console.log(error);
return Promise.reject(undefined)
//return undefined


})
}
else {
return undefined
}
/* .then(function () {
// always executed
});*/

/*
const keycloak = require('keycloak-backend')({
"realm": "micado",
"auth-server-url": "http://keycloak:8100",
"client_id": "migrant",
// "client_secret": "c88a2c21-9d1a-4f83-a18d-66d75c4d8020", // if required
"username": "admin",
"password": "Pa55w0rd"
});
console.log(keycloak)
console.log('prima verify')
let token = await keycloak.jwt.verify(tokenparts[1]);
console.log(token.isExpired());
console.log(token.hasRealmRole('user'));
*/
//}
/*let uu = new AuthUser({username: 'pippo'})
return uu;*/
}
}
Loading

0 comments on commit 1db8950

Please sign in to comment.