/*
 * Copyright 2012-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.security.oauth2.oidc.core;

import org.springframework.security.oauth2.core.SecurityToken;
import org.springframework.util.Assert;

import java.time.Instant;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * An implementation of a {@link SecurityToken} representing an <i>OpenID Connect Core 1.0 ID Token</i>.
 *
 * <p>
 * The <code>IdToken</code> is a security token that contains &quot;Claims&quot;
 * about the authentication of an End-User by an Authorization Server.
 *
 * @author Joe Grandja
 * @since 5.0
 * @see SecurityToken
 * @see IdTokenClaimAccessor
 * @see StandardClaimAccessor
 * @see <a target="_blank" href="http://openid.net/specs/openid-connect-core-1_0.html#IDToken">ID Token</a>
 * @see <a target="_blank" href="http://openid.net/specs/openid-connect-core-1_0.html#StandardClaims">Standard Claims</a>
 */
public class IdToken extends SecurityToken implements IdTokenClaimAccessor {
	private final Map<String, Object> claims;

	public IdToken(String tokenValue, Instant issuedAt, Instant expiresAt, Map<String, Object> claims) {
		super(tokenValue, issuedAt, expiresAt);
		Assert.notEmpty(claims, "claims cannot be empty");
		this.claims = Collections.unmodifiableMap(new LinkedHashMap<>(this.sanitize(claims)));
	}

	@Override
	public Map<String, Object> getClaims() {
		return this.claims;
	}

	private Map<String, Object> sanitize(Map<String, Object> claims) {
		// NOTE:
		// Google's OpenID Connect implementation issues ID Tokens
		// that omit the required https:// scheme prefix from the iss claim.
		// This method will apply the required scheme prefix as a temporary workaround
		// until Google's OpenID Connect implementation is updated.
		// See http://openid.net/specs/openid-connect-core-1_0.html#GoogleIss

		String iss = (String)claims.get(IdTokenClaim.ISS);
		if (!iss.startsWith("https://")) {
			claims = new LinkedHashMap<>(claims);
			claims.put(IdTokenClaim.ISS, "https://" + iss);
		}
		return claims;
	}
}

@@ -1897,30 +1897,16 @@
 p%3C%3E(
-this.sanitize(
 claims))
 );%0A%09
@@ -1901,17 +1901,16 @@
 claims))
-)
 ;%0A%09%7D%0A%0A%09@
@@ -1988,641 +1988,7 @@
 ;%0A%09%7D
-%0A%0A%09private Map%3CString, Object%3E sanitize(Map%3CString, Object%3E claims) %7B%0A%09%09// NOTE:%0A%09%09// Google's OpenID Connect implementation issues ID Tokens%0A%09%09// that omit the required https:// scheme prefix from the iss claim.%0A%09%09// This method will apply the required scheme prefix as a temporary workaround%0A%09%09// until Google's OpenID Connect implementation is updated.%0A%09%09// See http://openid.net/specs/openid-connect-core-1_0.html#GoogleIss%0A%0A%09%09String iss = (String)claims.get(IdTokenClaim.ISS);%0A%09%09if (!iss.startsWith(%22https://%22)) %7B%0A%09%09%09claims = new LinkedHashMap%3C%3E(claims);%0A%09%09%09claims.put(IdTokenClaim.ISS, %22https://%22 + iss);%0A%09%09%7D%0A%09%09return claims;%0A%09%7D
 %0A%7D%0A

Revert "Provide fix for Google iss claim"