// Path: src/main/java/com/hillert/botanic/controller/dto/AuthenticationRequest.java
// public class AuthenticationRequest {
// 
// 	private String username;
// 	private String password;
// 
// 	public AuthenticationRequest() {
// 		super();
// 	}
// 
// 	public AuthenticationRequest(String username, String password) {
// 		this.username = username;
// 		this.password = password;
// 	}
// 
// 	/**
// 	 * @return the username
// 	 */
// 	public String getUsername() {
// 		return username;
// 	}
// 
// 	/**
// 	 * @param username the username to set
// 	 */
// 	public void setUsername(String username) {
// 		this.username = username;
// 	}
// 
// 	/**
// 	 * @return the password
// 	 */
// 	public String getPassword() {
// 		return password;
// 	}
// 
// 	/**
// 	 * @param password the password to set
// 	 */
// 	public void setPassword(String password) {
// 		this.password = password;
// 	}
// 
// }
// 
// Path: src/main/java/com/hillert/botanic/controller/dto/AuthenticationToken.java
// public class AuthenticationToken {
// 
// 	private String username;
// 	private Map<String, Boolean> roles;
// 	private String[] roles2;
// 
// 	public AuthenticationToken() {
// 		super();
// 	}
// 
// 	public AuthenticationToken(String username, List<String> roles) {
// 		Map<String, Boolean> mapOfRoles = new ConcurrentHashMap<String, Boolean>();
// 		for (String k : roles) {
// 			mapOfRoles.put(k, true);
// 		}
// 		this.roles = mapOfRoles;
// 		this.roles2 = roles.toArray(new String[roles.size()]);
// 		this.username = username;
// 	}
// 
// 	public Map<String, Boolean> getRoles() {
// 		return this.roles;
// 	}
// 
// 	public String[] getRoles2() {
// 		return this.roles2;
// 	}
// 
// 	/**
// 	 * @return the username
// 	 */
// 	public String getUsername() {
// 		return username;
// 	}
// 
// 	/**
// 	 * @param username the username to set
// 	 */
// 	public void setUsername(String username) {
// 		this.username = username;
// 	}
// 
// }


import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.hillert.botanic.controller.dto.AuthenticationRequest;
import com.hillert.botanic.controller.dto.AuthenticationToken;
/*
 * Copyright 2014 the original author or authors.
 *
 * 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 com.hillert.botanic.controller;





/**
 * This controller generates the {@link AuthenticationToken} that must be present
 * in subsequent REST invocations.
 *
 * @author Gunnar Hillert
 * @since 1.0
 *
 */
@RestController
public class AuthenticationController {

	private final AuthenticationManager authenticationManager;
	private final UserDetailsService userDetailsService;

	@Autowired
	public AuthenticationController(AuthenticationManager am, UserDetailsService userDetailsService) {
		this.authenticationManager = am;
		this.userDetailsService = userDetailsService;
	}

	@RequestMapping("/api/logout")
	@ResponseStatus(HttpStatus.NO_CONTENT)
	public void logout(HttpSession session) {
		session.invalidate();
	}

	@RequestMapping(value = "/api/info", method = { RequestMethod.GET })
	public AuthenticationToken info() {

		final String username = SecurityContextHolder.getContext().getAuthentication().getName();
		final UserDetails details = this.userDetailsService.loadUserByUsername(username);

		final List<String> roles = new ArrayList<>();

		for (GrantedAuthority authority : details.getAuthorities()) {
			roles.add(authority.toString());
		}

		return new AuthenticationToken(details.getUsername(), roles);
	}

	@RequestMapping(value = "/api/authenticate", method = { RequestMethod.POST })
	public AuthenticationToken authorize(
// Path: src/main/java/com/hillert/botanic/controller/dto/AuthenticationRequest.java
// public class AuthenticationRequest {
// 
// 	private String username;
// 	private String password;
// 
// 	public AuthenticationRequest() {
// 		super();
// 	}
// 
// 	public AuthenticationRequest(String username, String password) {
// 		this.username = username;
// 		this.password = password;
// 	}
// 
// 	/**
// 	 * @return the username
// 	 */
// 	public String getUsername() {
// 		return username;
// 	}
// 
// 	/**
// 	 * @param username the username to set
// 	 */
// 	public void setUsername(String username) {
// 		this.username = username;
// 	}
// 
// 	/**
// 	 * @return the password
// 	 */
// 	public String getPassword() {
// 		return password;
// 	}
// 
// 	/**
// 	 * @param password the password to set
// 	 */
// 	public void setPassword(String password) {
// 		this.password = password;
// 	}
// 
// }
// 
// Path: src/main/java/com/hillert/botanic/controller/dto/AuthenticationToken.java
// public class AuthenticationToken {
// 
// 	private String username;
// 	private Map<String, Boolean> roles;
// 	private String[] roles2;
// 
// 	public AuthenticationToken() {
// 		super();
// 	}
// 
// 	public AuthenticationToken(String username, List<String> roles) {
// 		Map<String, Boolean> mapOfRoles = new ConcurrentHashMap<String, Boolean>();
// 		for (String k : roles) {
// 			mapOfRoles.put(k, true);
// 		}
// 		this.roles = mapOfRoles;
// 		this.roles2 = roles.toArray(new String[roles.size()]);
// 		this.username = username;
// 	}
// 
// 	public Map<String, Boolean> getRoles() {
// 		return this.roles;
// 	}
// 
// 	public String[] getRoles2() {
// 		return this.roles2;
// 	}
// 
// 	/**
// 	 * @return the username
// 	 */
// 	public String getUsername() {
// 		return username;
// 	}
// 
// 	/**
// 	 * @param username the username to set
// 	 */
// 	public void setUsername(String username) {
// 		this.username = username;
// 	}
// 
// }


// Path: src/main/java/com/hillert/botanic/controller/AuthenticationController.java
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.hillert.botanic.controller.dto.AuthenticationRequest;
import com.hillert.botanic.controller.dto.AuthenticationToken;

/*
 * Copyright 2014 the original author or authors.
 *
 * 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 com.hillert.botanic.controller;





/**
 * This controller generates the {@link AuthenticationToken} that must be present
 * in subsequent REST invocations.
 *
 * @author Gunnar Hillert
 * @since 1.0
 *
 */
@RestController
public class AuthenticationController {

	private final AuthenticationManager authenticationManager;
	private final UserDetailsService userDetailsService;

	@Autowired
	public AuthenticationController(AuthenticationManager am, UserDetailsService userDetailsService) {
		this.authenticationManager = am;
		this.userDetailsService = userDetailsService;
	}

	@RequestMapping("/api/logout")
	@ResponseStatus(HttpStatus.NO_CONTENT)
	public void logout(HttpSession session) {
		session.invalidate();
	}

	@RequestMapping(value = "/api/info", method = { RequestMethod.GET })
	public AuthenticationToken info() {

		final String username = SecurityContextHolder.getContext().getAuthentication().getName();
		final UserDetails details = this.userDetailsService.loadUserByUsername(username);

		final List<String> roles = new ArrayList<>();

		for (GrantedAuthority authority : details.getAuthorities()) {
			roles.add(authority.toString());
		}

		return new AuthenticationToken(details.getUsername(), roles);
	}

	@RequestMapping(value = "/api/authenticate", method = { RequestMethod.POST })
	public AuthenticationToken authorize(
			@RequestBody AuthenticationRequest authenticationRequest,