/modules/dxp/apps/document-library-opener/document-library-opener-onedrive-web/src/main/java/com/liferay/document/library/opener/onedrive/web/internal/oauth/OAuth2Manager.java

https://github.com/vilmospapp/liferay-portal · Java · 190 lines · 129 code · 45 blank · 16 comment · 5 complexity · 94a191db046e85b3f36e8e7b35810183 MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * The contents of this file are subject to the terms of the Liferay Enterprise
  5. * Subscription License ("License"). You may not use this file except in
  6. * compliance with the License. You can obtain a copy of the License by
  7. * contacting Liferay, Inc. See the License for the specific language governing
  8. * permissions and limitations under the License, including but not limited to
  9. * distribution rights of the Software.
  10. *
  11. *
  12. *
  13. */
  14. package com.liferay.document.library.opener.onedrive.web.internal.oauth;
  15. import com.github.scribejava.apis.MicrosoftAzureActiveDirectory20Api;
  16. import com.github.scribejava.core.builder.ServiceBuilder;
  17. import com.github.scribejava.core.builder.ServiceBuilderOAuth20;
  18. import com.github.scribejava.core.oauth.OAuth20Service;
  19. import com.liferay.document.library.opener.onedrive.web.internal.configuration.DLOneDriveCompanyConfiguration;
  20. import com.liferay.portal.kernel.exception.PortalException;
  21. import com.liferay.portal.kernel.module.configuration.ConfigurationException;
  22. import com.liferay.portal.kernel.module.configuration.ConfigurationProvider;
  23. import com.liferay.portal.kernel.util.Portal;
  24. import java.io.IOException;
  25. import java.util.Optional;
  26. import java.util.concurrent.ExecutionException;
  27. import org.osgi.service.component.annotations.Component;
  28. import org.osgi.service.component.annotations.Reference;
  29. /**
  30. * @author Cristina González
  31. */
  32. @Component(service = OAuth2Manager.class)
  33. public class OAuth2Manager {
  34. public AccessToken createAccessToken(
  35. long companyId, long userId, String code, String portalURL)
  36. throws Exception {
  37. try (OAuth20Service oAuth20Service = _createOAuth20Service(
  38. companyId, _getRedirectURI(portalURL))) {
  39. AccessToken accessToken = new AccessToken(
  40. oAuth20Service.getAccessToken(code));
  41. _accessTokenStore.add(companyId, userId, accessToken);
  42. return accessToken;
  43. }
  44. catch (IOException ioException) {
  45. throw new PortalException(ioException);
  46. }
  47. }
  48. public Optional<AccessToken> getAccessTokenOptional(
  49. long companyId, long userId)
  50. throws PortalException {
  51. Optional<AccessToken> accessTokenOptional =
  52. _accessTokenStore.getAccessTokenOptional(companyId, userId);
  53. if (!accessTokenOptional.isPresent()) {
  54. return Optional.empty();
  55. }
  56. AccessToken accessToken = accessTokenOptional.get();
  57. if (!accessToken.isValid()) {
  58. return _refreshOAuth2AccessToken(companyId, userId, accessToken);
  59. }
  60. return Optional.of(accessToken);
  61. }
  62. public String getAuthorizationURL(
  63. long companyId, String portalURL, String state)
  64. throws PortalException {
  65. try (OAuth20Service oAuth20Service = _createOAuth20Service(
  66. companyId, _getRedirectURI(portalURL))) {
  67. return oAuth20Service.getAuthorizationUrl(state);
  68. }
  69. catch (IOException ioException) {
  70. throw new PortalException(ioException);
  71. }
  72. }
  73. public boolean hasAccessToken(long companyId, long userId)
  74. throws PortalException {
  75. Optional<AccessToken> accessTokenOptional = getAccessTokenOptional(
  76. companyId, userId);
  77. return accessTokenOptional.isPresent();
  78. }
  79. public void revokeOAuth2AccessToken(long companyId, long userId) {
  80. Optional<AccessToken> accessTokenOptional =
  81. _accessTokenStore.getAccessTokenOptional(companyId, userId);
  82. if (!accessTokenOptional.isPresent()) {
  83. return;
  84. }
  85. _accessTokenStore.delete(companyId, userId);
  86. }
  87. private OAuth20Service _createOAuth20Service(
  88. long companyId, String redirectURL)
  89. throws PortalException {
  90. DLOneDriveCompanyConfiguration dlOneDriveCompanyConfiguration =
  91. _getDLOneDriveCompanyConfiguration(companyId);
  92. ServiceBuilderOAuth20 serviceBuilderOAuth20 = new ServiceBuilder(
  93. dlOneDriveCompanyConfiguration.clientId()
  94. ).apiSecret(
  95. dlOneDriveCompanyConfiguration.clientSecret()
  96. ).callback(
  97. redirectURL
  98. ).withScope(
  99. "https://graph.microsoft.com/.default"
  100. ).apiKey(
  101. dlOneDriveCompanyConfiguration.clientId()
  102. );
  103. try (OAuth20Service oAuth20Service = serviceBuilderOAuth20.build(
  104. MicrosoftAzureActiveDirectory20Api.custom(
  105. dlOneDriveCompanyConfiguration.tenant()))) {
  106. return oAuth20Service;
  107. }
  108. catch (Exception exception) {
  109. throw new PortalException(
  110. "Unable to create OAuth20Service", exception);
  111. }
  112. }
  113. private DLOneDriveCompanyConfiguration _getDLOneDriveCompanyConfiguration(
  114. long companyId)
  115. throws ConfigurationException {
  116. return _configurationProvider.getCompanyConfiguration(
  117. DLOneDriveCompanyConfiguration.class, companyId);
  118. }
  119. private String _getRedirectURI(String portalURL) {
  120. return portalURL + Portal.PATH_MODULE +
  121. "/document_library/onedrive/oauth2";
  122. }
  123. private Optional<AccessToken> _refreshOAuth2AccessToken(
  124. long companyId, long userId, AccessToken accessToken)
  125. throws PortalException {
  126. if (accessToken.getRefreshToken() == null) {
  127. return Optional.empty();
  128. }
  129. try (OAuth20Service oAuth20Service = _createOAuth20Service(
  130. companyId, null)) {
  131. AccessToken newAccessToken = new AccessToken(
  132. oAuth20Service.refreshAccessToken(
  133. accessToken.getRefreshToken()));
  134. _accessTokenStore.add(companyId, userId, newAccessToken);
  135. return Optional.of(newAccessToken);
  136. }
  137. catch (ExecutionException | InterruptedException | IOException
  138. exception) {
  139. throw new PortalException(exception);
  140. }
  141. }
  142. private final AccessTokenStore _accessTokenStore = new AccessTokenStore();
  143. @Reference
  144. private ConfigurationProvider _configurationProvider;
  145. }