Search in sources :

Example 1 with CibaAuthCodeDO

use of org.wso2.carbon.identity.oauth.ciba.model.CibaAuthCodeDO 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 2 with CibaAuthCodeDO

use of org.wso2.carbon.identity.oauth.ciba.model.CibaAuthCodeDO 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 3 with CibaAuthCodeDO

use of org.wso2.carbon.identity.oauth.ciba.model.CibaAuthCodeDO 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)

Example 4 with CibaAuthCodeDO

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

the class CibaGrantHandlerTest method testValidateCorrectPollingFrequency.

@Test
public void testValidateCorrectPollingFrequency() throws Exception {
    CibaAuthCodeDO cibaAuthCodeDO = new CibaAuthCodeDO();
    long lastPolledTimeInMillis = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTimeInMillis();
    cibaAuthCodeDO.setInterval(2);
    Timestamp polledTimeforSucess = new Timestamp(lastPolledTimeInMillis - 10000);
    cibaAuthCodeDO.setLastPolledTime(polledTimeforSucess);
    when(CibaDAOFactory.getInstance().getCibaAuthMgtDAO()).thenReturn(cibaMgtDAO);
    Assert.assertNull(WhiteboxImpl.invokeMethod(cibaGrantHandler, "validatePollingFrequency", cibaAuthCodeDO));
}
Also used : CibaAuthCodeDO(org.wso2.carbon.identity.oauth.ciba.model.CibaAuthCodeDO) Timestamp(java.sql.Timestamp) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with CibaAuthCodeDO

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

the class CibaGrantHandlerTest method testIsAuthorized.

@Test
public void testIsAuthorized() throws Exception {
    CibaAuthCodeDO cibaAuthCodeDoDenied = new CibaAuthCodeDO();
    cibaAuthCodeDoDenied.setAuthReqStatus(AuthReqStatus.CONSENT_DENIED);
    Assert.assertFalse(WhiteboxImpl.invokeMethod(cibaGrantHandler, "isAuthorized", cibaAuthCodeDoDenied));
    CibaAuthCodeDO cibaAuthCodeDoAuth = new CibaAuthCodeDO();
    cibaAuthCodeDoAuth.setAuthReqStatus(AuthReqStatus.AUTHENTICATED);
    Assert.assertTrue(WhiteboxImpl.invokeMethod(cibaGrantHandler, "isAuthorized", cibaAuthCodeDoAuth));
}
Also used : CibaAuthCodeDO(org.wso2.carbon.identity.oauth.ciba.model.CibaAuthCodeDO) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

CibaAuthCodeDO (org.wso2.carbon.identity.oauth.ciba.model.CibaAuthCodeDO)13 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 Test (org.testng.annotations.Test)7 CibaCoreException (org.wso2.carbon.identity.oauth.ciba.exceptions.CibaCoreException)6 Timestamp (java.sql.Timestamp)4 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)4 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 ResultSet (java.sql.ResultSet)1 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)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