use of won.owner.model.User in project webofneeds by researchstudio-sat.
the class OwnerPersistenceTest method createUserWithAtom.
private void createUserWithAtom(URI atomUri, String email) {
UserAtom a = new UserAtom();
a.setUri(atomUri);
a = userAtomRepository.save(a);
String password = "password";
String role = "SOMEROLE";
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
User user = new User(email, passwordEncoder.encode(password), role);
user.setEmail(email);
// transfer only available when flag is set therefore we can just set
user.setAcceptedTermsOfService(true);
// this
// to true (i think)
KeystorePasswordHolder keystorePassword = new KeystorePasswordHolder();
// generate a password for the keystore and save it in the database, encrypted
// with a symmetric key
// derived from the user's password
keystorePassword.setPassword(KeystorePasswordUtils.generatePassword(KeystorePasswordUtils.KEYSTORE_PASSWORD_BYTES), password);
// keystorePassword = keystorePasswordRepository.save(keystorePassword);
// generate the keystore for the user
KeystoreHolder keystoreHolder = new KeystoreHolder();
try {
// create the keystore if it doesnt exist yet
keystoreHolder.getKeystore(keystorePassword.getPassword(password));
} catch (Exception e) {
throw new IllegalStateException("could not create keystore for user " + email);
}
// keystoreHolder = keystoreHolderRepository.save(keystoreHolder);
user.setKeystorePasswordHolder(keystorePassword);
user.setKeystoreHolder(keystoreHolder);
user = userRepository.save(user);
user.addUserAtom(a);
user = userRepository.save(user);
}
use of won.owner.model.User in project webofneeds by researchstudio-sat.
the class KeystoreEnabledDaoAuthenticationProvider method authenticate.
@Override
@Transactional
public Authentication authenticate(Authentication authentication) {
String password = (String) authentication.getCredentials();
String username = (String) authentication.getPrincipal();
UsernamePasswordAuthenticationToken auth = (UsernamePasswordAuthenticationToken) super.authenticate(authentication);
User user = (User) auth.getPrincipal();
// can't use that object as it's detached. load the user again:
user = userRepository.findById(user.getId()).get();
KeystorePasswordHolder keystorePasswordHolder = user.getKeystorePasswordHolder();
if (keystorePasswordHolder == null || keystorePasswordHolder.getEncryptedPassword() == null || keystorePasswordHolder.getEncryptedPassword().length() == 0) {
keystorePasswordHolder = new KeystorePasswordHolder();
// generate a password for the keystore and save it in the database, encrypted
// with a symmetric key
// derived from the user's password
keystorePasswordHolder.setPassword(KeystorePasswordUtils.generatePassword(KeystorePasswordUtils.KEYSTORE_PASSWORD_BYTES), password);
// keystorePasswordHolder =
// keystorePasswordRepository.save(keystorePasswordHolder);
// generate the keystore for the user
user.setKeystorePasswordHolder(keystorePasswordHolder);
}
String keystorePassword = keystorePasswordHolder.getPassword(password);
KeystoreHolder keystoreHolder = user.getKeystoreHolder();
KeyStore keystore = null;
if (keystoreHolder == null || keystoreHolder.getKeystoreBytes() == null || keystoreHolder.getKeystoreBytes().length == 0) {
// new user or legacy user that has no keystore yet: create keystoreHolder
keystoreHolder = new KeystoreHolder();
keystore = openOrCreateKeyStore(keystorePassword, auth.getName(), keystoreHolder);
// keystoreHolder = keystoreHolderRepository.save(keystoreHolder);
user.setKeystoreHolder(keystoreHolder);
} else {
try {
keystore = keystoreHolder.getKeystore(keystorePassword);
} catch (Exception e) {
throw new IllegalStateException("could not open keystore for user " + username);
}
}
userRepository.save(user);
KeystoreEnabledUserDetails ud = new KeystoreEnabledUserDetails(user, keystore, keystorePassword);
return new UsernamePasswordAuthenticationToken(ud, null, auth.getAuthorities());
}
Aggregations