Search in sources :

Example 41 with SEPASecurityException

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) {
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) ObjectInput(java.io.ObjectInput) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 42 with SEPASecurityException

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);
}
Also used : Response(it.unibo.arces.wot.sepa.commons.response.Response) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse) JWTResponse(it.unibo.arces.wot.sepa.commons.response.JWTResponse) SEPAProtocolException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAProtocolException) SEPAPropertiesException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) JWTResponse(it.unibo.arces.wot.sepa.commons.response.JWTResponse) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse)

Example 43 with SEPASecurityException

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;
}
Also used : SEPAProtocolException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAProtocolException) SEPAPropertiesException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException) FileNotFoundException(java.io.FileNotFoundException) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) IOException(java.io.IOException) ApplicationProfile(it.unibo.arces.wot.sepa.pattern.ApplicationProfile) Vector(java.util.Vector) FileInputStream(java.io.FileInputStream)

Example 44 with SEPASecurityException

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;
}
Also used : SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) SEPAPropertiesException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException)

Example 45 with SEPASecurityException

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);
}
Also used : SEPAPropertiesException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException) JsonObject(com.google.gson.JsonObject) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) JsonParseException(com.google.gson.JsonParseException) Date(java.util.Date) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse) UpdateResponse(it.unibo.arces.wot.sepa.commons.response.UpdateResponse) QueryResponse(it.unibo.arces.wot.sepa.commons.response.QueryResponse) RegistrationResponse(it.unibo.arces.wot.sepa.commons.response.RegistrationResponse) JsonParser(com.google.gson.JsonParser) JWTResponse(it.unibo.arces.wot.sepa.commons.response.JWTResponse)

Aggregations

SEPASecurityException (it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException)69 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)29 IOException (java.io.IOException)20 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)18 ErrorResponse (it.unibo.arces.wot.sepa.commons.response.ErrorResponse)15 Response (it.unibo.arces.wot.sepa.commons.response.Response)12 SEPAPropertiesException (it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException)11 SEPAProtocolException (it.unibo.arces.wot.sepa.commons.exceptions.SEPAProtocolException)10 JsonObject (com.google.gson.JsonObject)7 JsonParser (com.google.gson.JsonParser)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)7 Modification (org.apache.directory.api.ldap.model.entry.Modification)7 SEPABindingsException (it.unibo.arces.wot.sepa.commons.exceptions.SEPABindingsException)5 Credentials (it.unibo.arces.wot.sepa.commons.security.Credentials)5 HttpEntity (org.apache.http.HttpEntity)5 JOSEException (com.nimbusds.jose.JOSEException)4 SignedJWT (com.nimbusds.jwt.SignedJWT)4 JWTResponse (it.unibo.arces.wot.sepa.commons.response.JWTResponse)4 ParseException (java.text.ParseException)4