2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.validation;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.regex.Pattern;
28 import org.onap.aaf.auth.layer.Result;
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+
38 public static final Pattern INST_CHARS = Pattern.compile(
39 "["+ESSENTIAL+"]+[\\*]*" + // All AlphaNumeric+ possibly ending with *
41 "|(([:/]\\*)|([:/][!]{0,1}["+ESSENTIAL+"]+[\\*]*[:/]*))+" // Key :asdf:*:sdf*:sdk
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 [";
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");
66 actionChars = ACTION_CHARS;
67 instChars = INST_CHARS;
70 public final String errs() {
71 return msgs.toString();
74 public final Validator nullOrBlank(String name, String str) {
76 msg(name + " is null.");
77 } else if (str.length()==0) {
78 msg(name + " is blank.");
83 public final Validator isNull(String name, Object o) {
85 msg(name + " is null.");
90 protected final boolean noMatch(String str, Pattern p) {
91 return str==null || !p.matcher(str).matches();
94 protected final void match(String text, String str, Pattern p) {
95 if(str==null || !p.matcher(str).matches()) {
100 protected final boolean nob(String str, Pattern p) {
101 return str==null || !p.matcher(str).matches();
104 protected final void msg(String ... strs) {
106 msgs=new StringBuilder();
108 for (String str : strs) {
114 public final boolean err() {
118 public final Validator notOK(Result<?> res) {
120 msgs.append("Result object is blank");
121 } else if (res.notOK()) {
122 msgs.append(res.getClass().getSimpleName()).append(" is not OK");
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 + ')');
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 + ')');
141 protected Validator description(String type, String description) {
142 if (description != null && noMatch(description, DESC_CHAR)) {
143 msg(type + " Description is invalid.");
148 public final Validator permType(String type) {
149 if (nob(type,NAME_CHARS)) {
150 msg(PERM_TYPE +type + IS_INVALID);
155 public final Validator permTypeWithUser(String user, String type) {
157 msg("Perm Type is null");
158 } else if (user==null) {
161 if(!(type.startsWith(user) && type.endsWith(":id"))) {
162 if(nob(type,NAME_CHARS)) {
163 msg(PERM_TYPE + type + IS_INVALID);
170 public final Validator permType(String type, String ns) {
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);
181 public final Validator permInstance(String instance) {
182 if(!"/".equals(instance) && nob(instance,instChars)) {
183 msg("Perm Instance [" + instance + IS_INVALID);
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);
196 public final Validator role(String user, String role) {
197 boolean quit = false;
207 if(role.startsWith(user) && role.endsWith(":user")) {
208 if(!(role.length() == user.length() + 5)) {
209 msg(ROLE_STR + role + IS_INVALID);
211 } else if (nob(role, NAME_CHARS)) {
212 msg(ROLE_STR + role + IS_INVALID);
219 public final Validator role(String role) {
220 if (nob(role, NAME_CHARS)) {
221 msg(ROLE_STR + role + IS_INVALID);
226 public final Validator ns(String ns) {
230 } else if (nob(ns,NAME_CHARS)) {
231 msg("NS [" + ns + IS_INVALID);
233 for (String s : nsKeywords) {
234 if (ns.endsWith(s)) {
235 msg("NS [" + ns + "] may not be named with NS keywords");
242 public final Validator key(String key) {
243 if (nob(key,NAME_CHARS)) {
244 msg("NS Prop Key [" + key + "] is invalid");
249 public final Validator value(String value) {
250 if (nob(value,ESSENTIAL_CHARS)) {
251 msg("NS Prop value [" + value + "] is invalid");