use of org.eclipse.kura.crypto.CryptoService in project kura by eclipse.
the class ComponentUtil method getObjectValue.
private static Object[] getObjectValue(Scalar type, String[] defaultValues, ComponentContext ctx) {
List<Object> values = new ArrayList<Object>();
switch(type) {
case BOOLEAN:
for (String value : defaultValues) {
values.add(Boolean.valueOf(value));
}
return values.toArray(new Boolean[] {});
case BYTE:
for (String value : defaultValues) {
values.add(Byte.valueOf(value));
}
return values.toArray(new Byte[] {});
case CHAR:
for (String value : defaultValues) {
values.add(Character.valueOf(value.charAt(0)));
}
return values.toArray(new Character[] {});
case DOUBLE:
for (String value : defaultValues) {
values.add(Double.valueOf(value));
}
return values.toArray(new Double[] {});
case FLOAT:
for (String value : defaultValues) {
values.add(Float.valueOf(value));
}
return values.toArray(new Float[] {});
case INTEGER:
for (String value : defaultValues) {
values.add(Integer.valueOf(value));
}
return values.toArray(new Integer[] {});
case LONG:
for (String value : defaultValues) {
values.add(Long.valueOf(value));
}
return values.toArray(new Long[] {});
case SHORT:
for (String value : defaultValues) {
values.add(Short.valueOf(value));
}
return values.toArray(new Short[] {});
case PASSWORD:
ServiceReference<CryptoService> sr = ctx.getBundleContext().getServiceReference(CryptoService.class);
CryptoService cryptoService = ctx.getBundleContext().getService(sr);
for (String value : defaultValues) {
try {
values.add(new Password(cryptoService.encryptAes(value.toCharArray())));
} catch (Exception e) {
values.add(new Password(value));
}
}
return values.toArray(new Password[] {});
case STRING:
return defaultValues;
}
return null;
}
use of org.eclipse.kura.crypto.CryptoService in project kura by eclipse.
the class XmlConfigPropertiesAdapter method unmarshal.
public Map<String, Object> unmarshal(XmlConfigPropertiesAdapted adaptedPropsAdapted) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>();
XmlConfigPropertyAdapted[] adaptedProps = adaptedPropsAdapted.getProperties();
if (adaptedProps == null) {
return properties;
}
for (XmlConfigPropertyAdapted adaptedProp : adaptedProps) {
String propName = adaptedProp.getName();
ConfigPropertyType type = adaptedProp.getType();
if (type != null) {
Object propvalue = null;
if (adaptedProp.getArray() == false) {
switch(adaptedProp.getType()) {
case STRING_TYPE:
propvalue = adaptedProp.getValues()[0];
break;
case LONG_TYPE:
propvalue = Long.parseLong(adaptedProp.getValues()[0]);
break;
case DOUBLE_TYPE:
propvalue = Double.parseDouble(adaptedProp.getValues()[0]);
break;
case FLOAT_TYPE:
propvalue = Float.parseFloat(adaptedProp.getValues()[0]);
break;
case INTEGER_TYPE:
propvalue = Integer.parseInt(adaptedProp.getValues()[0]);
break;
case BYTE_TYPE:
propvalue = Byte.parseByte(adaptedProp.getValues()[0]);
break;
case CHAR_TYPE:
String s = adaptedProp.getValues()[0];
propvalue = Character.valueOf(s.charAt(0));
break;
case BOOLEAN_TYPE:
propvalue = Boolean.parseBoolean(adaptedProp.getValues()[0]);
break;
case SHORT_TYPE:
propvalue = Short.parseShort(adaptedProp.getValues()[0]);
break;
case PASSWORD_TYPE:
BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
ServiceReference<CryptoService> cryptoServiceRef = bundleContext.getServiceReference(CryptoService.class);
CryptoService cryptoService = bundleContext.getService(cryptoServiceRef);
propvalue = adaptedProp.getValues()[0];
if (adaptedProp.isEncrypted()) {
try {
propvalue = new Password(cryptoService.decryptAes(((String) propvalue).toCharArray()));
} catch (KuraException e) {
propvalue = new Password(cryptoService.decodeBase64((String) propvalue));
}
} else {
propvalue = new Password((String) propvalue);
}
break;
}
} else {
// Starting from 1.2.0 an empty array will never be present in a snapshot.
if (adaptedProp.getValues() == null) {
continue;
}
switch(adaptedProp.getType()) {
case STRING_TYPE:
propvalue = adaptedProp.getValues();
break;
case LONG_TYPE:
Long[] longValues = new Long[adaptedProp.getValues().length];
for (int i = 0; i < adaptedProp.getValues().length; i++) {
if (adaptedProp.getValues()[i] != null) {
longValues[i] = Long.parseLong(adaptedProp.getValues()[i]);
}
}
propvalue = longValues;
break;
case DOUBLE_TYPE:
Double[] doubleValues = new Double[adaptedProp.getValues().length];
for (int i = 0; i < adaptedProp.getValues().length; i++) {
if (adaptedProp.getValues()[i] != null) {
doubleValues[i] = Double.parseDouble(adaptedProp.getValues()[i]);
}
}
propvalue = doubleValues;
break;
case FLOAT_TYPE:
Float[] floatValues = new Float[adaptedProp.getValues().length];
for (int i = 0; i < adaptedProp.getValues().length; i++) {
if (adaptedProp.getValues()[i] != null) {
floatValues[i] = Float.parseFloat(adaptedProp.getValues()[i]);
}
}
propvalue = floatValues;
break;
case INTEGER_TYPE:
Integer[] intValues = new Integer[adaptedProp.getValues().length];
for (int i = 0; i < adaptedProp.getValues().length; i++) {
if (adaptedProp.getValues()[i] != null) {
intValues[i] = Integer.parseInt(adaptedProp.getValues()[i]);
}
}
propvalue = intValues;
break;
case BYTE_TYPE:
Byte[] byteValues = new Byte[adaptedProp.getValues().length];
for (int i = 0; i < adaptedProp.getValues().length; i++) {
if (adaptedProp.getValues()[i] != null) {
byteValues[i] = Byte.parseByte(adaptedProp.getValues()[i]);
}
}
propvalue = byteValues;
break;
case CHAR_TYPE:
Character[] charValues = new Character[adaptedProp.getValues().length];
for (int i = 0; i < adaptedProp.getValues().length; i++) {
if (adaptedProp.getValues()[i] != null) {
String s = adaptedProp.getValues()[i];
charValues[i] = Character.valueOf(s.charAt(0));
}
}
propvalue = charValues;
break;
case BOOLEAN_TYPE:
Boolean[] booleanValues = new Boolean[adaptedProp.getValues().length];
for (int i = 0; i < adaptedProp.getValues().length; i++) {
if (adaptedProp.getValues()[i] != null) {
booleanValues[i] = Boolean.parseBoolean(adaptedProp.getValues()[i]);
}
}
propvalue = booleanValues;
break;
case SHORT_TYPE:
Short[] shortValues = new Short[adaptedProp.getValues().length];
for (int i = 0; i < adaptedProp.getValues().length; i++) {
if (adaptedProp.getValues()[i] != null) {
shortValues[i] = Short.parseShort(adaptedProp.getValues()[i]);
}
}
propvalue = shortValues;
break;
case PASSWORD_TYPE:
BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
ServiceReference<CryptoService> cryptoServiceRef = bundleContext.getServiceReference(CryptoService.class);
CryptoService cryptoService = bundleContext.getService(cryptoServiceRef);
Password[] pwdValues = new Password[adaptedProp.getValues().length];
for (int i = 0; i < adaptedProp.getValues().length; i++) {
if (adaptedProp.getValues()[i] != null) {
if (adaptedProp.isEncrypted()) {
try {
pwdValues[i] = new Password(cryptoService.decryptAes(adaptedProp.getValues()[i].toCharArray()));
} catch (KuraException e) {
pwdValues[i] = new Password(cryptoService.decodeBase64(adaptedProp.getValues()[i]));
}
} else {
pwdValues[i] = new Password(adaptedProp.getValues()[i]);
}
}
}
propvalue = pwdValues;
break;
}
}
properties.put(propName, propvalue);
}
}
return properties;
}
use of org.eclipse.kura.crypto.CryptoService in project kura by eclipse.
the class SecureBasicHttpContext method handleSecurity.
/**
* Provides Basic authentication over HTTPS.
*/
@Override
public synchronized boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setHeader("X-FRAME-OPTIONS", "SAMEORIGIN");
response.setHeader("X-XSS-protection", "1; mode=block");
response.setHeader("X-Content-Type-Options", "nosniff");
response.setHeader("Cache-Control", "no-cache,no-store");
response.setHeader("Pragma", "no-cache");
// If a trailing "/" is used when accesssing the app, redirect
if (request.getRequestURI().equals(this.m_appRoot + "/")) {
response.sendRedirect(this.m_appRoot);
}
// If using root context, redirect
if (request.getRequestURI().equals("/")) {
response.sendRedirect(this.m_appRoot);
}
HttpSession session = request.getSession(false);
if (session != null) {
String logout = (String) session.getAttribute("logout");
if (logout != null) {
session.removeAttribute("logout");
session.invalidate();
return failAuthorization(response);
}
}
String authHeader = request.getHeader("Authorization");
if (authHeader == null) {
s_logger.debug("Missing 'Authorization' HTTP header");
return failAuthorization(response);
}
StringTokenizer tokens = new StringTokenizer(authHeader);
String authScheme = tokens.nextToken();
if (!"Basic".equals(authScheme)) {
s_logger.error("The authentication scheme is not 'Basic'");
return failAuthorization(response);
}
String base64 = tokens.nextToken();
String credentials = null;
try {
CryptoService cryptoService = ServiceLocator.getInstance().getService(CryptoService.class);
credentials = cryptoService.decodeBase64(base64);
} catch (GwtKuraException e) {
throw new IOException(e.getMessage());
} catch (NoSuchAlgorithmException e) {
throw new IOException(e.getMessage());
}
int colon = credentials.indexOf(':');
String userid = credentials.substring(0, colon);
String password = credentials.substring(colon + 1);
Subject subject = login(request, response, userid, password);
if (subject == null) {
return failAuthorization(response);
}
request.setAttribute(HttpContext.REMOTE_USER, null);
request.setAttribute(HttpContext.AUTHENTICATION_TYPE, request.getAuthType());
request.setAttribute(HttpContext.AUTHORIZATION, null);
return true;
}
use of org.eclipse.kura.crypto.CryptoService in project kura by eclipse.
the class GwtSslServiceImpl method updateSslConfiguration.
@Override
public void updateSslConfiguration(GwtXSRFToken xsrfToken, GwtSslConfig sslConfig) throws GwtKuraException {
checkXSRFToken(xsrfToken);
try {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PROP_PROTOCOL, sslConfig.getProtocol());
properties.put(PROP_HN_VERIFY, sslConfig.isHostnameVerification());
properties.put(PROP_TRUST_STORE, sslConfig.getKeyStore());
if (PLACEHOLDER.equals(sslConfig.getKeystorePassword())) {
CryptoService cryptoService = ServiceLocator.getInstance().getService(CryptoService.class);
SslManagerServiceOptions options = getSslConfiguration();
properties.put(PROP_TRUST_PASSWORD, new Password(cryptoService.decryptAes(options.getSslKeystorePassword().toCharArray())));
} else {
properties.put(PROP_TRUST_PASSWORD, new Password(sslConfig.getKeystorePassword()));
}
properties.put(PROP_CIPHERS, sslConfig.getCiphers());
ConfigurationService configService = ServiceLocator.getInstance().getService(ConfigurationService.class);
configService.updateConfiguration(SSL_PID, properties);
} catch (KuraException e) {
throw new GwtKuraException(e.getMessage());
}
}
use of org.eclipse.kura.crypto.CryptoService in project kura by eclipse.
the class AuthenticationManager method authenticate.
public boolean authenticate(String username, String password) {
try {
CryptoService cryptoService = ServiceLocator.getInstance().getService(CryptoService.class);
String sha1Password = cryptoService.sha1Hash(password);
boolean isUsernameMatching = username.equals(this.username);
boolean isPasswordMatching = Arrays.equals(sha1Password.toCharArray(), this.password);
return isUsernameMatching && isPasswordMatching;
} catch (Exception e) {
}
return false;
}
Aggregations