/*
  Copyright 2012 - 2015 Jerome Leleu

   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.pac4j.springframework.security.authentication;

import java.util.Collection;

import org.pac4j.core.credentials.Credentials;
import org.pac4j.core.profile.UserProfile;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * This token represents a credentials: {@link #credentials}, a client name: {@link #clientName},
 * a user profile: {@link #userProfile} and an optional user (details): {@link #userDetails}.
 *
 * @author Jerome Leleu
 * @since 1.0.0
 */
public final class ClientAuthenticationToken extends AbstractAuthenticationToken {

    private static final long serialVersionUID = 8303047831754762526L;

    private final Credentials credentials;

    private UserProfile userProfile = null;

    private final String clientName;

    private final UserDetails userDetails;

    public ClientAuthenticationToken(final Credentials credentials, final String clientName) {
        super(null);
        this.credentials = credentials;
        this.clientName = clientName;
        this.userDetails = null;
        setAuthenticated(false);
    }

    public ClientAuthenticationToken(final Credentials credentials, final String clientName,
            final UserProfile userProfile,
            final Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.credentials = credentials;
        this.clientName = clientName;
        this.userProfile = userProfile;
        this.userDetails = null;
        setAuthenticated(true);
    }

    public ClientAuthenticationToken(final Credentials credentials, final String clientName,
            final UserProfile userProfile, final Collection<? extends GrantedAuthority> authorities,
            final UserDetails userDetails) {
        super(authorities);
        this.credentials = credentials;
        this.clientName = clientName;
        this.userProfile = userProfile;
        this.userDetails = userDetails;
        setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return this.credentials;
    }

    @Override
    public Object getPrincipal() {
        if (this.userProfile != null) {
            return this.userProfile.getTypedId();
        } else {
            return null;
        }
    }

    public UserProfile getUserProfile() {
        return this.userProfile;
    }

    public String getClientName() {
        return this.clientName;
    }

    public UserDetails getUserDetails() {
        return this.userDetails;
    }
}

@@ -2838,24 +2838,90 @@
 incipal() %7B%0A
+    %09if (this.userDetails != null)%0A    %09%09return this.userDetails;%0A
         if (

Return the UserDetails object as first preference.