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