Skip to content

Package: ScratchCodeServiceImpl

ScratchCodeServiceImpl

nameinstructionbranchcomplexitylinemethod
ScratchCodeServiceImpl()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
count(KapuaQuery)
M: 31 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
create(ScratchCodeCreator)
M: 70 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 22 C: 0
0%
M: 1 C: 0
0%
createAllScratchCodes(ScratchCodeCreator)
M: 55 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
delete(KapuaId, KapuaId)
M: 33 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
deleteScratchCodeByAccountId(KapuaId, KapuaId)
M: 35 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
deleteScratchCodeByMfaOptionId(KapuaId, KapuaId)
M: 41 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
find(KapuaId, KapuaId)
M: 33 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
findByMfaOptionId(KapuaId, KapuaId)
M: 42 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
lambda$count$3(KapuaQuery, EntityManager)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$delete$4(KapuaId, KapuaId, EntityManager)
M: 16 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
lambda$find$1(KapuaId, KapuaId, EntityManager)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$query$2(KapuaQuery, EntityManager)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$update$0(ScratchCode, EntityManager)
M: 20 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
query(KapuaQuery)
M: 30 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
update(ScratchCode)
M: 46 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2020, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.service.authentication.credential.mfa.shiro;
14:
15: import org.eclipse.kapua.KapuaEntityNotFoundException;
16: import org.eclipse.kapua.KapuaException;
17: import org.eclipse.kapua.commons.jpa.EntityManager;
18: import org.eclipse.kapua.commons.service.internal.AbstractKapuaService;
19: import org.eclipse.kapua.commons.util.ArgumentValidator;
20: import org.eclipse.kapua.commons.util.KapuaExceptionUtils;
21: import org.eclipse.kapua.locator.KapuaLocator;
22: import org.eclipse.kapua.locator.KapuaProvider;
23: import org.eclipse.kapua.model.KapuaEntityAttributes;
24: import org.eclipse.kapua.model.domain.Actions;
25: import org.eclipse.kapua.model.id.KapuaId;
26: import org.eclipse.kapua.model.query.KapuaQuery;
27: import org.eclipse.kapua.model.query.predicate.QueryPredicate;
28: import org.eclipse.kapua.service.authentication.AuthenticationDomains;
29: import org.eclipse.kapua.service.authentication.credential.mfa.KapuaExistingScratchCodesException;
30: import org.eclipse.kapua.service.authentication.credential.mfa.ScratchCode;
31: import org.eclipse.kapua.service.authentication.credential.mfa.ScratchCodeAttributes;
32: import org.eclipse.kapua.service.authentication.credential.mfa.ScratchCodeCreator;
33: import org.eclipse.kapua.service.authentication.credential.mfa.ScratchCodeFactory;
34: import org.eclipse.kapua.service.authentication.credential.mfa.ScratchCodeListResult;
35: import org.eclipse.kapua.service.authentication.credential.mfa.ScratchCodeQuery;
36: import org.eclipse.kapua.service.authentication.credential.mfa.ScratchCodeService;
37: import org.eclipse.kapua.service.authentication.mfa.MfaAuthenticator;
38: import org.eclipse.kapua.service.authentication.shiro.AuthenticationEntityManagerFactory;
39: import org.eclipse.kapua.service.authentication.shiro.mfa.MfaAuthenticatorServiceLocator;
40: import org.eclipse.kapua.service.authorization.AuthorizationService;
41: import org.eclipse.kapua.service.authorization.permission.PermissionFactory;
42: import org.slf4j.Logger;
43: import org.slf4j.LoggerFactory;
44:
45: import java.util.List;
46:
47: /**
48: * {@link ScratchCodeService} implementation.
49: */
50: @KapuaProvider
51: public class ScratchCodeServiceImpl extends AbstractKapuaService implements ScratchCodeService {
52:
53: private static final Logger LOGGER = LoggerFactory.getLogger(ScratchCodeServiceImpl.class);
54:
55: private static final MfaAuthenticatorServiceLocator MFA_AUTH_SERVICE_LOCATOR = MfaAuthenticatorServiceLocator.getInstance();
56: private static final MfaAuthenticator MFA_AUTHENTICATOR = MFA_AUTH_SERVICE_LOCATOR.getMfaAuthenticator();
57:
58: public ScratchCodeServiceImpl() {
59: super(ScratchCodeEntityManagerFactory.getInstance());
60: }
61:
62: @Override
63: public ScratchCode create(ScratchCodeCreator scratchCodeCreator) throws KapuaException {
64: //
65: // Argument Validation
66: ArgumentValidator.notNull(scratchCodeCreator, "scratchCodeCreator");
67: ArgumentValidator.notNull(scratchCodeCreator.getScopeId(), "scratchCodeCreator.scopeId");
68: ArgumentValidator.notNull(scratchCodeCreator.getMfaOptionId(), "scratchCodeCreator.mfaOptionId");
69: ArgumentValidator.notEmptyOrNull(scratchCodeCreator.getCode(), "scratchCodeCreator.code");
70:
71: //
72: // Check access
73: KapuaLocator locator = KapuaLocator.getInstance();
74: AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
75: PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
76: authorizationService.checkPermission(permissionFactory.newPermission(AuthenticationDomains.CREDENTIAL_DOMAIN, Actions.write,
77: scratchCodeCreator.getScopeId()));
78:
79: //
80: // Do create
81: ScratchCode scratchCode = null;
82: EntityManager em = AuthenticationEntityManagerFactory.getEntityManager();
83: try {
84: em.beginTransaction();
85:
86: //
87: // Do pre persist magic on key values
88: String fullKey = scratchCodeCreator.getCode();
89: scratchCode = ScratchCodeDAO.create(em, scratchCodeCreator);
90: scratchCode = ScratchCodeDAO.find(em, scratchCode.getScopeId(), scratchCode.getId());
91: em.commit();
92:
93: // Do post persist magic on key values
94: scratchCode.setCode(fullKey);
95: } catch (Exception pe) {
96: em.rollback();
97: throw KapuaExceptionUtils.convertPersistenceException(pe);
98: } finally {
99: em.close();
100: }
101:
102: return scratchCode;
103: }
104:
105: @Override
106: public ScratchCode update(ScratchCode scratchCode) throws KapuaException {
107: //
108: // Argument Validation
109: ArgumentValidator.notNull(scratchCode, "scratchCode");
110: ArgumentValidator.notNull(scratchCode.getId(), "scratchCode.id");
111: ArgumentValidator.notNull(scratchCode.getScopeId(), "scratchCode.scopeId");
112: ArgumentValidator.notNull(scratchCode.getMfaOptionId(), "scratchCode.mfaOptionId");
113: ArgumentValidator.notEmptyOrNull(scratchCode.getCode(), "scratchCode.code");
114:
115: //
116: // Check access
117: KapuaLocator locator = KapuaLocator.getInstance();
118: AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
119: PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
120: authorizationService.checkPermission(permissionFactory.newPermission(AuthenticationDomains.CREDENTIAL_DOMAIN, Actions.write, scratchCode.getScopeId()));
121:
122: return entityManagerSession.doTransactedAction(em -> {
123: ScratchCode currentscratchCode = ScratchCodeDAO.find(em, scratchCode.getScopeId(), scratchCode.getId());
124:
125:• if (currentscratchCode == null) {
126: throw new KapuaEntityNotFoundException(ScratchCode.TYPE, scratchCode.getId());
127: }
128:
129: // Passing attributes??
130: return ScratchCodeDAO.update(em, scratchCode);
131: });
132: }
133:
134: @Override
135: public ScratchCode find(KapuaId scopeId, KapuaId scratchCodeId) throws KapuaException {
136: // Validation of the fields
137: ArgumentValidator.notNull(scopeId, KapuaEntityAttributes.SCOPE_ID);
138: ArgumentValidator.notNull(scratchCodeId, "scratchCodeId");
139:
140: //
141: // Check Access
142: KapuaLocator locator = KapuaLocator.getInstance();
143: AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
144: PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
145: authorizationService.checkPermission(permissionFactory.newPermission(AuthenticationDomains.CREDENTIAL_DOMAIN, Actions.read, scopeId));
146:
147: return entityManagerSession.doAction(em -> ScratchCodeDAO.find(em, scopeId, scratchCodeId));
148: }
149:
150: @Override
151: public ScratchCodeListResult query(KapuaQuery query) throws KapuaException {
152: //
153: // Argument Validation
154: ArgumentValidator.notNull(query, "query");
155:
156: //
157: // Check Access
158: KapuaLocator locator = KapuaLocator.getInstance();
159: AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
160: PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
161: authorizationService.checkPermission(permissionFactory.newPermission(AuthenticationDomains.CREDENTIAL_DOMAIN, Actions.read, query.getScopeId()));
162:
163: return entityManagerSession.doAction(em -> ScratchCodeDAO.query(em, query));
164: }
165:
166: @Override
167: public long count(KapuaQuery query) throws KapuaException {
168: //
169: // Argument Validation
170: ArgumentValidator.notNull(query, "query");
171:
172: //
173: // Check Access
174: KapuaLocator locator = KapuaLocator.getInstance();
175: AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
176: PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
177: authorizationService.checkPermission(permissionFactory.newPermission(AuthenticationDomains.CREDENTIAL_DOMAIN, Actions.read, query.getScopeId()));
178:
179: return entityManagerSession.doAction(em -> ScratchCodeDAO.count(em, query));
180: }
181:
182: @Override
183: public void delete(KapuaId scopeId, KapuaId scratchCodeId) throws KapuaException {
184: //
185: // Argument Validation
186: ArgumentValidator.notNull(scratchCodeId, "scratchCode.id");
187: ArgumentValidator.notNull(scopeId, "scratchCode.scopeId");
188:
189: //
190: // Check Access
191: KapuaLocator locator = KapuaLocator.getInstance();
192: AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
193: PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
194: authorizationService.checkPermission(permissionFactory.newPermission(AuthenticationDomains.CREDENTIAL_DOMAIN, Actions.delete, scopeId));
195:
196: entityManagerSession.doTransactedAction(em -> {
197:• if (ScratchCodeDAO.find(em, scopeId, scratchCodeId) == null) {
198: throw new KapuaEntityNotFoundException(ScratchCode.TYPE, scratchCodeId);
199: }
200: return ScratchCodeDAO.delete(em, scopeId, scratchCodeId);
201: });
202: }
203:
204: @Override
205: public ScratchCodeListResult createAllScratchCodes(ScratchCodeCreator scratchCodeCreator) throws KapuaException {
206: //
207: // Argument Validation
208: ArgumentValidator.notNull(scratchCodeCreator, "scratchCodeCreator");
209: ArgumentValidator.notNull(scratchCodeCreator.getScopeId(), "scratchCodeCreator.scopeId");
210: ArgumentValidator.notNull(scratchCodeCreator.getMfaOptionId(), "scratchCodeCreator.mfaOptionId");
211:
212: List<String> codes = MFA_AUTHENTICATOR.generateCodes();
213: ScratchCodeListResult scratchCodeListResult = new ScratchCodeListResultImpl();
214:
215: //
216: // Check existing ScratchCodes
217: ScratchCodeListResult existingScratchCodeListResult = findByMfaOptionId(scratchCodeCreator.getScopeId(), scratchCodeCreator.getMfaOptionId());
218:• if (!existingScratchCodeListResult.isEmpty()) {
219: throw new KapuaExistingScratchCodesException();
220: }
221:
222:• for (String code : codes) {
223: scratchCodeCreator.setCode(code);
224: ScratchCode scratchCode = create(scratchCodeCreator);
225: scratchCodeListResult.addItem(scratchCode);
226: }
227:
228: return scratchCodeListResult;
229: }
230:
231: @Override
232: public ScratchCodeListResult findByMfaOptionId(KapuaId scopeId, KapuaId mfaOptionId) throws KapuaException {
233: //
234: // Argument Validation
235: ArgumentValidator.notNull(scopeId, KapuaEntityAttributes.SCOPE_ID);
236: ArgumentValidator.notNull(mfaOptionId, ScratchCodeAttributes.MFA_OPTION_ID);
237:
238: //
239: // Check Access
240: KapuaLocator locator = KapuaLocator.getInstance();
241: AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
242: PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
243: authorizationService.checkPermission(permissionFactory.newPermission(AuthenticationDomains.CREDENTIAL_DOMAIN, Actions.read, scopeId));
244:
245: //
246: // Build query
247: ScratchCodeQuery query = new ScratchCodeQueryImpl(scopeId);
248: QueryPredicate predicate = query.attributePredicate(ScratchCodeAttributes.MFA_OPTION_ID, mfaOptionId);
249: query.setPredicate(predicate);
250:
251: //
252: // Query and return result
253: return query(query);
254: }
255:
256: private void deleteScratchCodeByMfaOptionId(KapuaId scopeId, KapuaId mfaOptionId) throws KapuaException {
257: KapuaLocator locator = KapuaLocator.getInstance();
258: ScratchCodeFactory scratchCodeFactory = locator.getFactory(ScratchCodeFactory.class);
259:
260: ScratchCodeQuery query = scratchCodeFactory.newQuery(scopeId);
261: query.setPredicate(query.attributePredicate(ScratchCodeAttributes.MFA_OPTION_ID, mfaOptionId));
262:
263: ScratchCodeListResult scratchCodesToDelete = query(query);
264:
265:• for (ScratchCode c : scratchCodesToDelete.getItems()) {
266: delete(c.getScopeId(), c.getId());
267: }
268: }
269:
270: private void deleteScratchCodeByAccountId(KapuaId scopeId, KapuaId accountId) throws KapuaException {
271: KapuaLocator locator = KapuaLocator.getInstance();
272: ScratchCodeFactory scratchCodeFactory = locator.getFactory(ScratchCodeFactory.class);
273:
274: ScratchCodeQuery query = scratchCodeFactory.newQuery(accountId);
275:
276: ScratchCodeListResult scratchCodesToDelete = query(query);
277:
278:• for (ScratchCode c : scratchCodesToDelete.getItems()) {
279: delete(c.getScopeId(), c.getId());
280: }
281: }
282:
283: }