001package org.apache.archiva.redback.authentication;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.archiva.redback.users.User;
023
024import java.io.Serializable;
025import java.util.ArrayList;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029
030/**
031 * AuthenticationResult: wrapper object for information that comes back from the authentication system
032 *
033 * @author Jesse McConnell
034 */
035public class AuthenticationResult
036    implements Serializable
037{
038
039    private static final long serialVersionUID = 354054054054L;
040
041    private boolean isAuthenticated;
042
043    private String principal;
044
045    /**
046     * as we can search the User store it here for reuse.
047     *
048     * @since 2.1
049     */
050    private User user;
051
052    // TODO: why aren't these just thrown from the authenticate() method?
053    private Exception exception;
054
055    private List<AuthenticationFailureCause> authenticationFailureCauses;
056
057    public AuthenticationResult()
058    {
059        this.isAuthenticated = false;
060        this.principal = null;
061        this.exception = null;
062    }
063
064    public AuthenticationResult( boolean authenticated, String principal, Exception exception )
065    {
066        isAuthenticated = authenticated;
067        this.principal = principal;
068        this.exception = exception;
069    }
070
071    public AuthenticationResult( boolean authenticated, String principal, Exception exception,
072                                 List<AuthenticationFailureCause> authenticationFailureCauses )
073    {
074        isAuthenticated = authenticated;
075        this.principal = principal;
076        this.exception = exception;
077        this.authenticationFailureCauses = authenticationFailureCauses;
078    }
079
080    public boolean isAuthenticated()
081    {
082        return isAuthenticated;
083    }
084
085    public String getPrincipal()
086    {
087        return principal;
088    }
089
090    public Exception getException()
091    {
092        return exception;
093    }
094
095    public List<AuthenticationFailureCause> getAuthenticationFailureCauses()
096    {
097        if ( authenticationFailureCauses == null )
098        {
099            this.authenticationFailureCauses = new ArrayList<>( );
100        }
101        return authenticationFailureCauses;
102    }
103
104    /**
105     * <b>can be <code>null</code></b>
106     */
107    public User getUser()
108    {
109        return user;
110    }
111
112    public void setUser( User user )
113    {
114        this.user = user;
115    }
116
117    public AuthenticationResult user( User user )
118    {
119        this.setUser( user );
120        return this;
121    }
122
123    public String toString()
124    {
125        StringBuilder sb = new StringBuilder();
126        sb.append( "AuthenticationResult[" );
127        sb.append( "principal=" ).append( principal );
128        sb.append( ",isAuthenticated=" ).append( Boolean.toString( isAuthenticated ) );
129        sb.append( ",exception=" );
130        if ( exception != null )
131        {
132            sb.append( exception.getClass().getName() );
133            sb.append( " : " ).append( exception.getMessage() );
134        }
135        else
136        {
137            sb.append( "<null>" );
138        }
139        sb.append( ']' );
140        return sb.toString();
141    }
142}