PageRenderTime 250ms CodeModel.GetById 20ms RepoModel.GetById 3ms app.codeStats 0ms

/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/MySQLAuthenticationProvider.java

https://gitlab.com/oytunistrator/guacamole-client
Java | 185 lines | 106 code | 27 blank | 52 comment | 2 complexity | df5202fa7529862ad4f8d765b4f68c59 MD5 | raw file
  1. /*
  2. * Copyright (C) 2013 Glyptodon LLC
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. package net.sourceforge.guacamole.net.auth.mysql;
  23. import com.google.inject.Binder;
  24. import com.google.inject.Guice;
  25. import com.google.inject.Injector;
  26. import com.google.inject.Module;
  27. import com.google.inject.name.Names;
  28. import java.util.Properties;
  29. import org.glyptodon.guacamole.GuacamoleException;
  30. import org.glyptodon.guacamole.net.auth.AuthenticationProvider;
  31. import org.glyptodon.guacamole.net.auth.Credentials;
  32. import org.glyptodon.guacamole.net.auth.UserContext;
  33. import net.sourceforge.guacamole.net.auth.mysql.dao.ConnectionGroupMapper;
  34. import net.sourceforge.guacamole.net.auth.mysql.dao.ConnectionGroupPermissionMapper;
  35. import net.sourceforge.guacamole.net.auth.mysql.dao.ConnectionHistoryMapper;
  36. import net.sourceforge.guacamole.net.auth.mysql.dao.ConnectionMapper;
  37. import net.sourceforge.guacamole.net.auth.mysql.dao.ConnectionParameterMapper;
  38. import net.sourceforge.guacamole.net.auth.mysql.dao.ConnectionPermissionMapper;
  39. import net.sourceforge.guacamole.net.auth.mysql.dao.SystemPermissionMapper;
  40. import net.sourceforge.guacamole.net.auth.mysql.dao.UserMapper;
  41. import net.sourceforge.guacamole.net.auth.mysql.dao.UserPermissionMapper;
  42. import net.sourceforge.guacamole.net.auth.mysql.properties.MySQLGuacamoleProperties;
  43. import net.sourceforge.guacamole.net.auth.mysql.service.ConnectionGroupService;
  44. import net.sourceforge.guacamole.net.auth.mysql.service.ConnectionService;
  45. import net.sourceforge.guacamole.net.auth.mysql.service.PasswordEncryptionService;
  46. import net.sourceforge.guacamole.net.auth.mysql.service.PermissionCheckService;
  47. import net.sourceforge.guacamole.net.auth.mysql.service.SHA256PasswordEncryptionService;
  48. import net.sourceforge.guacamole.net.auth.mysql.service.SaltService;
  49. import net.sourceforge.guacamole.net.auth.mysql.service.SecureRandomSaltService;
  50. import net.sourceforge.guacamole.net.auth.mysql.service.UserService;
  51. import org.glyptodon.guacamole.properties.GuacamoleProperties;
  52. import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
  53. import org.mybatis.guice.MyBatisModule;
  54. import org.mybatis.guice.datasource.builtin.PooledDataSourceProvider;
  55. import org.mybatis.guice.datasource.helper.JdbcHelper;
  56. /**
  57. * Provides a MySQL based implementation of the AuthenticationProvider
  58. * functionality.
  59. *
  60. * @author James Muehlner
  61. */
  62. public class MySQLAuthenticationProvider implements AuthenticationProvider {
  63. /**
  64. * Set of all active connections.
  65. */
  66. private ActiveConnectionMap activeConnectionMap = new ActiveConnectionMap();
  67. /**
  68. * Injector which will manage the object graph of this authentication
  69. * provider.
  70. */
  71. private Injector injector;
  72. @Override
  73. public UserContext getUserContext(Credentials credentials) throws GuacamoleException {
  74. // Get user service
  75. UserService userService = injector.getInstance(UserService.class);
  76. // Get user
  77. MySQLUser authenticatedUser = userService.retrieveUser(credentials);
  78. if (authenticatedUser != null) {
  79. MySQLUserContext context = injector.getInstance(MySQLUserContext.class);
  80. context.init(authenticatedUser.getUserID());
  81. return context;
  82. }
  83. // Otherwise, unauthorized
  84. return null;
  85. }
  86. /**
  87. * Creates a new MySQLAuthenticationProvider that reads and writes
  88. * authentication data to a MySQL database defined by properties in
  89. * guacamole.properties.
  90. *
  91. * @throws GuacamoleException If a required property is missing, or
  92. * an error occurs while parsing a property.
  93. */
  94. public MySQLAuthenticationProvider() throws GuacamoleException {
  95. final Properties myBatisProperties = new Properties();
  96. final Properties driverProperties = new Properties();
  97. // Set the mysql properties for MyBatis.
  98. myBatisProperties.setProperty("mybatis.environment.id", "guacamole");
  99. myBatisProperties.setProperty("JDBC.host", GuacamoleProperties.getRequiredProperty(MySQLGuacamoleProperties.MYSQL_HOSTNAME));
  100. myBatisProperties.setProperty("JDBC.port", String.valueOf(GuacamoleProperties.getRequiredProperty(MySQLGuacamoleProperties.MYSQL_PORT)));
  101. myBatisProperties.setProperty("JDBC.schema", GuacamoleProperties.getRequiredProperty(MySQLGuacamoleProperties.MYSQL_DATABASE));
  102. myBatisProperties.setProperty("JDBC.username", GuacamoleProperties.getRequiredProperty(MySQLGuacamoleProperties.MYSQL_USERNAME));
  103. myBatisProperties.setProperty("JDBC.password", GuacamoleProperties.getRequiredProperty(MySQLGuacamoleProperties.MYSQL_PASSWORD));
  104. myBatisProperties.setProperty("JDBC.autoCommit", "false");
  105. myBatisProperties.setProperty("mybatis.pooled.pingEnabled", "true");
  106. myBatisProperties.setProperty("mybatis.pooled.pingQuery", "SELECT 1");
  107. driverProperties.setProperty("characterEncoding","UTF-8");
  108. // Set up Guice injector.
  109. injector = Guice.createInjector(
  110. JdbcHelper.MySQL,
  111. new Module() {
  112. @Override
  113. public void configure(Binder binder) {
  114. Names.bindProperties(binder, myBatisProperties);
  115. binder.bind(Properties.class)
  116. .annotatedWith(Names.named("JDBC.driverProperties"))
  117. .toInstance(driverProperties);
  118. }
  119. },
  120. new MyBatisModule() {
  121. @Override
  122. protected void initialize() {
  123. // Datasource
  124. bindDataSourceProviderType(PooledDataSourceProvider.class);
  125. // Transaction factory
  126. bindTransactionFactoryType(JdbcTransactionFactory.class);
  127. // Add MyBatis mappers
  128. addMapperClass(ConnectionHistoryMapper.class);
  129. addMapperClass(ConnectionMapper.class);
  130. addMapperClass(ConnectionGroupMapper.class);
  131. addMapperClass(ConnectionGroupPermissionMapper.class);
  132. addMapperClass(ConnectionParameterMapper.class);
  133. addMapperClass(ConnectionPermissionMapper.class);
  134. addMapperClass(SystemPermissionMapper.class);
  135. addMapperClass(UserMapper.class);
  136. addMapperClass(UserPermissionMapper.class);
  137. // Bind interfaces
  138. bind(MySQLUserContext.class);
  139. bind(UserDirectory.class);
  140. bind(MySQLUser.class);
  141. bind(SaltService.class).to(SecureRandomSaltService.class);
  142. bind(PasswordEncryptionService.class).to(SHA256PasswordEncryptionService.class);
  143. bind(PermissionCheckService.class);
  144. bind(ConnectionService.class);
  145. bind(ConnectionGroupService.class);
  146. bind(UserService.class);
  147. bind(ActiveConnectionMap.class).toInstance(activeConnectionMap);
  148. }
  149. } // end of mybatis module
  150. );
  151. } // end of constructor
  152. @Override
  153. public UserContext updateUserContext(UserContext context,
  154. Credentials credentials) throws GuacamoleException {
  155. // No need to update the context
  156. return context;
  157. }
  158. }