Search in sources :

Example 6 with OAuthServer

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

the class OAuthServiceImpl method getRemoteToken.

@Override
public String getRemoteToken(Long soid, String code, Long serverId) throws ServerException, UserException {
    try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
        NewService newService = session.get(soid, OldQuery.getDefault());
        ObjectNode objectNode = OBJECT_MAPPER.createObjectNode();
        objectNode.put("grant_type", "authorization_code");
        objectNode.put("code", code);
        OAuthServer oAuthServer = session.get(serverId, OldQuery.getDefault());
        objectNode.put("client_id", oAuthServer.getClientId());
        objectNode.put("client_secret", oAuthServer.getClientSecret());
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpPost post = new HttpPost(newService.getTokenUrl());
            post.setHeader("Content-Type", "application/x-www-form-urlencoded");
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("grant_type", "authorization_code"));
            nvps.add(new BasicNameValuePair("code", code));
            nvps.add(new BasicNameValuePair("client_id", oAuthServer.getClientId()));
            nvps.add(new BasicNameValuePair("client_secret", oAuthServer.getClientSecret()));
            nvps.add(new BasicNameValuePair("redirect_uri", "crap"));
            post.setEntity(new UrlEncodedFormEntity(nvps));
            CloseableHttpResponse httpResponse = httpclient.execute(post);
            ObjectNode response = OBJECT_MAPPER.readValue(httpResponse.getEntity().getContent(), ObjectNode.class);
            if (response.has("access_token")) {
                String accessToken = response.get("access_token").asText();
                newService.setAccessToken(accessToken);
                newService.setStatus(ServiceStatus.AUTHENTICATED);
                newService.setResourceUrl(response.get("resource_url").asText());
                session.store(newService);
                session.commit();
                return accessToken;
            } else {
                throw new UserException("No access_token received from oauth server");
            }
        } finally {
            httpclient.close();
        }
    } catch (Exception e) {
        return handleException(e);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DatabaseSession(org.bimserver.database.DatabaseSession) ArrayList(java.util.ArrayList) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) SOAuthServer(org.bimserver.interfaces.objects.SOAuthServer) OAuthServer(org.bimserver.models.store.OAuthServer) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) UserException(org.bimserver.shared.exceptions.UserException) NewService(org.bimserver.models.store.NewService)

Example 7 with OAuthServer

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

the class OAuthServiceImpl method revokeApplication.

@Override
public void revokeApplication(Long oid) throws ServerException, UserException {
    try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
        OAuthServer oAuthServer = session.get(oid, OldQuery.getDefault());
        session.delete(oAuthServer, -1);
        session.commit();
    } catch (Exception e) {
        handleException(e);
    }
}
Also used : DatabaseSession(org.bimserver.database.DatabaseSession) SOAuthServer(org.bimserver.interfaces.objects.SOAuthServer) OAuthServer(org.bimserver.models.store.OAuthServer) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException)

Example 8 with OAuthServer

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

Example 9 with OAuthServer

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

the class OAuthServiceImpl method registerApplication.

@Override
public Long registerApplication(String registrationEndpoint, String apiUrl, String redirectUrl) throws UserException, ServerException {
    try {
        try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
            OAuthServer oAuthServer = session.querySingle(StorePackage.eINSTANCE.getOAuthServer_RegistrationEndpoint(), registrationEndpoint);
            if (oAuthServer != null) {
                return oAuthServer.getOid();
            }
            ServerSettings serverSettings = getBimServer().getServerSettingsCache().getServerSettings();
            OAuthClientRequest request = OAuthClientRegistrationRequest.location(registrationEndpoint, OAuthRegistration.Type.PUSH).setName(serverSettings.getName()).setUrl(redirectUrl).setDescription(serverSettings.getDescription()).setIcon(serverSettings.getIcon()).setRedirectURL(redirectUrl).buildJSONMessage();
            OAuthRegistrationClient oauthclient = new OAuthRegistrationClient(new URLConnectionClient());
            OAuthClientRegistrationResponse response = oauthclient.clientInfo(request);
            oAuthServer = session.create(OAuthServer.class);
            oAuthServer.setApiUrl(apiUrl);
            oAuthServer.setClientId(response.getClientId());
            oAuthServer.setClientSecret(response.getClientSecret());
            oAuthServer.setIssuedAt(new Date(Long.parseLong(response.getIssuedAt())));
            GregorianCalendar expiresAt = new GregorianCalendar();
            expiresAt.setTimeInMillis(new GregorianCalendar().getTimeInMillis() + response.getExpiresIn());
            oAuthServer.setExpiresAt(expiresAt.getTime());
            oAuthServer.setRegistrationEndpoint(registrationEndpoint);
            oAuthServer.setClientDescription(serverSettings.getDescription());
            oAuthServer.setClientName(serverSettings.getName());
            if (serverSettings.getIcon() != null) {
                byte[] icon = NetUtils.getContentAsBytes(new URL(serverSettings.getIcon()), 500);
                oAuthServer.setClientIcon(icon);
            }
            oAuthServer.setIncoming(false);
            oAuthServer.setRedirectUrl(redirectUrl);
            session.commit();
            return oAuthServer.getOid();
        }
    } catch (Exception e) {
        return handleException(e);
    }
}
Also used : OAuthRegistrationClient(org.apache.oltu.oauth2.ext.dynamicreg.client.OAuthRegistrationClient) DatabaseSession(org.bimserver.database.DatabaseSession) OAuthClientRegistrationResponse(org.apache.oltu.oauth2.ext.dynamicreg.client.response.OAuthClientRegistrationResponse) ServerSettings(org.bimserver.models.store.ServerSettings) GregorianCalendar(java.util.GregorianCalendar) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) SOAuthServer(org.bimserver.interfaces.objects.SOAuthServer) OAuthServer(org.bimserver.models.store.OAuthServer) Date(java.util.Date) URL(java.net.URL) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException)

Example 10 with OAuthServer

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

the class ServiceImpl method getAllPrivateProfiles.

@Override
public List<SProfileDescriptor> getAllPrivateProfiles(String notificationsUrl, String serviceIdentifier) throws ServerException, UserException {
    if (notificationsUrl == null) {
        throw new UserException("Missing notificationsUrl");
    }
    requireRealUserAuthentication();
    try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
        try (BimServerClientFactory factory = new JsonBimServerClientFactory(notificationsUrl, getBimServer().getServicesMap(), getBimServer().getJsonSocketReflectorFactory(), getBimServer().getReflectorFactory(), getBimServer().getMetaDataManager())) {
            BimServerClientInterface client = factory.create();
            OAuthServer oAuthServer = session.querySingle(StorePackage.eINSTANCE.getOAuthServer_ApiUrl(), notificationsUrl);
            User user = session.get(StorePackage.eINSTANCE.getUser(), getAuthorization().getUoid(), OldQuery.getDefault());
            for (OAuthAuthorizationCode oAuthAuthorizationCode : user.getOAuthAuthorizationCodes()) {
                if (oAuthAuthorizationCode.getOauthServer() == oAuthServer) {
                    return client.getRemoteServiceInterface().getPrivateProfiles(serviceIdentifier, oAuthAuthorizationCode.getCode());
                }
            }
            return null;
        }
    } catch (Exception e) {
        return handleException(e);
    }
}
Also used : SUser(org.bimserver.interfaces.objects.SUser) User(org.bimserver.models.store.User) DatabaseSession(org.bimserver.database.DatabaseSession) JsonBimServerClientFactory(org.bimserver.client.json.JsonBimServerClientFactory) BimServerClientInterface(org.bimserver.plugins.services.BimServerClientInterface) UserException(org.bimserver.shared.exceptions.UserException) OAuthServer(org.bimserver.models.store.OAuthServer) JsonBimServerClientFactory(org.bimserver.client.json.JsonBimServerClientFactory) BimServerClientFactory(org.bimserver.shared.BimServerClientFactory) OAuthAuthorizationCode(org.bimserver.models.store.OAuthAuthorizationCode) IOException(java.io.IOException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) SerializerException(org.bimserver.plugins.serializers.SerializerException) BcfException(org.opensourcebim.bcf.BcfException) UserException(org.bimserver.shared.exceptions.UserException) CannotBeScheduledException(org.bimserver.longaction.CannotBeScheduledException) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) ServerException(org.bimserver.shared.exceptions.ServerException) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException)

Aggregations

BimserverDatabaseException (org.bimserver.BimserverDatabaseException)10 DatabaseSession (org.bimserver.database.DatabaseSession)10 OAuthServer (org.bimserver.models.store.OAuthServer)10 SOAuthServer (org.bimserver.interfaces.objects.SOAuthServer)7 ServerException (org.bimserver.shared.exceptions.ServerException)6 UserException (org.bimserver.shared.exceptions.UserException)6 OAuthAuthorizationCode (org.bimserver.models.store.OAuthAuthorizationCode)3 User (org.bimserver.models.store.User)3 IOException (java.io.IOException)2 URL (java.net.URL)2 GregorianCalendar (java.util.GregorianCalendar)2 ServletException (javax.servlet.ServletException)2 OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)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 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1