Search in sources :

Example 1 with WonProtocolException

use of won.protocol.exception.WonProtocolException in project webofneeds by researchstudio-sat.

the class LinkedDataWebController method register.

@RequestMapping(value = "${uri.path.resource}", method = RequestMethod.POST, produces = { "text/plain" })
public ResponseEntity<String> register(@RequestParam("register") String registeredType, HttpServletRequest request) throws CertificateException, UnsupportedEncodingException {
    logger.debug("REGISTERING " + registeredType);
    PreAuthenticatedAuthenticationToken authentication = (PreAuthenticatedAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
    if (!(authentication instanceof PreAuthenticatedAuthenticationToken)) {
        throw new BadCredentialsException("Could not register: PreAuthenticatedAuthenticationToken expected");
    }
    // Object principal = authentication.getPrincipal();
    Object credentials = authentication.getCredentials();
    X509Certificate cert;
    if (credentials instanceof X509Certificate) {
        cert = (X509Certificate) credentials;
    } else {
        throw new BadCredentialsException("Could not register: expected to find a X509Certificate in the request");
    }
    try {
        if ("owner".equals(registeredType)) {
            String result = registrationServer.registerOwner(cert);
            logger.debug("successfully registered owner");
            return new ResponseEntity<>(result, HttpStatus.OK);
        }
        if ("node".equals(registeredType)) {
            String result = registrationServer.registerNode(cert);
            logger.debug("successfully registered node");
            return new ResponseEntity<>(result, HttpStatus.OK);
        } else {
            String supportedTypesMsg = "Request parameter error; supported 'register' parameter values: 'owner', 'node'";
            logger.debug(supportedTypesMsg);
            return new ResponseEntity<>(supportedTypesMsg, HttpStatus.BAD_REQUEST);
        }
    } catch (WonProtocolException e) {
        logger.info("Could not register " + registeredType, e);
        return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
    }
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) WonProtocolException(won.protocol.exception.WonProtocolException) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) X509Certificate(java.security.cert.X509Certificate) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with WonProtocolException

use of won.protocol.exception.WonProtocolException in project webofneeds by researchstudio-sat.

the class RegistrationServerCertificateBased method registerOwner.

@Transactional
public String registerOwner(Object certificateChainObj) throws WonProtocolException {
    String alias = null;
    X509Certificate[] ownerCertChain = new X509Certificate[] { (X509Certificate) certificateChainObj };
    checkTrusted(ownerCertChain);
    try {
        alias = aliasGenerator.generateAlias(ownerCertChain[0]);
        logger.info("Public key hash to be used as ownerApplicationId: {}", alias);
        alias = ownerManagementService.registerOwnerApplication(alias);
    } catch (Exception e) {
        logger.warn("could not register owner", e);
        throw new WonProtocolException(e);
    }
    return alias;
}
Also used : WonProtocolException(won.protocol.exception.WonProtocolException) X509Certificate(java.security.cert.X509Certificate) WonProtocolException(won.protocol.exception.WonProtocolException) CertificateException(java.security.cert.CertificateException) Transactional(javax.transaction.Transactional)

Aggregations

X509Certificate (java.security.cert.X509Certificate)2 WonProtocolException (won.protocol.exception.WonProtocolException)2 CertificateException (java.security.cert.CertificateException)1 Transactional (javax.transaction.Transactional)1 ResponseEntity (org.springframework.http.ResponseEntity)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 PreAuthenticatedAuthenticationToken (org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1