Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public interface CacheProvider {

<V> Cache<V> createApiKeyCache();

<V> Cache<V> createUserDetailsAuthzCache();

<V> Cache<V> createTeAttributesCache();

<V> Cache<V> createProgramTeAttributesCache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@ public enum Region {
dataIntegrityDetailsCache,
queryAliasCache,
corsWhitelistCache,
notificationTemplateCache
notificationTemplateCache,
userDetailsAuthzCache
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2004-2026, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.user.authz;

/**
* Session attribute names used by UserDetails soft-refresh.
*
* @author Morten Svanæs
*/
public final class AuthzConstants {
/** HttpSession attribute: effective authz generation stamped at last soft-refresh check. */
public static final String SESSION_AUTHZ_GEN_ATTR = "DHIS2_AUTHZ_GEN";

/** HttpSession attribute: global authz epoch at last soft-refresh check. */
public static final String SESSION_AUTHZ_EPOCH_ATTR = "DHIS2_AUTHZ_EPOCH";

private AuthzConstants() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2004-2026, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.user.authz;

import java.util.Collection;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.hisp.dhis.user.UserDetails;

/**
* Soft-refresh facade over authz generation stamps and cached UserDetails snapshots.
*
* @author Morten Svanæs
*/
public interface AuthzService {
long currentEpoch();

/** One batched read: max of the principal's user-uid gen and role gens. */
long effectiveGen(@Nonnull UserDetails principal);

/**
* Epoch-validated, cached, immutable snapshot for username. Returns a snapshot that reflects
* every authz change committed up to the epoch value read at call entry. Null if user unknown.
*/
@CheckForNull
UserDetails getFreshUserDetails(@Nonnull String username);

void bumpUserAuthz(@Nonnull String userUid);

void bumpRoleAuthz(@Nonnull String roleUid);

void bumpUsers(@Nonnull Collection<String> userUids);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (c) 2004-2026, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.user.authz;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.Table;
import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType;
import java.io.Serializable;
import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Generation-stamp row for UserDetails soft-refresh.
*
* <p>Exists solely so Hibernate schema tooling knows the table (H2 test contexts generate schema
* from mappings; production schema is Flyway-managed and validated at startup). All runtime access
* goes through JdbcAuthzVersionStore, never ORM.
*
* @author Morten Svanæs
*/
@Entity
@Table(name = "authz_version")
@IdClass(AuthzVersion.AuthzVersionId.class)
public class AuthzVersion {

@Id
@Column(name = "scope", length = 16, nullable = false)
private String scope;

@Id
@Column(name = "key_name", length = 255, nullable = false)
private String keyName;

@Column(name = "gen", nullable = false)
private long gen;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_at", nullable = false)
private Date updatedAt;

protected AuthzVersion() {}

public String getScope() {
return scope;
}

public void setScope(String scope) {
this.scope = scope;
}

public String getKeyName() {
return keyName;
}

public void setKeyName(String keyName) {
this.keyName = keyName;
}

public long getGen() {
return gen;
}

public void setGen(long gen) {
this.gen = gen;
}

public Date getUpdatedAt() {
return updatedAt;
}

public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public static class AuthzVersionId implements Serializable {
private String scope;
private String keyName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2004-2026, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.user.authz;

import java.util.Collection;
import javax.annotation.Nonnull;

/**
* Generation stamps that drive UserDetails soft-refresh.
*
* <p>Every bump advances the global epoch. Bumps participate in the caller's ambient transaction
* (JdbcTemplate joins it), so a reader can never observe a gen/epoch value without also seeing the
* committed data that caused it.
*
* @author Morten Svanæs
*/
public interface AuthzVersionStore {
/** Global epoch; advanced by every bump. Missing row = 0. */
long getEpoch();

/** max(user gen for userUid, role gens for roleUids); missing keys count as 0. */
long getMaxGen(@Nonnull String userUid, @Nonnull Collection<String> roleUids);

void bumpUserGen(@Nonnull String userUid);

void bumpRoleGen(@Nonnull String roleUid);

/** Bumps each distinct uid once and the epoch once. */
void bumpUserGens(@Nonnull Collection<String> userUids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.hisp.dhis.security.oidc.DhisOidcProviderRepository;
import org.hisp.dhis.user.UserDetails;
import org.hisp.dhis.user.UserService;
import org.hisp.dhis.user.authz.AuthzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.security.authentication.AuthenticationManager;
Expand Down Expand Up @@ -109,6 +110,7 @@ public class Dhis2JwtAuthenticationManagerResolver
@Autowired private DhisOidcProviderRepository clientRegistrationRepository;
@Autowired private Dhis2OAuth2ClientService oAuth2ClientService;
@Autowired private UserService userService;
@Autowired private AuthzService authzService;

private final Map<String, AuthenticationManager> authenticationManagers =
new ConcurrentHashMap<>();
Expand Down Expand Up @@ -218,7 +220,7 @@ private Converter<Jwt, DhisJwtAuthenticationToken> getTokenConverter(
String mappingValue = jwt.getClaim(mappingClaimKey);
UserDetails currentUserDetails =
switch (mappingClaimKey) {
case "username" -> userService.createUserDetailsByUsername(mappingValue);
case "username" -> authzService.getFreshUserDetails(mappingValue);
case "email" -> userService.createUserDetailsByOpenId(mappingValue);
default -> throw new InvalidBearerTokenException("Invalid mapping claim");
};
Expand Down
Loading
Loading