Search in sources :

Example 16 with ErrorResponse

use of it.unibo.arces.wot.sepa.commons.response.ErrorResponse in project SEPA by arces-wot.

the class AuthorizationManager method validateToken.

public Response validateToken(String accessToken) {
    logger.debug("Validate token");
    // Parse and verify the token
    SignedJWT signedJWT = null;
    try {
        signedJWT = SignedJWT.parse(accessToken);
    } catch (ParseException e) {
        return new ErrorResponse(HttpStatus.SC_UNAUTHORIZED, e.getMessage());
    }
    try {
        if (!signedJWT.verify(verifier))
            return new ErrorResponse(HttpStatus.SC_UNAUTHORIZED);
    } catch (JOSEException e) {
        return new ErrorResponse(HttpStatus.SC_UNAUTHORIZED, e.getMessage());
    }
    // Process the token
    JWTClaimsSet claimsSet;
    try {
        claimsSet = jwtProcessor.process(accessToken, context);
    } catch (ParseException | BadJOSEException | JOSEException e) {
        return new ErrorResponse(HttpStatus.SC_UNAUTHORIZED, e.getMessage());
    }
    // Check token expiration
    Date now = new Date();
    if (now.after(claimsSet.getExpirationTime()))
        return new ErrorResponse(0, HttpStatus.SC_UNAUTHORIZED, "Token is expired " + claimsSet.getExpirationTime());
    if (now.before(claimsSet.getNotBeforeTime()))
        return new ErrorResponse(0, HttpStatus.SC_UNAUTHORIZED, "Token can not be used before: " + claimsSet.getNotBeforeTime());
    return new JWTResponse(accessToken, "bearer", now.getTime() - claimsSet.getExpirationTime().getTime());
}
Also used : BadJOSEException(com.nimbusds.jose.proc.BadJOSEException) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) SignedJWT(com.nimbusds.jwt.SignedJWT) ParseException(java.text.ParseException) JOSEException(com.nimbusds.jose.JOSEException) BadJOSEException(com.nimbusds.jose.proc.BadJOSEException) Date(java.util.Date) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse) JWTResponse(it.unibo.arces.wot.sepa.commons.response.JWTResponse)

Example 17 with ErrorResponse

use of it.unibo.arces.wot.sepa.commons.response.ErrorResponse in project SEPA by arces-wot.

the class SPUManager method unsubscribe.

public Response unsubscribe(UnsubscribeRequest req) {
    logger.debug(req);
    SPUManagerBeans.unsubscribeRequest();
    String spuid = req.getSubscribeUUID();
    if (!spus.containsKey(spuid))
        return new ErrorResponse(req.getToken(), 404, "SPUID not found: " + spuid);
    synchronized (unsubscribeQueue) {
        unsubscribeQueue.offer(spus.get(spuid));
        unsubscribeQueue.notify();
    }
    return new UnsubscribeResponse(req.getToken(), spuid);
}
Also used : UnsubscribeResponse(it.unibo.arces.wot.sepa.commons.response.UnsubscribeResponse) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse)

Example 18 with ErrorResponse

use of it.unibo.arces.wot.sepa.commons.response.ErrorResponse in project SEPA by arces-wot.

the class SPUManager method subscribe.

public Response subscribe(SubscribeRequest req, EventHandler handler) {
    logger.debug(req.toString());
    SPUManagerBeans.subscribeRequest();
    // TODO: choose different kinds of SPU based on subscribe request
    SPU spu = null;
    try {
        spu = new SPUNaive(req, handler, endpointProperties, endpointSemaphore, processingSpus);
    // spu.addObserver(this);
    } catch (SEPAProtocolException e) {
        logger.debug("SPU creation failed: " + e.getMessage());
        return new ErrorResponse(req.getToken(), 500, "SPU creation failed: " + req.toString());
    }
    logger.debug("SPU init");
    if (!spu.init()) {
        logger.debug("SPU initialization failed");
        return new ErrorResponse(req.getToken(), 500, "SPU initialization failed: " + req.toString());
    }
    synchronized (subscribeQueue) {
        logger.debug("Add SPU to activation queue");
        subscribeQueue.offer(spu);
        subscribeQueue.notify();
    }
    return new SubscribeResponse(req.getToken(), spu.getUUID(), req.getAlias(), spu.getFirstResults());
}
Also used : SEPAProtocolException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAProtocolException) SubscribeResponse(it.unibo.arces.wot.sepa.commons.response.SubscribeResponse) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse)

Example 19 with ErrorResponse

use of it.unibo.arces.wot.sepa.commons.response.ErrorResponse in project SEPA by arces-wot.

the class SecureWebsocketServer method validateToken.

private Response validateToken(String request) {
    JsonObject req;
    try {
        req = new JsonParser().parse(request).getAsJsonObject();
    } catch (JsonParseException | IllegalStateException e) {
        return new ErrorResponse(HttpStatus.SC_UNAUTHORIZED, e.getMessage());
    }
    if (req.get("authorization") == null)
        return new ErrorResponse(HttpStatus.SC_UNAUTHORIZED, "authorization key is missing");
    String oauthRequest = null;
    String jwt = null;
    try {
        oauthRequest = req.get("authorization").getAsString();
        if (!oauthRequest.startsWith("Bearer "))
            new ErrorResponse(HttpStatus.SC_UNAUTHORIZED, "authorization value MUST be of type Bearer");
        jwt = oauthRequest.substring(7);
    } catch (Exception e) {
        return new ErrorResponse(HttpStatus.SC_UNAUTHORIZED, "authorization key value is wrong");
    }
    // Token validation
    return oauth.validateToken(jwt);
}
Also used : JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException) JsonParseException(com.google.gson.JsonParseException) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) KeyManagementException(java.security.KeyManagementException) SEPAProtocolException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAProtocolException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) JsonParser(com.google.gson.JsonParser) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse)

Example 20 with ErrorResponse

use of it.unibo.arces.wot.sepa.commons.response.ErrorResponse in project SEPA by arces-wot.

the class SEPAWebsocketClient method onMessage.

@Override
public void onMessage(String message) {
    logger.debug("@onMessage " + message);
    // Parse message
    JsonObject notify = new JsonParser().parse(message).getAsJsonObject();
    if (notify.get("ping") != null) {
        if (handler != null)
            handler.onPing();
    } else if (notify.get("subscribed") != null) {
        response = new SubscribeResponse(notify);
        setResponse();
    } else if (notify.get("unsubscribed") != null) {
        response = new UnsubscribeResponse(notify);
        setResponse();
    } else if (notify.get("results") != null) {
        if (handler != null)
            handler.onSemanticEvent(new Notification(notify));
    } else if (notify.get("code") != null) {
        if (handler != null)
            handler.onError(new ErrorResponse(notify));
    } else
        logger.error("Unknown message: " + message);
}
Also used : JsonObject(com.google.gson.JsonObject) SubscribeResponse(it.unibo.arces.wot.sepa.commons.response.SubscribeResponse) UnsubscribeResponse(it.unibo.arces.wot.sepa.commons.response.UnsubscribeResponse) Notification(it.unibo.arces.wot.sepa.commons.response.Notification) JsonParser(com.google.gson.JsonParser) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse)

Aggregations

ErrorResponse (it.unibo.arces.wot.sepa.commons.response.ErrorResponse)23 Response (it.unibo.arces.wot.sepa.commons.response.Response)8 JsonParser (com.google.gson.JsonParser)6 IOException (java.io.IOException)6 JsonObject (com.google.gson.JsonObject)5 JsonParseException (com.google.gson.JsonParseException)5 JWTResponse (it.unibo.arces.wot.sepa.commons.response.JWTResponse)4 QueryResponse (it.unibo.arces.wot.sepa.commons.response.QueryResponse)4 HttpEntity (org.apache.http.HttpEntity)4 SEPAProtocolException (it.unibo.arces.wot.sepa.commons.exceptions.SEPAProtocolException)3 SEPASecurityException (it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException)3 UpdateRequest (it.unibo.arces.wot.sepa.commons.request.UpdateRequest)3 SubscribeResponse (it.unibo.arces.wot.sepa.commons.response.SubscribeResponse)3 UpdateResponse (it.unibo.arces.wot.sepa.commons.response.UpdateResponse)3 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)3 JOSEException (com.nimbusds.jose.JOSEException)2 BadJOSEException (com.nimbusds.jose.proc.BadJOSEException)2 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)2 SignedJWT (com.nimbusds.jwt.SignedJWT)2 SubscribeRequest (it.unibo.arces.wot.sepa.commons.request.SubscribeRequest)2