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 public static List<String> nsKeywords;
47 protected final Pattern actionChars;
48 protected final Pattern instChars;
49 private StringBuilder msgs;
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");
64 actionChars = ACTION_CHARS;
65 instChars = INST_CHARS;
68 public final String errs() {
69 return msgs.toString();
72 public final Validator nullOrBlank(String name, String str) {
74 msg(name + " is null.");
75 } else if(str.length()==0) {
76 msg(name + " is blank.");
81 public final Validator isNull(String name, Object o) {
83 msg(name + " is null.");
88 protected final boolean noMatch(String str, Pattern p) {
89 return !p.matcher(str).matches();
91 protected final boolean nob(String str, Pattern p) {
92 return str==null || !p.matcher(str).matches();
95 protected final void msg(String ... strs) {
97 msgs=new StringBuilder();
99 for(String str : strs) {
105 public final boolean err() {
109 public final Validator notOK(Result<?> res) {
111 msgs.append("Result object is blank");
112 } else if(res.notOK()) {
113 msgs.append(res.getClass().getSimpleName() + " is not OK");
118 protected Validator intRange(String text, int target, int start, int end) {
119 if(target<start || target>end) {
120 msg(text + " is out of range (" + start + '-' + end + ')');
125 protected Validator floatRange(String text, float target, float start, float end) {
126 if(target<start || target>end) {
127 msg(text + " is out of range (" + start + '-' + end + ')');
132 protected Validator description(String type, String description) {
133 if(description!=null) {
134 if(noMatch(description, DESC_CHAR)) {
135 msg(type + " Description is invalid.");
141 public final Validator permType(String type) {
142 if(nob(type,NAME_CHARS)) {
143 msg("Perm Type [" +type + "] is invalid.");
148 public final Validator permType(String type, String ns) {
150 msg("Perm Type is null");
151 } else if(ns==null) {
152 msg("Perm NS is null");
153 } else if(nob(type,NAME_CHARS)) {
154 msg("Perm Type [" + (ns+(type.length()==0?"":'.'))+type + "] is invalid.");
159 public final Validator permInstance(String instance) {
160 if(nob(instance,instChars)) {
161 msg("Perm Instance [" + instance + "] is invalid.");
166 public final Validator permAction(String action) {
167 // TODO check for correct Splits? Type|Instance|Action ?
168 if(nob(action, actionChars)) {
169 msg("Perm Action [" + action + "] is invalid.");
174 public final Validator role(String role) {
175 if(nob(role, NAME_CHARS)) {
176 msg("Role [" + role + "] is invalid.");
181 public final Validator ns(String ns) {
185 } else if(nob(ns,NAME_CHARS)) {
186 msg("NS [" + ns + "] is invalid.");
188 for(String s : nsKeywords) {
190 msg("NS [" + ns + "] may not be named with NS keywords");
197 public final Validator key(String key) {
198 if(nob(key,NAME_CHARS)) {
199 msg("NS Prop Key [" + key + "] is invalid");
204 public final Validator value(String value) {
205 if(nob(value,ESSENTIAL_CHARS)) {
206 msg("NS Prop value [" + value + "] is invalid");