|
| 1 | +/* |
| 2 | + * Copyright (c) 2004-2025, University of Oslo |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * 3. Neither the name of the copyright holder nor the names of its contributors |
| 16 | + * may be used to endorse or promote products derived from this software without |
| 17 | + * specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 23 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 26 | + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | +package org.hisp.dhis.security.oauth2.authorization; |
| 31 | + |
| 32 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 35 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 36 | + |
| 37 | +import java.security.Principal; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.HashSet; |
| 40 | +import java.util.LinkedHashMap; |
| 41 | +import java.util.List; |
| 42 | +import java.util.Map; |
| 43 | +import java.util.Set; |
| 44 | +import java.util.stream.Collectors; |
| 45 | +import org.hisp.dhis.security.oidc.DhisOidcUser; |
| 46 | +import org.hisp.dhis.user.UserDetailsImpl; |
| 47 | +import org.junit.jupiter.api.Test; |
| 48 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 49 | +import org.springframework.security.core.GrantedAuthority; |
| 50 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 51 | +import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; |
| 52 | +import org.springframework.security.oauth2.core.oidc.IdTokenClaimNames; |
| 53 | +import org.springframework.security.oauth2.core.oidc.OidcIdToken; |
| 54 | + |
| 55 | +/** |
| 56 | + * Unit tests for {@link Dhis2OAuth2AuthorizationServiceImpl#leanPrincipal} — the swap that keeps |
| 57 | + * the persisted OAuth2 authorization principal Spring-native (so it never trips the Jackson |
| 58 | + * allowlist). |
| 59 | + */ |
| 60 | +class LeanPrincipalTest { |
| 61 | + |
| 62 | + @Test |
| 63 | + void noPrincipalAttribute_isUnchanged() { |
| 64 | + Map<String, Object> attributes = new LinkedHashMap<>(); |
| 65 | + attributes.put("state", "xyz"); |
| 66 | + assertSame(attributes, Dhis2OAuth2AuthorizationServiceImpl.leanPrincipal(attributes)); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void nonUserDetailsPrincipal_isUnchanged() { |
| 71 | + // client_credentials-style: an Authentication whose principal is the client id (a String). |
| 72 | + Map<String, Object> attributes = new LinkedHashMap<>(); |
| 73 | + attributes.put( |
| 74 | + Principal.class.getName(), |
| 75 | + new UsernamePasswordAuthenticationToken("client-id", null, List.of())); |
| 76 | + assertSame(attributes, Dhis2OAuth2AuthorizationServiceImpl.leanPrincipal(attributes)); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void formLoginUserDetailsImplPrincipal_isLeaned() { |
| 81 | + UserDetailsImpl userDetails = userDetails("formuser"); |
| 82 | + Map<String, Object> attributes = new LinkedHashMap<>(); |
| 83 | + attributes.put( |
| 84 | + Principal.class.getName(), |
| 85 | + new UsernamePasswordAuthenticationToken( |
| 86 | + userDetails, "secret", userDetails.getAuthorities())); |
| 87 | + |
| 88 | + assertLean(Dhis2OAuth2AuthorizationServiceImpl.leanPrincipal(attributes), "formuser"); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void federatedDhisOidcUserPrincipal_isLeaned() { |
| 93 | + UserDetailsImpl userDetails = userDetails("oidcuser"); |
| 94 | + Map<String, Object> oidcClaims = Map.of(IdTokenClaimNames.SUB, "google-sub-1"); |
| 95 | + OidcIdToken idToken = |
| 96 | + OidcIdToken.withTokenValue("id-token") |
| 97 | + .subject("google-sub-1") |
| 98 | + .claims(c -> c.putAll(oidcClaims)) |
| 99 | + .build(); |
| 100 | + DhisOidcUser oidcPrincipal = |
| 101 | + new DhisOidcUser(userDetails, oidcClaims, IdTokenClaimNames.SUB, idToken); |
| 102 | + Map<String, Object> attributes = new LinkedHashMap<>(); |
| 103 | + attributes.put( |
| 104 | + Principal.class.getName(), |
| 105 | + new OAuth2AuthenticationToken(oidcPrincipal, oidcPrincipal.getAuthorities(), "google")); |
| 106 | + |
| 107 | + // The DhisOidcUser's getName() is the IdP sub; the leaned principal must carry the DHIS2 |
| 108 | + // username. |
| 109 | + assertLean(Dhis2OAuth2AuthorizationServiceImpl.leanPrincipal(attributes), "oidcuser"); |
| 110 | + } |
| 111 | + |
| 112 | + private static void assertLean(Map<String, Object> result, String expectedUsername) { |
| 113 | + Object principal = result.get(Principal.class.getName()); |
| 114 | + assertInstanceOf(UsernamePasswordAuthenticationToken.class, principal); |
| 115 | + UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) principal; |
| 116 | + assertEquals(expectedUsername, token.getName()); |
| 117 | + assertTrue(token.isAuthenticated()); |
| 118 | + assertEquals( |
| 119 | + Set.of("ALL"), |
| 120 | + token.getAuthorities().stream() |
| 121 | + .map(GrantedAuthority::getAuthority) |
| 122 | + .collect(Collectors.toSet())); |
| 123 | + } |
| 124 | + |
| 125 | + private static UserDetailsImpl userDetails(String username) { |
| 126 | + return UserDetailsImpl.builder() |
| 127 | + .uid("uid-" + username) |
| 128 | + .username(username) |
| 129 | + .authorities(new ArrayList<>(List.of(new SimpleGrantedAuthority("ALL")))) |
| 130 | + .allAuthorities(new HashSet<>(Set.of("ALL"))) |
| 131 | + .allRestrictions(new HashSet<>()) |
| 132 | + .userGroupIds(new HashSet<>()) |
| 133 | + .userOrgUnitIds(new HashSet<>()) |
| 134 | + .userDataOrgUnitIds(new HashSet<>()) |
| 135 | + .userSearchOrgUnitIds(new HashSet<>()) |
| 136 | + .userEffectiveSearchOrgUnitIds(new HashSet<>()) |
| 137 | + .userRoleIds(new HashSet<>()) |
| 138 | + .managedGroupLongIds(new HashSet<>()) |
| 139 | + .userRoleLongIds(new HashSet<>()) |
| 140 | + .build(); |
| 141 | + } |
| 142 | +} |
0 commit comments