Skip to content

Package: ScratchCodeDAO

ScratchCodeDAO

nameinstructionbranchcomplexitylinemethod
ScratchCodeDAO()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
count(EntityManager, KapuaQuery)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
create(EntityManager, ScratchCodeCreator)
M: 19 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
delete(EntityManager, KapuaId, KapuaId)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
find(EntityManager, KapuaId, KapuaId)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
query(EntityManager, KapuaQuery)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
update(EntityManager, ScratchCode)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 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.ServiceDAO;
19: import org.eclipse.kapua.model.id.KapuaId;
20: import org.eclipse.kapua.model.query.KapuaQuery;
21: import org.eclipse.kapua.service.authentication.credential.mfa.ScratchCode;
22: import org.eclipse.kapua.service.authentication.credential.mfa.ScratchCodeCreator;
23: import org.eclipse.kapua.service.authentication.credential.mfa.ScratchCodeListResult;
24: import org.eclipse.kapua.service.authentication.shiro.utils.AuthenticationUtils;
25: import org.eclipse.kapua.service.authentication.shiro.utils.CryptAlgorithm;
26:
27: /**
28: * {@link ScratchCode} {@link ServiceDAO}
29: */
30: public class ScratchCodeDAO extends ServiceDAO {
31:
32: /**
33: * Creates and return new {@link ScratchCode}
34: *
35: * @param em
36: * @param scratchCodeCreator
37: * @return
38: * @throws KapuaException
39: */
40: public static ScratchCode create(EntityManager em, ScratchCodeCreator scratchCodeCreator) throws KapuaException {
41:
42: //
43: // Crypto code (it's ok to do than if BCrypt is used when checking a provided scratch code against the stored one)
44: String encryptedCode = AuthenticationUtils.cryptCredential(CryptAlgorithm.BCRYPT, scratchCodeCreator.getCode());
45:
46: //
47: // Create code
48: ScratchCodeImpl codeImpl = new ScratchCodeImpl(scratchCodeCreator.getScopeId(), scratchCodeCreator.getMfaOptionId(), encryptedCode);
49: //
50: // Do create
51: return ServiceDAO.create(em, codeImpl);
52: }
53:
54: /**
55: * Update the provided {@link ScratchCode}
56: *
57: * @param em
58: * @param code
59: * @return
60: * @throws KapuaException
61: */
62: public static ScratchCode update(EntityManager em, ScratchCode code) throws KapuaException {
63: ScratchCodeImpl scratchCodeImpl = (ScratchCodeImpl) code;
64: return ServiceDAO.update(em, ScratchCodeImpl.class, scratchCodeImpl);
65: }
66:
67: /**
68: * Find the {@link ScratchCode} by its {@link KapuaId}
69: *
70: * @param em
71: * @param scopeId
72: * @param scratchCodeId
73: * @return
74: */
75: public static ScratchCode find(EntityManager em, KapuaId scopeId, KapuaId scratchCodeId) {
76: return ServiceDAO.find(em, ScratchCodeImpl.class, scopeId, scratchCodeId);
77: }
78:
79: /**
80: * Return the {@link ScratchCode} list matching the provided query
81: *
82: * @param em
83: * @param scratchCodeQuery
84: * @return
85: * @throws KapuaException
86: */
87: public static ScratchCodeListResult query(EntityManager em, KapuaQuery scratchCodeQuery) throws KapuaException {
88: return ServiceDAO.query(em, ScratchCode.class, ScratchCodeImpl.class, new ScratchCodeListResultImpl(), scratchCodeQuery);
89: }
90:
91: /**
92: * Return the {@link ScratchCode} count matching the provided query
93: *
94: * @param em
95: * @param scratchCodeQuery
96: * @return
97: * @throws KapuaException
98: */
99: public static long count(EntityManager em, KapuaQuery scratchCodeQuery) throws KapuaException {
100: return ServiceDAO.count(em, ScratchCode.class, ScratchCodeImpl.class, scratchCodeQuery);
101: }
102:
103: /**
104: * Delete the {@link ScratchCode} by its {@link KapuaId}
105: *
106: * @param em
107: * @param scopeId
108: * @param scratchCodeId
109: * @return deleted entity
110: * @throws KapuaEntityNotFoundException If {@link ScratchCode} is now found.
111: */
112: public static ScratchCode delete(EntityManager em, KapuaId scopeId, KapuaId scratchCodeId) throws KapuaEntityNotFoundException {
113: return ServiceDAO.delete(em, ScratchCodeImpl.class, scopeId, scratchCodeId);
114: }
115: }