Search in sources :

Example 1 with CibaCoreException

use of org.wso2.carbon.identity.oauth.ciba.exceptions.CibaCoreException in project identity-inbound-auth-oauth by wso2-extensions.

the class CibaMgtDAOImpl method persistAuthenticationSuccess.

@Override
public void persistAuthenticationSuccess(String authCodeKey, AuthenticatedUser authenticatedUser) throws CibaCoreException {
    // Obtain authenticated identity provider's identifier.
    String authenticatedIDP = OAuth2Util.getAuthenticatedIDP(authenticatedUser);
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(true)) {
        try (PreparedStatement prepStmt = connection.prepareStatement(SQLQueries.CibaSQLQueries.UPDATE_AUTHENTICATION_SUCCESS)) {
            int authenticatedTenant = OAuth2Util.getTenantId(authenticatedUser.getTenantDomain());
            prepStmt.setString(1, authenticatedUser.getUserName());
            prepStmt.setString(2, authenticatedUser.getUserStoreDomain());
            prepStmt.setInt(3, authenticatedTenant);
            prepStmt.setString(4, authenticatedIDP);
            prepStmt.setInt(5, authenticatedTenant);
            prepStmt.setString(6, AuthReqStatus.AUTHENTICATED.toString());
            prepStmt.setString(7, authCodeKey);
            prepStmt.execute();
            IdentityDatabaseUtil.commitTransaction(connection);
            if (log.isDebugEnabled()) {
                log.debug("Successfully updated the authentication request status to 'AUTHENTICATED' for the " + "request identified by authCodeKey: " + authCodeKey);
            }
        } catch (SQLException | IdentityOAuth2Exception e) {
            IdentityDatabaseUtil.rollbackTransaction(connection);
            throw new CibaCoreException("Error occurred in persisting the successful authentication identified by" + " authCodeKey: " + authCodeKey, e);
        }
    } catch (SQLException e) {
        throw new CibaCoreException("Error occurred in persisting the successful authentication identified by " + "authCodeKey: " + authCodeKey, e);
    }
}
Also used : IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) CibaCoreException(org.wso2.carbon.identity.oauth.ciba.exceptions.CibaCoreException)

Example 2 with CibaCoreException

use of org.wso2.carbon.identity.oauth.ciba.exceptions.CibaCoreException in project identity-inbound-auth-oauth by wso2-extensions.

the class CibaMgtDAOImpl method persistCibaAuthCode.

@Override
public void persistCibaAuthCode(CibaAuthCodeDO cibaAuthCodeDO) throws CibaCoreException {
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(true)) {
        try (PreparedStatement prepStmt = connection.prepareStatement(SQLQueries.CibaSQLQueries.STORE_CIBA_AUTH_CODE)) {
            prepStmt.setString(1, cibaAuthCodeDO.getCibaAuthCodeKey());
            prepStmt.setString(2, cibaAuthCodeDO.getAuthReqId());
            prepStmt.setString(3, cibaAuthCodeDO.getConsumerKey());
            prepStmt.setTimestamp(4, cibaAuthCodeDO.getIssuedTime(), Calendar.getInstance(TimeZone.getTimeZone(CibaConstants.UTC)));
            prepStmt.setTimestamp(5, cibaAuthCodeDO.getLastPolledTime(), Calendar.getInstance(TimeZone.getTimeZone(CibaConstants.UTC)));
            prepStmt.setLong(6, cibaAuthCodeDO.getInterval());
            prepStmt.setLong(7, cibaAuthCodeDO.getExpiresIn());
            prepStmt.setString(8, cibaAuthCodeDO.getAuthReqStatus().toString());
            prepStmt.execute();
            if (log.isDebugEnabled()) {
                log.debug("Successfully persisted cibaAuthCodeDO for unique CibaAuthCodeKey : " + cibaAuthCodeDO.getCibaAuthCodeKey());
            }
        } catch (SQLException e) {
            IdentityDatabaseUtil.rollbackTransaction(connection);
            throw new CibaCoreException("Error occurred while persisting cibaAuthCode for the application with " + "consumer key: " + cibaAuthCodeDO.getConsumerKey() + " and with authCodeKey: " + cibaAuthCodeDO.getCibaAuthCodeKey(), e);
        }
        try (PreparedStatement prepStmtForScope = connection.prepareStatement(SQLQueries.CibaSQLQueries.STORE_SCOPES)) {
            for (String singleScopeValue : cibaAuthCodeDO.getScopes()) {
                prepStmtForScope.setString(1, cibaAuthCodeDO.getCibaAuthCodeKey());
                prepStmtForScope.setString(2, singleScopeValue);
                prepStmtForScope.addBatch();
            }
            prepStmtForScope.executeBatch();
            if (log.isDebugEnabled()) {
                log.debug("Successfully persisted scopes for unique authCodeKey : " + cibaAuthCodeDO.getCibaAuthCodeKey());
            }
        } catch (SQLException e) {
            IdentityDatabaseUtil.rollbackTransaction(connection);
            throw new CibaCoreException("Error occurred while persisting scopes for the application with " + "consumer key: " + cibaAuthCodeDO.getConsumerKey() + " and with authCodeKey: " + cibaAuthCodeDO.getCibaAuthCodeKey(), e);
        }
        IdentityDatabaseUtil.commitTransaction(connection);
    } catch (SQLException e) {
        throw new CibaCoreException("Error occurred while persisting cibaAuthCode for the application with " + "consumer key: " + cibaAuthCodeDO.getConsumerKey() + " and with authCodeKey: " + cibaAuthCodeDO.getCibaAuthCodeKey(), e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) CibaCoreException(org.wso2.carbon.identity.oauth.ciba.exceptions.CibaCoreException)

Example 3 with CibaCoreException

use of org.wso2.carbon.identity.oauth.ciba.exceptions.CibaCoreException in project identity-inbound-auth-oauth by wso2-extensions.

the class CibaMgtDAOImpl method updateStatus.

@Override
public void updateStatus(String authCodeKey, Enum authenticationStatus) throws CibaCoreException {
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(true)) {
        try (PreparedStatement prepStmt = connection.prepareStatement(SQLQueries.CibaSQLQueries.UPDATE_AUTHENTICATION_STATUS)) {
            prepStmt.setString(1, authenticationStatus.toString());
            prepStmt.setString(2, authCodeKey);
            prepStmt.execute();
            IdentityDatabaseUtil.commitTransaction(connection);
            if (log.isDebugEnabled()) {
                log.debug("Successfully persisted the authentication status: " + authenticationStatus + " identified by authCodeKey: " + authCodeKey);
            }
        } catch (SQLException e) {
            IdentityDatabaseUtil.rollbackTransaction(connection);
            throw new CibaCoreException("Error occurred in persisting authentication status for the authCodeKey: " + authCodeKey, e);
        }
    } catch (SQLException e) {
        throw new CibaCoreException("Error occurred in persisting authentication status for the authCodeKey: " + authCodeKey, e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) CibaCoreException(org.wso2.carbon.identity.oauth.ciba.exceptions.CibaCoreException)

Example 4 with CibaCoreException

use of org.wso2.carbon.identity.oauth.ciba.exceptions.CibaCoreException in project identity-inbound-auth-oauth by wso2-extensions.

the class CibaGrantHandler method issue.

@Override
public OAuth2AccessTokenRespDTO issue(OAuthTokenReqMessageContext tokReqMsgCtx) throws IdentityOAuth2Exception {
    OAuth2AccessTokenRespDTO responseDTO = super.issue(tokReqMsgCtx);
    String authReqId = getAuthReqId(tokReqMsgCtx);
    CibaAuthCodeDO cibaAuthCodeDO = retrieveCibaAuthCode(authReqId);
    try {
        CibaDAOFactory.getInstance().getCibaAuthMgtDAO().updateStatus(cibaAuthCodeDO.getCibaAuthCodeKey(), AuthReqStatus.TOKEN_ISSUED);
        if (log.isDebugEnabled()) {
            log.debug("Successfully updated the status of authentication request made by client:" + tokReqMsgCtx.getOauth2AccessTokenReqDTO().getClientId());
        }
    } catch (CibaCoreException e) {
        throw new IdentityOAuth2Exception("Error occurred in persisting status for the request made with " + "auth_req_id: " + authReqId, e);
    }
    return responseDTO;
}
Also used : OAuth2AccessTokenRespDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenRespDTO) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) CibaAuthCodeDO(org.wso2.carbon.identity.oauth.ciba.model.CibaAuthCodeDO) CibaCoreException(org.wso2.carbon.identity.oauth.ciba.exceptions.CibaCoreException)

Example 5 with CibaCoreException

use of org.wso2.carbon.identity.oauth.ciba.exceptions.CibaCoreException in project identity-inbound-auth-oauth by wso2-extensions.

the class CibaGrantHandler method retrieveCibaAuthCode.

/**
 * Validates whether provided auth_req_id exists in and return AuthCode if exists.
 *
 * @param authReqId Authentication Request Identifier.
 * @throws IdentityOAuth2Exception
 */
private CibaAuthCodeDO retrieveCibaAuthCode(String authReqId) throws IdentityOAuth2Exception {
    try {
        String authCodeKey = CibaDAOFactory.getInstance().getCibaAuthMgtDAO().getCibaAuthCodeKey(authReqId);
        if (StringUtils.isBlank(authCodeKey)) {
            if (log.isDebugEnabled()) {
                log.debug("Provided auth_req_id : " + authReqId + " with the token request is not valid.Or not issued by Identity server.");
            }
            throw new IdentityOAuth2Exception(INVALID_AUTH_REQ_ID);
        }
        CibaAuthCodeDO cibaAuthCodeDO = CibaDAOFactory.getInstance().getCibaAuthMgtDAO().getCibaAuthCode(authCodeKey);
        if (cibaAuthCodeDO.getAuthReqStatus().equals(AuthReqStatus.AUTHENTICATED)) {
            // Retrieve scopes.
            List<String> scope = CibaDAOFactory.getInstance().getCibaAuthMgtDAO().getScopes(cibaAuthCodeDO.getCibaAuthCodeKey());
            cibaAuthCodeDO.setScopes(scope.toArray(new String[scope.size()]));
            // Retrieve authenticated user.
            AuthenticatedUser authenticatedUser = CibaDAOFactory.getInstance().getCibaAuthMgtDAO().getAuthenticatedUser(cibaAuthCodeDO.getCibaAuthCodeKey());
            cibaAuthCodeDO.setAuthenticatedUser(authenticatedUser);
        }
        return cibaAuthCodeDO;
    } catch (CibaCoreException e) {
        throw new IdentityOAuth2Exception(INVALID_AUTH_REQ_ID, e);
    }
}
Also used : IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) CibaAuthCodeDO(org.wso2.carbon.identity.oauth.ciba.model.CibaAuthCodeDO) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) CibaCoreException(org.wso2.carbon.identity.oauth.ciba.exceptions.CibaCoreException)

Aggregations

CibaCoreException (org.wso2.carbon.identity.oauth.ciba.exceptions.CibaCoreException)15 Connection (java.sql.Connection)8 PreparedStatement (java.sql.PreparedStatement)8 SQLException (java.sql.SQLException)8 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)7 CibaAuthCodeDO (org.wso2.carbon.identity.oauth.ciba.model.CibaAuthCodeDO)5 ResultSet (java.sql.ResultSet)3 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)3 OAuthErrorDTO (org.wso2.carbon.identity.oauth.dto.OAuthErrorDTO)2 ArrayList (java.util.ArrayList)1 CibaClientException (org.wso2.carbon.identity.oauth.ciba.exceptions.CibaClientException)1 CibaAuthCodeResponse (org.wso2.carbon.identity.oauth.ciba.model.CibaAuthCodeResponse)1 InvalidOAuthClientException (org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException)1 OAuthAppDO (org.wso2.carbon.identity.oauth.dao.OAuthAppDO)1 OAuth2AccessTokenRespDTO (org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenRespDTO)1 OAuth2AuthorizeReqDTO (org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeReqDTO)1 OAuth2AuthorizeRespDTO (org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeRespDTO)1