1 package org.apache.fulcrum.security.authenticator;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.security.NoSuchAlgorithmException;
22
23 import org.apache.avalon.framework.activity.Disposable;
24 import org.apache.avalon.framework.configuration.Configurable;
25 import org.apache.avalon.framework.configuration.Configuration;
26 import org.apache.avalon.framework.configuration.ConfigurationException;
27 import org.apache.avalon.framework.logger.AbstractLogEnabled;
28 import org.apache.avalon.framework.service.ServiceException;
29 import org.apache.avalon.framework.service.ServiceManager;
30 import org.apache.avalon.framework.service.Serviceable;
31 import org.apache.fulcrum.crypto.CryptoAlgorithm;
32 import org.apache.fulcrum.crypto.CryptoService;
33 import org.apache.fulcrum.security.entity.User;
34 import org.apache.fulcrum.security.util.DataBackendException;
35
36
37
38
39
40
41
42
43
44
45
46
47 public class CryptoAuthenticator extends AbstractLogEnabled implements Authenticator, Serviceable, Disposable, Configurable
48 {
49 boolean composed = false;
50 protected CryptoService cryptoService = null;
51 private String algorithm;
52 private String cipher;
53
54
55
56
57
58
59
60
61
62
63
64
65
66 @Override
67 public boolean authenticate(User user, String password) throws DataBackendException
68 {
69 try
70 {
71 CryptoAlgorithm ca = cryptoService.getCryptoAlgorithm(algorithm);
72 ca.setCipher(cipher);
73 String output = ca.encrypt(password);
74 return output.equals(user.getPassword());
75 }
76 catch (NoSuchAlgorithmException e)
77 {
78 throw new DataBackendException(e.getMessage(), e);
79 }
80 catch (Exception ex)
81 {
82 throw new DataBackendException(ex.getMessage(), ex);
83 }
84 }
85
86
87
88
89
90 @Override
91 public void configure(Configuration conf) throws ConfigurationException
92 {
93 algorithm = conf.getChild("algorithm").getValue();
94 cipher = conf.getChild("cipher").getValue();
95 }
96
97
98
99
100 @Override
101 public void service(ServiceManager manager) throws ServiceException
102 {
103 this.cryptoService = (CryptoService) manager.lookup(CryptoService.ROLE);
104 }
105
106
107
108
109 @Override
110 public void dispose()
111 {
112 cryptoService = null;
113 }
114 }