| 1 | //  Copyright 2004-2007 Jean-Francois Poilpret | 
| 2 | // | 
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
| 4 | // you may not use this file except in compliance with the License. | 
| 5 | // You may obtain a copy of the License at | 
| 6 | // | 
| 7 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
| 8 | // | 
| 9 | // Unless required by applicable law or agreed to in writing, software | 
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, | 
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| 12 | // See the License for the specific language governing permissions and | 
| 13 | // limitations under the License. | 
| 14 |   | 
| 15 | package net.sourceforge.hiveboard.security; | 
| 16 |   | 
| 17 | import java.security.Principal; | 
| 18 | import java.util.Arrays; | 
| 19 |   | 
| 20 | import org.apache.commons.logging.Log; | 
| 21 | import org.apache.commons.logging.LogFactory; | 
| 22 | import org.securityfilter.realm.SecurityRealmInterface; | 
| 23 |   | 
| 24 | import net.sourceforge.hiveboard.Account; | 
| 25 | import net.sourceforge.hiveboard.business.WhiteBoardManager; | 
| 26 | import net.sourceforge.hivelock.SecurityService; | 
| 27 | import net.sourceforge.hiveutils.util.PasswordEncryption; | 
| 28 | import net.sourceforge.hiveutils.web.HiveMindRegistryPublishFilter; | 
| 29 |   | 
| 30 | public class HiveBoardRealm implements SecurityRealmInterface | 
| 31 | { | 
| 32 |         static final private Log        _logger = LogFactory.getLog(HiveBoardRealm.class); | 
| 33 |   | 
| 34 |         public Principal authenticate(String username, String password) | 
| 35 |         { | 
| 36 |                 _logger.debug("authenticate(): " + username); | 
| 37 |                 // Check account exists | 
| 38 |                 Account account = getManager().getAccount(username); | 
| 39 |                 if (account == null) | 
| 40 |                 { | 
| 41 |                         _logger.info(username + " does not exist!"); | 
| 42 |                         return null; | 
| 43 |                 } | 
| 44 |                 // Check password is OK | 
| 45 |                 String encryptedPassword = password; | 
| 46 |                 if (encryptedPassword != null && encryptedPassword.length() == 0) | 
| 47 |                 { | 
| 48 |                         encryptedPassword = null; | 
| 49 |                 } | 
| 50 |                 encryptedPassword = PasswordEncryption.encrypt(encryptedPassword); | 
| 51 |                 if (!equals(encryptedPassword, account.getPassword())) | 
| 52 |                 { | 
| 53 |                         _logger.info(username + " provided bad password"); | 
| 54 |                         return null; | 
| 55 |                 } | 
| 56 |                  | 
| 57 |                 // Check that account is not already connected! | 
| 58 |                 if (account.isConnected()) | 
| 59 |                 { | 
| 60 |                         _logger.info(username + " is already connected"); | 
| 61 |                         return null; | 
| 62 |                 } | 
| 63 |   | 
| 64 |                 String[] roles; | 
| 65 |                 if (account.isAdmin()) | 
| 66 |                 { | 
| 67 |                         roles = new String[] {account.getRole().toString(), "ADMIN"}; | 
| 68 |                 } | 
| 69 |                 else | 
| 70 |                 { | 
| 71 |                         roles = new String[] {account.getRole().toString()}; | 
| 72 |                 } | 
| 73 |                 _logger.debug(username + " has role <" + roles[0] + ">"); | 
| 74 |                 Principal principal = new HiveBoardPrincipal(        account.getId(), | 
| 75 |                                                                                                                 username,  | 
| 76 |                                                                                                                 account.getName(),  | 
| 77 |                                                                                                                 roles); | 
| 78 |                 getSecurityService().login(principal); | 
| 79 |                 _logger.info(username + " has logged in"); | 
| 80 |                 return principal; | 
| 81 |         } | 
| 82 |          | 
| 83 |         static private boolean        equals(String s1, String s2) | 
| 84 |         { | 
| 85 |                 return ((s1 == s2) || (s1 != null && s1.equals(s2))); | 
| 86 |         } | 
| 87 |          | 
| 88 |         synchronized private WhiteBoardManager        getManager() | 
| 89 |         { | 
| 90 |                 if (_manager == null) | 
| 91 |                 { | 
| 92 |                         _manager = (WhiteBoardManager) | 
| 93 |                                 HiveMindRegistryPublishFilter.getRegistry().getService( | 
| 94 |                                                                                 "hiveboard.server.WhiteBoardManager",  | 
| 95 |                                                                                 WhiteBoardManager.class); | 
| 96 |                 } | 
| 97 |                 return _manager; | 
| 98 |         } | 
| 99 |          | 
| 100 |         synchronized private SecurityService        getSecurityService() | 
| 101 |         { | 
| 102 |                 if (_security == null) | 
| 103 |                 { | 
| 104 |                         _security = (SecurityService)  | 
| 105 |                                 HiveMindRegistryPublishFilter.getRegistry().getService( | 
| 106 |                                         "hivelock.core.SecurityService", SecurityService.class); | 
| 107 |                 } | 
| 108 |                 return _security; | 
| 109 |         } | 
| 110 |          | 
| 111 |         public boolean isUserInRole(Principal principal, String rolename) | 
| 112 |         { | 
| 113 |                 if (principal instanceof HiveBoardPrincipal) | 
| 114 |                 { | 
| 115 |                         String[] roles = ((HiveBoardPrincipal) principal).getRoles(); | 
| 116 |                         return (Arrays.binarySearch(roles, rolename) >= 0); | 
| 117 |                 } | 
| 118 |                 else | 
| 119 |                 { | 
| 120 |                         return false; | 
| 121 |                 } | 
| 122 |         } | 
| 123 |   | 
| 124 |         private WhiteBoardManager        _manager; | 
| 125 |         private SecurityService                _security; | 
| 126 | } |