/nexus/nexus-core-plugins/nexus-ldap-plugin-parent/ldap-common/src/main/java/org/sonatype/security/ldap/dao/LdapAuthConfiguration.java

https://github.com/galak/nexus · Java · 728 lines · 394 code · 92 blank · 242 comment · 135 complexity · ba0bc1cefb7153d82d9af81d96fa246b MD5 · raw file

  1. /**
  2. * Copyright (c) 2008-2011 Sonatype, Inc.
  3. * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions
  4. *
  5. * This program is free software: you can redistribute it and/or modify it only under the terms of the GNU Affero General
  6. * Public License Version 3 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  9. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License Version 3
  10. * for more details.
  11. *
  12. * You should have received a copy of the GNU Affero General Public License Version 3 along with this program. If not, see
  13. * http://www.gnu.org/licenses.
  14. *
  15. * Sonatype Nexus (TM) Open Source Version is available from Sonatype, Inc. Sonatype and Sonatype Nexus are trademarks of
  16. * Sonatype, Inc. Apache Maven is a trademark of the Apache Foundation. M2Eclipse is a trademark of the Eclipse Foundation.
  17. * All other trademarks are the property of their respective owners.
  18. */
  19. package org.sonatype.security.ldap.dao;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.Iterator;
  23. import java.util.LinkedHashSet;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Set;
  27. import org.codehaus.plexus.util.StringUtils;
  28. public class LdapAuthConfiguration
  29. {
  30. /** The Constant DEFAULT_NAME_ATTRIBUTE. */
  31. public static final String DEFAULT_NAME_ATTRIBUTE = "sn";
  32. /** The Constant DEFAULT_USER_MEMBER_OF. */
  33. public static final String DEFAULT_USER_MEMBER_OF = "memberOf";
  34. /** The Constant DEFAULT_EMAIL_ADDRESS_ATTRIBUTE. */
  35. public static final String DEFAULT_EMAIL_ADDRESS_ATTRIBUTE = "mail";
  36. /** The Constant DEFAULT_WEBSITE_ATTRIBUTE. */
  37. public static final String DEFAULT_WEBSITE_ATTRIBUTE = "labeledUri";
  38. /** The Constant DEFAULT_WEB_URI_LABEL. */
  39. public static final String DEFAULT_WEB_URI_LABEL = "Web Site";
  40. /** The Constant DEFAULT_USERNAME_ATTRIBUTE. */
  41. private static final String DEFAULT_USERNAME_ATTRIBUTE = "uid";
  42. /** The Constant DEFAULT_GROUP_ID_ATTRIBUTE. */
  43. private static final String DEFAULT_GROUP_ID_ATTRIBUTE = "cn";
  44. /** The Constant DEFAULT_USER_PASSWORD_ATTRIBUTE. */
  45. private static final String DEFAULT_USER_PASSWORD_ATTRIBUTE = "userPassword";
  46. /** The Constant DEFAULT_USER_PASSWORD_ENCODING. */
  47. private static final String DEFAULT_USER_PASSWORD_ENCODING = "crypt";
  48. /** The Constant DEFAULT_USER_OBJECTCLASS. */
  49. private static final String DEFAULT_USER_OBJECTCLASS = "inetOrgPerson";
  50. /** The Constant DEFAULT_GROUP_OBJECTCLASS. */
  51. private static final String DEFAULT_GROUP_OBJECTCLASS = "groupOfNames";
  52. // calculated.
  53. /** The group reverse mappings. */
  54. private Map<String, Set<String>> groupReverseMappings;
  55. /** The group mappings. */
  56. private Map<String, String> groupMappings;
  57. /** The group member format. */
  58. private String groupMemberFormat;
  59. /** The group object class. */
  60. private String groupObjectClass = DEFAULT_GROUP_OBJECTCLASS;
  61. /** The group base dn. */
  62. private String groupBaseDn;
  63. /** The group id attribute. */
  64. private String groupIdAttribute = DEFAULT_GROUP_ID_ATTRIBUTE;
  65. /** The group member attribute. */
  66. private String groupMemberAttribute;
  67. /** The user object class. */
  68. private String userObjectClass = DEFAULT_USER_OBJECTCLASS;
  69. /** The user base dn. */
  70. private String userBaseDn;
  71. /** The user id attribute. */
  72. private String userIdAttribute = DEFAULT_USERNAME_ATTRIBUTE;
  73. /** The password attribute. */
  74. private String passwordAttribute = DEFAULT_USER_PASSWORD_ATTRIBUTE;
  75. // /** The password encoding. */
  76. // private String passwordEncoding = DEFAULT_USER_PASSWORD_ENCODING;
  77. /** The user real name attribute. */
  78. private String userRealNameAttribute = DEFAULT_NAME_ATTRIBUTE;
  79. /** The user member of attribute. */
  80. private String userMemberOfAttribute = DEFAULT_USER_MEMBER_OF;
  81. /** The email address attribute. */
  82. private String emailAddressAttribute = DEFAULT_EMAIL_ADDRESS_ATTRIBUTE;
  83. /** The website attribute. */
  84. private String websiteAttribute = DEFAULT_WEBSITE_ATTRIBUTE;
  85. /** for parsing labeledUri attributes... */
  86. private String websiteUriLabel = DEFAULT_WEB_URI_LABEL;
  87. /** If not, don't parse it as a labelUri. */
  88. private boolean isWebsiteAttributeLabelUri = true;
  89. private boolean ldapGroupsAsRoles;
  90. private boolean userSubtree;
  91. private boolean groupSubtree;
  92. /**
  93. * Gets the group base dn.
  94. *
  95. * @return the group base dn
  96. */
  97. public String getGroupBaseDn()
  98. {
  99. return groupBaseDn;
  100. }
  101. /**
  102. * Sets the group base dn.
  103. *
  104. * @param groupBaseDn the new group base dn
  105. */
  106. public void setGroupBaseDn( String groupBaseDn )
  107. {
  108. this.groupBaseDn = groupBaseDn;
  109. }
  110. /**
  111. * Gets the group id attribute.
  112. *
  113. * @return the group id attribute
  114. */
  115. public String getGroupIdAttribute()
  116. {
  117. return groupIdAttribute;
  118. }
  119. /**
  120. * Sets the group id attribute.
  121. *
  122. * @param groupIdAttribute the new group id attribute
  123. */
  124. public void setGroupIdAttribute( String groupIdAttribute )
  125. {
  126. this.groupIdAttribute = groupIdAttribute;
  127. }
  128. /**
  129. * Gets the group mappings.
  130. *
  131. * @return the group mappings
  132. */
  133. public Map<String, String> getGroupMappings()
  134. {
  135. return groupMappings;
  136. }
  137. /**
  138. * Sets the group mappings.
  139. *
  140. * @param groupMappings the group mappings
  141. */
  142. public synchronized void setGroupMappings( Map<String, String> groupMappings )
  143. {
  144. this.groupMappings = groupMappings;
  145. this.groupReverseMappings = null;
  146. }
  147. /**
  148. * Gets the group member attribute.
  149. *
  150. * @return the group member attribute
  151. */
  152. public String getGroupMemberAttribute()
  153. {
  154. return groupMemberAttribute;
  155. }
  156. /**
  157. * Sets the group member attribute.
  158. *
  159. * @param groupMemberAttribute the new group member attribute
  160. */
  161. public void setGroupMemberAttribute( String groupMemberAttribute )
  162. {
  163. this.groupMemberAttribute = groupMemberAttribute;
  164. }
  165. /**
  166. * Gets the group reverse mappings.
  167. *
  168. * @return the group reverse mappings
  169. */
  170. public synchronized Map<String, Set<String>> getGroupReverseMappings()
  171. {
  172. if ( groupReverseMappings == null )
  173. {
  174. groupReverseMappings = new HashMap<String, Set<String>>();
  175. if ( groupMappings != null )
  176. {
  177. for ( Iterator it = groupMappings.entrySet().iterator(); it.hasNext(); )
  178. {
  179. Map.Entry entry = (Map.Entry) it.next();
  180. String logical = (String) entry.getKey();
  181. String real = (String) entry.getValue();
  182. Set<String> logicalMappings = groupReverseMappings.get( real );
  183. if ( logicalMappings == null )
  184. {
  185. logicalMappings = new LinkedHashSet<String>();
  186. groupReverseMappings.put( real, logicalMappings );
  187. }
  188. logicalMappings.add( logical );
  189. }
  190. }
  191. }
  192. return groupReverseMappings;
  193. }
  194. /**
  195. * Gets the email address attribute.
  196. *
  197. * @return the email address attribute
  198. */
  199. public String getEmailAddressAttribute()
  200. {
  201. return emailAddressAttribute;
  202. }
  203. /**
  204. * Sets the email address attribute.
  205. *
  206. * @param emailAddressAttribute the new email address attribute
  207. */
  208. public void setEmailAddressAttribute( String emailAddressAttribute )
  209. {
  210. this.emailAddressAttribute = emailAddressAttribute;
  211. }
  212. /**
  213. * Gets the user real name attribute.
  214. *
  215. * @return the user real name attribute
  216. */
  217. public String getUserRealNameAttribute()
  218. {
  219. return userRealNameAttribute;
  220. }
  221. /**
  222. * Sets the user real name attribute.
  223. *
  224. * @param nameAttribute the new user real name attribute
  225. */
  226. public void setUserRealNameAttribute( String nameAttribute )
  227. {
  228. this.userRealNameAttribute = nameAttribute;
  229. }
  230. /**
  231. * Gets the password attribute.
  232. *
  233. * @return the password attribute
  234. */
  235. public String getPasswordAttribute()
  236. {
  237. return passwordAttribute;
  238. }
  239. /**
  240. * Sets the password attribute.
  241. *
  242. * @param passwordAttribute the new password attribute
  243. */
  244. public void setPasswordAttribute( String passwordAttribute )
  245. {
  246. this.passwordAttribute = passwordAttribute;
  247. }
  248. /**
  249. * Gets the user base dn.
  250. *
  251. * @return the user base dn
  252. */
  253. public String getUserBaseDn()
  254. {
  255. return userBaseDn;
  256. }
  257. /**
  258. * Sets the user base dn.
  259. *
  260. * @param userBaseDn the new user base dn
  261. */
  262. public void setUserBaseDn( String userBaseDn )
  263. {
  264. this.userBaseDn = userBaseDn;
  265. }
  266. /**
  267. * Gets the user id attribute.
  268. *
  269. * @return the user id attribute
  270. */
  271. public String getUserIdAttribute()
  272. {
  273. return userIdAttribute;
  274. }
  275. /**
  276. * Sets the user id attribute.
  277. *
  278. * @param userIdAttribute the new user id attribute
  279. */
  280. public void setUserIdAttribute( String userIdAttribute )
  281. {
  282. this.userIdAttribute = userIdAttribute;
  283. }
  284. /**
  285. * Gets the website attribute.
  286. *
  287. * @return the website attribute
  288. */
  289. public String getWebsiteAttribute()
  290. {
  291. return websiteAttribute;
  292. }
  293. /**
  294. * Sets the website attribute.
  295. *
  296. * @param websiteAttribute the new website attribute
  297. */
  298. public void setWebsiteAttribute( String websiteAttribute )
  299. {
  300. this.websiteAttribute = websiteAttribute;
  301. }
  302. /**
  303. * Checks if is website attribute label uri.
  304. *
  305. * @return true, if is website attribute label uri
  306. */
  307. public boolean isWebsiteAttributeLabelUri()
  308. {
  309. return isWebsiteAttributeLabelUri;
  310. }
  311. /**
  312. * Sets the website attribute label uri.
  313. *
  314. * @param websiteIsLabelUri the new website attribute label uri
  315. */
  316. public void setWebsiteAttributeLabelUri( boolean websiteIsLabelUri )
  317. {
  318. this.isWebsiteAttributeLabelUri = websiteIsLabelUri;
  319. }
  320. /**
  321. * Gets the website uri label.
  322. *
  323. * @return the website uri label
  324. */
  325. public String getWebsiteUriLabel()
  326. {
  327. return websiteUriLabel;
  328. }
  329. /**
  330. * Sets the website uri label.
  331. *
  332. * @param websiteUriLabel the new website uri label
  333. */
  334. public void setWebsiteUriLabel( String websiteUriLabel )
  335. {
  336. this.websiteUriLabel = websiteUriLabel;
  337. }
  338. /**
  339. * Gets the user attributes.
  340. *
  341. * @return the user attributes
  342. */
  343. public synchronized String[] getUserAttributes()
  344. {
  345. List<String> result = new ArrayList<String>();
  346. String[] allAttributes =
  347. new String[]{ userIdAttribute, passwordAttribute, userRealNameAttribute, emailAddressAttribute,
  348. websiteAttribute, userMemberOfAttribute };
  349. for ( String attribute : allAttributes )
  350. {
  351. if ( StringUtils.isNotBlank( attribute ) )
  352. {
  353. result.add( attribute );
  354. }
  355. }
  356. return result.toArray( new String[result.size()] );
  357. }
  358. /**
  359. * Gets the group member format.
  360. *
  361. * @return the group member format
  362. */
  363. public String getGroupMemberFormat()
  364. {
  365. return groupMemberFormat;
  366. }
  367. /**
  368. * Sets the group member format.
  369. *
  370. * @param groupMemberFormat the new group member format
  371. */
  372. public void setGroupMemberFormat( String groupMemberFormat )
  373. {
  374. this.groupMemberFormat = groupMemberFormat;
  375. }
  376. /**
  377. * Gets the group object class.
  378. *
  379. * @return the group object class
  380. */
  381. public String getGroupObjectClass()
  382. {
  383. return groupObjectClass;
  384. }
  385. /**
  386. * Sets the group object class.
  387. *
  388. * @param groupObjectClass the new group object class
  389. */
  390. public void setGroupObjectClass( String groupObjectClass )
  391. {
  392. this.groupObjectClass = groupObjectClass;
  393. }
  394. /**
  395. * Gets the user object class.
  396. *
  397. * @return the user object class
  398. */
  399. public String getUserObjectClass()
  400. {
  401. return userObjectClass;
  402. }
  403. /**
  404. * Sets the user object class.
  405. *
  406. * @param userObjectClass the new user object class
  407. */
  408. public void setUserObjectClass( String userObjectClass )
  409. {
  410. this.userObjectClass = userObjectClass;
  411. }
  412. // /**
  413. // * @return the passwordEncoding
  414. // */
  415. // public String getPasswordEncoding()
  416. // {
  417. // return passwordEncoding;
  418. // }
  419. //
  420. // /**
  421. // * @param passwordEncoding the passwordEncoding to set
  422. // */
  423. // public void setPasswordEncoding( String passwordEncoding )
  424. // {
  425. // this.passwordEncoding = passwordEncoding;
  426. // }
  427. /**
  428. * @return the ldapGroupsAsRoles
  429. */
  430. public boolean isLdapGroupsAsRoles()
  431. {
  432. return ldapGroupsAsRoles;
  433. }
  434. /**
  435. * @param ldapGroupsAsRoles the ldapGroupsAsRoles to set
  436. */
  437. public void setLdapGroupsAsRoles( boolean ldapGroupsAsRoles )
  438. {
  439. this.ldapGroupsAsRoles = ldapGroupsAsRoles;
  440. }
  441. /**
  442. * @return the userSubtree
  443. */
  444. public boolean isUserSubtree()
  445. {
  446. return userSubtree;
  447. }
  448. /**
  449. * @param userSubtree the userSubtree to set
  450. */
  451. public void setUserSubtree( boolean userSubtree )
  452. {
  453. this.userSubtree = userSubtree;
  454. }
  455. /**
  456. * @return the groupSubtree
  457. */
  458. public boolean isGroupSubtree()
  459. {
  460. return groupSubtree;
  461. }
  462. /**
  463. * @param groupSubtree the groupSubtree to set
  464. */
  465. public void setGroupSubtree( boolean groupSubtree )
  466. {
  467. this.groupSubtree = groupSubtree;
  468. }
  469. public String getUserMemberOfAttribute()
  470. {
  471. return userMemberOfAttribute;
  472. }
  473. public void setUserMemberOfAttribute( String userMemberOfAttribute )
  474. {
  475. this.userMemberOfAttribute = userMemberOfAttribute;
  476. }
  477. @Override
  478. public int hashCode()
  479. {
  480. final int prime = 31;
  481. int result = 1;
  482. result = prime * result + ( ( emailAddressAttribute == null ) ? 0 : emailAddressAttribute.hashCode() );
  483. result = prime * result + ( ( groupBaseDn == null ) ? 0 : groupBaseDn.hashCode() );
  484. result = prime * result + ( ( groupIdAttribute == null ) ? 0 : groupIdAttribute.hashCode() );
  485. result = prime * result + ( ( groupMappings == null ) ? 0 : groupMappings.hashCode() );
  486. result = prime * result + ( ( groupMemberAttribute == null ) ? 0 : groupMemberAttribute.hashCode() );
  487. result = prime * result + ( ( groupMemberFormat == null ) ? 0 : groupMemberFormat.hashCode() );
  488. result = prime * result + ( ( groupObjectClass == null ) ? 0 : groupObjectClass.hashCode() );
  489. result = prime * result + ( ( groupReverseMappings == null ) ? 0 : groupReverseMappings.hashCode() );
  490. result = prime * result + ( groupSubtree ? 1231 : 1237 );
  491. result = prime * result + ( isWebsiteAttributeLabelUri ? 1231 : 1237 );
  492. result = prime * result + ( ldapGroupsAsRoles ? 1231 : 1237 );
  493. result = prime * result + ( ( passwordAttribute == null ) ? 0 : passwordAttribute.hashCode() );
  494. result = prime * result + ( ( userBaseDn == null ) ? 0 : userBaseDn.hashCode() );
  495. result = prime * result + ( ( userIdAttribute == null ) ? 0 : userIdAttribute.hashCode() );
  496. result = prime * result + ( ( userMemberOfAttribute == null ) ? 0 : userMemberOfAttribute.hashCode() );
  497. result = prime * result + ( ( userObjectClass == null ) ? 0 : userObjectClass.hashCode() );
  498. result = prime * result + ( ( userRealNameAttribute == null ) ? 0 : userRealNameAttribute.hashCode() );
  499. result = prime * result + ( userSubtree ? 1231 : 1237 );
  500. result = prime * result + ( ( websiteAttribute == null ) ? 0 : websiteAttribute.hashCode() );
  501. result = prime * result + ( ( websiteUriLabel == null ) ? 0 : websiteUriLabel.hashCode() );
  502. return result;
  503. }
  504. @Override
  505. public boolean equals( Object obj )
  506. {
  507. if ( this == obj )
  508. return true;
  509. if ( obj == null )
  510. return false;
  511. if ( getClass() != obj.getClass() )
  512. return false;
  513. final LdapAuthConfiguration other = (LdapAuthConfiguration) obj;
  514. if ( emailAddressAttribute == null )
  515. {
  516. if ( other.emailAddressAttribute != null )
  517. return false;
  518. }
  519. else if ( !emailAddressAttribute.equals( other.emailAddressAttribute ) )
  520. return false;
  521. if ( groupBaseDn == null )
  522. {
  523. if ( other.groupBaseDn != null )
  524. return false;
  525. }
  526. else if ( !groupBaseDn.equals( other.groupBaseDn ) )
  527. return false;
  528. if ( groupIdAttribute == null )
  529. {
  530. if ( other.groupIdAttribute != null )
  531. return false;
  532. }
  533. else if ( !groupIdAttribute.equals( other.groupIdAttribute ) )
  534. return false;
  535. if ( groupMappings == null )
  536. {
  537. if ( other.groupMappings != null )
  538. return false;
  539. }
  540. else if ( !groupMappings.equals( other.groupMappings ) )
  541. return false;
  542. if ( groupMemberAttribute == null )
  543. {
  544. if ( other.groupMemberAttribute != null )
  545. return false;
  546. }
  547. else if ( !groupMemberAttribute.equals( other.groupMemberAttribute ) )
  548. return false;
  549. if ( groupMemberFormat == null )
  550. {
  551. if ( other.groupMemberFormat != null )
  552. return false;
  553. }
  554. else if ( !groupMemberFormat.equals( other.groupMemberFormat ) )
  555. return false;
  556. if ( groupObjectClass == null )
  557. {
  558. if ( other.groupObjectClass != null )
  559. return false;
  560. }
  561. else if ( !groupObjectClass.equals( other.groupObjectClass ) )
  562. return false;
  563. if ( groupReverseMappings == null )
  564. {
  565. if ( other.groupReverseMappings != null )
  566. return false;
  567. }
  568. else if ( !groupReverseMappings.equals( other.groupReverseMappings ) )
  569. return false;
  570. if ( groupSubtree != other.groupSubtree )
  571. return false;
  572. if ( isWebsiteAttributeLabelUri != other.isWebsiteAttributeLabelUri )
  573. return false;
  574. if ( ldapGroupsAsRoles != other.ldapGroupsAsRoles )
  575. return false;
  576. if ( passwordAttribute == null )
  577. {
  578. if ( other.passwordAttribute != null )
  579. return false;
  580. }
  581. else if ( !passwordAttribute.equals( other.passwordAttribute ) )
  582. return false;
  583. if ( userBaseDn == null )
  584. {
  585. if ( other.userBaseDn != null )
  586. return false;
  587. }
  588. else if ( !userBaseDn.equals( other.userBaseDn ) )
  589. return false;
  590. if ( userIdAttribute == null )
  591. {
  592. if ( other.userIdAttribute != null )
  593. return false;
  594. }
  595. else if ( !userIdAttribute.equals( other.userIdAttribute ) )
  596. return false;
  597. if ( userMemberOfAttribute == null )
  598. {
  599. if ( other.userMemberOfAttribute != null )
  600. return false;
  601. }
  602. else if ( !userMemberOfAttribute.equals( other.userMemberOfAttribute ) )
  603. return false;
  604. if ( userObjectClass == null )
  605. {
  606. if ( other.userObjectClass != null )
  607. return false;
  608. }
  609. else if ( !userObjectClass.equals( other.userObjectClass ) )
  610. return false;
  611. if ( userRealNameAttribute == null )
  612. {
  613. if ( other.userRealNameAttribute != null )
  614. return false;
  615. }
  616. else if ( !userRealNameAttribute.equals( other.userRealNameAttribute ) )
  617. return false;
  618. if ( userSubtree != other.userSubtree )
  619. return false;
  620. if ( websiteAttribute == null )
  621. {
  622. if ( other.websiteAttribute != null )
  623. return false;
  624. }
  625. else if ( !websiteAttribute.equals( other.websiteAttribute ) )
  626. return false;
  627. if ( websiteUriLabel == null )
  628. {
  629. if ( other.websiteUriLabel != null )
  630. return false;
  631. }
  632. else if ( !websiteUriLabel.equals( other.websiteUriLabel ) )
  633. return false;
  634. return true;
  635. }
  636. }