/*
 * Copyright 2006-2011 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 org.springframework.security.oauth2.provider.authentication;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * A holder of selected HTTP details related to an OAuth2 authentication request.
 * 
 * @author Dave Syer
 * 
 */
public class OAuth2AuthenticationDetails implements Serializable {
	
	private static final long serialVersionUID = -4809832298438307309L;

	public static final String ACCESS_TOKEN_VALUE = OAuth2AuthenticationDetails.class.getSimpleName() + ".ACCESS_TOKEN_VALUE";

	private final String remoteAddress;

	private final String sessionId;

	private final String tokenValue;

	/**
	 * Records the access token value and remote address and will also set the session Id if a session already exists
	 * (it won't create one).
	 * 
	 * @param request that the authentication request was received from
	 */
	public OAuth2AuthenticationDetails(HttpServletRequest request) {
		this.tokenValue = (String) request.getAttribute(ACCESS_TOKEN_VALUE);
		this.remoteAddress = request.getRemoteAddr();

		HttpSession session = request.getSession(false);
		this.sessionId = (session != null) ? session.getId() : null;
	}

	/**
	 * The access token value used to authenticate the request (normally in an authorization header).
	 * 
	 * @return the tokenValue used to authenticate the request
	 */
	public String getTokenValue() {
		return tokenValue;
	}

	/**
	 * Indicates the TCP/IP address the authentication request was received from.
	 * 
	 * @return the address
	 */
	public String getRemoteAddress() {
		return remoteAddress;
	}

	/**
	 * Indicates the <code>HttpSession</code> id the authentication request was received from.
	 * 
	 * @return the session ID
	 */
	public String getSessionId() {
		return sessionId;
	}

}

@@ -1278,21 +1278,53 @@
 Value;%0A%0A
+%09private final String display;%0A%0A
 %09/**%0A
-
 %09 * Reco
@@ -1836,16 +1836,448 @@
 : null;%0A
+%09%09StringBuilder builder = new StringBuilder();%0A%09%09if (remoteAddress!=null) %7B%0A%09%09%09builder.append(%22remoteAddress=%22).append(remoteAddress);%0A%09%09%7D%0A%09%09if (builder.length()%3E1) %7B%0A%09%09%09builder.append(%22, %22);%0A%09%09%7D%0A%09%09if (sessionId!=null) %7B%0A%09%09%09builder.append(%22sessionId=%3CSESSION%3E%22);%0A%09%09%7D%0A%09%09if (builder.length()%3E1) %7B%0A%09%09%09builder.append(%22, %22);%0A%09%09%7D%0A%09%09if (tokenValue!=null) %7B%0A%09%09%09builder.append(%22tokenValue=%3CTOKEN%3E%22);%0A%09%09%7D%0A%09%09this.display = builder.toString();%0A
 %09%7D%0A%0A%09/**
@@ -2848,24 +2848,24 @@
 ssionId() %7B%0A
-
 %09%09return ses
@@ -2876,10 +2876,71 @@
 Id;%0A%09%7D%0A%0A
+%09@Override%0A%09public String toString() %7B%0A%09%09return display;%0A%09%7D%0A%0A
 %7D%0A

add toSTring() to auth details