01: /*
02: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.romaframework.aspect.authentication;
18:
19: import java.security.MessageDigest;
20: import java.security.NoSuchAlgorithmException;
21:
22: import org.romaframework.aspect.session.SessionAccount;
23: import org.romaframework.aspect.session.SessionAspect;
24: import org.romaframework.aspect.session.SessionInfo;
25: import org.romaframework.core.Utility;
26: import org.romaframework.core.aspect.SelfRegistrantConfigurableAspect;
27: import org.romaframework.core.config.RomaApplicationContext;
28: import org.romaframework.core.flow.ObjectContext;
29: import org.romaframework.core.schema.SchemaClassResolver;
30:
31: /**
32: * Base abstract class implementing Authentication Aspect interface.
33: *
34: * @author Luca Garulli (luca.garulli@assetdata.it)
35: */
36: public abstract class AuthenticationAspectAbstract extends
37: SelfRegistrantConfigurableAspect<String> implements
38: AuthenticationAspect {
39:
40: public static final String DEF_ALGORITHM = "MD5";
41:
42: private sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
43:
44: public String getEncryptionAlgorithm() {
45: return DEF_ALGORITHM;
46: }
47:
48: public String encryptPassword(String iPassword)
49: throws NoSuchAlgorithmException {
50: return encoder.encode(encryptPasswordInBytes(
51: getEncryptionAlgorithm(), iPassword));
52: }
53:
54: public byte[] encryptPasswordInBytes(String iAlgorithm,
55: String iPassword) throws NoSuchAlgorithmException {
56: MessageDigest md = MessageDigest.getInstance(iAlgorithm);
57: md.update(iPassword.getBytes());
58: return md.digest();
59: }
60:
61: public Object getCurrentAccount() {
62: SessionInfo sess = ObjectContext.getInstance().getComponent(
63: SessionAspect.class).getActiveSessionInfo();
64: if (sess == null)
65: return null;
66: return sess.getAccount();
67: }
68:
69: protected void setCurrentAccount(SessionAccount iAccount) {
70: ObjectContext.getInstance().getComponent(SessionAspect.class)
71: .getActiveSessionInfo().setAccount(iAccount);
72: }
73:
74: @Override
75: public void startup() {
76: super .startup();
77:
78: // REGISTER THE VIEW DOMAIN TO SCHEMA CLASS RESOLVER
79: RomaApplicationContext.getInstance().getBean(
80: SchemaClassResolver.class).addDomainPackage(
81: Utility.getApplicationAspectPackage(aspectName()));
82: }
83:
84: public String aspectName() {
85: return ASPECT_NAME;
86: }
87: }
|