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.jose.util.JSONObjectUtils; 027import com.nimbusds.jwt.JWTClaimsSet; 028 029 030/** 031 * JSON Web Token (JWT) claims set utilities. 032 */ 033public final class JWTClaimsSetUtils { 034 035 036 /** 037 * Creates a JWT claims set from the specified multi-valued parameters. 038 * Single-valued parameters are mapped to a string claim. Multi-valued 039 * parameters are mapped to a string array claim. 040 * 041 * @param params The multi-valued parameters. Must not be {@code null}. 042 * 043 * @return The JWT claims set. 044 */ 045 public static JWTClaimsSet toJWTClaimsSet(final Map<String, List<String>> params) { 046 047 JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder(); 048 049 for (Map.Entry<String, List<String>> en: params.entrySet()) { 050 051 if (en.getValue().size() == 1) { 052 053 String singleValue = en.getValue().get(0); 054 builder.claim(en.getKey(), singleValue); 055 056 } else if (en.getValue().size() > 0) { 057 058 List<String> multiValue = en.getValue(); 059 builder.claim(en.getKey(), multiValue); 060 } 061 } 062 063 return builder.build(); 064 } 065 066 067 /** 068 * Creates a multi-valued string parameters map from the specified JWT 069 * claims set. {@link JWTClaimsSet#getRegisteredNames() registered JWT 070 * claims} and {@code null} valued claims are not included in the 071 * returned parameters. 072 * 073 * @param claimsSet The JWT claims set. Must not be {@code null}. 074 * 075 * @return The string parameters map. 076 */ 077 public static Map<String,List<String>> toMultiValuedParameters(final JWTClaimsSet claimsSet) { 078 079 Map<String,List<String>> params = new HashMap<>(); 080 081 for (Map.Entry<String,Object> entry: claimsSet.toJSONObject().entrySet()) { 082 083 if (JWTClaimsSet.getRegisteredNames().contains(entry.getKey())) 084 continue; // skip sub, aud, iat, etc... 085 086 if (entry.getValue() == null) 087 continue; // skip null value 088 089 String value; 090 if (entry.getValue() instanceof Map) { 091 value = JSONObjectUtils.toJSONString((Map<String, ?>) entry.getValue()); 092 } else { 093 value = entry.getValue().toString(); 094 } 095 096 params.put(entry.getKey(), Collections.singletonList(value)); 097 } 098 099 return params; 100 } 101 102 103 /** 104 * Prevents public instantiation. 105 */ 106 private JWTClaimsSetUtils() {} 107}