Merge "Refactor frontend JS"
[controller.git] / opendaylight / usermanager / src / main / java / org / opendaylight / controller / usermanager / internal / UserConfig.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.usermanager.internal;
10
11 import java.io.Serializable;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
14
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;
21
22 /**
23  * Configuration Java Object which represents a Local AAA user configuration
24  * information for User Manager.
25  */
26 public class UserConfig implements Serializable {
27     private static final long serialVersionUID = 1L;
28
29     /*
30      * Clear text password as we are moving to some MD5 digest for when saving
31      * configurations
32      */
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\\.\\?#%;\\\\]+)");
41
42     public UserConfig() {
43     }
44
45     public UserConfig(String user, String password, String role) {
46         this.user = user;
47         this.password = password;
48         this.role = role;
49     }
50
51     public String getUser() {
52         return user;
53     }
54
55     public String getPassword() {
56         return password;
57     }
58
59     public String getRole() {
60         return role;
61     }
62
63     @Override
64     public int hashCode() {
65         return HashCodeBuilder.reflectionHashCode(this);
66     }
67
68     @Override
69     public boolean equals(Object obj) {
70         return EqualsBuilder.reflectionEquals(this, obj);
71     }
72
73     @Override
74     public String toString() {
75         return "UserConfig[user=" + user + ", password=" + password + "]";
76     }
77
78     public Status validate() {
79         Status validCheck = new Status(StatusCode.SUCCESS, null);
80         validCheck = isRoleValid();
81
82         if (validCheck.isSuccess())
83             validCheck = isUsernameValid();
84         if (validCheck.isSuccess())
85             validCheck = isPasswordValid();
86
87         return validCheck;
88     }
89
90     protected Status isUsernameValid() {
91         if (user == null || user.isEmpty()) {
92             return new Status(StatusCode.BADREQUEST, "Username cannot be empty");
93         }
94
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 ./#%;?\\");
102         }
103
104         return new Status(StatusCode.SUCCESS, null);
105     }
106
107     private Status isPasswordValid() {
108         if (password == null || password.isEmpty()) {
109             return new Status(StatusCode.BADREQUEST, "Password cannot be empty");
110         }
111
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");
116         }
117         return new Status(StatusCode.SUCCESS, null);
118     }
119
120     protected Status isRoleValid() {
121         if (role == null || role.isEmpty()) {
122             return new Status(StatusCode.BADREQUEST,
123                     "Role name cannot be empty");
124         }
125         return new Status(StatusCode.SUCCESS, null);
126     }
127
128     public boolean update(String currentPassword, String newPassword,
129             String newRole) {
130         // To make any changes to a user configured profile, current password
131         // must always be provided
132         if (!this.password.equals(currentPassword)) {
133             return false;
134         }
135         if (newPassword != null) {
136             this.password = newPassword;
137         }
138         if (newRole != null) {
139             this.role = newRole;
140         }
141         return true;
142     }
143
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(",", " "));
149         } else {
150             locResponse.setStatus(AuthResultEnum.AUTH_REJECT_LOC);
151         }
152         return locResponse;
153     }
154 }