Revert "Checkstyle enforcer"
[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.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
19 import org.opendaylight.controller.sal.utils.Status;
20 import org.opendaylight.controller.sal.utils.StatusCode;
21 import org.opendaylight.controller.usermanager.AuthResponse;
22
23 /**
24  * Configuration Java Object which represents a Local AAA user configuration
25  * information for User Manager.
26  */
27 public class UserConfig implements Serializable {
28     private static final long serialVersionUID = 1L;
29
30     /*
31      * Clear text password as we are moving to some MD5 digest for when saving
32      * configurations
33      */
34     protected String user;
35     protected List<String> roles;
36     private String password;
37     private static final int USERNAME_MAXLENGTH = 32;
38     private static final int PASSWORD_MINLENGTH = 5;
39     private static final int PASSWORD_MAXLENGTH = 256;
40     private static final Pattern INVALID_USERNAME_CHARACTERS = Pattern
41             .compile("([/\\s\\.\\?#%;\\\\]+)");
42
43     public UserConfig() {
44     }
45
46     public UserConfig(String user, String password, List<String> roles) {
47         this.user = user;
48         this.password = password;
49         this.roles = (roles == null) ? new ArrayList<String>()
50                 : new ArrayList<String>(roles);
51     }
52     
53     public String getUser() {
54         return user;
55     }
56
57     public String getPassword() {
58         return password;
59     }
60
61     public List<String> getRoles() {
62         return new ArrayList<String>(roles);
63     }
64
65     @Override
66     public int hashCode() {
67         final int prime = 31;
68         int result = 1;
69         result = prime * result
70                 + ((password == null) ? 0 : password.hashCode());
71         result = prime * result + ((roles == null) ? 0 : roles.hashCode());
72         result = prime * result + ((user == null) ? 0 : user.hashCode());
73         return result;
74     }
75
76     @Override
77     public boolean equals(Object obj) {
78         if (this == obj)
79             return true;
80         if (obj == null)
81             return false;
82         if (getClass() != obj.getClass())
83             return false;
84         UserConfig other = (UserConfig) obj;
85         if (password == null) {
86             if (other.password != null)
87                 return false;
88         } else if (!password.equals(other.password))
89             return false;
90         if (roles == null) {
91             if (other.roles != null)
92                 return false;
93         } else if (!roles.equals(other.roles))
94             return false;
95         if (user == null) {
96             if (other.user != null)
97                 return false;
98         } else if (!user.equals(other.user))
99             return false;
100         return true;
101     }
102
103     @Override
104     public String toString() {
105         return "UserConfig[user=" + user + ", password=" + password + ", roles=" + roles +"]";
106     }
107
108     public Status validate() {
109         Status validCheck = validateRoles();
110         if (validCheck.isSuccess()) {
111             validCheck = validateUsername();
112         }
113         if (validCheck.isSuccess()) {
114             validCheck = validatePassword();
115         }
116         return validCheck;
117     }
118
119     protected Status validateUsername() {
120         if (user == null || user.isEmpty()) {
121             return new Status(StatusCode.BADREQUEST, "Username cannot be empty");
122         }
123
124         Matcher mUser = UserConfig.INVALID_USERNAME_CHARACTERS.matcher(user);
125         if (user.length() > UserConfig.USERNAME_MAXLENGTH
126                 || mUser.find() == true) {
127             return new Status(StatusCode.BADREQUEST,
128                     "Username can have 1-32 non-whitespace "
129                             + "alphanumeric characters and any special "
130                             + "characters except ./#%;?\\");
131         }
132
133         return new Status(StatusCode.SUCCESS);
134     }
135
136     private Status validatePassword() {
137         if (password == null || password.isEmpty()) {
138             return new Status(StatusCode.BADREQUEST, "Password cannot be empty");
139         }
140
141         if (password.length() < UserConfig.PASSWORD_MINLENGTH
142                 || password.length() > UserConfig.PASSWORD_MAXLENGTH) {
143             return new Status(StatusCode.BADREQUEST,
144                     "Password should have 5-256 characters");
145         }
146         return new Status(StatusCode.SUCCESS);
147     }
148
149     protected Status validateRoles() {
150         if (roles == null || roles.isEmpty()) {
151             return new Status(StatusCode.BADREQUEST, "No role specified");
152         }
153         return new Status(StatusCode.SUCCESS);
154     }
155
156     public Status update(String currentPassword, String newPassword,
157             List<String> newRoles) {
158         // To make any changes to a user configured profile, current password
159         // must always be provided
160         if (!this.password.equals(currentPassword)) {
161             return new Status(StatusCode.BADREQUEST,
162                     "Current password is incorrect");
163         }
164         
165         // Create a new object with the proposed modifications
166         UserConfig proposed = new UserConfig();
167         proposed.user = this.user;
168         proposed.password = (newPassword != null)? newPassword : this.password;
169         proposed.roles = (newRoles != null)? newRoles : this.roles;
170         
171         // Validate it
172         Status status = proposed.validate();
173         if (!status.isSuccess()) {
174             return status;
175         }
176         
177         // Accept the modifications
178         this.user = proposed.user;
179         this.password = proposed.password;
180         this.roles = new ArrayList<String>(proposed.roles);
181         
182         return status;
183     }
184
185     public AuthResponse authenticate(String clearTextPass) {
186         AuthResponse locResponse = new AuthResponse();
187         if (password.equals(clearTextPass)) {
188             locResponse.setStatus(AuthResultEnum.AUTH_ACCEPT_LOC);
189             locResponse.addData(getRolesString());
190         } else {
191             locResponse.setStatus(AuthResultEnum.AUTH_REJECT_LOC);
192         }
193         return locResponse;
194     }
195     
196     protected String getRolesString() {
197         StringBuffer buffer = new StringBuffer();
198         if (!roles.isEmpty()) {
199             Iterator<String> iter = roles.iterator();
200             buffer.append(iter.next());
201             while (iter.hasNext()) {
202                 buffer.append(" ");
203                 buffer.append(iter.next());
204             }
205         }
206         return buffer.toString();
207     }
208 }