@@ -541,7 +541,7 @@ At runtime, the **Spring Boot IAM SDK** (specific to each provider) synchronizes
541541 │ Source Generator → Generates PermissionsRegistry.java │
542542 │ ↓ (Runtime) │
543543 │ IAM Core SDK │
544- │ PermissionSyncRunner │
544+ │ AuthorizationSyncRunner │
545545 │ ↓ │
546546 │ AccessManagementClient → Access Management API │
547547 │ │
@@ -749,7 +749,7 @@ public final class PermissionsRegistry {
749749
750750** 5. Runtime Synchronization via Provider SDK**
751751
752- The IAM Core has a ** Spring Boot SDK module** that includes an autoconfigured ` PermissionSyncRunner ` .
752+ The IAM Core has a ** Spring Boot SDK module** that includes an autoconfigured ` AuthorizationSyncRunner ` .
753753
754754This runner:
755755
@@ -764,19 +764,21 @@ This runner:
764764** Package** :
765765` cv.igrp.framework.auth.core.autoconfig `
766766
767- ** PermissionSyncRunner .java**
767+ ** AuthorizationSyncRunner .java**
768768
769769``` java
770770package cv.igrp.framework.auth.core.autoconfig ;
771771
772772import cv.igrp.framework.auth.generated.PermissionsRegistry ;
773773import cv.igrp.platform.access.client.ApiClient ;
774+ import cv.igrp.platform.access.client.api.M2MApi ;
774775import cv.igrp.platform.access.client.constants.Status ;
775776import cv.igrp.platform.access.client.model.PermissionDTO ;
777+ import cv.igrp.platform.access.client.model.ResourceDTO ;
776778import jakarta.annotation.PostConstruct ;
777779import org.slf4j.Logger ;
778780import org.slf4j.LoggerFactory ;
779- import org.springframework.context. annotation.Conditional ;
781+ import org.springframework.beans.factory. annotation.Value ;
780782import org.springframework.stereotype.Component ;
781783
782784import java.util.Arrays ;
@@ -786,33 +788,59 @@ import java.util.List;
786788 * Automatically synchronizes code-defined permissions with the Access Management API.
787789 */
788790@Component
789- public class PermissionSyncRunner {
791+ public class AuthorizationSyncRunner {
790792
791- private static final Logger LOGGER = LoggerFactory . getLogger(PermissionSyncRunner . class);
793+ private static final Logger LOGGER = LoggerFactory . getLogger(AuthorizationSyncRunner . class);
792794
793795 private final ApiClient accessClient;
794796
795- public PermissionSyncRunner (ApiClient accessClient ) {
797+ @Value (" ${igrp.access.m2m.sync-token:}" )
798+ private String m2mToken;
799+
800+ @Value (" ${spring.application.name:}" )
801+ private String applicationName;
802+
803+ public AuthorizationSyncRunner (ApiClient accessClient ) {
796804 this . accessClient = accessClient;
797805 }
798806
799807 @PostConstruct
800- public void syncPermissions () {
808+ public void syncAuthorization () {
801809 try {
802- LOGGER . info(" [Permission Sync] Starting permission synchronization with Access Management API..." );
810+ LOGGER . info(" [Authorization Sync] Starting authorization synchronization with Access Management API..." );
803811
804812 List<PermissionDTO > permissions = Arrays . stream(PermissionsRegistry . Permission . values())
805- .map(p - > new PermissionDTO ()
806- .setName(p. getCode())
807- .setDescription(p. getDescription())
808- .setStatus(p. enabled() ? Status . ACTIVE : Status . INACTIVE ))
813+ .map(p - > {
814+ var perm = new PermissionDTO ();
815+ perm. setName(p. getCode());
816+ perm. setDescription(p. getDescription());
817+ perm. setStatus(p. enabled() ? Status . ACTIVE : Status . INACTIVE );
818+ return perm;
819+ })
809820 .toList();
810821
811- accessClient. syncPermissions(permissions);
822+ M2MApi m2mApi = new M2MApi (accessClient);
823+
824+ ResourceDTO resource = new ResourceDTO ();
825+
826+ resource. setName(applicationName);
827+ resource. setType(" API" );
828+ resource. setDescription(" Resource for application: " + applicationName);
829+
830+ LOGGER . info(" [Authorization Sync] Synchronizing resource for application '{}'" , applicationName);
831+
832+ m2mApi. syncResources(resource, m2mToken, applicationName);
833+
834+ LOGGER . info(" [Authorization Sync] Resource synchronization completed." );
835+
836+ LOGGER . info(" [Authorization Sync] Synchronizing {} permissions for application '{}'" , permissions. size(), applicationName);
837+
838+ m2mApi. syncPermissions(permissions, m2mToken, applicationName);
812839
813840 LOGGER . info(" [Permission Sync] Successfully synchronized {} permissions." , permissions. size());
841+
814842 } catch (Exception ex) {
815- LOGGER . error(" [Permission Sync] Failed to synchronize permissions with Access Management API" , ex);
843+ LOGGER . error(" [Permission Sync] Failed to synchronize authorization with Access Management API" , ex);
816844 }
817845 }
818846}
@@ -859,15 +887,19 @@ public class AutoConfiguration {
859887
860888In the business microservice:
861889
890+ - Must indicate the URL of the Access Management API
891+ - Provide the machine-to-machine sync token
892+
862893``` properties
863894igrp.access.api.base-url =http://access-management-service:8080
895+ igrp.access.m2m.sync-token =igrp-access-m2m-sync-token-1234
864896```
865897
866898The SDK will automatically:
867899
868900* Generate ` PermissionsRegistry ` at build time
869- * Run ` PermissionSyncRunner ` on startup
870- * Sync all permissions to the Access Management API
901+ * Run ` AuthorizationSyncRunner ` on startup
902+ * Sync the resource and all permissions to the Access Management API
871903
872904---
873905
@@ -893,13 +925,38 @@ import java.util.stream.Collectors;
893925public class PermissionsBeanConfig {
894926
895927 @Bean (name = " permissions" )
896- public Map<String , String > permissions () {
897- return Map . ofEntries(
898- PermissionsRegistry . Permission . values()
899- .stream()
928+ public PermissionAccessor permissions () {
929+ Map<String , String > map = Map . ofEntries(
930+ Arrays . stream(PermissionsRegistry . Permission . values())
900931 .map(p - > Map . entry(p. name(), p. getCode()))
901932 .toArray(Map . Entry []:: new )
902933 );
934+ return new PermissionAccessor (map);
935+ }
936+ }
937+ ```
938+
939+ To use an accessor for the permissions entries we define the following class:
940+
941+ ```java
942+ package cv. igrp. framework. auth. core. config;
943+
944+ import java.util. Map ;
945+
946+ public class PermissionAccessor {
947+
948+ private final Map<String , String > permissions;
949+
950+ public PermissionAccessor (Map<String , String > permissions ) {
951+ this . permissions = permissions;
952+ }
953+
954+ public String get (String key ) {
955+ return permissions. get(key);
956+ }
957+
958+ public Object getProperty (String name ) {
959+ return permissions. get(name);
903960 }
904961}
905962```
@@ -926,14 +983,14 @@ public class IgrpAuthorizationService {
926983 this . authHelper = authHelper;
927984 }
928985
929- public boolean checkPermission (String resource , String action ) {
986+ public boolean checkPermission (String action ) {
930987 try {
931988 String token = authHelper. getToken();
932989 client. setAuthToken(token);
933990 AuthorizeApi authorizeApi = new AuthorizeApi (client);
934991
935992 return authorizeApi. checkAuthorization(
936- new PermissionCheckRequestDTO (resource ,
993+ new PermissionCheckRequestDTO (null ,
937994 action)
938995 ). isAllowed();
939996 } catch (Exception e) {
@@ -946,7 +1003,7 @@ public class IgrpAuthorizationService {
9461003Permissions can be referenced directly in code with constants generated at build time:
9471004
9481005```java
949- @PreAuthorize (" @igrpAuthorization.checkPermission(permissions.USER_EDIT)" )
1006+ @PreAuthorize (" @igrpAuthorization.checkPermission(@ permissions.get(' USER_EDIT') )" )
9501007public ResponseEntity<?> updateUser(... ) {
9511008 // business logic
9521009}
0 commit comments