(@Valid ApiKeyPostDTO apiKeyReq, User user)
| 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() { |
nothing calls this directly
no test coverage detected