use of org.apache.oltu.oauth2.common.exception.OAuthSystemException in project pyramus by otavanopisto.
the class AbstractRESTPermissionsTest method createAccessTokens.
@Before
public void createAccessTokens() {
OAuthClientRequest tokenRequest = null;
if (!Role.EVERYONE.name().equals(role)) {
try {
tokenRequest = OAuthClientRequest.tokenLocation("https://dev.pyramus.fi:8443/1/oauth/token").setGrantType(GrantType.AUTHORIZATION_CODE).setClientId(fi.otavanopisto.pyramus.Common.CLIENT_ID).setClientSecret(fi.otavanopisto.pyramus.Common.CLIENT_SECRET).setRedirectURI(fi.otavanopisto.pyramus.Common.REDIRECT_URL).setCode(fi.otavanopisto.pyramus.Common.getRoleAuth(Common.strToRole(role))).buildBodyMessage();
} catch (OAuthSystemException e) {
e.printStackTrace();
}
Response response = given().contentType("application/x-www-form-urlencoded").body(tokenRequest.getBody()).post("/oauth/token");
String accessToken = response.body().jsonPath().getString("access_token");
setAccessToken(accessToken);
} else {
setAccessToken("");
}
/**
* AdminAccessToken
*/
if (!Role.ADMINISTRATOR.name().equals(role)) {
tokenRequest = null;
try {
tokenRequest = OAuthClientRequest.tokenLocation("https://dev.pyramus.fi:8443/1/oauth/token").setGrantType(GrantType.AUTHORIZATION_CODE).setClientId(fi.otavanopisto.pyramus.Common.CLIENT_ID).setClientSecret(fi.otavanopisto.pyramus.Common.CLIENT_SECRET).setRedirectURI(fi.otavanopisto.pyramus.Common.REDIRECT_URL).setCode(fi.otavanopisto.pyramus.Common.getRoleAuth(Role.ADMINISTRATOR)).buildBodyMessage();
} catch (OAuthSystemException e) {
e.printStackTrace();
}
Response response = given().contentType("application/x-www-form-urlencoded").body(tokenRequest.getBody()).post("/oauth/token");
String adminAccessToken = response.body().jsonPath().getString("access_token");
setAdminAccessToken(adminAccessToken);
} else {
setAdminAccessToken(accessToken);
}
}
use of org.apache.oltu.oauth2.common.exception.OAuthSystemException in project pyramus by otavanopisto.
the class AbstractRESTPermissionsTestJUnit5 method getOauthToken.
protected String getOauthToken(Role role) {
if (!Role.EVERYONE.equals(role)) {
OAuthClientRequest tokenRequest = null;
try {
tokenRequest = OAuthClientRequest.tokenLocation("https://dev.pyramus.fi:8443/1/oauth/token").setGrantType(GrantType.AUTHORIZATION_CODE).setClientId(fi.otavanopisto.pyramus.Common.CLIENT_ID).setClientSecret(fi.otavanopisto.pyramus.Common.CLIENT_SECRET).setRedirectURI(fi.otavanopisto.pyramus.Common.REDIRECT_URL).setCode(fi.otavanopisto.pyramus.Common.getRoleAuth(role)).buildBodyMessage();
} catch (OAuthSystemException e) {
e.printStackTrace();
}
Response response = given().contentType("application/x-www-form-urlencoded").body(tokenRequest.getBody()).post("/oauth/token");
return response.body().jsonPath().getString("access_token");
}
return "";
}
use of org.apache.oltu.oauth2.common.exception.OAuthSystemException in project pyramus by otavanopisto.
the class AuthorizeClientApplicationViewController method processSend.
@Override
public void processSend(PageRequestContext requestContext) {
if (!requestContext.isLoggedIn()) {
HttpServletRequest request = requestContext.getRequest();
StringBuilder currentUrl = new StringBuilder(request.getRequestURL());
String queryString = request.getQueryString();
if (!StringUtils.isBlank(queryString)) {
currentUrl.append('?');
currentUrl.append(queryString);
}
throw new LoginRequiredException(currentUrl.toString());
}
UserDAO userDAO = DAOFactory.getInstance().getUserDAO();
ClientApplicationDAO clientApplicationDAO = DAOFactory.getInstance().getClientApplicationDAO();
ClientApplicationAuthorizationCodeDAO clientApplicationAuthorizationCodeDAO = DAOFactory.getInstance().getClientApplicationAuthorizationCodeDAO();
HttpServletRequest request = requestContext.getRequest();
HttpSession session = request.getSession();
Boolean authorized = "Authorize".equals(request.getParameter("authorize"));
if (authorized) {
Long userId = (Long) session.getAttribute("loggedUserId");
String authorizationCode = (String) session.getAttribute("pendingAuthCode");
String redirectURI = (String) session.getAttribute("pendingOauthRedirectUrl");
ClientApplication clientApplication = clientApplicationDAO.findByClientId((String) session.getAttribute("clientAppId"));
if (userId != null && authorizationCode != null && redirectURI != null && clientApplication != null) {
try {
OAuthASResponse.OAuthAuthorizationResponseBuilder builder = OAuthASResponse.authorizationResponse(request, HttpServletResponse.SC_FOUND);
builder.setCode(authorizationCode);
final OAuthResponse response = builder.location(redirectURI).buildQueryMessage();
User user = userDAO.findById(userId);
clientApplicationAuthorizationCodeDAO.create(user, clientApplication, authorizationCode, redirectURI);
requestContext.setRedirectURL(response.getLocationUri());
} catch (OAuthSystemException e) {
requestContext.setIncludeJSP("/templates/generic/errorpage.jsp");
throw new SmvcRuntimeException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
}
} else {
requestContext.setIncludeJSP("/templates/generic/errorpage.jsp");
throw new SmvcRuntimeException(HttpServletResponse.SC_BAD_REQUEST, "Invalid parameters");
}
}
}
use of org.apache.oltu.oauth2.common.exception.OAuthSystemException in project pyramus by otavanopisto.
the class AuthorizeClientApplicationViewController method processForm.
@Override
public void processForm(PageRequestContext requestContext) {
ClientApplicationDAO clientApplicationDAO = DAOFactory.getInstance().getClientApplicationDAO();
if (!requestContext.isLoggedIn()) {
HttpServletRequest request = requestContext.getRequest();
StringBuilder currentUrl = new StringBuilder(request.getRequestURL());
String queryString = request.getQueryString();
if (!StringUtils.isBlank(queryString)) {
currentUrl.append('?');
currentUrl.append(queryString);
}
String clientId = requestContext.getString("client_id");
if (StringUtils.isNotBlank(clientId)) {
ClientApplication clientApplication = clientApplicationDAO.findByClientId(clientId);
if (clientApplication == null) {
throw new SmvcRuntimeException(HttpServletResponse.SC_FORBIDDEN, "Client application not found");
}
throw new LoginRequiredException(currentUrl.toString(), "OAUTHCLIENT", clientId);
} else {
throw new SmvcRuntimeException(HttpServletResponse.SC_FORBIDDEN, "Client application not defined");
}
}
HttpServletRequest request = requestContext.getRequest();
OAuthAuthzRequest oauthRequest;
OAuthIssuerImpl oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator());
try {
oauthRequest = new OAuthAuthzRequest(request);
ClientApplication clientApplication = clientApplicationDAO.findByClientId(oauthRequest.getClientId());
if (clientApplication != null) {
request.getSession().setAttribute("clientAppId", oauthRequest.getClientId());
String responseType = oauthRequest.getParam(OAuth.OAUTH_RESPONSE_TYPE);
if (!responseType.equals(ResponseType.CODE.toString())) {
requestContext.setIncludeJSP("/templates/generic/errorpage.jsp");
throw new SmvcRuntimeException(HttpServletResponse.SC_NOT_IMPLEMENTED, String.format("Response type: %s not supported", responseType));
}
String authorizationCode = oauthIssuerImpl.authorizationCode();
request.getSession().setAttribute("pendingAuthCode", authorizationCode);
String redirectURI = oauthRequest.getParam(OAuth.OAUTH_REDIRECT_URI);
request.getSession().setAttribute("pendingOauthRedirectUrl", redirectURI);
request.setAttribute("clientAppName", clientApplication.getClientName());
if (clientApplication.getSkipPrompt()) {
ClientApplicationAuthorizationCodeDAO clientApplicationAuthorizationCodeDAO = DAOFactory.getInstance().getClientApplicationAuthorizationCodeDAO();
UserDAO userDAO = DAOFactory.getInstance().getUserDAO();
HttpSession session = request.getSession();
Long userId = (Long) session.getAttribute("loggedUserId");
if (userId != null && authorizationCode != null && redirectURI != null && clientApplication != null) {
try {
OAuthASResponse.OAuthAuthorizationResponseBuilder builder = OAuthASResponse.authorizationResponse(request, HttpServletResponse.SC_FOUND);
builder.setCode(authorizationCode);
final OAuthResponse response = builder.location(redirectURI).buildQueryMessage();
User user = userDAO.findById(userId);
clientApplicationAuthorizationCodeDAO.create(user, clientApplication, authorizationCode, redirectURI);
requestContext.setRedirectURL(response.getLocationUri());
} catch (OAuthSystemException e) {
requestContext.setIncludeJSP("/templates/generic/errorpage.jsp");
throw new SmvcRuntimeException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
}
} else {
requestContext.setIncludeJSP("/templates/generic/errorpage.jsp");
throw new SmvcRuntimeException(HttpServletResponse.SC_BAD_REQUEST, "Invalid parameters");
}
}
} else {
requestContext.setIncludeJSP("/templates/generic/errorpage.jsp");
throw new SmvcRuntimeException(HttpServletResponse.SC_FORBIDDEN, "Client application not found");
}
} catch (OAuthProblemException | OAuthSystemException e) {
throw new SmvcRuntimeException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
}
// TODO: show auth page only if everything is ok
requestContext.setIncludeJSP("/templates/users/authorizeclientapp.jsp");
}
use of org.apache.oltu.oauth2.common.exception.OAuthSystemException in project vcita-client-java-sdk by SimonIT.
the class OAuthOkHttpClient method execute.
@Override
public <T extends OAuthClientResponse> T execute(OAuthClientRequest request, Map<String, String> headers, String requestMethod, Class<T> responseClass) throws OAuthSystemException, OAuthProblemException {
MediaType mediaType = MediaType.parse("application/json");
Request.Builder requestBuilder = new Request.Builder().url(request.getLocationUri());
if (headers != null) {
for (Entry<String, String> entry : headers.entrySet()) {
if (entry.getKey().equalsIgnoreCase("Content-Type")) {
mediaType = MediaType.parse(entry.getValue());
} else {
requestBuilder.addHeader(entry.getKey(), entry.getValue());
}
}
}
RequestBody body = request.getBody() != null ? RequestBody.create(request.getBody(), mediaType) : null;
requestBuilder.method(requestMethod, body);
try {
Response response = client.newCall(requestBuilder.build()).execute();
return OAuthClientResponseFactory.createCustomResponse(response.body().string(), response.body().contentType().toString(), response.code(), responseClass);
} catch (IOException e) {
throw new OAuthSystemException(e);
}
}
Aggregations