/*
- * Copyright 2000-2010 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* This exception is thrown when master password is not available (process of entering password is cancelled, or IDEA is running headless mode)
*/
public class MasterPasswordUnavailableException extends PasswordSafeException {
-
- public MasterPasswordUnavailableException(String message, Throwable cause) {
- super(message, cause);
- }
-
public MasterPasswordUnavailableException(String message) {
super(message);
}
/**
* The exception that is thrown when password safe is not available (unable to ask for master password)
*/
-public class PasswordSafeException extends Exception {
-
+public class PasswordSafeException extends RuntimeException {
private static final long MIN_INTERVAL = 1000L;
private long myTimeMillis = System.currentTimeMillis();
super(message);
}
- public long getTimeMillis() {
- return myTimeMillis;
- }
-
public boolean justHappened() {
long timeMillis = System.currentTimeMillis();
if (timeMillis - myTimeMillis < MIN_INTERVAL) {
* @throws IllegalStateException if the method is called from the read action.
*/
@Nullable
- String getPassword(@Nullable Project project, @NotNull Class requestor, String key) throws PasswordSafeException;
+ String getPassword(@Nullable Project project, @NotNull Class requestor, String key);
+
/**
* Remove password stored in a password safe
*
* @param project the project, that is used to ask for the master password if this is the first access to password safe
* @param requestor the requestor class
* @param key the key for the password
- * @return the plugin key
* @throws PasswordSafeException if password safe cannot be accessed
*/
- void removePassword(@Nullable Project project, @NotNull Class requestor, String key) throws PasswordSafeException;
+ void removePassword(@Nullable Project project, @NotNull Class requestor, String key);
+
/**
* Store password in password safe
*
* @param value the value to store
* @throws PasswordSafeException if password safe cannot be accessed
*/
- void storePassword(@Nullable Project project, @NotNull Class requestor, String key, String value) throws PasswordSafeException;
+ void storePassword(@Nullable Project project, @NotNull Class requestor, String key, String value);
}
/*
- * Copyright 2000-2011 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import org.jetbrains.annotations.Nullable;
public class PasswordSafeImpl extends PasswordSafe {
- private static final Logger LOG = Logger.getInstance(PasswordSafeImpl.class.getName());
+ private static final Logger LOG = Logger.getInstance(PasswordSafeImpl.class);
+
private final PasswordSafeSettings mySettings;
private final MasterKeyPasswordSafe myMasterKeyProvider;
private final NilProvider myNilProvider;
default:
LOG.error("Unknown provider type: " + mySettings.getProviderType());
}
- if (p == null || !p.isSupported()) {
+ if (!p.isSupported()) {
p = myMemoryProvider;
}
return p;
/*
- * Copyright 2000-2010 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
/**
* @return true, the implementation is supported in the current environment
*/
- public abstract boolean isSupported();
+ public boolean isSupported() {
+ return true;
+ }
/**
* @return the description of the provider
*/
/*
- * Copyright 2000-2010 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* @throws IllegalStateException if the method is called from the read action.
*/
@NotNull
- protected abstract byte[] key(@Nullable Project project, @NotNull Class requestor) throws PasswordSafeException;
+ protected abstract byte[] key(@Nullable Project project, @NotNull Class requestor);
@Nullable
- public String getPassword(@Nullable Project project, @NotNull Class requestor, String key) throws PasswordSafeException {
- byte[] k = dbKey(project, requestor, key);
- byte[] ct = getEncryptedPassword(k);
+ public String getPassword(@Nullable Project project, @NotNull Class requestor, String key) {
+ byte[] ct = getEncryptedPassword(dbKey(project, requestor, key));
return ct == null ? null : EncryptionUtil.decryptText(key(project, requestor), ct);
}
* @param key the key to get
* @return the encrypted password
*/
- protected abstract byte[] getEncryptedPassword(byte[] key);
+ protected abstract byte[] getEncryptedPassword(@NotNull byte[] key);
/**
* Get database key
* @param key the key to use
* @return the key to use for map
*/
- private byte[] dbKey(@Nullable Project project, Class requestor, String key) throws PasswordSafeException {
+ @NotNull
+ private byte[] dbKey(@Nullable Project project, Class requestor, String key) {
return EncryptionUtil.dbKey(key(project, requestor), requestor, key);
}
/*
- * Copyright 2000-2014 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* @param rawKey the raw key to encrypt
* @return the encrypted key
*/
+ @NotNull
public static byte[] encryptKey(@NotNull byte[] password, byte[] rawKey) {
try {
Cipher c = Cipher.getInstance(ENCRYPT_KEY_ALGORITHM);
return c.doFinal(rawKey);
}
catch (GeneralSecurityException e) {
- throw new IllegalStateException(e.getMessage(), e);
+ throw new IllegalStateException(e);
}
}
* @param key the key within requestor
* @return the key to use in the database
*/
+ @NotNull
public static byte[] dbKey(@NotNull byte[] password, Class requestor, String key) {
return encryptKey(password, rawKey(requestor, key));
}
-
/**
* Decrypt key (does not use salting, so the encryption result is the same for the same input)
*
* @param data the bytes to decrypt
* @return encrypted text
*/
+ @NotNull
public static String decryptText(byte[] password, byte[] data) {
byte[] plain = decryptData(password, data);
int len = ((plain[0] & 0xff) << 24) + ((plain[1] & 0xff) << 16) + ((plain[2] & 0xff) << 8) + (plain[3] & 0xff);
/*
- * Copyright 2000-2015 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@Override
protected byte[] key(@Nullable final Project project, @NotNull final Class requestor) throws PasswordSafeException {
Object key = myKey.get().get();
- if (key instanceof byte[]) return (byte[])key;
- if (key instanceof PasswordSafeException && ((PasswordSafeException)key).justHappened()) throw (PasswordSafeException)key;
+ if (key instanceof byte[]) {
+ return (byte[])key;
+ }
+ if (key instanceof PasswordSafeException && ((PasswordSafeException)key).justHappened()) {
+ throw (PasswordSafeException)key;
+ }
if (isPasswordEncrypted()) {
try {
}
try {
if (myDatabase.isEmpty()) {
- if (!MasterPasswordDialog.resetMasterPasswordDialog(project, MasterKeyPasswordSafe.this, requestor).showAndGet()) {
+ if (!MasterPasswordDialog.resetMasterPasswordDialog(project, this, requestor).showAndGet()) {
throw new MasterPasswordUnavailableException("Master password is required to store passwords in the database.");
}
}
else {
- MasterPasswordDialog.askPassword(project, MasterKeyPasswordSafe.this, requestor);
+ MasterPasswordDialog.askPassword(project, this, requestor);
}
}
catch (PasswordSafeException e) {
@Override
public String getPassword(@Nullable Project project, @NotNull Class requestor, String key) throws PasswordSafeException {
- if (myDatabase.isEmpty()) {
- return null;
- }
- return super.getPassword(project, requestor, key);
+ return myDatabase.isEmpty() ? null : super.getPassword(project, requestor, key);
}
@Override
}
@Override
- protected byte[] getEncryptedPassword(byte[] key) {
+ protected byte[] getEncryptedPassword(@NotNull byte[] key) {
return myDatabase.get(key);
}
/*
- * Copyright 2000-2015 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
}
@Override
- protected byte[] getEncryptedPassword(byte[] key) {
+ protected byte[] getEncryptedPassword(@NotNull byte[] key) {
return database.get().get(new ByteArrayWrapper(key));
}
database.get().put(new ByteArrayWrapper(key), encryptedPassword);
}
- @Override
- public boolean isSupported() {
- return true;
- }
-
@Override
public String getDescription() {
return "Memory-based password safe provider. The passwords are stored only for the duration of IDEA process.";
/*
- * Copyright 2000-2010 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* The most secure provider that does not store anything, so it cannot be cracked
*/
public final class NilProvider extends PasswordSafeProvider {
-
- @Override
- public boolean isSupported() {
- return true;
- }
-
@Override
public String getDescription() {
return "The provider that does not remembers password.";