MCPcopy Create free account
hub / github.com/Grashjs/cmms / create

Method create

api/src/main/java/com/grash/service/ApiKeyService.java:42–62  ·  view source on GitHub ↗
(@Valid ApiKeyPostDTO apiKeyReq, User user)

Source from the content-addressed store, hash-verified

40 private final LicenseService licenseService;
41
42 public Pair<ApiKey, String> create(@Valid ApiKeyPostDTO apiKeyReq, User user) {
43 if (!user.getRole().getViewPermissions().contains(PermissionEntity.SETTINGS)
44 || !user.getCompany().getSubscription().getSubscriptionPlan().getFeatures().contains(PlanFeatures.API_ACCESS)
45 || !licenseService.hasEntitlement(LicenseEntitlement.API_ACCESS))
46 throw new CustomException("Access denied", HttpStatus.FORBIDDEN);
47 ApiKey apiKey =
48 apiKeyMapper.fromPostDto(apiKeyReq);
49 apiKey.setUser(user);
50
51 SecureRandom secureRandom = new SecureRandom();
52 byte[] key = new byte[32];
53 secureRandom.nextBytes(key);
54 String code = Base64.getUrlEncoder().withoutPadding().encodeToString(key);
55 try {
56 apiKey.setCode(Helper.hashKey(code));
57 } catch (NoSuchAlgorithmException e) {
58 throw new RuntimeException(e);
59 }
60
61 return Pair.of(apiKeyRepository.save(apiKey), code);
62 }
63
64
65 public List<ApiKey> getAll() {

Callers

nothing calls this directly

Calls 5

hashKeyMethod · 0.95
hasEntitlementMethod · 0.80
setUserMethod · 0.80
fromPostDtoMethod · 0.65
saveMethod · 0.45

Tested by

no test coverage detected