-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a qrcode MFA, webservice, service and encryption for the secret key #19
Draft
benoitvasseur
wants to merge
6
commits into
master
Choose a base branch
from
mfa
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bbf8a89
Add a qrcode MFA, webservice, service and encryption for the secret key
bvasseur-urw d1aea4d
Handles multi mfa for a user
bvasseur-urw 9b16f1a
Start of implementation of browser authent with yubico
bvasseur-urw c3c4459
Ajout d'un bout de code pour finir l'enregistrement web authent
bvasseur-urw 8362634
continue webauthn with registered of the public key
bvasseur-urw 51696d6
Add authent with webbrowser
bvasseur-urw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...dmin-security/src/main/java/com/coreoz/plume/admin/websession/MfaSecretKeyEncryption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.coreoz.plume.admin.websession; | ||
|
||
import java.security.SecureRandom; | ||
import java.util.Base64; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.KeyGenerator; | ||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.GCMParameterSpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
public class MfaSecretKeyEncryption { | ||
|
||
private static final String ALGORITHM = "AES/GCM/NoPadding"; | ||
private static final int IV_SIZE = 12; // 96 bits | ||
private static final int TAG_SIZE = 128; // 128 bits | ||
private final SecretKey secretKey; | ||
|
||
public MfaSecretKeyEncryption(String base64SecretKey) { | ||
byte[] decodedKey = Base64.getDecoder().decode(base64SecretKey); | ||
this.secretKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES"); | ||
} | ||
|
||
public String encrypt(String data) throws Exception { | ||
Cipher cipher = Cipher.getInstance(ALGORITHM); | ||
byte[] iv = new byte[IV_SIZE]; | ||
SecureRandom random = new SecureRandom(); | ||
random.nextBytes(iv); | ||
GCMParameterSpec parameterSpec = new GCMParameterSpec(TAG_SIZE, iv); | ||
cipher.init(Cipher.ENCRYPT_MODE, secretKey, parameterSpec); | ||
byte[] encrypted = cipher.doFinal(data.getBytes()); | ||
byte[] encryptedWithIv = new byte[IV_SIZE + encrypted.length]; | ||
System.arraycopy(iv, 0, encryptedWithIv, 0, IV_SIZE); | ||
System.arraycopy(encrypted, 0, encryptedWithIv, IV_SIZE, encrypted.length); | ||
return Base64.getEncoder().encodeToString(encryptedWithIv); | ||
} | ||
|
||
public String decrypt(String encryptedData) throws Exception { | ||
byte[] encryptedWithIv = Base64.getDecoder().decode(encryptedData); | ||
byte[] iv = new byte[IV_SIZE]; | ||
byte[] encrypted = new byte[encryptedWithIv.length - IV_SIZE]; | ||
System.arraycopy(encryptedWithIv, 0, iv, 0, IV_SIZE); | ||
System.arraycopy(encryptedWithIv, IV_SIZE, encrypted, 0, encrypted.length); | ||
Cipher cipher = Cipher.getInstance(ALGORITHM); | ||
GCMParameterSpec parameterSpec = new GCMParameterSpec(TAG_SIZE, iv); | ||
cipher.init(Cipher.DECRYPT_MODE, secretKey, parameterSpec); | ||
byte[] original = cipher.doFinal(encrypted); | ||
return new String(original); | ||
} | ||
|
||
public static String generateSecretKey() throws Exception { | ||
KeyGenerator keyGen = KeyGenerator.getInstance("AES"); | ||
keyGen.init(256); // Use 256 bits for strong encryption | ||
SecretKey secretKey = keyGen.generateKey(); | ||
return Base64.getEncoder().encodeToString(secretKey.getEncoded()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...urity/src/main/java/com/coreoz/plume/admin/websession/MfaSecretKeyEncryptionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.coreoz.plume.admin.websession; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Provider; | ||
import javax.inject.Singleton; | ||
|
||
import com.coreoz.plume.admin.services.configuration.AdminSecurityConfigurationService; | ||
|
||
@Singleton | ||
public class MfaSecretKeyEncryptionProvider implements Provider<MfaSecretKeyEncryption> { | ||
|
||
private final MfaSecretKeyEncryption mfaSecretKeyEncryption; | ||
|
||
@Inject | ||
private MfaSecretKeyEncryptionProvider(AdminSecurityConfigurationService conf) { | ||
this.mfaSecretKeyEncryption = new MfaSecretKeyEncryption(conf.mfaSecret()); | ||
} | ||
|
||
@Override | ||
public MfaSecretKeyEncryption get() { | ||
return mfaSecretKeyEncryption; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
DROP TABLE IF EXISTS `PLM_USER_MFA`; | ||
DROP TABLE IF EXISTS `PLM_ROLE_PERMISSION`; | ||
DROP TABLE IF EXISTS `PLM_USER`; | ||
DROP TABLE IF EXISTS `PLM_ROLE`; | ||
CREATE TABLE `PLM_ROLE` ( | ||
`id` bigint(20) NOT NULL, | ||
|
@@ -6,7 +9,7 @@ CREATE TABLE `PLM_ROLE` ( | |
UNIQUE KEY `uniq_plm_role_label` (`label`) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
|
||
DROP TABLE IF EXISTS `PLM_USER`; | ||
|
||
CREATE TABLE `PLM_USER` ( | ||
`id` bigint(20) NOT NULL, | ||
`id_role` bigint(20) NOT NULL, | ||
|
@@ -16,13 +19,14 @@ CREATE TABLE `PLM_USER` ( | |
`email` varchar(255) NOT NULL, | ||
`user_name` varchar(255) NOT NULL, | ||
`password` varchar(255) NOT NULL, | ||
`mfa_user_handle` BLOB DEFAULT NULL, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pourquoi ce n'est pas dans la table |
||
PRIMARY KEY (`id`), | ||
UNIQUE KEY `uniq_plm_user_email` (`email`), | ||
UNIQUE KEY `uniq_plm_user_username` (`user_name`), | ||
CONSTRAINT `plm_user_role` FOREIGN KEY (`id_role`) REFERENCES `PLM_ROLE` (`id`) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
|
||
DROP TABLE IF EXISTS `PLM_ROLE_PERMISSION`; | ||
|
||
CREATE TABLE `PLM_ROLE_PERMISSION` ( | ||
`id_role` bigint(20) NOT NULL, | ||
`permission` varchar(255) NOT NULL, | ||
|
@@ -31,8 +35,41 @@ CREATE TABLE `PLM_ROLE_PERMISSION` ( | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
|
||
|
||
DROP TABLE IF EXISTS `PLM_MFA_AUTHENTICATOR`; | ||
CREATE TABLE `PLM_MFA_AUTHENTICATOR` ( | ||
`id` bigint(20) NOT NULL, | ||
`secret_key` varchar(255) DEFAULT NULL, | ||
`credential_id` BLOB DEFAULT NULL, | ||
PRIMARY KEY (`id`) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
|
||
DROP TABLE IF EXISTS `PLM_MFA_BROWSER`; | ||
CREATE TABLE `PLM_MFA_BROWSER` ( | ||
`id` bigint(20) NOT NULL, | ||
`key_id` BLOB NOT NULL, | ||
`public_key_cose` BLOB NOT NULL, | ||
`attestation` BLOB NOT NULL, | ||
`client_data_json` BLOB NOT NULL, | ||
`is_discoverable` tinyint(1) DEFAULT NULL, | ||
`signature_count` int(11) NOT NULL, | ||
PRIMARY KEY (`id`) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
|
||
|
||
CREATE TABLE `PLM_USER_MFA` ( | ||
`id` bigint(20) NOT NULL, | ||
`type` ENUM('authenticator', 'browser') NOT NULL, | ||
`id_user` bigint(20) NOT NULL, | ||
`id_mfa_authenticator` bigint(20) DEFAULT NULL, | ||
`id_mfa_browser` bigint(20) DEFAULT NULL, | ||
PRIMARY KEY (`id`), | ||
CONSTRAINT `plm_user_mfa_user` FOREIGN KEY (`id_user`) REFERENCES `PLM_USER` (`id`), | ||
CONSTRAINT `plm_user_mfa_mfa_authenticator` FOREIGN KEY (`id_mfa_authenticator`) REFERENCES `PLM_MFA_AUTHENTICATOR` (`id`), | ||
CONSTRAINT `plm_user_mfa_mfa_browser` FOREIGN KEY (`id_mfa_browser`) REFERENCES `PLM_MFA_BROWSER` (`id`) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
|
||
INSERT INTO PLM_ROLE VALUES(1, 'Administrator'); | ||
INSERT INTO PLM_USER VALUES(1, 1, NOW(), 'Admin', 'Admin', 'admin@admin', 'admin', '$2a$11$FfgtfoHeNo/m9jGj9D5rTO0zDDI4LkMXnXHai744Ee32P3CHoBVqm'); | ||
INSERT INTO PLM_USER VALUES(1, 1, NOW(), 'Admin', 'Admin', 'admin@admin', 'admin', '$2a$11$FfgtfoHeNo/m9jGj9D5rTO0zDDI4LkMXnXHai744Ee32P3CHoBVqm', NULL); | ||
INSERT INTO PLM_ROLE_PERMISSION VALUES(1, 'MANAGE_USERS'); | ||
INSERT INTO PLM_ROLE_PERMISSION VALUES(1, 'MANAGE_ROLES'); | ||
INSERT INTO PLM_ROLE_PERMISSION VALUES(1, 'MANAGE_SYSTEM'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
plume-admin-ws/src/main/java/com/coreoz/plume/admin/db/daos/AdminMfaAuthenticatorDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.coreoz.plume.admin.db.daos; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
import com.coreoz.plume.admin.db.generated.AdminMfaAuthenticator; | ||
import com.coreoz.plume.admin.db.generated.QAdminMfaAuthenticator; | ||
import com.coreoz.plume.db.querydsl.crud.CrudDaoQuerydsl; | ||
import com.coreoz.plume.db.querydsl.transaction.TransactionManagerQuerydsl; | ||
|
||
@Singleton | ||
public class AdminMfaAuthenticatorDao extends CrudDaoQuerydsl<AdminMfaAuthenticator> { | ||
|
||
@Inject | ||
private AdminMfaAuthenticatorDao(TransactionManagerQuerydsl transactionManager) { | ||
super(transactionManager, QAdminMfaAuthenticator.adminMfaAuthenticator); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SecureRandom est thread safe => autant l'initialiser qu'une seule fois