Search in sources :

Example 46 with OAuthSystemException

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);
    }
}
Also used : Response(io.restassured.response.Response) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) Before(org.junit.Before)

Example 47 with OAuthSystemException

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 "";
}
Also used : Response(io.restassured.response.Response) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Example 48 with OAuthSystemException

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");
        }
    }
}
Also used : LoginRequiredException(fi.internetix.smvc.LoginRequiredException) User(fi.otavanopisto.pyramus.domainmodel.users.User) HttpSession(javax.servlet.http.HttpSession) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) SmvcRuntimeException(fi.internetix.smvc.SmvcRuntimeException) OAuthResponse(org.apache.oltu.oauth2.common.message.OAuthResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) ClientApplicationAuthorizationCodeDAO(fi.otavanopisto.pyramus.dao.clientapplications.ClientApplicationAuthorizationCodeDAO) ClientApplication(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplication) ClientApplicationDAO(fi.otavanopisto.pyramus.dao.clientapplications.ClientApplicationDAO) UserDAO(fi.otavanopisto.pyramus.dao.users.UserDAO) OAuthASResponse(org.apache.oltu.oauth2.as.response.OAuthASResponse)

Example 49 with OAuthSystemException

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");
}
Also used : LoginRequiredException(fi.internetix.smvc.LoginRequiredException) User(fi.otavanopisto.pyramus.domainmodel.users.User) HttpSession(javax.servlet.http.HttpSession) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) SmvcRuntimeException(fi.internetix.smvc.SmvcRuntimeException) OAuthResponse(org.apache.oltu.oauth2.common.message.OAuthResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) OAuthIssuerImpl(org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl) ClientApplication(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplication) ClientApplicationAuthorizationCodeDAO(fi.otavanopisto.pyramus.dao.clientapplications.ClientApplicationAuthorizationCodeDAO) ClientApplicationDAO(fi.otavanopisto.pyramus.dao.clientapplications.ClientApplicationDAO) UserDAO(fi.otavanopisto.pyramus.dao.users.UserDAO) OAuthAuthzRequest(org.apache.oltu.oauth2.as.request.OAuthAuthzRequest) MD5Generator(org.apache.oltu.oauth2.as.issuer.MD5Generator) OAuthASResponse(org.apache.oltu.oauth2.as.response.OAuthASResponse)

Example 50 with OAuthSystemException

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);
    }
}
Also used : OAuthClientResponse(org.apache.oltu.oauth2.client.response.OAuthClientResponse) Response(okhttp3.Response) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) Request(okhttp3.Request) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) MediaType(okhttp3.MediaType) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Aggregations

OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)100 OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)55 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)49 OAuthResponse (org.apache.oltu.oauth2.common.message.OAuthResponse)49 IOException (java.io.IOException)40 Request (okhttp3.Request)29 Response (okhttp3.Response)29 OAuthJSONAccessTokenResponse (org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse)22 Builder (okhttp3.Request.Builder)19 URI (java.net.URI)17 OAuthBearerClientRequest (org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest)17 Map (java.util.Map)16 OAuthClientResponse (org.apache.oltu.oauth2.client.response.OAuthClientResponse)15 MediaType (okhttp3.MediaType)14 RequestBody (okhttp3.RequestBody)14 TokenRequestBuilder (org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder)14 MD5Generator (org.apache.oltu.oauth2.as.issuer.MD5Generator)12 OAuthClient (org.apache.oltu.oauth2.client.OAuthClient)12 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)12 Path (javax.ws.rs.Path)11