use of it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException in project SEPA by arces-wot.
the class Credentials method deserialize.
public static Credentials deserialize(byte[] stream) throws SEPASecurityException {
ByteArrayInputStream bis = new ByteArrayInputStream(stream);
ObjectInput in = null;
try {
in = new ObjectInputStream(bis);
return (Credentials) in.readObject();
} catch (IOException | ClassNotFoundException e) {
logger.error(e.getMessage());
throw new SEPASecurityException("Deserialize exception: " + e.getMessage());
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
}
}
}
use of it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException in project SEPA by arces-wot.
the class WebsocketSubscriptionProtocol method onError.
@Override
public void onError(ErrorResponse errorResponse) {
// REFRESH TOKEN
if (errorResponse.isTokenExpiredError()) {
String authHeader = null;
try {
Response ret = sm.refreshToken();
if (ret.isError()) {
logger.error(ret);
handler.onError((ErrorResponse) ret);
return;
}
JWTResponse token = (JWTResponse) ret;
authHeader = token.getTokenType() + " " + token.getAccessToken();
} catch (SEPAPropertiesException | SEPASecurityException e1) {
logger.error(e1.getMessage());
handler.onError(errorResponse);
return;
}
synchronized (mutex) {
if (lastRequest == null) {
handler.onError(errorResponse);
return;
}
}
try {
lastRequest.setAuthorizationHeader(authHeader);
logger.trace("SEND LAST REQUEST WITH NEW TOKEN");
client.send(lastRequest.toString());
} catch (SEPAProtocolException e) {
logger.error(e.getMessage());
if (logger.isTraceEnabled())
e.printStackTrace();
ErrorResponse err = new ErrorResponse(401, "invalid_grant", "Failed to send request after refreshing token. " + e.getMessage());
handler.onError(err);
}
} else
handler.onError(errorResponse);
}
use of it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException in project SEPA by arces-wot.
the class Dashboard method loadSAP.
private boolean loadSAP(String file) {
if (file == null) {
FileInputStream in = null;
try {
in = new FileInputStream("dashboard.properties");
} catch (FileNotFoundException e) {
logger.warn(e.getMessage());
lblInfo.setText("Error: " + e.getMessage());
lblInfo.setToolTipText("Error: " + e.getMessage());
frmSepaDashboard.setTitle(versionLabel + " - " + e.getMessage());
return false;
}
// Properties sapFile = new Properties();
try {
appProperties.load(in);
} catch (IOException e) {
logger.error(e.getMessage());
lblInfo.setText("Error: " + e.getMessage());
lblInfo.setToolTipText("Error: " + e.getMessage());
frmSepaDashboard.setTitle(versionLabel + " - " + e.getMessage());
return false;
}
String path = appProperties.getProperty("appProfile");
if (path == null) {
lblInfo.setText("Error: path in dashboard.properties is null");
lblInfo.setToolTipText("Error: path in dashboard.properties is null");
frmSepaDashboard.setTitle(versionLabel + " - " + "path in dashboard.properties is null");
return false;
}
file = path;
}
labelUrl.setText("---");
labelHttpPort.setText("---");
labelHttpsPort.setText("---");
labelWsPort.setText("---");
labelWssPort.setText("---");
labelUpdatePath.setText("---");
labelQueryPath.setText("---");
labelSubscribePath.setText("---");
labelSecurePath.setText("---");
SPARQLSubscribe.setText("");
SPARQLUpdate.setText("");
namespacesDM.getDataVector().clear();
updateListDM.clear();
subscribeListDM.clear();
updateForcedBindingsDM.clearBindings();
subscribeForcedBindingsDM.clearBindings();
updatesList.clearSelection();
subscribesList.clearSelection();
try {
appProfile = new ApplicationProfile(file);
} catch (SEPAPropertiesException e) {
logger.error(e.getMessage());
lblInfo.setText("Error: " + e.getMessage());
lblInfo.setToolTipText("Error: " + e.getMessage());
frmSepaDashboard.setTitle(versionLabel + " - " + e.getMessage());
return false;
}
try {
sepaClient = new SEPADashbooard(appProfile);
} catch (SEPAProtocolException | SEPASecurityException e) {
logger.error(e.getMessage());
lblInfo.setText("Error: " + e.getMessage());
lblInfo.setToolTipText("Error: " + e.getMessage());
frmSepaDashboard.setTitle(versionLabel + " - " + e.getMessage());
return false;
}
frmSepaDashboard.setTitle(versionLabel + " - " + file);
// Loading namespaces
for (String prefix : appProfile.getPrefixes()) {
Vector<String> row = new Vector<String>();
row.add(prefix);
row.addElement(appProfile.getNamespaceURI(prefix));
namespacesDM.addRow(row);
}
// Loading updates
for (String update : appProfile.getUpdateIds()) {
// updateListDM.addElement(update);
updateListDM.add(update);
}
// Loading subscribes
for (String subscribe : appProfile.getSubscribeIds()) {
// subscribeListDM.addElement(subscribe);
subscribeListDM.add(subscribe);
}
// Enable all the buttons
btnUpdate.setEnabled(true);
btnSubscribe.setEnabled(true);
btnQuery.setEnabled(true);
labelUrl.setText(appProfile.getHost());
labelHttpPort.setText(String.format("%d", appProfile.getHttpPort()));
labelHttpsPort.setText(String.format("%d", appProfile.getHttpsPort()));
labelWsPort.setText(String.format("%d", appProfile.getWsPort()));
labelWssPort.setText(String.format("%d", appProfile.getWssPort()));
labelUpdatePath.setText(appProfile.getUpdatePath());
labelQueryPath.setText(appProfile.getQueryPath());
labelSubscribePath.setText(appProfile.getSubscribePath());
labelSecurePath.setText(appProfile.getSecurePath());
lblInfo.setText("JSAP loaded");
lblInfo.setToolTipText("JSAP loaded");
return true;
}
use of it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException in project SEPA by arces-wot.
the class SPARQL11SEProperties method getBasicAuthorization.
/**
* Gets the basic authorization.
*
* @return the basic authorization
* @throws SEPASecurityException
*/
public String getBasicAuthorization() throws SEPASecurityException {
String encryptedValue;
if (parameters.get("security").getAsJsonObject().get("client_id") != null && parameters.get("security").getAsJsonObject().get("client_secret") != null) {
encryptedValue = parameters.get("security").getAsJsonObject().get("client_id").getAsString();
String id = SEPAEncryption.decrypt(encryptedValue);
encryptedValue = parameters.get("security").getAsJsonObject().get("client_secret").getAsString();
String secret = SEPAEncryption.decrypt(encryptedValue);
String authorization;
try {
byte[] buf = Base64.getEncoder().encode((id + ":" + secret).getBytes("UTF-8"));
// authorization = Base64.getEncoder().encode((id + ":" +
// secret).getBytes("UTF-8")).toString();
authorization = new String(buf, "UTF-8");
} catch (Exception e) {
throw new SEPASecurityException(e);
}
// .replace("\n", "");
return authorization;
}
return null;
}
use of it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException in project SEPA by arces-wot.
the class SPARQL11SEProtocol method parseSPARQL11SEResponse.
protected Response parseSPARQL11SEResponse(String response, SPARQL11SEPrimitive op) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
if (response == null)
return new ErrorResponse(0, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Response is null");
JsonObject json = null;
try {
json = new JsonParser().parse(response).getAsJsonObject();
} catch (JsonParseException | IllegalStateException e) {
return new ErrorResponse(0, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Unknown response: " + response);
}
// Error response
if (json.get("code") != null)
if (json.get("code").getAsInt() >= 400)
return new ErrorResponse(0, json.get("code").getAsInt(), json.get("body").getAsString());
if (op == SPARQL11SEPrimitive.SECUREQUERY)
return new QueryResponse(json);
if (op == SPARQL11SEPrimitive.SECUREUPDATE)
return new UpdateResponse(response);
if (op == SPARQL11SEPrimitive.REGISTER) {
if (json.get("client_id") != null && json.get("client_secret") != null) {
try {
properties.setCredentials(json.get("client_id").getAsString(), json.get("client_secret").getAsString());
} catch (SEPASecurityException | SEPAPropertiesException e) {
return new ErrorResponse(-1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Failed to save credentials");
}
return new RegistrationResponse(json.get("client_id").getAsString(), json.get("client_secret").getAsString(), json.get("signature"));
}
return new ErrorResponse(-1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Credentials not found in registration response");
}
if (op == SPARQL11SEPrimitive.REQUESTTOKEN) {
if (json.get("access_token") != null && json.get("expires_in") != null && json.get("token_type") != null) {
int seconds = json.get("expires_in").getAsInt();
Date expires = new Date();
expires.setTime(expires.getTime() + (1000 * seconds));
try {
properties.setJWT(json.get("access_token").getAsString(), expires, json.get("token_type").getAsString());
} catch (SEPASecurityException | SEPAPropertiesException e) {
return new ErrorResponse(-1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Failed to save JWT");
}
return new JWTResponse(json.get("access_token").getAsString(), json.get("token_type").getAsString(), json.get("expires_in").getAsLong());
} else if (json.get("code") != null && json.get("body") != null)
return new ErrorResponse(0, json.get("code").getAsInt(), json.get("body").getAsString());
else if (json.get("code") != null)
return new ErrorResponse(0, json.get("code").getAsInt(), "");
return new ErrorResponse(0, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Response not recognized: " + json.toString());
}
return new ErrorResponse(0, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Response unknown: " + response);
}
Aggregations