Skip to content

Commit 9550a99

Browse files
authored
Merge pull request #11 from aiman-mumtaz/issue-ai-sec
Add AI Risk Assesment Logic
2 parents 463a2a2 + 9bbf0c1 commit 9550a99

3 files changed

Lines changed: 37 additions & 40 deletions

File tree

backend/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@
4747
<scope>runtime</scope>
4848
</dependency>
4949

50-
<!-- <dependency>
50+
<dependency>
5151
<groupId>org.springframework.ai</groupId>
5252
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
53-
</dependency> -->
53+
</dependency>
5454

5555
<dependency>
5656
<groupId>org.springframework.boot</groupId>
@@ -59,7 +59,7 @@
5959
</dependency>
6060
</dependencies>
6161

62-
<!-- <dependencyManagement>
62+
<dependencyManagement>
6363
<dependencies>
6464
<dependency>
6565
<groupId>org.springframework.ai</groupId>
@@ -69,7 +69,7 @@
6969
<scope>import</scope>
7070
</dependency>
7171
</dependencies>
72-
</dependencyManagement> -->
72+
</dependencyManagement>
7373

7474
<build>
7575
<plugins>

backend/src/main/java/com/aiman/api/controller/NafathController.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,21 @@ public class NafathController {
4141

4242
@PostMapping("/initiate")
4343
public ResponseEntity<?> initiate(@RequestBody InitiateRequest req, HttpServletRequest request) {
44-
// 1. Context Collection
44+
4545
String ip = request.getRemoteAddr();
4646
String ua = request.getHeader("User-Agent");
4747

48-
// 2. AI Risk Assessment (The "Gatekeeper")
4948
var risk = riskService.analyze(req.nationalId(), ip, ua);
5049

5150
if ("BLOCK".equals(risk.status()) || risk.score() > 0.85) {
5251
return ResponseEntity.status(HttpStatus.FORBIDDEN)
5352
.body(Map.of("error", "Security Block", "reason", risk.reason()));
5453
}
5554

56-
// 3. Only if safe, proceed to business logic
5755
NafathRequest nafath = nafathService.initiateNafathRequest(req.nationalId());
5856
return ResponseEntity.ok(Map.of("nafath", nafath, "aiInsight", risk));
5957
}
60-
// 2. Poll for status
58+
6159
@GetMapping("/status/{id}")
6260
public ResponseEntity<?> poll (@PathVariable String id) {
6361
// String status = nafathService.checkStatus(id);
@@ -74,8 +72,7 @@ public ResponseEntity<?> poll (@PathVariable String id) {
7472
.body("Invalid ID format. Please provide a valid UUID.");
7573
}
7674
}
77-
78-
// 3. Mock simulate the user clicking "Approve" in the Nafath App
75+
7976
@PatchMapping("/simulate-approval/{id}")
8077
public void simulateApproval(@PathVariable UUID id, @RequestBody Map<String, String> body){
8178
nafathService.simulateApproval(id, body.get("status"));

backend/src/main/java/com/aiman/api/service/RiskAssessmentService.java

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
package com.aiman.api.service;
22

3-
// import org.springframework.ai.chat.client.ChatClient;
3+
import org.springframework.ai.chat.client.ChatClient;
44
import org.springframework.stereotype.Service;
55
import java.time.LocalDateTime;
66
import java.util.Map;
77
@Service
88
public class RiskAssessmentService {
99

10-
// private final ChatClient chatClient;
10+
private final ChatClient chatClient;
1111

12-
// public RiskAssessmentService(ChatClient.Builder builder) {
13-
// this.chatClient = builder.build();
14-
// }
12+
public RiskAssessmentService(ChatClient.Builder builder) {
13+
this.chatClient = builder.build();
14+
}
1515

1616
public AssessmentResult analyze(String nationalId, String ip, String userAgent) {
17-
// String userPromptTemplate = """
18-
// Evaluate the security risk of this Saudi Nafath login attempt:
19-
// - National ID: {id}
20-
// - Origin IP: {ip}
21-
// - Device: {ua}
22-
// - Time: {time}
17+
String userPromptTemplate = """
18+
Evaluate the security risk of this Saudi Nafath login attempt:
19+
- National ID: {id}
20+
- Origin IP: {ip}
21+
- Device: {ua}
22+
- Time: {time}
2323
24-
// Return JSON only: { "score": 0.0-1.0, "status": "SAFE|CAUTION|BLOCK", "reason": "string" }
25-
// """;
26-
27-
// return chatClient.prompt()
28-
// .user(u -> u.text(userPromptTemplate)
29-
// .params(Map.of(
30-
// "id", nationalId,
31-
// "ip", ip,
32-
// "ua", userAgent,
33-
// "time", LocalDateTime.now().toString()
34-
// )))
35-
// .call()
36-
// .entity(AssessmentResult.class);
37-
38-
// Mock Assesment begin
39-
double score = Math.random();
40-
String status = (score > 0.9) ? "BLOCK" : "SAFE";
41-
return new AssessmentResult(score, status, "Mocked AI Assessment for testing");
42-
// Mock Assesment end
24+
Return JSON only: { "score": 0.0-1.0, "status": "SAFE|CAUTION|BLOCK", "reason": "string" }
25+
""";
26+
27+
return chatClient.prompt()
28+
.user(u -> u.text(userPromptTemplate)
29+
.params(Map.of(
30+
"id", nationalId,
31+
"ip", ip,
32+
"ua", userAgent,
33+
"time", LocalDateTime.now().toString()
34+
)))
35+
.call()
36+
.entity(AssessmentResult.class);
37+
38+
// // Mock Assesment begin
39+
// double score = Math.random();
40+
// String status = (score > 0.9) ? "BLOCK" : "SAFE";
41+
// return new AssessmentResult(score, status, "Mocked AI Assessment for testing");
42+
// // Mock Assesment end
4343

4444
}
4545

0 commit comments

Comments
 (0)