2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.usermanager.internal;
11 import java.io.Serializable;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
15 import org.apache.commons.lang3.builder.EqualsBuilder;
16 import org.apache.commons.lang3.builder.HashCodeBuilder;
17 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
18 import org.opendaylight.controller.sal.utils.Status;
19 import org.opendaylight.controller.sal.utils.StatusCode;
20 import org.opendaylight.controller.usermanager.AuthResponse;
23 * Configuration Java Object which represents a Local AAA user configuration
24 * information for User Manager.
26 public class UserConfig implements Serializable {
27 private static final long serialVersionUID = 1L;
30 * Clear text password as we are moving to some MD5 digest for when saving
33 protected String user;
34 protected String role;
35 private String password;
36 private static final int USERNAME_MAXLENGTH = 32;
37 private static final int PASSWORD_MINLENGTH = 5;
38 private static final int PASSWORD_MAXLENGTH = 256;
39 private static final Pattern INVALID_USERNAME_CHARACTERS = Pattern
40 .compile("([/\\s\\.\\?#%;\\\\]+)");
45 public UserConfig(String user, String password, String role) {
47 this.password = password;
51 public String getUser() {
55 public String getPassword() {
59 public String getRole() {
64 public int hashCode() {
65 return HashCodeBuilder.reflectionHashCode(this);
69 public boolean equals(Object obj) {
70 return EqualsBuilder.reflectionEquals(this, obj);
74 public String toString() {
75 return "UserConfig[user=" + user + ", password=" + password + "]";
78 public Status validate() {
79 Status validCheck = new Status(StatusCode.SUCCESS, null);
80 validCheck = isRoleValid();
82 if (validCheck.isSuccess())
83 validCheck = isUsernameValid();
84 if (validCheck.isSuccess())
85 validCheck = isPasswordValid();
90 protected Status isUsernameValid() {
91 if (user == null || user.isEmpty()) {
92 return new Status(StatusCode.BADREQUEST, "Username cannot be empty");
95 Matcher mUser = UserConfig.INVALID_USERNAME_CHARACTERS.matcher(user);
96 if (user.length() > UserConfig.USERNAME_MAXLENGTH
97 || mUser.find() == true) {
98 return new Status(StatusCode.BADREQUEST,
99 "Username can have 1-32 non-whitespace "
100 + "alphanumeric characters and any special "
101 + "characters except ./#%;?\\");
104 return new Status(StatusCode.SUCCESS, null);
107 private Status isPasswordValid() {
108 if (password == null || password.isEmpty()) {
109 return new Status(StatusCode.BADREQUEST, "Password cannot be empty");
112 if (password.length() < UserConfig.PASSWORD_MINLENGTH
113 || password.length() > UserConfig.PASSWORD_MAXLENGTH) {
114 return new Status(StatusCode.BADREQUEST,
115 "Password should have 5-256 characters");
117 return new Status(StatusCode.SUCCESS, null);
120 protected Status isRoleValid() {
121 if (role == null || role.isEmpty()) {
122 return new Status(StatusCode.BADREQUEST,
123 "Role name cannot be empty");
125 return new Status(StatusCode.SUCCESS, null);
128 public boolean update(String currentPassword, String newPassword,
130 // To make any changes to a user configured profile, current password
131 // must always be provided
132 if (!this.password.equals(currentPassword)) {
135 if (newPassword != null) {
136 this.password = newPassword;
138 if (newRole != null) {
144 public AuthResponse authenticate(String clearTextPass) {
145 AuthResponse locResponse = new AuthResponse();
146 if (password.equals(clearTextPass)) {
147 locResponse.setStatus(AuthResultEnum.AUTH_ACCEPT_LOC);
148 locResponse.addData(role.replace(",", " "));
150 locResponse.setStatus(AuthResultEnum.AUTH_REJECT_LOC);