Search in sources :

Example 1 with SingleProjectAuthorization

use of org.bimserver.models.store.SingleProjectAuthorization 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 SingleProjectAuthorization

use of org.bimserver.models.store.SingleProjectAuthorization 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 3 with SingleProjectAuthorization

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

the class OAuthAuthorizationServlet method makeUrl.

private URI makeUrl(String redirectURI, OAuthAuthorizationCode oauthCode, OAuthAuthorizationResponseBuilder builder) throws OAuthSystemException, URISyntaxException {
    OAuthAuthorizationResponseBuilder build = builder.location(redirectURI).setParam("address", getBimServer().getServerSettingsCache().getServerSettings().getSiteAddress() + "/json");
    build.setParam("serviceaddress", getBimServer().getServerSettingsCache().getServerSettings().getSiteAddress() + "/services");
    if (oauthCode.getAuthorization() instanceof SingleProjectAuthorization) {
        SingleProjectAuthorization singleProjectAuthorization = (SingleProjectAuthorization) oauthCode.getAuthorization();
        build.setParam("poid", "" + singleProjectAuthorization.getProject().getOid());
    } else if (oauthCode.getAuthorization() instanceof RunServiceAuthorization) {
        RunServiceAuthorization auth = (RunServiceAuthorization) oauthCode.getAuthorization();
        build.setParam("soid", "" + auth.getService().getOid());
    }
    final OAuthResponse response = build.buildQueryMessage();
    String locationUri = response.getLocationUri();
    URI url = new URI(locationUri);
    return url;
}
Also used : OAuthAuthorizationResponseBuilder(org.apache.oltu.oauth2.as.response.OAuthASResponse.OAuthAuthorizationResponseBuilder) SingleProjectAuthorization(org.bimserver.models.store.SingleProjectAuthorization) RunServiceAuthorization(org.bimserver.models.store.RunServiceAuthorization) OAuthResponse(org.apache.oltu.oauth2.common.message.OAuthResponse) URI(java.net.URI)

Aggregations

RunServiceAuthorization (org.bimserver.models.store.RunServiceAuthorization)3 SingleProjectAuthorization (org.bimserver.models.store.SingleProjectAuthorization)3 OAuthResponse (org.apache.oltu.oauth2.common.message.OAuthResponse)2 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)2 DatabaseSession (org.bimserver.database.DatabaseSession)2 Authorization (org.bimserver.models.store.Authorization)2 OAuthAuthorizationCode (org.bimserver.models.store.OAuthAuthorizationCode)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 URI (java.net.URI)1 ServletException (javax.servlet.ServletException)1 MD5Generator (org.apache.oltu.oauth2.as.issuer.MD5Generator)1 OAuthIssuer (org.apache.oltu.oauth2.as.issuer.OAuthIssuer)1 OAuthIssuerImpl (org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl)1 OAuthTokenRequest (org.apache.oltu.oauth2.as.request.OAuthTokenRequest)1 OAuthAuthorizationResponseBuilder (org.apache.oltu.oauth2.as.response.OAuthASResponse.OAuthAuthorizationResponseBuilder)1 OAuthTokenResponseBuilder (org.apache.oltu.oauth2.as.response.OAuthASResponse.OAuthTokenResponseBuilder)1 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)1 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)1 SAuthorization (org.bimserver.interfaces.objects.SAuthorization)1