1 package org.codehaus.plexus.redback.authentication.keystore;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.redback.authentication.AuthenticationDataSource;
23 import org.apache.archiva.redback.authentication.AuthenticationException;
24 import org.apache.archiva.redback.authentication.AuthenticationResult;
25 import org.apache.archiva.redback.authentication.Authenticator;
26 import org.apache.archiva.redback.authentication.TokenBasedAuthenticationDataSource;
27 import org.codehaus.plexus.redback.keys.AuthenticationKey;
28 import org.codehaus.plexus.redback.keys.KeyManager;
29 import org.codehaus.plexus.redback.keys.KeyManagerException;
30 import org.codehaus.plexus.redback.keys.KeyNotFoundException;
31 import org.codehaus.plexus.redback.policy.AccountLockedException;
32 import org.codehaus.plexus.redback.policy.MustChangePasswordException;
33 import org.apache.archiva.redback.users.User;
34 import org.apache.archiva.redback.users.UserManager;
35 import org.apache.archiva.redback.users.UserNotFoundException;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Service;
40 import javax.annotation.Resource;
43 * KeyStoreAuthenticator:
45 * @author: Jesse McConnell <jesse@codehaus.org>
48 @Service( "authenticator#keystore" )
49 public class KeyStoreAuthenticator
50 implements Authenticator
52 private Logger log = LoggerFactory.getLogger( getClass() );
54 @Resource( name = "keyManager#cached" )
55 private KeyManager keystore;
57 @Resource( name = "userManager#configurable" )
58 private UserManager userManager;
65 public AuthenticationResult authenticate( AuthenticationDataSource source )
66 throws AccountLockedException, AuthenticationException, MustChangePasswordException
68 TokenBasedAuthenticationDataSource dataSource = (TokenBasedAuthenticationDataSource) source;
70 String key = dataSource.getToken();
73 AuthenticationKey authKey = keystore.findKey( key );
75 // if we find a key (exception was probably thrown if not) then we should be authentic
76 if ( authKey != null )
78 User user = userManager.findUser( dataSource.getPrincipal() );
80 if ( user.isLocked() )
82 throw new AccountLockedException( "Account " + source.getPrincipal() + " is locked.", user );
85 if ( user.isPasswordChangeRequired() && source.isEnforcePasswordChange() )
87 throw new MustChangePasswordException( "Password expired.", user );
90 return new AuthenticationResult( true, dataSource.getPrincipal(), null );
94 return new AuthenticationResult( false, dataSource.getPrincipal(),
95 new AuthenticationException( "unable to find key" ) );
98 catch ( KeyNotFoundException ne )
100 return new AuthenticationResult( false, null, ne );
102 catch ( KeyManagerException ke )
104 throw new AuthenticationException( "underlaying keymanager issue", ke );
106 catch ( UserNotFoundException e )
108 log.warn( "Login for user " + source.getPrincipal() + " failed. user not found." );
109 return new AuthenticationResult( false, null, e );
113 public boolean supportsDataSource( AuthenticationDataSource source )
115 return source instanceof TokenBasedAuthenticationDataSource;