Search in sources :

Example 6 with ClientApplication

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

the class TokenEndpointRESTService method authorize.

@Unsecure
@Path("/token")
@POST
public Response authorize(@Context HttpServletResponse res, @Context HttpServletRequest req) throws OAuthSystemException {
    OAuthTokenRequest oauthRequest;
    boolean refreshing = false;
    OAuthIssuer oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator());
    try {
        oauthRequest = new OAuthTokenRequest(req);
        ClientApplication clientApplication = oauthController.findByClientIdAndClientSecret(oauthRequest.getClientId(), oauthRequest.getClientSecret());
        if (clientApplication == null) {
            logger.severe("Invalid client application");
            OAuthResponse response = OAuthASResponse.errorResponse(HttpServletResponse.SC_FORBIDDEN).setError(OAuthError.TokenResponse.INVALID_CLIENT).setErrorDescription("Invalid client").buildJSONMessage();
            return Response.status(response.getResponseStatus()).entity(response.getBody()).build();
        }
        ClientApplicationAuthorizationCode clientApplicationAuthorizationCode = null;
        if (oauthRequest.getParam(OAuth.OAUTH_GRANT_TYPE).equals(GrantType.AUTHORIZATION_CODE.toString())) {
            clientApplicationAuthorizationCode = oauthController.findByClientApplicationAndAuthorizationCode(clientApplication, oauthRequest.getParam(OAuth.OAUTH_CODE));
            if (clientApplicationAuthorizationCode == null) {
                logger.severe(String.format("Client application authorization code not found for token %s", oauthRequest.getParam(OAuth.OAUTH_CODE)));
                OAuthResponse response = OAuthASResponse.errorResponse(HttpServletResponse.SC_FORBIDDEN).setError(OAuthError.TokenResponse.INVALID_GRANT).setErrorDescription("invalid authorization code").buildJSONMessage();
                return Response.status(response.getResponseStatus()).entity(response.getBody()).build();
            }
        } else if (oauthRequest.getParam(OAuth.OAUTH_GRANT_TYPE).equals(GrantType.REFRESH_TOKEN.toString())) {
            refreshing = true;
        } else {
            return Response.status(HttpServletResponse.SC_NOT_IMPLEMENTED).build();
        }
        String accessToken = oauthIssuerImpl.accessToken();
        String refreshToken = oauthIssuerImpl.refreshToken();
        ClientApplicationAccessToken clientApplicationAccessToken;
        Long expires = (System.currentTimeMillis() / 1000L) + TOKEN_LIFETIME;
        if (refreshing) {
            // New access token and expiration time but refresh token remains unchanged
            refreshToken = oauthRequest.getParam(OAuth.OAUTH_REFRESH_TOKEN);
            clientApplicationAccessToken = oauthController.findByRefreshToken(refreshToken);
            if (clientApplicationAccessToken != null) {
                oauthController.refresh(clientApplicationAccessToken, expires, accessToken);
            } else {
                logger.severe(String.format("Invalid refresh token %s", refreshToken));
                OAuthResponse response = OAuthASResponse.errorResponse(HttpServletResponse.SC_FORBIDDEN).setError("Invalid refresh token").buildJSONMessage();
                return Response.status(response.getResponseStatus()).entity(response.getBody()).build();
            }
        } else {
            clientApplicationAccessToken = oauthController.findByClientApplicationAuthorizationCode(clientApplicationAuthorizationCode);
            if (clientApplicationAccessToken == null) {
                oauthController.createAccessToken(accessToken, refreshToken, expires, clientApplication, clientApplicationAuthorizationCode);
            } else {
                oauthController.renewAccessToken(clientApplicationAccessToken, expires, accessToken, refreshToken);
            }
        }
        OAuthResponse response = OAuthASResponse.tokenResponse(HttpServletResponse.SC_OK).setAccessToken(accessToken).setRefreshToken(refreshToken).setExpiresIn(String.valueOf(TOKEN_LIFETIME)).buildJSONMessage();
        return Response.status(response.getResponseStatus()).entity(response.getBody()).build();
    } catch (OAuthProblemException e) {
        logger.log(Level.SEVERE, "Oauth problem", e);
        OAuthResponse response = OAuthASResponse.errorResponse(HttpServletResponse.SC_BAD_REQUEST).error(e).buildJSONMessage();
        return Response.status(response.getResponseStatus()).entity(response.getBody()).build();
    }
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) OAuthIssuerImpl(org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl) ClientApplication(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplication) ClientApplicationAccessToken(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAccessToken) OAuthTokenRequest(org.apache.oltu.oauth2.as.request.OAuthTokenRequest) MD5Generator(org.apache.oltu.oauth2.as.issuer.MD5Generator) OAuthIssuer(org.apache.oltu.oauth2.as.issuer.OAuthIssuer) ClientApplicationAuthorizationCode(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAuthorizationCode) OAuthResponse(org.apache.oltu.oauth2.common.message.OAuthResponse) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Unsecure(fi.otavanopisto.pyramus.rest.annotation.Unsecure)

Example 7 with ClientApplication

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

the class ClientApplicationAuthorizationCodeAPI method create.

public Long create(String authorizationCode, String redirectUrl, Long userId, Long clientApplicationId) throws InvalidScriptException {
    DAOFactory daoFactory = DAOFactory.getInstance();
    User user = daoFactory.getStaffMemberDAO().findById(userId);
    if (user == null) {
        user = daoFactory.getStudentDAO().findById(userId);
    }
    if (user == null) {
        throw new InvalidScriptException("User not found");
    }
    ClientApplication clientApplication = daoFactory.getClientApplicationDAO().findById(clientApplicationId);
    if (clientApplication == null) {
        throw new InvalidScriptException("Client application not found");
    }
    ClientApplicationAuthorizationCode code = DAOFactory.getInstance().getClientApplicationAuthorizationCodeDAO().create(user, clientApplication, authorizationCode, redirectUrl);
    return code.getId();
}
Also used : ClientApplication(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplication) User(fi.otavanopisto.pyramus.domainmodel.users.User) InvalidScriptException(fi.otavanopisto.pyramus.util.dataimport.scripting.InvalidScriptException) DAOFactory(fi.otavanopisto.pyramus.dao.DAOFactory) ClientApplicationAuthorizationCode(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAuthorizationCode)

Example 8 with ClientApplication

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

the class ClientApplicationDAO method findByClientIdAndClientSecret.

public ClientApplication findByClientIdAndClientSecret(String clientId, String clientSecret) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ClientApplication> criteria = criteriaBuilder.createQuery(ClientApplication.class);
    Root<ClientApplication> root = criteria.from(ClientApplication.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(ClientApplication_.clientId), clientId), criteriaBuilder.equal(root.get(ClientApplication_.clientSecret), clientSecret)));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) ClientApplication(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplication)

Example 9 with ClientApplication

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

the class ClientApplicationDAO method findByClientId.

public ClientApplication findByClientId(String clientId) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ClientApplication> criteria = criteriaBuilder.createQuery(ClientApplication.class);
    Root<ClientApplication> root = criteria.from(ClientApplication.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(ClientApplication_.clientId), clientId));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) ClientApplication(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplication)

Example 10 with ClientApplication

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

the class ClientApplicationsViewController method processForm.

@Override
public void processForm(PageRequestContext requestContext) {
    ClientApplicationDAO clientApplicationDAO = DAOFactory.getInstance().getClientApplicationDAO();
    List<ClientApplication> clientApplications = clientApplicationDAO.listAll();
    requestContext.getRequest().setAttribute("clientApplications", clientApplications);
    requestContext.setIncludeJSP("/templates/system/clientapplications.jsp");
}
Also used : ClientApplication(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplication) ClientApplicationDAO(fi.otavanopisto.pyramus.dao.clientapplications.ClientApplicationDAO)

Aggregations

ClientApplication (fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplication)10 ClientApplicationDAO (fi.otavanopisto.pyramus.dao.clientapplications.ClientApplicationDAO)4 ClientApplicationAuthorizationCodeDAO (fi.otavanopisto.pyramus.dao.clientapplications.ClientApplicationAuthorizationCodeDAO)3 ClientApplicationAuthorizationCode (fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAuthorizationCode)3 User (fi.otavanopisto.pyramus.domainmodel.users.User)3 OAuthResponse (org.apache.oltu.oauth2.common.message.OAuthResponse)3 LoginRequiredException (fi.internetix.smvc.LoginRequiredException)2 SmvcRuntimeException (fi.internetix.smvc.SmvcRuntimeException)2 UserDAO (fi.otavanopisto.pyramus.dao.users.UserDAO)2 ClientApplicationAccessToken (fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAccessToken)2 EntityManager (javax.persistence.EntityManager)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpSession (javax.servlet.http.HttpSession)2 Path (javax.ws.rs.Path)2 MD5Generator (org.apache.oltu.oauth2.as.issuer.MD5Generator)2 OAuthIssuerImpl (org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl)2 OAuthASResponse (org.apache.oltu.oauth2.as.response.OAuthASResponse)2 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)2 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)2