Repository navigation
Expand file tree
/
Copy pathClientOwnerScopeResolver.java
More file actions
51 lines (44 loc) · 2.22 KB
/
Copy pathClientOwnerScopeResolver.java
File metadata and controls
51 lines (44 loc) · 2.22 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
package com.openframe.api.service.device;
import com.openframe.core.exception.ErrorCode;
import com.openframe.core.exception.ForbiddenException;
import com.openframe.data.document.device.Machine;
import com.openframe.data.model.ClientOwnerScope;
import com.openframe.data.repository.device.MachineRepository;
import com.openframe.security.authentication.AuthPrincipal;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import static com.openframe.api.util.AuthPrincipalUtils.isAgent;
import static org.springframework.util.StringUtils.hasText;
// Builds the ClientOwnerScope of a device from its machine, read on every call: a cached organization would keep
// showing the previous customer's items for the cache's lifetime after the device moves.
@Component
@Slf4j
@RequiredArgsConstructor
public class ClientOwnerScopeResolver {
private final MachineRepository machineRepository;
// Only AGENT tokens are owner-scoped; admins read everything.
public boolean isOwnerScoped(AuthPrincipal principal) {
return isAgent(principal);
}
// An AGENT token without a machineId would match the owners that have none, i.e. every admin item: refused.
public ClientOwnerScope scopeOf(AuthPrincipal principal) {
String machineId = principal.getMachineId();
if (!hasText(machineId)) {
throw new ForbiddenException(ErrorCode.FORBIDDEN, "AGENT token without machineId");
}
return forMachine(machineId);
}
// For deliveries that have no principal (notifications).
public ClientOwnerScope forMachine(String machineId) {
if (!hasText(machineId)) {
throw new IllegalArgumentException("machineId is required for a client owner scope");
}
Machine machine = machineRepository.findByMachineId(machineId).orElse(null);
if (machine == null || machine.getOrganizationId() == null) {
log.debug("No current organization for machine {}; owner scope falls back to machine only", machineId);
return new ClientOwnerScope(machineId, null, null);
}
return new ClientOwnerScope(machineId, machine.getOrganizationId(), machine.getOrganizationAssignedAt());
}
}