001/*
002 * Copyright 2012-2018 the original author or authors.
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 */
016package org.springframework.boot.autoconfigure.security.oauth2.resource;
017
018import org.springframework.boot.autoconfigure.condition.ConditionMessage;
019import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
020import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
021import org.springframework.context.annotation.ConditionContext;
022import org.springframework.core.env.Environment;
023import org.springframework.core.type.AnnotatedTypeMetadata;
024import org.springframework.security.oauth2.jwt.JwtDecoder;
025import org.springframework.util.StringUtils;
026
027/**
028 * Condition for creating {@link JwtDecoder} by oidc issuer location.
029 *
030 * @author Artsiom Yudovin
031 * @since 2.1.0
032 */
033public class IssuerUriCondition extends SpringBootCondition {
034
035        @Override
036        public ConditionOutcome getMatchOutcome(ConditionContext context,
037                        AnnotatedTypeMetadata metadata) {
038                ConditionMessage.Builder message = ConditionMessage
039                                .forCondition("OpenID Connect Issuer URI Condition");
040                Environment environment = context.getEnvironment();
041                String issuerUri = environment
042                                .getProperty("spring.security.oauth2.resourceserver.jwt.issuer-uri");
043                String jwkSetUri = environment
044                                .getProperty("spring.security.oauth2.resourceserver.jwt.jwk-set-uri");
045                if (!StringUtils.hasText(issuerUri)) {
046                        return ConditionOutcome
047                                        .noMatch(message.didNotFind("issuer-uri property").atAll());
048                }
049                if (StringUtils.hasText(jwkSetUri)) {
050                        return ConditionOutcome
051                                        .noMatch(message.found("jwk-set-uri property").items(jwkSetUri));
052                }
053                return ConditionOutcome.match(message.foundExactly("issuer-uri property"));
054        }
055
056}