Search in sources :

Example 51 with GeneralException

use of edu.uiuc.ncsa.security.core.exceptions.GeneralException in project OA4MP by ncsa.

the class AssetConverter method fromMap.

@Override
public Asset fromMap(ConversionMap<String, Object> map, Asset asset) {
    super.fromMap(map, asset);
    asset.setUsername(map.getString(getAR().username()));
    try {
        String rawCert = map.getString(getAR().certificates());
        if (rawCert != null && 0 < rawCert.length()) {
            asset.setCertificates(CertUtil.fromX509PEM(rawCert));
        }
    } catch (CertificateException e) {
        throw new GeneralException("Error: could not create certificate", e);
    }
    String temp = map.getString(getAR().privateKey());
    if (temp != null) {
        asset.setPrivateKey(KeyUtil.fromPKCS8PEM(temp));
    }
    asset.setRedirect(map.getURI(getAR().redirect()));
    asset.setCreationTime(map.getDate(getAR().creationTime()));
    asset.setToken(map.getIdentifier(getAR().token()));
    String rawCertReq = map.getString(getAR().certReq());
    if (rawCertReq != null) {
        asset.setCertReq(CertUtil.fromStringToCertReq(rawCertReq));
    }
    return asset;
}
Also used : GeneralException(edu.uiuc.ncsa.security.core.exceptions.GeneralException) CertificateException(java.security.cert.CertificateException)

Example 52 with GeneralException

use of edu.uiuc.ncsa.security.core.exceptions.GeneralException in project OA4MP by ncsa.

the class SQLAssetStore method getByToken.

@Override
public Asset getByToken(Identifier token) {
    if (token == null) {
        return null;
    }
    Connection c = getConnection();
    Asset t = null;
    try {
        PreparedStatement stmt = c.prepareStatement(getAST().getByTokenStatement());
        stmt.setString(1, token.toString());
        stmt.executeQuery();
        ResultSet rs = stmt.getResultSet();
        // Now we have to pull in all the values.
        if (!rs.next()) {
            rs.close();
            stmt.close();
            // returning a null fulfills contract for this being a map.
            return null;
        }
        ColumnMap map = rsToMap(rs);
        rs.close();
        stmt.close();
        t = create();
        populate(map, t);
    } catch (SQLException e) {
        destroyConnection(c);
        throw new GeneralException("Error getting object with identifier \"" + token + "\"", e);
    } finally {
        releaseConnection(c);
    }
    return t;
}
Also used : ColumnMap(edu.uiuc.ncsa.security.storage.sql.internals.ColumnMap) GeneralException(edu.uiuc.ncsa.security.core.exceptions.GeneralException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Asset(edu.uiuc.ncsa.myproxy.oa4mp.client.Asset) PreparedStatement(java.sql.PreparedStatement)

Example 53 with GeneralException

use of edu.uiuc.ncsa.security.core.exceptions.GeneralException in project airavata by apache.

the class CredentialStoreCallbackServlet method doIt.

@Override
protected void doIt(HttpServletRequest request, HttpServletResponse response) throws Throwable {
    String gatewayName = request.getParameter(CredentialStoreConstants.GATEWAY_NAME_QUERY_PARAMETER);
    String portalUserName = request.getParameter(CredentialStoreConstants.PORTAL_USER_QUERY_PARAMETER);
    String durationParameter = request.getParameter(CredentialStoreConstants.DURATION_QUERY_PARAMETER);
    String contactEmail = request.getParameter(CredentialStoreConstants.PORTAL_USER_EMAIL_QUERY_PARAMETER);
    String portalTokenId = request.getParameter(CredentialStoreConstants.PORTAL_TOKEN_ID_ASSIGNED);
    // TODO remove hard coded values, once passing query parameters is
    // fixed in OA4MP client api
    long duration = 864000;
    if (durationParameter != null) {
        duration = Long.parseLong(durationParameter);
    }
    if (portalTokenId == null) {
        error("Token given by portal is invalid.");
        GeneralException ge = new GeneralException("Error: The token presented by portal is null.");
        request.setAttribute("exception", ge);
        JSPUtil.fwd(request, response, configurationReader.getErrorUrl());
        return;
    }
    info("Gateway name " + gatewayName);
    info("Portal user name " + portalUserName);
    info("Community user contact email " + contactEmail);
    info("Token id presented " + portalTokenId);
    info("2.a. Getting token and verifier.");
    String token = request.getParameter(CONST(ClientEnvironment.TOKEN));
    String verifier = request.getParameter(CONST(ClientEnvironment.VERIFIER));
    if (token == null || verifier == null) {
        warn("2.a. The token is " + (token == null ? "null" : token) + " and the verifier is " + (verifier == null ? "null" : verifier));
        GeneralException ge = new GeneralException("Error: This servlet requires parameters for the token and verifier. It cannot be called directly.");
        request.setAttribute("exception", ge);
        JSPUtil.fwd(request, response, configurationReader.getErrorUrl());
        return;
    }
    info("2.a Token and verifier found.");
    X509Certificate[] certificates;
    AssetResponse assetResponse = null;
    PrivateKey privateKey;
    try {
        PrivateKeyStore privateKeyStore = PrivateKeyStore.getPrivateKeyStore();
        privateKey = privateKeyStore.getKey(portalTokenId);
        if (privateKey != null) {
            info("Found private key for token " + portalTokenId);
        } else {
            info("Could not find private key for token " + portalTokenId);
        }
        info("2.a. Getting the cert(s) from the service");
        assetResponse = getOA4MPService().getCert(token, verifier);
        certificates = assetResponse.getX509Certificates();
    } catch (Throwable t) {
        warn("2.a. Exception from the server: " + t.getCause().getMessage());
        error("Exception while trying to get cert. message:" + t.getMessage());
        request.setAttribute("exception", t);
        JSPUtil.fwd(request, response, configurationReader.getErrorUrl());
        return;
    }
    info("2.b. Done! Displaying success page.");
    CertificateCredential certificateCredential = new CertificateCredential();
    // TODO check this is correct
    certificateCredential.setNotBefore(Utility.convertDateToString(certificates[0].getNotBefore()));
    certificateCredential.setNotAfter(Utility.convertDateToString(certificates[0].getNotAfter()));
    certificateCredential.setCertificates(certificates);
    certificateCredential.setPrivateKey(privateKey);
    certificateCredential.setCommunityUser(new CommunityUser(gatewayName, assetResponse.getUsername(), contactEmail));
    certificateCredential.setPortalUserName(portalUserName);
    certificateCredential.setLifeTime(duration);
    certificateCredential.setToken(portalTokenId);
    certificateCredentialWriter.writeCredentials(certificateCredential);
    StringBuilder stringBuilder = new StringBuilder("Certificate for community user ");
    stringBuilder.append(assetResponse.getUsername()).append(" successfully persisted.");
    stringBuilder.append(" Certificate DN - ").append(certificates[0].getSubjectDN());
    info(stringBuilder.toString());
    if (isUrlInSameServer(configurationReader.getSuccessUrl())) {
        String contextPath = request.getContextPath();
        if (!contextPath.endsWith("/")) {
            contextPath = contextPath + "/";
        }
        request.setAttribute("action", contextPath);
        request.setAttribute("tokenId", portalTokenId);
        JSPUtil.fwd(request, response, configurationReader.getSuccessUrl());
    } else {
        String urlToRedirect = decorateUrlWithToken(configurationReader.getSuccessUrl(), portalTokenId);
        info("Redirecting to url - " + urlToRedirect);
        response.sendRedirect(urlToRedirect);
    }
    info("2.a. Completely finished with delegation.");
}
Also used : CertificateCredential(org.apache.airavata.credential.store.credential.impl.certificate.CertificateCredential) GeneralException(edu.uiuc.ncsa.security.core.exceptions.GeneralException) PrivateKey(java.security.PrivateKey) CommunityUser(org.apache.airavata.credential.store.credential.CommunityUser) PrivateKeyStore(org.apache.airavata.credential.store.util.PrivateKeyStore) AssetResponse(edu.uiuc.ncsa.myproxy.oa4mp.client.AssetResponse) X509Certificate(java.security.cert.X509Certificate)

Aggregations

GeneralException (edu.uiuc.ncsa.security.core.exceptions.GeneralException)53 ServiceTransaction (edu.uiuc.ncsa.security.delegation.server.ServiceTransaction)9 SQLException (java.sql.SQLException)8 Connection (java.sql.Connection)7 PreparedStatement (java.sql.PreparedStatement)7 ResultSet (java.sql.ResultSet)7 Identifier (edu.uiuc.ncsa.security.core.Identifier)5 BasicIdentifier (edu.uiuc.ncsa.security.core.util.BasicIdentifier)5 MyPKCS10CertRequest (edu.uiuc.ncsa.security.util.pkcs.MyPKCS10CertRequest)5 X509Certificate (java.security.cert.X509Certificate)5 AssetResponse (edu.uiuc.ncsa.myproxy.oa4mp.client.AssetResponse)4 TransactionState (edu.uiuc.ncsa.security.delegation.servlet.TransactionState)4 AccessToken (edu.uiuc.ncsa.security.delegation.token.AccessToken)4 AuthorizationGrant (edu.uiuc.ncsa.security.delegation.token.AuthorizationGrant)4 OA2SE (edu.uiuc.ncsa.myproxy.oa4mp.oauth2.OA2SE)3 OA2ServiceTransaction (edu.uiuc.ncsa.myproxy.oa4mp.oauth2.OA2ServiceTransaction)3 ColumnMap (edu.uiuc.ncsa.security.storage.sql.internals.ColumnMap)3 File (java.io.File)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3