/*
 * @(#)CrowdAuthenticationToken.java
 *
 * The MIT License
 *
 * Copyright (C)2011 Thorsten Heit.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package de.theit.jenkins.crowd;

import java.util.List;

import jenkins.model.Jenkins;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.providers.AbstractAuthenticationToken;
import org.acegisecurity.userdetails.UserDetails;
import org.apache.commons.lang.StringUtils;

/**
 * This class represents an authentication token that is created after a user
 * was successfully authenticated against the remote Crowd server.
 *
 * @author <a href="mailto:theit@gmx.de">Thorsten Heit (theit@gmx.de)</a>
 * @since 07.09.2011
 * @version $Id$
 */
public class CrowdAuthenticationToken extends AbstractAuthenticationToken {
    /** For serialization. */
    private static final long serialVersionUID = 7685110934682676618L;

    /** The SSO token. */
    private String credentials;

    /** The authenticated Crowd user. */
    private UserDetails principal;

    /** The Crowd SSO token after a successful login. */
    private String ssoToken;

    /**
     * Creates a new authorization token.
     *
     * @param pPrincipal   The name of the authenticated Crowd user. May not be
     *                     <code>null</code>.
     * @param pCredentials The credentials. Normally the users password. May only be
     *                     <code>null</code> when the SSO token is given.
     * @param authorities  The list of granted authorities for the user. May not be
     *                     <code>null</code>.
     * @param pSsoToken    The Crowd SSO token. May be <code>null</code> if the
     *                     token is not (yet) available.
     */
    public CrowdAuthenticationToken(String pPrincipal, String pCredentials,
            List<GrantedAuthority> authorities, String pSsoToken) {
        super(authorities.toArray(new GrantedAuthority[authorities.size()]));
        this.principal = Jenkins.get().getSecurityRealm().loadUserByUsername(pPrincipal);
        this.credentials = pCredentials;
        this.ssoToken = pSsoToken;
        super.setAuthenticated(true);
    }

    /**
     * {@inheritDoc}
     *
     * @see org.acegisecurity.Authentication#getCredentials()
     */
    @Override
    public String getCredentials() {
        return this.credentials;
    }

    /**
     * {@inheritDoc}
     *
     * @see org.acegisecurity.Authentication#getPrincipal()
     */
    @Override
    public UserDetails getPrincipal() {
        return this.principal;
    }

    /**
     * Returns the SSO token.
     *
     * @return The SSO token. <code>null</code> if the token is not (yet)
     *         available.
     */
    public String getSSOToken() {
        return this.ssoToken;
    }

    /**
     * {@inheritDoc}
     *
     * @see org.acegisecurity.providers.AbstractAuthenticationToken#getName()
     */
    @Override
    public String getName() {
        return super.getName();
    }

    public static void updateUserInfo(com.atlassian.crowd.model.user.User user) {
        final String displayName = user == null ? null : user.getDisplayName();
        if (StringUtils.isNotBlank(displayName)) {
            final String username = user.getName();
            getJenkinsUser(username).setFullName(displayName + " (" + username + ')');
        }
    }

    /**
     * Gets the corresponding {@link hudson.model.User} object.
     */
    private static hudson.model.User getJenkinsUser(String username) {
        return hudson.model.User.get(username);
    }
}

@@ -1263,37 +1263,48 @@
 ins;%0Aimport org.
-acegi
+springframework.
 security.Granted
@@ -1296,16 +1296,21 @@
 ecurity.
+core.
 GrantedA
@@ -1330,39 +1330,55 @@
 ort org.
-acegisecurity.providers
+springframework.security.authentication
 .Abstrac
@@ -1406,29 +1406,40 @@
 %0Aimport org.
-acegi
+springframework.
 security.use
@@ -1435,16 +1435,21 @@
 ecurity.
+core.
 userdeta
@@ -2969,58 +2969,8 @@
 ties
-.toArray(new GrantedAuthority%5Bauthorities.size()%5D)
 );%0A 
@@ -3044,16 +3044,17 @@
 Username
+2
 (pPrinci
@@ -3224,37 +3224,48 @@
      * @see org.
-acegi
+springframework.
 security.Authent
@@ -3249,32 +3249,37 @@
 mework.security.
+core.
 Authentication#g
@@ -3437,37 +3437,48 @@
      * @see org.
-acegi
+springframework.
 security.Authent
@@ -3470,16 +3470,21 @@
 ecurity.
+core.
 Authenti
@@ -3885,31 +3885,47 @@
 org.
-acegisecurity.providers
+springframework.security.authentication
 .Abs
@@ -4606,16 +4606,20 @@
 User.get
+ById
 (usernam
@@ -4619,16 +4619,23 @@
 username
+, false
 );%0A    %7D

Remove deprecations from token lib