Search in sources :

Example 1 with ClientApplicationAccessToken

use of fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAccessToken in project pyramus by otavanopisto.

the class ClientApplicationTokenCleaner method removeExpiredTokens.

@Schedule(dayOfWeek = "*", hour = "6", persistent = false)
private void removeExpiredTokens() {
    int removed = 0;
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(new Date());
    calendar.add(Calendar.DATE, -1);
    long threshold = calendar.getTimeInMillis() / 1000;
    List<ClientApplicationAccessToken> tokens = clientApplicationAccessTokenDAO.listByExpired(threshold, BATCH_SIZE);
    if (tokens.size() == BATCH_SIZE) {
        logger.warning("Client application access tokens possibly piling up");
    }
    for (ClientApplicationAccessToken token : tokens) {
        ClientApplicationAuthorizationCode authCode = token.getClientApplicationAuthorizationCode();
        if (authCode.getUser().getRole() == Role.TRUSTED_SYSTEM) {
            continue;
        }
        clientApplicationAccessTokenDAO.delete(token);
        clientApplicationAuthorizationCodeDAO.delete(authCode);
        removed++;
    }
    if (removed > 0) {
        logger.info(String.format("Removed %d expired client application access tokens", removed));
    }
}
Also used : ClientApplicationAccessToken(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAccessToken) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) ClientApplicationAuthorizationCode(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAuthorizationCode) Date(java.util.Date) Schedule(javax.ejb.Schedule)

Example 2 with ClientApplicationAccessToken

use of fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAccessToken in project pyramus by otavanopisto.

the class ClientApplicationsViewController method processSend.

@Override
public void processSend(PageRequestContext requestContext) {
    ClientApplicationDAO clientApplicationDAO = DAOFactory.getInstance().getClientApplicationDAO();
    ClientApplicationAuthorizationCodeDAO clientApplicationAuthorizationCodeDAO = DAOFactory.getInstance().getClientApplicationAuthorizationCodeDAO();
    ClientApplicationAccessTokenDAO clientApplicationAccessTokenDAO = DAOFactory.getInstance().getClientApplicationAccessTokenDAO();
    Long clientApplicationsRowCount = requestContext.getLong("clientApplicationsTable.rowCount");
    for (int i = 0; i < clientApplicationsRowCount; i++) {
        String colPrefix = "clientApplicationsTable." + i;
        Long id = requestContext.getLong(colPrefix + ".id");
        Boolean remove = "1".equals(requestContext.getString(colPrefix + ".remove"));
        Boolean regenerateSecret = "1".equals(requestContext.getString(colPrefix + ".regenerateSecret"));
        Boolean skipPrompt = "1".equals(requestContext.getString(colPrefix + ".skipPrompt"));
        String clientName = requestContext.getString(colPrefix + ".appName");
        String clientId = requestContext.getString(colPrefix + ".appId");
        String clientSecret = requestContext.getString(colPrefix + ".appSecret");
        if (id == null && !remove) {
            clientId = UUID.randomUUID().toString();
            clientSecret = new OauthClientSecretGenerator(80).nextString();
            clientApplicationDAO.create(clientName, clientId, clientSecret, skipPrompt);
        } else if (id != null) {
            ClientApplication clientApplication = clientApplicationDAO.findById(id);
            if (remove) {
                List<ClientApplicationAuthorizationCode> authCodes = clientApplicationAuthorizationCodeDAO.listByClientApplication(clientApplication);
                for (ClientApplicationAuthorizationCode clientApplicationAuthorizationCode : authCodes) {
                    ClientApplicationAccessToken clientApplicationAccessToken = clientApplicationAccessTokenDAO.findByAuthCode(clientApplicationAuthorizationCode);
                    if (clientApplicationAccessToken != null) {
                        clientApplicationAccessTokenDAO.delete(clientApplicationAccessToken);
                    }
                    clientApplicationAuthorizationCodeDAO.delete(clientApplicationAuthorizationCode);
                }
                clientApplicationDAO.delete(clientApplication);
            } else {
                if (regenerateSecret) {
                    clientSecret = new OauthClientSecretGenerator(80).nextString();
                    clientApplicationDAO.updateClientSecret(clientApplication, clientSecret);
                }
                clientApplicationDAO.updateName(clientApplication, clientName);
                clientApplicationDAO.updateSkipPrompt(clientApplication, skipPrompt);
            }
        }
    }
    processForm(requestContext);
}
Also used : ClientApplicationAccessTokenDAO(fi.otavanopisto.pyramus.dao.clientapplications.ClientApplicationAccessTokenDAO) ClientApplicationAuthorizationCodeDAO(fi.otavanopisto.pyramus.dao.clientapplications.ClientApplicationAuthorizationCodeDAO) ClientApplication(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplication) ClientApplicationDAO(fi.otavanopisto.pyramus.dao.clientapplications.ClientApplicationDAO) ClientApplicationAccessToken(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAccessToken) List(java.util.List) ClientApplicationAuthorizationCode(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAuthorizationCode) OauthClientSecretGenerator(fi.otavanopisto.pyramus.util.OauthClientSecretGenerator)

Example 3 with ClientApplicationAccessToken

use of fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAccessToken in project pyramus by otavanopisto.

the class ClientApplicationController method getClientApplication.

public ClientApplication getClientApplication() {
    try {
        OAuthAccessResourceRequest oauthRequest = new OAuthAccessResourceRequest(httpRequest, ParameterStyle.HEADER);
        String accessToken = oauthRequest.getAccessToken();
        ClientApplicationAccessToken clientApplicationAccessToken = oauthController.findByAccessToken(accessToken);
        return clientApplicationAccessToken.getClientApplication();
    } catch (Exception ex) {
        return null;
    }
}
Also used : OAuthAccessResourceRequest(org.apache.oltu.oauth2.rs.request.OAuthAccessResourceRequest) ClientApplicationAccessToken(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAccessToken)

Example 4 with ClientApplicationAccessToken

use of fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAccessToken in project pyramus by otavanopisto.

the class SecurityFilter method hasOAuthApiAccess.

private boolean hasOAuthApiAccess() {
    try {
        OAuthAccessResourceRequest oauthRequest = new OAuthAccessResourceRequest(request, ParameterStyle.HEADER);
        String accessToken = oauthRequest.getAccessToken();
        ClientApplicationAccessToken clientApplicationAccessToken = oauthController.findByAccessToken(accessToken);
        if (clientApplicationAccessToken == null) {
            return false;
        } else {
            Long currentTime = System.currentTimeMillis() / 1000L;
            if (currentTime > clientApplicationAccessToken.getExpires()) {
                return false;
            } else {
                return true;
            }
        }
    } catch (OAuthProblemException e) {
        return false;
    } catch (OAuthSystemException e) {
        return false;
    }
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) OAuthAccessResourceRequest(org.apache.oltu.oauth2.rs.request.OAuthAccessResourceRequest) ClientApplicationAccessToken(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAccessToken) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException)

Example 5 with ClientApplicationAccessToken

use of fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAccessToken in project pyramus by otavanopisto.

the class ClientApplicationAccessTokenDAO method create.

public ClientApplicationAccessToken create(String accessToken, String refreshToken, Long expires, ClientApplication clientApplication, ClientApplicationAuthorizationCode clientApplicationAuthorizationCode) {
    EntityManager entityManager = getEntityManager();
    ClientApplicationAccessToken clientApplicationAccessToken = new ClientApplicationAccessToken();
    clientApplicationAccessToken.setAccessToken(accessToken);
    clientApplicationAccessToken.setRefreshToken(refreshToken);
    clientApplicationAccessToken.setClientApplication(clientApplication);
    clientApplicationAccessToken.setExpires(expires);
    clientApplicationAccessToken.setClientApplicationAuthorizationCode(clientApplicationAuthorizationCode);
    entityManager.persist(clientApplicationAccessToken);
    return clientApplicationAccessToken;
}
Also used : EntityManager(javax.persistence.EntityManager) ClientApplicationAccessToken(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAccessToken)

Aggregations

ClientApplicationAccessToken (fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAccessToken)11 EntityManager (javax.persistence.EntityManager)5 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 ClientApplicationAuthorizationCode (fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAuthorizationCode)3 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)3 OAuthAccessResourceRequest (org.apache.oltu.oauth2.rs.request.OAuthAccessResourceRequest)3 ClientApplication (fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplication)2 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)2 ClientApplicationAccessTokenDAO (fi.otavanopisto.pyramus.dao.clientapplications.ClientApplicationAccessTokenDAO)1 ClientApplicationAuthorizationCodeDAO (fi.otavanopisto.pyramus.dao.clientapplications.ClientApplicationAuthorizationCodeDAO)1 ClientApplicationDAO (fi.otavanopisto.pyramus.dao.clientapplications.ClientApplicationDAO)1 Unsecure (fi.otavanopisto.pyramus.rest.annotation.Unsecure)1 OauthClientSecretGenerator (fi.otavanopisto.pyramus.util.OauthClientSecretGenerator)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 GregorianCalendar (java.util.GregorianCalendar)1 List (java.util.List)1 Schedule (javax.ejb.Schedule)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1