001package com.nimbusds.openid.connect.provider.spi.claims;
002
003
004import java.util.HashSet;
005import java.util.List;
006import java.util.Set;
007
008import org.checkerframework.checker.nullness.qual.Nullable;
009
010import com.nimbusds.langtag.LangTag;
011
012
013/**
014 * Claim utilities.
015 */
016public class ClaimUtils {
017
018
019        /**
020         * Applies the specified language tags to a set of claims.
021         *
022         * <p>Example:
023         *
024         * <pre>
025         * Claims: "name", "given_name", "family_name"
026         * Language tags: "bg-BG", "en-US"
027         * Result: "name#bg-BG", "name#en-US",
028         *         "given_name#bg-BG", "given_name#en-US",
029         *         "family_name#bg-BG", "family_name#en-US"
030         * </pre>
031         *
032         * @param claims   The claims to apply the language tags to. Claims
033         *                 that already have a language will be returned
034         *                 unmodified. Must not be {@code null}.
035         * @param langTags The language tags to apply, {@code null} if not
036         *                 specified.
037         *
038         * @return The claims with applied language tags, or the original
039         *         claims if no language tags are specified.
040         */
041        public static Set<String> applyLangTags(final Set<String> claims, final @Nullable List<LangTag> langTags) {
042
043                if (langTags == null || langTags.isEmpty())
044                        return claims;
045
046                Set<String> out = new HashSet<>();
047
048                for (String claim: claims) {
049
050                        for (LangTag tag: langTags) {
051
052                                if (claim.contains("#")) {
053                                        out.add(claim);
054                                } else {
055                                        out.add(claim + "#" + tag);
056                                }
057                        }
058                }
059
060                return out;
061        }
062
063        /**
064         * Prevents public instantiation.
065         */
066        private ClaimUtils() {}
067}