Search in sources :

Example 1 with OAuthAuthorizationCode

use of org.bimserver.models.store.OAuthAuthorizationCode in project BIMserver by opensourceBIM.

the class OAuthAccessTokenServlet method service.

@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    OAuthTokenRequest oauthRequest = null;
    OAuthIssuer oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator());
    if (!request.getContentType().equals("application/x-www-form-urlencoded")) {
        response.setStatus(405);
        PrintWriter pw = response.getWriter();
        pw.print("ContentType must be application/x-www-form-urlencoded");
        pw.flush();
        pw.close();
        return;
    }
    try {
        oauthRequest = new OAuthTokenRequest(request);
        OAuthAuthorizationCode code = null;
        try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
            String codeAsString = oauthRequest.getCode();
            code = session.querySingle(StorePackage.eINSTANCE.getOAuthAuthorizationCode_Code(), codeAsString);
            validateClient(oauthRequest);
            String resourceUrl = "";
            Authorization auth = code.getAuthorization();
            org.bimserver.webservices.authorization.Authorization authorization = null;
            if (auth instanceof SingleProjectAuthorization) {
                SingleProjectAuthorization singleProjectAuthorization = (SingleProjectAuthorization) auth;
                authorization = new org.bimserver.webservices.authorization.SingleProjectAuthorization(getBimServer(), code.getUser().getOid(), singleProjectAuthorization.getProject().getOid());
            } else if (auth instanceof RunServiceAuthorization) {
                RunServiceAuthorization runServiceAuthorization = (RunServiceAuthorization) auth;
                authorization = new org.bimserver.webservices.authorization.RunServiceAuthorization(getBimServer(), code.getUser().getOid(), runServiceAuthorization.getService().getOid());
                resourceUrl = getBimServer().getServerSettingsCache().getServerSettings().getSiteAddress() + "/services/" + runServiceAuthorization.getService().getOid();
            } else {
                throw new Exception("Unknown auth");
            }
            String accessToken = authorization.asHexToken(getBimServer().getEncryptionKey());
            String refreshToken = oauthIssuerImpl.refreshToken();
            OAuthTokenResponseBuilder builder = OAuthASResponse.tokenResponse(HttpServletResponse.SC_OK).setAccessToken(accessToken).setExpiresIn("3600").setRefreshToken(refreshToken);
            builder.setParam("resource_url", resourceUrl);
            if (auth instanceof SingleProjectAuthorization) {
                builder.setParam("poid", "" + ((SingleProjectAuthorization) code.getAuthorization()).getProject().getOid());
            } else if (auth instanceof RunServiceAuthorization) {
                builder.setParam("soid", "" + ((RunServiceAuthorization) code.getAuthorization()).getService().getOid());
            }
            OAuthResponse r = builder.buildJSONMessage();
            response.setStatus(r.getResponseStatus());
            response.setContentType("application/json");
            PrintWriter pw = response.getWriter();
            pw.print(r.getBody());
            pw.flush();
            pw.close();
        } catch (BimserverDatabaseException e) {
            LOGGER.error("", e);
        }
    } catch (OAuthProblemException ex) {
        LOGGER.error("", ex);
        try {
            OAuthResponse r = OAuthResponse.errorResponse(401).error(ex).buildJSONMessage();
            response.setStatus(r.getResponseStatus());
            PrintWriter pw = response.getWriter();
            pw.print(r.getBody());
            pw.flush();
            pw.close();
        } catch (OAuthSystemException e) {
            LOGGER.error("", ex);
        }
    } catch (Exception e) {
        LOGGER.error("", e);
    }
}
Also used : DatabaseSession(org.bimserver.database.DatabaseSession) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) OAuthTokenRequest(org.apache.oltu.oauth2.as.request.OAuthTokenRequest) SingleProjectAuthorization(org.bimserver.models.store.SingleProjectAuthorization) RunServiceAuthorization(org.bimserver.models.store.RunServiceAuthorization) OAuthResponse(org.apache.oltu.oauth2.common.message.OAuthResponse) OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) Authorization(org.bimserver.models.store.Authorization) RunServiceAuthorization(org.bimserver.models.store.RunServiceAuthorization) SingleProjectAuthorization(org.bimserver.models.store.SingleProjectAuthorization) OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) OAuthIssuerImpl(org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) OAuthTokenResponseBuilder(org.apache.oltu.oauth2.as.response.OAuthASResponse.OAuthTokenResponseBuilder) MD5Generator(org.apache.oltu.oauth2.as.issuer.MD5Generator) OAuthIssuer(org.apache.oltu.oauth2.as.issuer.OAuthIssuer) OAuthAuthorizationCode(org.bimserver.models.store.OAuthAuthorizationCode) PrintWriter(java.io.PrintWriter)

Example 2 with OAuthAuthorizationCode

use of org.bimserver.models.store.OAuthAuthorizationCode in project BIMserver by opensourceBIM.

the class OAuthAuthorizationServlet method service.

@Override
public void service(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException {
    OAuthAuthzRequest oauthRequest = null;
    String authType = request.getParameter("auth_type");
    if (request.getParameter("token") == null) {
        String location = "/apps/bimviews/?page=OAuth&auth_type=" + authType + "&client_id=" + request.getParameter("client_id") + "&response_type=" + request.getParameter("response_type") + "&redirect_uri=" + request.getParameter("redirect_uri");
        if (request.getParameter("state") != null) {
            String state = request.getParameter("state");
            LOGGER.info("Incoming state: " + state);
            String encodedState = UrlEscapers.urlFragmentEscaper().escape(state);
            LOGGER.info("Encoded state: " + encodedState);
            location += "&state=" + encodedState;
        }
        LOGGER.info("Redirecting to " + location);
        httpServletResponse.sendRedirect(location);
        return;
    }
    OAuthAuthorizationCode oauthCode = null;
    String token = request.getParameter("token");
    try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
        OAuthServer oAuthServer = session.querySingle(StorePackage.eINSTANCE.getOAuthServer_ClientId(), request.getParameter("client_id"));
        org.bimserver.webservices.authorization.Authorization realAuth = org.bimserver.webservices.authorization.Authorization.fromToken(getBimServer().getEncryptionKey(), token);
        long uoid = realAuth.getUoid();
        User user = session.get(uoid, OldQuery.getDefault());
        for (OAuthAuthorizationCode oAuthAuthorizationCode : user.getOAuthIssuedAuthorizationCodes()) {
            if (oAuthAuthorizationCode.getOauthServer() == oAuthServer) {
                if (oAuthAuthorizationCode.getAuthorization() != null) {
                    oauthCode = oAuthAuthorizationCode;
                }
            }
        }
        try {
            if (oauthCode == null) {
                throw new ServletException("No auth found for token " + token);
            }
            oauthRequest = new OAuthAuthzRequest(request);
            String responseType = oauthRequest.getParam(OAuth.OAUTH_RESPONSE_TYPE);
            OAuthASResponse.OAuthAuthorizationResponseBuilder builder = OAuthASResponse.authorizationResponse(request, HttpServletResponse.SC_FOUND);
            if (responseType.equals(ResponseType.CODE.toString())) {
                builder.setCode(oauthCode.getCode());
            // } else if (responseType.equals(ResponseType.TOKEN))) {
            // builder.setAccessToken(oauthCode.get)
            }
            // if (responseType.equals(ResponseType.TOKEN.toString())) {
            // builder.setAccessToken(oauthIssuerImpl.accessToken());
            // // builder.setTokenType(OAuth.DEFAULT_TOKEN_TYPE.toString());
            // builder.setExpiresIn(3600l);
            // }
            String redirectURI = oauthRequest.getParam(OAuth.OAUTH_REDIRECT_URI);
            if (redirectURI != null && !redirectURI.equals("")) {
                URI uri = makeUrl(redirectURI, oauthCode, builder);
                LOGGER.info("Redirecting to " + uri);
                httpServletResponse.sendRedirect(uri.toString());
            } else {
                URI uri = makeUrl("http://fakeaddress", oauthCode, builder);
                httpServletResponse.getWriter().println("No redirectURI provided");
                httpServletResponse.getWriter().println("Would have redirected to: " + uri);
            }
        } catch (OAuthProblemException e) {
            final Response.ResponseBuilder responseBuilder = Response.status(HttpServletResponse.SC_FOUND);
            String redirectUri = e.getRedirectUri();
            if (OAuthUtils.isEmpty(redirectUri)) {
                throw new WebApplicationException(responseBuilder.entity("OAuth callback url needs to be provided by client!!!").build());
            }
            try {
                OAuthResponse response = OAuthASResponse.errorResponse(HttpServletResponse.SC_FOUND).error(e).location(redirectUri).buildQueryMessage();
                // final URI location = new URI(response.getLocationUri());
                httpServletResponse.sendRedirect(response.getLocationUri());
            } catch (OAuthSystemException e1) {
                e1.printStackTrace();
            }
        }
    } catch (OAuthSystemException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (BimserverLockConflictException e2) {
        e2.printStackTrace();
    } catch (BimserverDatabaseException e2) {
        e2.printStackTrace();
    } catch (AuthenticationException e2) {
        e2.printStackTrace();
    }
}
Also used : User(org.bimserver.models.store.User) WebApplicationException(javax.ws.rs.WebApplicationException) DatabaseSession(org.bimserver.database.DatabaseSession) AuthenticationException(org.bimserver.webservices.authorization.AuthenticationException) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) URISyntaxException(java.net.URISyntaxException) OAuthServer(org.bimserver.models.store.OAuthServer) OAuthAuthorizationResponseBuilder(org.apache.oltu.oauth2.as.response.OAuthASResponse.OAuthAuthorizationResponseBuilder) URI(java.net.URI) OAuthResponse(org.apache.oltu.oauth2.common.message.OAuthResponse) ServletException(javax.servlet.ServletException) OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) OAuthAuthzRequest(org.apache.oltu.oauth2.as.request.OAuthAuthzRequest) OAuthASResponse(org.apache.oltu.oauth2.as.response.OAuthASResponse) OAuthAuthorizationResponseBuilder(org.apache.oltu.oauth2.as.response.OAuthASResponse.OAuthAuthorizationResponseBuilder) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException) OAuthAuthorizationCode(org.bimserver.models.store.OAuthAuthorizationCode)

Example 3 with OAuthAuthorizationCode

use of org.bimserver.models.store.OAuthAuthorizationCode in project BIMserver by opensourceBIM.

the class OAuthServiceImpl method authorize.

@Override
public String authorize(Long oAuthServerOid, SAuthorization authorization) throws ServerException, UserException {
    try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
        if (authorization instanceof SSingleProjectAuthorization) {
            User user = session.get(getCurrentUser().getOid(), OldQuery.getDefault());
            SSingleProjectAuthorization sSingleProjectAuthorization = (SSingleProjectAuthorization) authorization;
            SingleProjectAuthorization singleProjectAuthorization = session.create(SingleProjectAuthorization.class);
            Project project = session.get(sSingleProjectAuthorization.getProjectId(), OldQuery.getDefault());
            if (project == null) {
                throw new UserException("No project found with poid " + sSingleProjectAuthorization.getProjectId());
            }
            singleProjectAuthorization.setProject(project);
            OAuthAuthorizationCode code = session.create(OAuthAuthorizationCode.class);
            org.bimserver.webservices.authorization.Authorization auth = new org.bimserver.webservices.authorization.SingleProjectAuthorization(getBimServer(), user.getOid(), project.getOid());
            String asHexToken = auth.asHexToken(getBimServer().getEncryptionKey());
            code.setCode(asHexToken);
            code.setOauthServer(session.get(oAuthServerOid, OldQuery.getDefault()));
            code.setAuthorization(singleProjectAuthorization);
            code.setUser(user);
            user.getOAuthIssuedAuthorizationCodes().add(code);
            session.store(user);
            session.store(singleProjectAuthorization);
            session.commit();
            return code.getCode();
        } else if (authorization instanceof SRunServiceAuthorization) {
            SRunServiceAuthorization serviceAuthorization = (SRunServiceAuthorization) authorization;
            User user = session.get(getCurrentUser().getOid(), OldQuery.getDefault());
            RunServiceAuthorization runServiceAuth = session.create(RunServiceAuthorization.class);
            InternalServicePluginConfiguration conf = session.get(serviceAuthorization.getServiceId(), OldQuery.getDefault());
            if (conf == null) {
                throw new UserException("No service found with soid " + serviceAuthorization.getServiceId());
            }
            runServiceAuth.setService(conf);
            OAuthAuthorizationCode code = session.create(OAuthAuthorizationCode.class);
            org.bimserver.webservices.authorization.Authorization auth = new org.bimserver.webservices.authorization.RunServiceAuthorization(getBimServer(), user.getOid(), conf.getOid());
            String asHexToken = auth.asHexToken(getBimServer().getEncryptionKey());
            code.setCode(asHexToken);
            code.setOauthServer(session.get(oAuthServerOid, OldQuery.getDefault()));
            code.setAuthorization(runServiceAuth);
            code.setUser(user);
            user.getOAuthIssuedAuthorizationCodes().add(code);
            session.store(user);
            session.store(code);
            session.store(runServiceAuth);
            session.commit();
            return code.getCode();
        } else {
            throw new UserException("Unimplemented type of authorization " + authorization.getClass().getSimpleName());
        }
    } catch (Exception e) {
        return handleException(e);
    }
}
Also used : User(org.bimserver.models.store.User) DatabaseSession(org.bimserver.database.DatabaseSession) SSingleProjectAuthorization(org.bimserver.interfaces.objects.SSingleProjectAuthorization) SingleProjectAuthorization(org.bimserver.models.store.SingleProjectAuthorization) SRunServiceAuthorization(org.bimserver.interfaces.objects.SRunServiceAuthorization) RunServiceAuthorization(org.bimserver.models.store.RunServiceAuthorization) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) SAuthorization(org.bimserver.interfaces.objects.SAuthorization) Authorization(org.bimserver.models.store.Authorization) SRunServiceAuthorization(org.bimserver.interfaces.objects.SRunServiceAuthorization) RunServiceAuthorization(org.bimserver.models.store.RunServiceAuthorization) SSingleProjectAuthorization(org.bimserver.interfaces.objects.SSingleProjectAuthorization) SingleProjectAuthorization(org.bimserver.models.store.SingleProjectAuthorization) SSingleProjectAuthorization(org.bimserver.interfaces.objects.SSingleProjectAuthorization) Project(org.bimserver.models.store.Project) InternalServicePluginConfiguration(org.bimserver.models.store.InternalServicePluginConfiguration) UserException(org.bimserver.shared.exceptions.UserException) SRunServiceAuthorization(org.bimserver.interfaces.objects.SRunServiceAuthorization) OAuthAuthorizationCode(org.bimserver.models.store.OAuthAuthorizationCode) SOAuthAuthorizationCode(org.bimserver.interfaces.objects.SOAuthAuthorizationCode)

Example 4 with OAuthAuthorizationCode

use of org.bimserver.models.store.OAuthAuthorizationCode in project BIMserver by opensourceBIM.

the class OAuthServiceImpl method revokeAuthorization.

@Override
public void revokeAuthorization(Long oid) throws ServerException, UserException {
    try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
        OAuthAuthorizationCode code = session.get(oid, OldQuery.getDefault());
        User user = session.get(getCurrentUser().getOid(), OldQuery.getDefault());
        user.getOAuthIssuedAuthorizationCodes().remove(code);
        session.store(user);
        session.delete(code, -1);
        session.commit();
    } catch (Exception e) {
        handleException(e);
    }
}
Also used : User(org.bimserver.models.store.User) DatabaseSession(org.bimserver.database.DatabaseSession) OAuthAuthorizationCode(org.bimserver.models.store.OAuthAuthorizationCode) SOAuthAuthorizationCode(org.bimserver.interfaces.objects.SOAuthAuthorizationCode) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException)

Example 5 with OAuthAuthorizationCode

use of org.bimserver.models.store.OAuthAuthorizationCode in project BIMserver by opensourceBIM.

the class OAuthServiceImpl method setAuthorizationCode.

@Override
public void setAuthorizationCode(Long applicationId, String code) throws UserException, ServerException {
    try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
        OAuthAuthorizationCode oAuthAuthorizationCode = session.create(OAuthAuthorizationCode.class);
        OAuthServer server = session.get(applicationId, OldQuery.getDefault());
        oAuthAuthorizationCode.setOauthServer(server);
        oAuthAuthorizationCode.setCode(code);
        User user = session.get(StorePackage.eINSTANCE.getUser(), getAuthorization().getUoid(), OldQuery.getDefault());
        user.getOAuthAuthorizationCodes().add(oAuthAuthorizationCode);
        session.store(user);
        session.commit();
    } catch (Exception e) {
        handleException(e);
    }
}
Also used : User(org.bimserver.models.store.User) DatabaseSession(org.bimserver.database.DatabaseSession) SOAuthServer(org.bimserver.interfaces.objects.SOAuthServer) OAuthServer(org.bimserver.models.store.OAuthServer) OAuthAuthorizationCode(org.bimserver.models.store.OAuthAuthorizationCode) SOAuthAuthorizationCode(org.bimserver.interfaces.objects.SOAuthAuthorizationCode) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException)

Aggregations

BimserverDatabaseException (org.bimserver.BimserverDatabaseException)6 DatabaseSession (org.bimserver.database.DatabaseSession)6 OAuthAuthorizationCode (org.bimserver.models.store.OAuthAuthorizationCode)6 User (org.bimserver.models.store.User)5 ServerException (org.bimserver.shared.exceptions.ServerException)4 UserException (org.bimserver.shared.exceptions.UserException)4 SOAuthAuthorizationCode (org.bimserver.interfaces.objects.SOAuthAuthorizationCode)3 OAuthServer (org.bimserver.models.store.OAuthServer)3 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)2 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)2 OAuthResponse (org.apache.oltu.oauth2.common.message.OAuthResponse)2 Authorization (org.bimserver.models.store.Authorization)2 RunServiceAuthorization (org.bimserver.models.store.RunServiceAuthorization)2 SingleProjectAuthorization (org.bimserver.models.store.SingleProjectAuthorization)2 PrintWriter (java.io.PrintWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1