HtmlController.java

/*
 * Copyright 2019 Global Crop Diversity Trust
 *
 * 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.gringlobal.mvc;

import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

import org.gringlobal.util.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ResolvableType;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * Basic MVC controller handling index page and other html URLs.
 *
 * @author Matija Obreza
 */
@Controller
public class HtmlController extends AbstractMvcController {

	@Value("${base.url}")
	private String baseUrl;

	@Value("${build.name}")
	private String buildName;

	@Value("${build.version}")
	private String buildVersion;

	@Value("${allow.local.login:true}") 
	private boolean localLoginEnabled;

	private static String authorizationRequestBaseUri = "oauth2/authorization";

	@Autowired
	private ClientRegistrationRepository clientRegistrationRepository;

	@GetMapping({ "/" })
	public String welcome(final ModelMap model) throws MalformedURLException {
		model.addAttribute("baseUrl", UrlUtils.toCleanUrl(baseUrl));
		model.addAttribute("buildName", buildName);
		model.addAttribute("buildVersion", buildVersion);
		return "/index";
	}

	@GetMapping({ "/login" })
	public String login(final ModelMap model) {
		Map<String, String> oauth2AuthenticationUrls = new HashMap<>();

		Iterable<ClientRegistration> clientRegistrations = null;
		ResolvableType type = ResolvableType.forInstance(clientRegistrationRepository).as(Iterable.class);
		if (type != ResolvableType.NONE &&
			ClientRegistration.class.isAssignableFrom(type.resolveGenerics()[0])) {
			clientRegistrations = (Iterable<ClientRegistration>) clientRegistrationRepository;
			clientRegistrations.forEach(registration -> {
				// Add all but self!
				if (! registration.getRegistrationId().equals("local")) {
					oauth2AuthenticationUrls.put(
						registration.getClientName(),
						authorizationRequestBaseUri + "/" + registration.getRegistrationId()
					);
				}
			});
		}

		model.addAttribute("urls", oauth2AuthenticationUrls);
		model.addAttribute("localLoginEnabled", localLoginEnabled);
		return "/login";
	}

}