Sonar Fixes: Variable name changes
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / validation / Validator.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.validation;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.regex.Pattern;
27
28 import org.onap.aaf.auth.layer.Result;
29
30
31 public class Validator {
32     private static final String ESSENTIAL = "\\x25\\x28\\x29\\x2C-\\x2E\\x30-\\x39\\x3D\\x40-\\x5A\\x5F\\x61-\\x7A";
33     private static final Pattern ESSENTIAL_CHARS = Pattern.compile("["+ESSENTIAL+"]+");
34     public static final Pattern ACTION_CHARS = Pattern.compile(
35                 "["+ESSENTIAL+"]+" +    // All AlphaNumeric+
36                 "|\\*"                        // Just Star
37                 );
38     public static final Pattern INST_CHARS = Pattern.compile(
39                 "["+ESSENTIAL+"]+[\\*]*" +                // All AlphaNumeric+ possibly ending with *
40                 "|\\*" +                                // Just Star
41                 "|(([:/]\\*)|([:/][!]{0,1}["+ESSENTIAL+"]+[\\*]*[:/]*))+"    // Key :asdf:*:sdf*:sdk
42                 );
43     public static final Pattern ID_CHARS = Pattern.compile("[\\w.-]+@[\\w.-]+");
44     public static final Pattern NAME_CHARS = Pattern.compile("[\\w.-]+");
45     public static final Pattern DESC_CHAR = Pattern.compile("["+ESSENTIAL+"\\x20]+");
46     protected static List<String> nsKeywords;
47     private final Pattern actionChars;
48     private final Pattern instChars;
49     private StringBuilder msgs;
50     public static final String PERM_TYPE = "Perm Type [";
51     public static final String IS_INVALID = "] is invalid.";
52     public static final String ROLE_STR = "Role [";
53     static {
54         nsKeywords = new ArrayList<>();
55         nsKeywords.add(".access");
56         nsKeywords.add(".owner");
57         nsKeywords.add(".admin");
58         nsKeywords.add(".member");
59         nsKeywords.add(".perm");
60         nsKeywords.add(".role");
61         nsKeywords.add(".ns");
62         nsKeywords.add(".cred");
63     }
64
65     public Validator() {
66         actionChars = ACTION_CHARS;
67         instChars = INST_CHARS;
68     }
69
70     public final String errs() {
71         return msgs.toString();
72     }
73
74     public final Validator nullOrBlank(String name, String str) {
75         if (str==null) {
76             msg(name + " is null.");
77         } else if (str.length()==0) {
78             msg(name + " is blank.");
79         }
80         return this;
81     }
82
83     public final Validator isNull(String name, Object o) {
84         if (o==null) {
85             msg(name + " is null.");
86         }
87         return this;
88     }
89
90     protected final boolean noMatch(String str, Pattern p) {
91         return str==null || !p.matcher(str).matches();
92     }
93
94     protected final void match(String text, String str, Pattern p) {
95         if(str==null || !p.matcher(str).matches()) {
96             msg(text);
97         }
98     }
99
100     protected final boolean nob(String str, Pattern p) {
101         return str==null || !p.matcher(str).matches();
102     }
103
104     protected final void msg(String ... strs) {
105         if (msgs==null) {
106             msgs=new StringBuilder();
107         }
108         for (String str : strs) {
109             msgs.append(str);
110         }
111         msgs.append('\n');
112     }
113
114     public final boolean err() {
115         return msgs!=null;
116     }
117
118     public final Validator notOK(Result<?> res) {
119         if (res==null) {
120             msgs.append("Result object is blank");
121         } else if (res.notOK()) {
122             msgs.append(res.getClass().getSimpleName()).append(" is not OK");
123         }
124         return this;
125     }
126
127     protected Validator intRange(String text, int target, int start, int end) {
128         if (target<start || target>end) {
129             msg(text + " is out of range (" + start + '-' + end + ')');
130         }
131         return this;
132     }
133
134     protected Validator floatRange(String text, float target, float start, float end) {
135         if (target<start || target>end) {
136             msg(text + " is out of range (" + start + '-' + end + ')');
137         }
138         return this;
139     }
140
141     protected Validator description(String type, String description) {
142         if (description != null && noMatch(description, DESC_CHAR)) {
143             msg(type + " Description is invalid.");
144         }
145         return this;
146     }
147
148     public final Validator permType(String type) {
149         if (nob(type,NAME_CHARS)) {
150             msg(PERM_TYPE +type + IS_INVALID);
151         }
152         return this;
153     }
154
155     public final Validator permTypeWithUser(String user, String type) {
156         if (type==null) {
157             msg("Perm Type is null");
158         } else if (user==null) {
159             msg("User is null");
160         } else {
161             if(!(type.startsWith(user) && type.endsWith(":id"))) {
162               if(nob(type,NAME_CHARS)) {
163                 msg(PERM_TYPE + type + IS_INVALID);
164               }
165             }
166         }
167         return this;
168     }
169
170     public final Validator permType(String type, String ns) {
171         if (type==null) {
172             msg("Perm Type is null");
173         } else if (ns==null) {
174             msg("Perm NS is null");
175         } else if (nob(type,NAME_CHARS)) {
176             msg(PERM_TYPE + (ns+(type.length()==0?"":'.')) + type + IS_INVALID);
177         }
178         return this;
179     }
180
181     public final Validator permInstance(String instance) {
182         if(!"/".equals(instance) && nob(instance,instChars)) {
183             msg("Perm Instance [" + instance + IS_INVALID);
184         }
185         return this;
186     }
187
188     public final Validator permAction(String action) {
189         // TODO check for correct Splits?  Type|Instance|Action ?
190         if (nob(action, actionChars)) {
191             msg("Perm Action [" + action + IS_INVALID);
192         }
193         return this;
194     }
195
196     public final Validator role(String user, String role) {
197         boolean quit = false;
198         if(role==null) {
199             msg("Role is null");
200             quit = true;
201         }
202         if(user==null) {
203             msg("User is null");
204             quit = true;
205         }
206         if(!quit) {
207             if(role.startsWith(user) && role.endsWith(":user")) {
208                 if(!(role.length() == user.length() + 5)) {
209                     msg(ROLE_STR + role + IS_INVALID);
210                 }
211             } else if (nob(role, NAME_CHARS)) {
212                 msg(ROLE_STR + role + IS_INVALID);
213             }
214         }
215         return this;
216     }
217
218
219     public final Validator role(String role) {
220         if (nob(role, NAME_CHARS)) {
221             msg(ROLE_STR + role + IS_INVALID);
222         }
223         return this;
224     }
225
226     public final Validator ns(String ns) {
227         if (ns==null) {
228             msg("NS is null");
229             return this;
230         } else if (nob(ns,NAME_CHARS)) {
231             msg("NS [" + ns + IS_INVALID);
232         }
233         for (String s : nsKeywords) {
234             if (ns.endsWith(s)) {
235                 msg("NS [" + ns + "] may not be named with NS keywords");
236                 break;
237             }
238         }
239         return this;
240     }
241
242     public final Validator key(String key) {
243         if (nob(key,NAME_CHARS)) {
244             msg("NS Prop Key [" + key + "] is invalid");
245         }
246         return this;
247     }
248
249     public final Validator value(String value) {
250         if (nob(value,ESSENTIAL_CHARS)) {
251             msg("NS Prop value [" + value + "] is invalid");
252         }
253         return this;
254     }
255
256
257 }