001/******************************************************************************* 002 * Copyright 2017 The MIT Internet Trust Consortium 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 *******************************************************************************/ 016 017package org.mitre.jwt.assertion.impl; 018 019import java.text.ParseException; 020 021import org.mitre.jwt.assertion.AssertionValidator; 022import org.mitre.jwt.signer.service.JWTSigningAndValidationService; 023import org.mitre.openid.connect.config.ConfigurationPropertiesBean; 024import org.slf4j.Logger; 025import org.slf4j.LoggerFactory; 026import org.springframework.beans.factory.annotation.Autowired; 027import org.springframework.stereotype.Component; 028 029import com.google.common.base.Strings; 030import com.nimbusds.jwt.JWT; 031import com.nimbusds.jwt.JWTClaimsSet; 032import com.nimbusds.jwt.SignedJWT; 033 034/** 035 * Validates all assertions generated by this server 036 * 037 * @author jricher 038 * 039 */ 040@Component("selfAssertionValidator") 041public class SelfAssertionValidator implements AssertionValidator { 042 043 private static Logger logger = LoggerFactory.getLogger(SelfAssertionValidator.class); 044 045 @Autowired 046 private ConfigurationPropertiesBean config; 047 048 @Autowired 049 private JWTSigningAndValidationService jwtService; 050 051 @Override 052 public boolean isValid(JWT assertion) { 053 if (!(assertion instanceof SignedJWT)) { 054 // unsigned assertion 055 return false; 056 } 057 058 JWTClaimsSet claims; 059 try { 060 claims = assertion.getJWTClaimsSet(); 061 } catch (ParseException e) { 062 logger.debug("Invalid assertion claims"); 063 return false; 064 } 065 066 // make sure the issuer exists 067 if (Strings.isNullOrEmpty(claims.getIssuer())) { 068 logger.debug("No issuer for assertion, rejecting"); 069 return false; 070 } 071 072 // make sure the issuer is us 073 if (!claims.getIssuer().equals(config.getIssuer())) { 074 logger.debug("Issuer is not the same as this server, rejecting"); 075 return false; 076 } 077 078 // validate the signature based on our public key 079 if (jwtService.validateSignature((SignedJWT) assertion)) { 080 return true; 081 } else { 082 return false; 083 } 084 085 } 086 087}