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());
}
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);
}
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());
}
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);
}
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);
}
Aggregations