fcbccb12da4b3d0b778f29d8563003e9504ff646
[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
51     static {
52         nsKeywords = new ArrayList<>();
53         nsKeywords.add(".access");
54         nsKeywords.add(".owner");
55         nsKeywords.add(".admin");
56         nsKeywords.add(".member");
57         nsKeywords.add(".perm");
58         nsKeywords.add(".role");
59         nsKeywords.add(".ns");
60         nsKeywords.add(".cred");
61     }
62
63     public Validator() {
64         actionChars = ACTION_CHARS;
65         instChars = INST_CHARS;
66     }
67
68     public final String errs() {
69         return msgs.toString();
70     }
71
72     public final Validator nullOrBlank(String name, String str) {
73         if (str==null) {
74             msg(name + " is null.");
75         } else if (str.length()==0) {
76             msg(name + " is blank.");
77         }
78         return this;
79     }
80
81     public final Validator isNull(String name, Object o) {
82         if (o==null) {
83             msg(name + " is null.");
84         }
85         return this;
86     }
87
88     protected final boolean noMatch(String str, Pattern p) {
89         return str==null || !p.matcher(str).matches();
90     }
91
92     protected final void match(String text, String str, Pattern p) {
93         if(str==null || !p.matcher(str).matches()) {
94             msg(text);
95         }
96     }
97
98     protected final boolean nob(String str, Pattern p) {
99         return str==null || !p.matcher(str).matches();
100     }
101
102     protected final void msg(String ... strs) {
103         if (msgs==null) {
104             msgs=new StringBuilder();
105         }
106         for (String str : strs) {
107             msgs.append(str);
108         }
109         msgs.append('\n');
110     }
111
112     public final boolean err() {
113         return msgs!=null;
114     }
115
116     public final Validator notOK(Result<?> res) {
117         if (res==null) {
118             msgs.append("Result object is blank");
119         } else if (res.notOK()) {
120             msgs.append(res.getClass().getSimpleName()).append(" is not OK");
121         }
122         return this;
123     }
124
125     protected Validator intRange(String text, int target, int start, int end) {
126         if (target<start || target>end) {
127             msg(text + " is out of range (" + start + '-' + end + ')');
128         }
129         return this;
130     }
131
132     protected Validator floatRange(String text, float target, float start, float end) {
133         if (target<start || target>end) {
134             msg(text + " is out of range (" + start + '-' + end + ')');
135         }
136         return this;
137     }
138
139     protected Validator description(String type, String description) {
140         if (description != null && noMatch(description, DESC_CHAR)) {
141             msg(type + " Description is invalid.");
142         }
143         return this;
144     }
145
146     public final Validator permType(String type) {
147         if (nob(type,NAME_CHARS)) {
148             msg("Perm Type [" +type + "] is invalid.");
149         }
150         return this;
151     }
152
153     public final Validator permTypeWithUser(String user, String type) {
154         if (type==null) {
155             msg("Perm Type is null");
156         } else if (user==null) {
157             msg("User is null");
158         } else {
159             if(!(type.startsWith(user) && type.endsWith(":id"))) {
160               if(nob(type,NAME_CHARS)) {
161                 msg("Perm Type [" + type + "] is invalid.");
162               }
163             }
164         }
165         return this;
166     }
167
168     public final Validator permType(String type, String ns) {
169         if (type==null) {
170             msg("Perm Type is null");
171         } else if (ns==null) {
172             msg("Perm NS is null");
173         } else if (nob(type,NAME_CHARS)) {
174             msg("Perm Type [" + (ns+(type.length()==0?"":'.')) + type + "] is invalid.");
175         }
176         return this;
177     }
178
179     public final Validator permInstance(String instance) {
180         if(!"/".equals(instance) && nob(instance,instChars)) {
181             msg("Perm Instance [" + instance + "] is invalid.");
182         }
183         return this;
184     }
185
186     public final Validator permAction(String action) {
187         // TODO check for correct Splits?  Type|Instance|Action ?
188         if (nob(action, actionChars)) {
189             msg("Perm Action [" + action + "] is invalid.");
190         }
191         return this;
192     }
193
194     public final Validator role(String user, String role) {
195         boolean quit = false;
196         if(role==null) {
197             msg("Role is null");
198             quit = true;
199         }
200         if(user==null) {
201             msg("User is null");
202             quit = true;
203         }
204         if(!quit) {
205             if(role.startsWith(user) && role.endsWith(":user")) {
206                 if(!(role.length() == user.length() + 5)) {
207                     msg("Role [" + role + "] is invalid.");
208                 }
209             } else if (nob(role, NAME_CHARS)) {
210                 msg("Role [" + role + "] is invalid.");
211             }
212         }
213         return this;
214     }
215
216
217     public final Validator role(String role) {
218         if (nob(role, NAME_CHARS)) {
219             msg("Role [" + role + "] is invalid.");
220         }
221         return this;
222     }
223
224     public final Validator ns(String ns) {
225         if (ns==null) {
226             msg("NS is null");
227             return this;
228         } else if (nob(ns,NAME_CHARS)) {
229             msg("NS [" + ns + "] is invalid.");
230         }
231         for (String s : nsKeywords) {
232             if (ns.endsWith(s)) {
233                 msg("NS [" + ns + "] may not be named with NS keywords");
234                 break;
235             }
236         }
237         return this;
238     }
239
240     public final Validator key(String key) {
241         if (nob(key,NAME_CHARS)) {
242             msg("NS Prop Key [" + key + "] is invalid");
243         }
244         return this;
245     }
246
247     public final Validator value(String value) {
248         if (nob(value,ESSENTIAL_CHARS)) {
249             msg("NS Prop value [" + value + "] is invalid");
250         }
251         return this;
252     }
253
254
255 }