001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.oauth2.sdk.util;
019
020
021import java.util.Collections;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import com.nimbusds.jwt.JWTClaimsSet;
027
028
029/**
030 * JSON Web Token (JWT) claims set utilities.
031 */
032public final class JWTClaimsSetUtils {
033        
034        
035        /**
036         * Creates a JWT claims set from the specified multi-valued parameters.
037         * Single-valued parameters are mapped to a string claim. Multi-valued
038         * parameters are mapped to a string array claim.
039         *
040         * @param params The multi-valued parameters. Must not be {@code null}.
041         *
042         * @return The JWT claims set.
043         */
044        public static JWTClaimsSet toJWTClaimsSet(final Map<String, List<String>> params) {
045                
046                JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder();
047                
048                for (Map.Entry<String, List<String>> en: params.entrySet()) {
049                        
050                        if (en.getValue().size() == 1) {
051                                
052                                String singleValue = en.getValue().get(0);
053                                builder.claim(en.getKey(), singleValue);
054                                
055                        } else if (en.getValue().size() > 0) {
056                                
057                                List<String> multiValue = en.getValue();
058                                builder.claim(en.getKey(), multiValue);
059                        }
060                }
061                
062                return builder.build();
063        }
064        
065        
066        /**
067         * Creates a multi-valued string parameters map from the specified JWT
068         * claims set. {@link JWTClaimsSet#getRegisteredNames() registered JWT
069         * claims} and {@code null} valued claims are not included in the
070         * returned parameters.
071         *
072         * @param claimsSet The JWT claims set. Must not be {@code null}.
073         *
074         * @return The string parameters map.
075         */
076        public static Map<String,List<String>> toMultiValuedParameters(final JWTClaimsSet claimsSet) {
077                
078                Map<String,List<String>> params = new HashMap<>();
079                
080                for (Map.Entry<String,Object> entry: claimsSet.toJSONObject().entrySet()) {
081                        
082                        if (JWTClaimsSet.getRegisteredNames().contains(entry.getKey()))
083                                continue; // skip sub, aud, iat, etc...
084                        
085                        if (entry.getValue() == null)
086                                continue; // skip null value
087                        
088                        params.put(entry.getKey(), Collections.singletonList(entry.getValue().toString()));
089                }
090                
091                return params;
092        }
093        
094        
095        /**
096         * Prevents public instantiation.
097         */
098        private JWTClaimsSetUtils() {}
099}