This project has retired. For details please refer to its
Attic page.
KeyStoreAuthenticator xref
1 package org.apache.archiva.redback.authentication.keystore;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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;
41
42 import javax.annotation.Resource;
43
44
45
46
47
48
49 @Service("authenticator#keystore")
50 public class KeyStoreAuthenticator
51 extends AbstractAuthenticator
52 implements Authenticator
53 {
54 private Logger log = LoggerFactory.getLogger( getClass() );
55
56 @Resource(name = "keyManager#cached")
57 private KeyManager keystore;
58
59 @Resource(name = "userManager#default")
60 private UserManager userManager;
61
62 public String getId()
63 {
64 return getClass().getName();
65 }
66
67 public AuthenticationResult authenticate( AuthenticationDataSource source )
68 throws AccountLockedException, AuthenticationException, MustChangePasswordException
69 {
70 TokenBasedAuthenticationDataSource dataSource = (TokenBasedAuthenticationDataSource) source;
71
72 String key = dataSource.getToken();
73 try
74 {
75 AuthenticationKey authKey = keystore.findKey( key );
76
77
78 if ( authKey != null )
79 {
80 User user = userManager.findUser( dataSource.getUsername() );
81
82 if ( user.isLocked() )
83 {
84 throw new AccountLockedException( "Account " + source.getUsername() + " is locked.", user );
85 }
86
87 if ( user.isPasswordChangeRequired() && source.isEnforcePasswordChange() )
88 {
89 throw new MustChangePasswordException( "Password expired.", user );
90 }
91
92 return new AuthenticationResult( true, dataSource.getUsername(), null );
93 }
94 else
95 {
96 return new AuthenticationResult( false, dataSource.getUsername(),
97 new AuthenticationException( "unable to find key" ) );
98 }
99 }
100 catch ( KeyNotFoundException ne )
101 {
102 return new AuthenticationResult( false, null, ne );
103 }
104 catch ( KeyManagerException ke )
105 {
106 throw new AuthenticationException( "underlaying keymanager issue", ke );
107 }
108 catch ( UserNotFoundException e )
109 {
110 log.warn( "Login for user {} failed. user not found.", source.getUsername() );
111 return new AuthenticationResult( false, null, e );
112 }
113 catch ( UserManagerException e )
114 {
115 log.warn( "Login fail for user {} failed. message: {}", source.getUsername(), e.getMessage() );
116 return new AuthenticationResult( false, null, e );
117 }
118 }
119
120 public boolean supportsDataSource( AuthenticationDataSource source )
121 {
122 return source instanceof TokenBasedAuthenticationDataSource;
123 }
124 }