1 package de.dlr.shepard.auth.apikey.services;
2
3 import de.dlr.shepard.auth.apikey.daos.ApiKeyDAO;
4 import de.dlr.shepard.auth.apikey.entities.ApiKey;
5 import de.dlr.shepard.auth.apikey.io.ApiKeyIO;
6 import de.dlr.shepard.auth.users.entities.User;
7 import de.dlr.shepard.auth.users.services.UserService;
8 import de.dlr.shepard.common.exceptions.InvalidAuthException;
9 import de.dlr.shepard.common.exceptions.InvalidPathException;
10 import de.dlr.shepard.common.util.DateHelper;
11 import de.dlr.shepard.common.util.PkiHelper;
12 import io.jsonwebtoken.Jwts;
13 import jakarta.annotation.Nonnull;
14 import jakarta.enterprise.context.RequestScoped;
15 import jakarta.inject.Inject;
16 import java.util.List;
17 import java.util.UUID;
18
19 @RequestScoped
20 public class ApiKeyService {
21
22 @Inject
23 ApiKeyDAO apiKeyDAO;
24
25 @Inject
26 UserService userService;
27
28 @Inject
29 DateHelper dateHelper;
30
31 @Inject
32 PkiHelper pkiHelper;
33
34
35
36
37
38
39
40
41
42 public List<ApiKey> getAllApiKeys(String username) {
43 userService.assertCurrentUserEquals(username);
44 User user = userService.getUser(username);
45 return user.getApiKeys();
46 }
47
48
49
50
51
52
53
54
55
56
57 public @Nonnull ApiKey getApiKey(String username, UUID apiKeyUid) {
58 userService.getUser(username);
59 userService.assertCurrentUserEquals(username);
60
61 ApiKey requestedKey = apiKeyDAO.find(apiKeyUid);
62
63 if (requestedKey == null) {
64 throw new InvalidPathException("ID ERROR - ApiKey does not exist");
65 }
66 if (!requestedKey.getBelongsTo().getUsername().equals(username)) {
67 throw new InvalidAuthException("You do not have permissions for this ApiKey.");
68 }
69
70 return requestedKey;
71 }
72
73
74
75
76
77
78
79
80 public @Nonnull ApiKey getApiKey(UUID apiKeyUid) {
81 ApiKey requestedKey = apiKeyDAO.find(apiKeyUid);
82
83 if (requestedKey == null) {
84 throw new InvalidPathException("ID ERROR - ApiKey does not exist");
85 }
86
87 return requestedKey;
88 }
89
90
91
92
93
94
95
96
97
98
99
100 public ApiKey createApiKey(ApiKeyIO apiKey, String username, String baseUri) {
101 var user = userService.getUser(username);
102 userService.assertCurrentUserEquals(username);
103
104 var toCreate = new ApiKey();
105 toCreate.setBelongsTo(user);
106 toCreate.setCreatedAt(dateHelper.getDate());
107 toCreate.setName(apiKey.getName());
108
109 var createdApiKey = apiKeyDAO.createOrUpdate(toCreate);
110 createdApiKey.setJws(generateJws(createdApiKey, baseUri));
111 return apiKeyDAO.createOrUpdate(createdApiKey);
112 }
113
114
115
116
117
118
119
120
121
122 public boolean deleteApiKey(String username, UUID apiKeyUid) {
123 userService.assertCurrentUserEquals(username);
124 getApiKey(username, apiKeyUid);
125
126 return apiKeyDAO.delete(apiKeyUid);
127 }
128
129
130
131
132
133
134
135
136
137
138
139
140 private String generateJws(ApiKey apiKey, String baseUri) {
141 pkiHelper.init();
142 var currentDate = dateHelper.getDate();
143 var jws = Jwts.builder()
144 .setSubject(apiKey.getBelongsTo().getUsername())
145 .setIssuer(baseUri)
146 .setNotBefore(currentDate)
147 .setIssuedAt(currentDate)
148 .setId(apiKey.getUid().toString())
149 .signWith(pkiHelper.getPrivateKey())
150 .compact();
151 return jws;
152 }
153 }