44import java .sql .SQLException ;
55import java .util .List ;
66import java .util .Map ;
7+ import io .sentrius .sso .core .annotations .LimitAccess ;
78import io .sentrius .sso .core .config .SystemOptions ;
89import io .sentrius .sso .core .controllers .BaseController ;
910import io .sentrius .sso .core .dto .JITTrackerDTO ;
1011import io .sentrius .sso .core .dto .ztat .ZtatRequestDTO ;
12+ import io .sentrius .sso .core .model .security .enums .ApplicationAccessEnum ;
1113import io .sentrius .sso .core .model .users .User ;
14+ import io .sentrius .sso .core .model .zt .ZeroTrustAccessTokenReason ;
1215import io .sentrius .sso .core .services .ErrorOutputService ;
1316import io .sentrius .sso .core .services .NotificationService ;
1417import io .sentrius .sso .core .services .UserService ;
@@ -110,7 +113,7 @@ private void manageTerminalZtAt(User operatingUser, Long ztatId, String status)
110113 @ PostMapping ("/request" )
111114 public ResponseEntity <?> requestZtat (
112115 @ RequestHeader ("Authorization" ) String token ,
113- @ RequestBody ZtatRequestDTO request ) {
116+ @ RequestBody ZtatRequestDTO ztatRequest , HttpServletRequest request , HttpServletResponse response ) {
114117
115118 String compactJwt = token .startsWith ("Bearer " ) ? token .substring (7 ) : token ;
116119
@@ -120,17 +123,91 @@ public ResponseEntity<?> requestZtat(
120123 return ResponseEntity .status (HttpStatus .SC_UNAUTHORIZED ).body ("Invalid Keycloak token" );
121124 }
122125
126+ // Extract agent identity from the JWT
127+ var operatingUser = getOperatingUser (request , response );
128+
123129 // Extract agent identity from the JWT
124130 String agentId = keycloakService .extractAgentId (compactJwt );
125131
132+ if (null == operatingUser ) {
133+ log .warn ("No operating user found for agent: {}" , agentId );
134+ var username = keycloakService .extractUsername (compactJwt );
135+ operatingUser = userService .getUserWithDetails (username );
136+
137+ }
138+
126139 log .info ("Received ZTAT request from agent: {}" , agentId );
127140 // Store the request in the database
128- //var ztatRequest = ztatService.createRequest(agentId, request.getCommand(), request.getCommandHash());
141+ ZeroTrustAccessTokenReason reason = ztatService .createReason (ztatRequest .getJustification (), "" , ztatRequest .getCommand ());
142+ var submittedZtatRequest = ztatService .createOpsRequest (ztatRequest .getCommand (), ztatRequest .getCommand (),
143+ reason , operatingUser );
144+ submittedZtatRequest = ztatService .addJITRequest (submittedZtatRequest );
145+
146+ return ResponseEntity .ok (Map .of ("ztat_request" , submittedZtatRequest .getId ()));
147+ }
148+
149+ @ GetMapping ("/status/{type}" )
150+ @ LimitAccess (applicationAccess = {ApplicationAccessEnum .CAN_LOG_IN })
151+ public ResponseEntity <?> getRequest (HttpServletRequest request , HttpServletResponse response ,
152+ @ RequestHeader ("Authorization" ) String token ,
153+ @ PathVariable ("type" ) String type ,
154+ @ RequestParam ("ztatId" ) Long ztatId ) throws SQLException , GeneralSecurityException {
155+ String compactJwt = token .startsWith ("Bearer " ) ? token .substring (7 ) : token ;
156+
157+
158+ log .info ("Received ZTAT request from agent: {}" , compactJwt );
159+ if (!keycloakService .validateJwt (compactJwt )) {
160+ log .warn ("Invalid Keycloak token" );
161+ return ResponseEntity .status (HttpStatus .SC_UNAUTHORIZED ).body ("Invalid Keycloak token" );
162+ }
163+
164+ // Extract agent identity from the JWT
165+ var operatingUser = getOperatingUser (request , response );
166+
167+ // Extract agent identity from the JWT
168+ String agentId = keycloakService .extractAgentId (compactJwt );
169+
170+ if (null == operatingUser ) {
171+ log .warn ("No operating user found for agent: {}" , agentId );
172+ var username = keycloakService .extractUsername (compactJwt );
173+ operatingUser = userService .getUserWithDetails (username );
174+
175+ }
176+
177+ if (null != type ){
178+ switch (type ){
179+ case "terminal" :
180+ var terminalJIT = ztatService .getZtatRequest (ztatId );
181+ if (terminalJIT .getUser ().getId () == operatingUser .getId ()){
182+ if ( terminalJIT .getApprovals ().size () > 0 && terminalJIT .getApprovals ().get (0 ).isApproved () ) {
183+ return ResponseEntity .ok (Map .of ("status" , "approved" , "ztat_token" , terminalJIT .getApprovals ().get (0 ).getToken ()));
184+ }
185+ else {
186+ log .info ("User {} is not the owner of the request {}" , operatingUser .getId (), ztatId );
187+ return ResponseEntity .ok (Map .of ("status" , "unknown" ));
188+ }
189+ } else {
190+ log .info ("User {} is not the owner of the request {}" , operatingUser .getId (), ztatId );
191+ return ResponseEntity .ok (Map .of ("status" , "unknown" ));
192+ }
193+ case "ops" :
194+ var opsJit = ztatService .getOpsJITRequest (ztatId );
195+ if (opsJit .getUser ().getId () == operatingUser .getId ()){
196+ if ( ztatService .isApproved (opsJit ) ) {
197+ return ResponseEntity .ok (Map .of ("status" , "approved" , "ztat_token" , opsJit .getApprovals ().get (0 ).getToken ()));
198+ }
199+ else {
200+ return ResponseEntity .ok (Map .of ("status" , "unknown" ));
201+ }
202+ } else {
203+ return ResponseEntity .ok (Map .of ("status" , "unknown" ));
204+ }
129205
130- // Generate a Zero Trust Access Token (ZTAT)
131- //String ztatToken = ztatService.generateZtatToken(ztatRequest);
132- var ztatToken = "lskejtgsadlkjg" ;
206+ default :
133207
134- return ResponseEntity .ok (Map .of ("ztat_token" , ztatToken ));
208+ }
209+ }
210+ return ResponseEntity .ok (Map .of ("status" , "unknown" ));
135211 }
212+
136213}
0 commit comments