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.List;
022import java.util.Map;
023
024import com.nimbusds.jwt.JWTClaimsSet;
025
026
027/**
028 * JSON Web Token (JWT) claims set utilities.
029 */
030public final class JWTClaimsSetUtils {
031        
032        
033        /**
034         * Creates a JWT claims set from the specified multi-valued parameters.
035         * Single-valued parameters are mapped to a string claim. Multi-valued
036         * parameters are mapped to a string array claim.
037         *
038         * @param params The multi-valued parameters. Must not be {@code null}.
039         *
040         * @return The JWT claims set.
041         */
042        public static JWTClaimsSet toJWTClaimsSet(final Map<String, List<String>> params) {
043                
044                JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder();
045                
046                for (Map.Entry<String, List<String>> en: params.entrySet()) {
047                        
048                        if (en.getValue().size() == 1) {
049                                
050                                String singleValue = en.getValue().get(0);
051                                builder.claim(en.getKey(), singleValue);
052                                
053                        } else if (en.getValue().size() > 0) {
054                                
055                                List<String> multiValue = en.getValue();
056                                builder.claim(en.getKey(), multiValue);
057                        }
058                }
059                
060                return builder.build();
061        }
062        
063        
064        /**
065         * Prevents public instantiation.
066         */
067        private JWTClaimsSetUtils() {}
068}