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.client;
017
018import java.util.Collections;
019import java.util.Map;
020import java.util.stream.Collectors;
021
022import org.springframework.boot.autoconfigure.condition.ConditionMessage;
023import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
024import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
025import org.springframework.boot.context.properties.bind.Bindable;
026import org.springframework.boot.context.properties.bind.Binder;
027import org.springframework.context.annotation.ConditionContext;
028import org.springframework.core.env.Environment;
029import org.springframework.core.type.AnnotatedTypeMetadata;
030
031/**
032 * Condition that matches if any {@code spring.security.oauth2.client.registration}
033 * properties are defined.
034 *
035 * @author Madhura Bhave
036 * @since 2.1.0
037 */
038
039public class ClientsConfiguredCondition extends SpringBootCondition {
040
041        private static final Bindable<Map<String, OAuth2ClientProperties.Registration>> STRING_REGISTRATION_MAP = Bindable
042                        .mapOf(String.class, OAuth2ClientProperties.Registration.class);
043
044        @Override
045        public ConditionOutcome getMatchOutcome(ConditionContext context,
046                        AnnotatedTypeMetadata metadata) {
047                ConditionMessage.Builder message = ConditionMessage
048                                .forCondition("OAuth2 Clients Configured Condition");
049                Map<String, OAuth2ClientProperties.Registration> registrations = getRegistrations(
050                                context.getEnvironment());
051                if (!registrations.isEmpty()) {
052                        return ConditionOutcome.match(message
053                                        .foundExactly("registered clients " + registrations.values().stream()
054                                                        .map(OAuth2ClientProperties.Registration::getClientId)
055                                                        .collect(Collectors.joining(", "))));
056                }
057                return ConditionOutcome.noMatch(message.notAvailable("registered clients"));
058        }
059
060        private Map<String, OAuth2ClientProperties.Registration> getRegistrations(
061                        Environment environment) {
062                return Binder.get(environment).bind("spring.security.oauth2.client.registration",
063                                STRING_REGISTRATION_MAP).orElse(Collections.emptyMap());
064        }
065
066}