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