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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations