index.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. .. _authkey:
  2. Key authentication module
  3. =========================
  4. The ``authkey`` module for GeoServer allows for a very simple authentication protocol designed for
  5. OGC clients that cannot handle any kind of security protocol, not even the HTTP basic authentication.
  6. For these clients the module allows a minimal form of authentication by appending a unique key in the
  7. URL that is used as the sole authentication token. Obviously this approach is open to security token
  8. sniffing and must always be associated with the use of HTTPS connections.
  9. A sample authenticated request looks like::
  10. http://localhost:8080/geoserver/topp/wms?service=WMS&version=1.3.0&request=GetCapabilities&authkey=ef18d7e7-963b-470f-9230-c7f9de166888
  11. Where ``authkey=ef18d7e7-963b-470f-9230-c7f9de166888`` is associated to a specific user (more on this later).
  12. The capabilities document contains backlinks to the server itself, linking to the URLs that can be used
  13. to perform GetMap, GetFeatureInfo and so on.
  14. When the ``authkey`` parameter is provided the backlinks will contain the authentication key as well,
  15. allowing any compliant WMS client to access secured resources.
  16. Limitations
  17. -----------
  18. The ``authkey`` module is meant to be used with OGC services. It won't work properly against the
  19. administration GUI, nor RESTConfig.
  20. The ``authkey`` module replaces the default basic authentication provider, therefore when editing a user
  21. the Enabled checkbox does not affect the ``authkey`` provider and you cannot disable users this way.
  22. Key providers
  23. -------------
  24. Key providers are responsible for mapping the authentication keys to a user. The authentication key
  25. itself is a UUID (Universally Unique IDentifier (UUID)). A key provider needs a user/group service and it is
  26. responsible for synchronizing the authentication keys with the users contained in this service.
  27. Key provider using user properties
  28. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  29. This key provider uses a user property ``UUID`` to map the authentication key to the user. User
  30. properties are stored in the user/group service. Synchronizing is simple since the logic has
  31. to search for users not having the property ``UUID`` and add it. The property value is a generated
  32. UUID.
  33. UUID=b52d2068-0a9b-45d7-aacc-144d16322018
  34. If the user/group service is read only, the property has to be added from outside, no synchronizing
  35. is possible.
  36. Key provider using a property file
  37. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  38. This key provider uses a property file named ``authkeys.properties``. The default user/group service
  39. is named ``default``. The ``authkeys.properties`` for this service would be located at
  40. ``$GEOSERVER_DATA_DIR/security/usergroup/default/authkeys.propeties``
  41. A sample file looks as follows::
  42. # Format is authkey=username
  43. b52d2068-0a9b-45d7-aacc-144d16322018=admin
  44. 1825efd3-20e1-4c18-9648-62c97d3ee5cb=sf
  45. ef18d7e7-963b-470f-9230-c7f9de166888=topp
  46. This key provider also works for read only user/group services. Synchronizing adds new users not
  47. having an entry in this file and removes entries for users deleted in the user/group service.
  48. Key provider using an external web service
  49. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  50. This key provider calls an external URL to map the authentication key to the user. This allows
  51. GeoServer to integrate into an existing security infrastructure, where a session token is shared
  52. among applications and managed through dedicated web services.
  53. The web service URL and some other parameters can be specified to configure the mapper in detail:
  54. .. list-table::
  55. :widths: 50 50
  56. * - **Option**
  57. - **Description**
  58. * - ``Web Service URL, with key placeholder``
  59. - the complete URL of the key mapping webservice, with a special placeholder ({key}) that will be replaced by the current authentication key
  60. * - ``Web Service Response User Search Regular Expression``
  61. - a regular expression used to extract the username from the webservice response; the first matched group (the part included in parentheses) in the regular expression will be used as the username; the default (^\\s*(.*)\\s*$) takes all the response text, trimming spaces at both ends
  62. * - ``Connection Timeout``
  63. - timeout to connect to the webservice
  64. * - ``Read Timeout``
  65. - timeout to read data from the webservice
  66. The mapper will call the webservice using an HTTP GET request (webservice requiring POST are not
  67. supported yet), replacing the {key} placeholder in the configured URL with the actual authentication
  68. key.
  69. If a response is received, it is parsed using the configured regular expression to extract the username
  70. from it. New lines are automatically stripped from the response. Some examples of regular expression
  71. that can be used are:
  72. .. list-table::
  73. :widths: 40 60
  74. * - **Regular Expression**
  75. - **Usage**
  76. * - ``^\s*(.*)\s*$``
  77. - all text trimming spaces at both ends
  78. * - ``^.*?\"user\"\s*:\s*\"([^\"]+)\".*$``
  79. - json response where the username is contained in a property named **user**
  80. * - ``^.*?<username>(.*?)</username>.*$``
  81. - xml response where the username is contained in a tag named **username**
  82. Synchronizing users with the user/group service means clearing the current Security context cache. The keys cached in memory will be removed and re-created at the next call.
  83. AuthKEY WebService Body Response UserGroup Service
  84. **************************************************
  85. When using an external web service to get Auth details, it is possible to define a custom ``GeoServer UserGroup Service`` being able to fetch Authorities - aka user's Roles - from the HTTP Body Response.
  86. The rationale is mostly the same; that kind of ``GeoServer UserGroup Service`` will apply a ``rolesRegex`` - Roles Regular Expression - to the body response - which can be either XML, JSON or Plain Text/HTML - in order to fetch the list of available Authorities.
  87. In order to do this, it is possible to configure instances of **AuthKEY WebService Body Response** User Group Service.
  88. First thing to do is to:
  89. 1. Login as an ``Administrator``
  90. 2. Move to ``Security`` > ``Users, Groups, Roles`` and select ``Add new`` from ``User Group Services``
  91. .. figure:: images/001_user_group_service.png
  92. :align: center
  93. 3. Click on ``AuthKEY WebService Body Response``
  94. .. figure:: images/002_user_group_service.png
  95. :align: center
  96. 4. Provide a ``Name`` and select anything you want from ``Passwords`` - those won't be used by this service, but they are still mandatory for GeoServer -
  97. .. figure:: images/003_user_group_service.png
  98. :align: center
  99. 5. Provide a suitable ``Roles Regex`` to apply to your Web Service Response
  100. .. note:: This is the only real mandatory value to provide. The others are optional and will allow you to customize the User Group Service behavior (see below)
  101. .. figure:: images/004_user_group_service.png
  102. :align: center
  103. Once the new ``GeoServer UserGroup Service`` has been configured, it can be easily linked to the ``Key Provider Web Service Mapper``.
  104. 1. From ``Authentication`` > ``Authentication Filters``, select - or add new - ``AuthKEY`` using ``Web Service`` as key mapper
  105. 2. Select the newly defined ``UserGroup Service`` and save
  106. .. figure:: images/005_user_group_service.png
  107. :align: center
  108. **Additional Options**
  109. 1. *Optional static comma-separated list of available Groups from the Web Service response*
  110. It is worth notice that this ``UserGroup Service`` will **always** translate fetched Roles in the form ``ROLE_<ROLENAME>``
  111. As an instance, if the ``Roles Regular Expression`` will match something like::
  112. my_user_role1, another_custom_user_role, role_External_Role_X
  113. this will be converted into **3** different ``GeoServer User Roles`` named as::
  114. ROLE_MY_USER_ROLE1
  115. ROLE_ANOTHER_CUSTOM_USER_ROLE
  116. ROLE_EXTERNAL_ROLE_X
  117. Of course the role names are known only at runtime; nevertheless it is possible to **statically** specify associated ``GeoServer User Groups`` to be mapped later to other internal ``GeoServer User Roles``.
  118. What does this mean? A ``GeoServer User Group`` can be defined on the GeoServer Catalog and can be mapped by the active ``Role Services`` to one or more specific ``GeoServer User Roles``.
  119. This mainly depends on the ``GeoServer Role Service`` you use. By default, the internal ``GeoServer Role Service`` can map Roles and Groups through static configuration stored on the GeoServer Data Dir.
  120. This is possible by editing ``GeoServer User Group`` details from the ``Users, Groups, and Roles`` panel
  121. .. figure:: images/006_user_group_service.png
  122. :align: center
  123. .. figure:: images/007_user_group_service.png
  124. :align: center
  125. Now, this custom ``UserGroup Service`` maps dynamically ``GeoServer User Role`` to ``GeoServer User Group`` as follows::
  126. ROLE_MY_USER_ROLE1 <> GROUP_MY_USER_ROLE1
  127. ROLE_ANOTHER_CUSTOM_USER_ROLE <> GROUP_ANOTHER_CUSTOM_USER_ROLE
  128. ROLE_EXTERNAL_ROLE_X <> GROUP_EXTERNAL_ROLE_X
  129. In order to be able to assign any ``GeoServer User Group`` to other internal ``GeoServer User Roles``, since those are known only at runtime, the ``UserGroup Service`` allows us to **statically** specify the ``GeoServer User Groups`` the Web Service can use;
  130. this possible by setting the ``Optional static comma-separated list of available Groups from the Web Service response`` option:
  131. .. figure:: images/008_user_group_service.png
  132. :align: center
  133. Once this is correctly configured, it will be possible to edit and assign ``GeoServer User Roles`` to the Groups by using the standard way
  134. .. figure:: images/009_user_group_service.png
  135. :align: center
  136. 2. *Role Service to use*
  137. By default, if no ``Role Service`` specified, the ``UserGroup Service`` will use the ``GeoServer Active Role Service`` to resolve ``GeoServer User Roles`` from ``GeoServer User Groups`` - as specified above -
  138. .. figure:: images/010_user_group_service.png
  139. :align: center
  140. It is possible to define a ``Custom Role Service`` to use instead, to resole ``GeoServer User Roles``; this is possible simply by selecting the ``Role Service`` to use from the ``Role Service to use`` option
  141. .. figure:: images/011_user_group_service.png
  142. :align: center
  143. Configuration
  144. -------------
  145. Configuration can be done using the administrator GUI. There is a new type of authentication filter
  146. named **authkey** offering the following options.
  147. #. URL parameter name. This the name of URL parameter used in client HTTP requests. Default is ``authkey``.
  148. #. Key Provider. GeoServer offers the providers described above.
  149. #. User/group service to be used.
  150. Some of the key providers can require additional configuration parameter. These will appear under the
  151. Key Provider combo-box when one of those is selected.
  152. After configuring the filter it is necessary to put this filter on the authentication filter chain(s).
  153. .. note::
  154. The administrator GUI for this filter has button **Synchronize**. Clicking on this button
  155. saves the current configuration and triggers a synchronize. If users are added/removed from
  156. the backing user/group service, the synchronize logic should be triggered.
  157. Enabling Mappers' Auto-Synchronization
  158. --------------------------------------
  159. The following check is available for all provides.
  160. .. figure:: images/001_auto_sync.png
  161. :align: center
  162. If enabled, the service will automatically invoke the corresponding mapper synchronize method; the one associated to the current AuthKey provider.
  163. By default the synchronization happens every 60 seconds. In the case an administrator needs to change the auto-sync frequency, he will need to:
  164. 1. Edit the file `applicationContext.xml` within the `gs-authkey` jar file
  165. 2. Edit the property `autoSyncDelaySeconds` of the `authenticationKeyProvider` bean
  166. 3. Restart GeoServer
  167. Provider pluggability
  168. ---------------------
  169. With some Java programming it is possible to programmatically create and register a new key to user
  170. name mapper that works under a different logic.
  171. For example, you could have daily tokens, token generators and the like.
  172. In order to provide your custom mapper you have to implement the ``org.geoserver.security.AuthenticationKeyMapper``
  173. interface and then register said bean in the Spring application context. Alternatively it is possible
  174. to subclass from ``org.geoserver.security.AbstractAuthenticationKeyMapper``. A mapper (key provider) has
  175. to implement
  176. .. code-block:: java
  177. /**
  178. *
  179. * Maps a unique authentication key to a username. Since usernames are
  180. * unique within a {@link GeoServerUserGroupService} an individual mapper
  181. * is needed for each service offering this feature.
  182. *
  183. * @author Andrea Aime - GeoSolution
  184. */
  185. public interface AuthenticationKeyMapper extends BeanNameAware {
  186. /**
  187. * Maps the key provided in the request to the {@link GeoServerUser} object
  188. * of the corresponding user, or returns null
  189. * if no corresponding user is found
  190. *
  191. * Returns <code>null</code> if the user is disabled
  192. *
  193. * @param key
  194. * @return
  195. */
  196. GeoServerUser getUser(String key) throws IOException;
  197. /**
  198. * Assures that each user in the corresponding {@link GeoServerUserGroupService} has
  199. * an authentication key.
  200. *
  201. * returns the number of added authentication keys
  202. *
  203. * @throws IOException
  204. */
  205. int synchronize() throws IOException;
  206. /**
  207. * Returns <code>true</code> it the mapper can deal with read only u
  208. * user/group services
  209. *
  210. * @return
  211. */
  212. boolean supportsReadOnlyUserGroupService();
  213. String getBeanName();
  214. void setUserGroupServiceName(String serviceName);
  215. String getUserGroupServiceName();
  216. public GeoServerSecurityManager getSecurityManager();
  217. public void setSecurityManager(GeoServerSecurityManager securityManager);
  218. }
  219. The mapper would have to be registered in the Spring application context in a ``applicationContext.xml``
  220. file in the root of your jar. Example for an implementation named ``com.mycompany.security.SuperpowersMapper``:
  221. .. code-block:: xml
  222. <?xml version="1.0" encoding="UTF-8"?>
  223. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
  224. <beans>
  225. <bean id="superpowersMapper" class="com.mycompany.security.SuperpowersMapper"/>
  226. </beans>
  227. At this point you can drop the ``authkey`` jar along with your custom mapper jar and use it in the
  228. administrator GUI of the authentication key filter.