/*******************************************************************************
 * Copyright 2012-2013 Trento RISE
 * 
 *    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 it.smartcommunitylab.aac.authority;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import it.smartcommunitylab.aac.Config;
import it.smartcommunitylab.aac.jaxbmodel.AuthorityMapping;
import it.smartcommunitylab.aac.model.Registration;
import it.smartcommunitylab.aac.repository.RegistrationRepository;

/**
 * Extract user attributes for the user identified by the request parameter 'token'. 
 * 
 * The token is checked to belong to the specified Google clientIds
 * provided as constructor argument for the bean (comma-separated list of client IDs)
 * 
 * The user attributes are extracted from the google userinfo API.
 * 
 * @author raman
 *
 */
public class InternalAuthorityHandler implements AuthorityHandler {

	@Autowired
	private RegistrationRepository repository;

	@Override
	public Map<String, String> extractAttributes(HttpServletRequest request, Map<String,String> map, AuthorityMapping mapping) {
		String email = null;
		if (request != null) email = request.getParameter("email");
		if (email == null) email = map.get("email");
		
		Registration user = repository.findByEmail(email);
		if (user == null) {
			throw new SecurityException("Error finding internal user " +email);
		}
		Map<String, String> result = new HashMap<String, String>();
		result.put("email", user.getEmail());
		result.put(Config.NAME_ATTR, user.getName());
		result.put(Config.SURNAME_ATTR, user.getSurname());
		
		return result;
	}
}

@@ -1993,26 +1993,24 @@
 (%22email%22);%0D%0A
-%09%09
 %0D%0A%09%09Registra
@@ -2083,82 +2083,34 @@
 %0A%09%09%09
-throw new SecurityException(%22Error finding internal user %22 +email);%0D%0A%09%09%7D
+return map;%0D%0A%09%09%7D else %7B
 %0D%0A
+%09
 %09%09Ma
@@ -2166,24 +2166,25 @@
 ring%3E();%0D%0A%09%09
+%09
 result.put(%22
@@ -2206,24 +2206,25 @@
 tEmail());%0D%0A
+%09
 %09%09result.put
@@ -2260,16 +2260,17 @@
 me());%0D%0A
+%09
 %09%09result
@@ -2318,19 +2318,16 @@
 ());%0D%0A%09%09
-%0D%0A%09
 %09return 
@@ -2335,15 +2335,24 @@
 esult;%0D%0A
+%09%09%7D%0D%0A%09%09%0D%0A
 %09%7D%0D%0A%7D%0D%0A

Fix for registration new providers