-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuthInternalController.java
More file actions
75 lines (65 loc) · 2.81 KB
/
Copy pathAuthInternalController.java
File metadata and controls
75 lines (65 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.dbaagent.controller;
import com.dbaagent.model.Permission;
import com.dbaagent.model.Role;
import com.dbaagent.model.User;
import com.dbaagent.repository.UserRepository;
import com.dbaagent.service.AuthSessionService;
import com.dbaagent.service.PermissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import jakarta.servlet.http.HttpServletRequest;
import java.security.MessageDigest;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Set;
/**
* Issues short-lived JWTs for internal test suite automation.
* Requires INTERNAL_TEST_TOKEN env var to be configured; disabled if blank.
*/
@RestController
@RequestMapping("/auth/internal")
@RequiredArgsConstructor
public class AuthInternalController {
private final UserRepository userRepository;
private final PermissionService permissionService;
private final AuthSessionService authSessionService;
@Value("${INTERNAL_TEST_TOKEN:}")
private String internalTestToken;
@PostMapping("/token")
public ResponseEntity<?> issueToken(
@RequestHeader(value = "X-Internal-Token", required = false) String providedToken,
HttpServletRequest request
) {
if (internalTestToken == null || internalTestToken.isBlank()) {
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE)
.body(Map.of("message", "Internal token auth not configured"));
}
if (providedToken == null || !MessageDigest.isEqual(
internalTestToken.getBytes(StandardCharsets.UTF_8),
providedToken.getBytes(StandardCharsets.UTF_8))) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body(Map.of("message", "Invalid internal token"));
}
User admin = userRepository.findByUsername("admin")
.orElse(null);
if (admin == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(Map.of("message", "Admin user not found"));
}
String roleCode = admin.getRoleCode();
Set<Permission> permissions = permissionService.getEffectivePermissions(roleCode);
AuthSessionService.SessionAuthentication session = authSessionService.createSession(
admin, roleCode, permissions,
request.getRemoteAddr(),
"DeepSQL-TestSuite/1.0",
true
);
return ResponseEntity.ok(Map.of("token", session.accessToken()));
}
}