multitenancy.adoc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. = OAuth 2.0 Resource Server Multi-tenancy
  2. [[webflux-oauth2resourceserver-multitenancy]]
  3. == Multi-tenancy
  4. A resource server is considered multi-tenant when there are multiple strategies for verifying a bearer token, keyed by some tenant identifier.
  5. For example, your resource server may accept bearer tokens from two different authorization servers.
  6. Or, your authorization server may represent a multiplicity of issuers.
  7. In each case, there are two things that need to be done and trade-offs associated with how you choose to do them:
  8. 1. Resolve the tenant
  9. 2. Propagate the tenant
  10. === Resolving the Tenant By Claim
  11. One way to differentiate tenants is by the issuer claim. Since the issuer claim accompanies signed JWTs, this can be done with the `JwtIssuerReactiveAuthenticationManagerResolver`, like so:
  12. ====
  13. .Java
  14. [source,java,role="primary"]
  15. ----
  16. JwtIssuerReactiveAuthenticationManagerResolver authenticationManagerResolver = new JwtIssuerReactiveAuthenticationManagerResolver
  17. ("https://idp.example.org/issuerOne", "https://idp.example.org/issuerTwo");
  18. http
  19. .authorizeExchange(exchanges -> exchanges
  20. .anyExchange().authenticated()
  21. )
  22. .oauth2ResourceServer(oauth2 -> oauth2
  23. .authenticationManagerResolver(authenticationManagerResolver)
  24. );
  25. ----
  26. .Kotlin
  27. [source,kotlin,role="secondary"]
  28. ----
  29. val customAuthenticationManagerResolver = JwtIssuerReactiveAuthenticationManagerResolver("https://idp.example.org/issuerOne", "https://idp.example.org/issuerTwo")
  30. return http {
  31. authorizeExchange {
  32. authorize(anyExchange, authenticated)
  33. }
  34. oauth2ResourceServer {
  35. authenticationManagerResolver = customAuthenticationManagerResolver
  36. }
  37. }
  38. ----
  39. ====
  40. This is nice because the issuer endpoints are loaded lazily.
  41. In fact, the corresponding `JwtReactiveAuthenticationManager` is instantiated only when the first request with the corresponding issuer is sent.
  42. This allows for an application startup that is independent from those authorization servers being up and available.
  43. ==== Dynamic Tenants
  44. Of course, you may not want to restart the application each time a new tenant is added.
  45. In this case, you can configure the `JwtIssuerReactiveAuthenticationManagerResolver` with a repository of `ReactiveAuthenticationManager` instances, which you can edit at runtime, like so:
  46. ====
  47. .Java
  48. [source,java,role="primary"]
  49. ----
  50. private Mono<ReactiveAuthenticationManager> addManager(
  51. Map<String, ReactiveAuthenticationManager> authenticationManagers, String issuer) {
  52. return Mono.fromCallable(() -> ReactiveJwtDecoders.fromIssuerLocation(issuer))
  53. .subscribeOn(Schedulers.boundedElastic())
  54. .map(JwtReactiveAuthenticationManager::new)
  55. .doOnNext(authenticationManager -> authenticationManagers.put(issuer, authenticationManager));
  56. }
  57. // ...
  58. JwtIssuerReactiveAuthenticationManagerResolver authenticationManagerResolver =
  59. new JwtIssuerReactiveAuthenticationManagerResolver(authenticationManagers::get);
  60. http
  61. .authorizeExchange(exchanges -> exchanges
  62. .anyExchange().authenticated()
  63. )
  64. .oauth2ResourceServer(oauth2 -> oauth2
  65. .authenticationManagerResolver(authenticationManagerResolver)
  66. );
  67. ----
  68. .Kotlin
  69. [source,kotlin,role="secondary"]
  70. ----
  71. private fun addManager(
  72. authenticationManagers: MutableMap<String, ReactiveAuthenticationManager>, issuer: String): Mono<JwtReactiveAuthenticationManager> {
  73. return Mono.fromCallable { ReactiveJwtDecoders.fromIssuerLocation(issuer) }
  74. .subscribeOn(Schedulers.boundedElastic())
  75. .map { jwtDecoder: ReactiveJwtDecoder -> JwtReactiveAuthenticationManager(jwtDecoder) }
  76. .doOnNext { authenticationManager: JwtReactiveAuthenticationManager -> authenticationManagers[issuer] = authenticationManager }
  77. }
  78. // ...
  79. var customAuthenticationManagerResolver = JwtIssuerReactiveAuthenticationManagerResolver(authenticationManagers::get)
  80. return http {
  81. authorizeExchange {
  82. authorize(anyExchange, authenticated)
  83. }
  84. oauth2ResourceServer {
  85. authenticationManagerResolver = customAuthenticationManagerResolver
  86. }
  87. }
  88. ----
  89. ====
  90. In this case, you construct `JwtIssuerReactiveAuthenticationManagerResolver` with a strategy for obtaining the `ReactiveAuthenticationManager` given the issuer.
  91. This approach allows us to add and remove elements from the repository (shown as a `Map` in the snippet) at runtime.
  92. NOTE: It would be unsafe to simply take any issuer and construct an `ReactiveAuthenticationManager` from it.
  93. The issuer should be one that the code can verify from a trusted source like an allowed list of issuers.