1 package org.apache.archiva.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.AbstractAuthenticator;
23 import org.apache.archiva.redback.authentication.AuthenticationDataSource;
24 import org.apache.archiva.redback.authentication.AuthenticationException;
25 import org.apache.archiva.redback.authentication.AuthenticationResult;
26 import org.apache.archiva.redback.authentication.Authenticator;
27 import org.apache.archiva.redback.authentication.TokenBasedAuthenticationDataSource;
28 import org.apache.archiva.redback.keys.AuthenticationKey;
29 import org.apache.archiva.redback.keys.KeyManager;
30 import org.apache.archiva.redback.keys.KeyManagerException;
31 import org.apache.archiva.redback.keys.KeyNotFoundException;
32 import org.apache.archiva.redback.policy.AccountLockedException;
33 import org.apache.archiva.redback.policy.MustChangePasswordException;
34 import org.apache.archiva.redback.users.User;
35 import org.apache.archiva.redback.users.UserManager;
36 import org.apache.archiva.redback.users.UserManagerException;
37 import org.apache.archiva.redback.users.UserNotFoundException;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.stereotype.Service;
42 import javax.annotation.Resource;
45 * KeyStoreAuthenticator:
47 * @author: Jesse McConnell <jesse@codehaus.org>
49 @Service("authenticator#keystore")
50 public class KeyStoreAuthenticator
51 extends AbstractAuthenticator
52 implements Authenticator
54 private Logger log = LoggerFactory.getLogger( getClass() );
56 @Resource(name = "keyManager#cached")
57 private KeyManager keystore;
59 @Resource(name = "userManager#configurable")
60 private UserManager userManager;
64 return getClass().getName();
67 public AuthenticationResult authenticate( AuthenticationDataSource source )
68 throws AccountLockedException, AuthenticationException, MustChangePasswordException
70 TokenBasedAuthenticationDataSource dataSource = (TokenBasedAuthenticationDataSource) source;
72 String key = dataSource.getToken();
75 AuthenticationKey authKey = keystore.findKey( key );
77 // if we find a key (exception was probably thrown if not) then we should be authentic
78 if ( authKey != null )
80 User user = userManager.findUser( dataSource.getPrincipal() );
82 if ( user.isLocked() )
84 throw new AccountLockedException( "Account " + source.getPrincipal() + " is locked.", user );
87 if ( user.isPasswordChangeRequired() && source.isEnforcePasswordChange() )
89 throw new MustChangePasswordException( "Password expired.", user );
92 return new AuthenticationResult( true, dataSource.getPrincipal(), null );
96 return new AuthenticationResult( false, dataSource.getPrincipal(),
97 new AuthenticationException( "unable to find key" ) );
100 catch ( KeyNotFoundException ne )
102 return new AuthenticationResult( false, null, ne );
104 catch ( KeyManagerException ke )
106 throw new AuthenticationException( "underlaying keymanager issue", ke );
108 catch ( UserNotFoundException e )
110 log.warn( "Login for user {} failed. user not found.", source.getPrincipal() );
111 return new AuthenticationResult( false, null, e );
113 catch ( UserManagerException e )
115 log.warn( "Login fail for user {} failed. message: {}", source.getPrincipal(), e.getMessage() );
116 return new AuthenticationResult( false, null, e );
120 public boolean supportsDataSource( AuthenticationDataSource source )
122 return source instanceof TokenBasedAuthenticationDataSource;