Batch, Remove unneeded Classes, refine, etc
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / org / Organization.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.org;
23
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.GregorianCalendar;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30
31 import org.onap.aaf.auth.env.AuthzTrans;
32
33 /**
34  * Organization
35  * 
36  * There is Organizational specific information required which we have extracted to a plugin
37  * 
38  * It supports using Company Specific User Directory lookups, as well as supporting an
39  * Approval/Validation Process to simplify control of Roles and Permissions for large organizations
40  * in lieu of direct manipulation by a set of Admins. 
41  *  
42  * @author Jonathan
43  *
44  */
45 public interface Organization {
46     public static final String N_A = "n/a";
47
48     public interface Identity {
49         public String id();
50         public String fullID() throws OrganizationException; // Fully Qualified ID (includes Domain of Organization)
51         public String type();                 // Must be one of "IdentityTypes", see below
52         public Identity responsibleTo() throws OrganizationException;         // Chain of Command, or Application ID Sponsor
53         public List<String> delegate();         // Someone who has authority to act on behalf of Identity
54         public String email();
55         public String fullName();
56         public String firstName();
57         /**
58          * If Responsible entity, then String returned is "null"  meaning "no Objection".  
59          * If String exists, it is the Policy objection text setup by the entity.
60          * @return
61          */
62         public String mayOwn();            // Is id passed belong to a person suitable to be Responsible for content Management
63         public boolean isFound();                // Is Identity found in Identity stores
64         public boolean isPerson();                // Whether a Person or a Machine (App)
65         public Organization org();                 // Organization of Identity
66
67
68         public static String mixedCase(String in) {
69                 StringBuilder sb = new StringBuilder();
70                 for(int i=0;i<in.length();++i) {
71                         if(i==0) {
72                                 sb.append(Character.toUpperCase(in.charAt(i)));
73                         } else {
74                                 sb.append(Character.toLowerCase(in.charAt(i)));
75                         }
76                 }
77                 return sb.toString();
78         }
79     }
80
81
82     /**
83      * Name of Organization, suitable for Logging
84      * @return
85      */
86     public String getName();
87
88     /**
89      * Realm, for use in distinguishing IDs from different systems/Companies
90      * @return
91      */
92     public String getRealm();
93     
94     public boolean supportsRealm(String user);
95
96     public void addSupportedRealm(String r);
97
98     public String getDomain();
99
100     /**
101      * Get Identity information based on userID
102      * 
103      * @param id
104      * @return
105      */
106     public Identity getIdentity(AuthzTrans trans, String id) throws OrganizationException;
107     
108     /**
109      * May AutoDelete
110      * 
111      * Deletion of an Identity that has been removed from an Organization can be dangerous.  Mistakes may have been made 
112      * in the Organization side, a Feed might be corrupted, an API might not be quite right.  
113      * 
114      * The implementation of this method can use a double check of some sort, such as comparsion of missing ID in Organization
115      * feed with a "Deleted ID" feed.  
116      * 
117      * The failure to be in Organization will still be reported, if returned "false", but if true, it is taken as an 
118      * ok to proceed with deletion. 
119      */
120         public boolean mayAutoDelete(AuthzTrans trans, String id);
121
122
123     /**
124      * Does the ID pass Organization Standards
125      * 
126      * Return a Blank (empty) String if empty, otherwise, return a "\n" separated list of 
127      * reasons why it fails
128      * 
129      * @param id
130      * @return
131      */
132     public String isValidID(AuthzTrans trans, String id);
133
134     /**
135      * Return a Blank (empty) String if empty, otherwise, return a "\n" separated list of 
136      * reasons why it fails
137      *  
138      *  Identity is passed in to allow policies regarding passwords that are the same as user ID
139      *  
140      *  any entries for "prev" imply a reset
141      *  
142      * @param id
143      * @param password
144      * @return
145      */
146     public String isValidPassword(final AuthzTrans trans, final String id, final String password, final String ... prev);
147
148     /**
149      * Return a list of Strings denoting Organization Password Rules, suitable for posting on a WebPage with <p>
150      */
151     public String[] getPasswordRules();
152
153     /**
154      * 
155      * @param id
156      * @return
157      */
158     public boolean isValidCred(final AuthzTrans trans, final String id);
159
160     /**
161      * If response is Null, then it is valid.  Otherwise, the Organization specific reason is returned.
162      *  
163      * @param trans
164      * @param policy
165      * @param executor
166      * @param vars
167      * @return
168      * @throws OrganizationException
169      */
170     public String validate(AuthzTrans trans, Policy policy, Executor executor, String ... vars) throws OrganizationException;
171
172     /**
173      * Does your Company distinguish essential permission structures by kind of Identity?
174      * i.e. Employee, Contractor, Vendor 
175      * @return
176      */
177     public Set<String> getIdentityTypes();
178
179     public enum Notify {
180         Approval(1),
181         PasswordExpiration(2),
182         RoleExpiration(3);
183
184         final int id;
185         Notify(int id) {this.id = id;}
186         public int getValue() {return id;}
187         public static Notify from(int type) {
188             for (Notify t : Notify.values()) {
189                 if (t.id==type) {
190                     return t;
191                 }
192             }
193             return null;
194         }
195     }
196
197     public enum Response{
198         OK,
199         ERR_NotImplemented,
200         ERR_UserNotExist,
201         ERR_NotificationFailure,
202         };
203         
204     public enum Expiration {
205         Password,
206         TempPassword, 
207         Future,
208         UserInRole,
209         UserDelegate, 
210         ExtendPassword
211     }
212     
213     public enum Policy {
214         CHANGE_JOB, 
215         LEFT_COMPANY, 
216         CREATE_MECHID, 
217         CREATE_MECHID_BY_PERM_ONLY,
218         OWNS_MECHID,
219         AS_RESPONSIBLE, 
220         MAY_EXTEND_CRED_EXPIRES,
221         MAY_APPLY_DEFAULT_REALM
222     }
223     
224     /**
225      * Notify a User of Action or Info
226      * 
227      * @param type
228      * @param url
229      * @param users (separated by commas)
230      * @param ccs (separated by commas)
231      * @param summary
232      */
233
234     public Response notify(AuthzTrans trans, Notify type, String url, String ids[], String ccs[], String summary, Boolean urgent);
235
236     /**
237      * (more) generic way to send an email
238      * 
239      * @param toList
240      * @param ccList
241      * @param subject
242      * @param body
243      * @param urgent
244      */
245
246     public int sendEmail(AuthzTrans trans, List<String> toList, List<String> ccList, String subject, String body, Boolean urgent) throws OrganizationException;
247
248     /**
249      * whenToValidate
250      * 
251      * Authz support services will ask the Organization Object at startup when it should
252      * kickoff Validation processes given particular types. 
253      * 
254      * This allows the Organization to express Policy
255      * 
256      * Turn off Validation behavior by returning "null"
257      * 
258      */
259     public Date whenToValidate(Notify type, Date lastValidated);
260
261     
262     /**
263      * Expiration
264      * 
265      * Given a Calendar item of Start (or now), set the Expiration Date based on the Policy
266      * based on type.
267      * 
268      * For instance, "Passwords expire in 3 months"
269      * 
270      * The Extra Parameter is used by certain Orgs.
271      * 
272      * For Password, the extra is UserID, so it can check the User Type
273      * 
274      * @param gc
275      * @param exp
276      * @return
277      */
278     public GregorianCalendar expiration(GregorianCalendar gc, Expiration exp, String ... extra);
279     
280     /**
281      * Get Email Warning timing policies
282      * @return
283      */
284     public EmailWarnings emailWarningPolicy();
285
286     /**
287      * 
288      * @param trans
289      * @param user
290      * @return
291      */
292     public List<Identity> getApprovers(AuthzTrans trans, String user) throws OrganizationException ;
293
294     /**
295      * Get Identities for Escalation Level
296      * 1 = self
297      * 2 = expects both self and immediate responsible party
298      * 3 = expects self, immediate report and any higher that the Organization wants to escalate to in the
299      *     hierarchy.
300      *     
301      * Note: this is used to notify of imminent danger of Application's Cred or Role expirations.
302      */
303     public List<Identity> getIDs(AuthzTrans trans, String user, int escalate) throws OrganizationException ;
304     
305
306     /*
307      * 
308      * @param user
309      * @param type
310      * @param users
311      * @return
312     public Response notifyRequest(AuthzTrans trans, String user, Approval type, List<User> approvers);
313     */
314     
315     /**
316      * 
317      * @return
318      */
319     public String getApproverType();
320
321     /*
322      * startOfDay - define for company what hour of day business starts (specifically for password and other expiration which
323      *   were set by Date only.)
324      *    
325      * @return
326      */
327     public int startOfDay();
328
329     /**
330      * implement this method to support any IDs that can have multiple entries in the cred table
331      * NOTE: the combination of ID/expiration date/(encryption type when implemented) must be unique.
332      *          Since expiration date is based on startOfDay for your company, you cannot create many
333      *          creds for the same ID in the same day.
334      * @param id
335      * @return
336      */
337     public boolean canHaveMultipleCreds(String id);
338     
339     boolean isTestEnv();
340
341     public void setTestMode(boolean dryRun);
342
343     public static final Organization NULL = new Organization() 
344     {
345         private final GregorianCalendar gc = new GregorianCalendar(1900, 1, 1);
346         private final List<Identity> nullList = new ArrayList<>();
347         private final Set<String> nullStringSet = new HashSet<>();
348         private String[] nullStringArray = new String[0];
349         private final Identity nullIdentity = new Identity() {
350             List<String> nullUser = new ArrayList<>();
351             @Override
352             public String type() {
353                 return N_A;
354             }
355
356             @Override
357             public String mayOwn() {
358                 return N_A; // negative case
359             }
360             
361             @Override
362             public boolean isFound() {
363                 return false;
364             }
365             
366             @Override
367             public String id() {
368                 return N_A;
369             }
370             
371             @Override
372             public String fullID() {
373                 return N_A;
374             }
375             
376             @Override
377             public String email() {
378                 return N_A;
379             }
380             
381             @Override
382             public List<String> delegate() {
383                 return nullUser;
384             }
385             @Override
386             public String fullName() {
387                 return N_A;
388             }
389             @Override
390             public Organization org() {
391                 return NULL;
392             }
393             @Override
394             public String firstName() {
395                 return N_A;
396             }
397             @Override
398             public boolean isPerson() {
399                 return false;
400             }
401
402             @Override
403             public Identity responsibleTo() {
404                 return null;
405             }
406         };
407         @Override
408         public String getName() {
409             return N_A;
410         }
411     
412         @Override
413         public String getRealm() {
414             return N_A;
415         }
416     
417         @Override
418         public boolean supportsRealm(String r) {
419             return false;
420         }
421
422         @Override
423         public void addSupportedRealm(String r) {
424         }
425
426         @Override
427         public String getDomain() {
428             return N_A;
429         }
430     
431         @Override
432         public Identity getIdentity(AuthzTrans trans, String id) {
433             return nullIdentity;
434         }
435     
436         @Override
437         public String isValidID(final AuthzTrans trans, String id) {
438             return N_A;
439         }
440     
441         @Override
442         public String isValidPassword(final AuthzTrans trans, final String user, final String password, final String... prev) {
443             return N_A;
444         }
445     
446         @Override
447         public Set<String> getIdentityTypes() {
448             return nullStringSet;
449         }
450     
451         @Override
452         public Response notify(AuthzTrans trans, Notify type, String url,
453                 String[] users, String[] ccs, String summary, Boolean urgent) {
454             return Response.ERR_NotImplemented;
455         }
456     
457         @Override
458         public int sendEmail(AuthzTrans trans, List<String> toList, List<String> ccList,
459                 String subject, String body, Boolean urgent) throws OrganizationException {
460             return 0;
461         }
462     
463         @Override
464         public Date whenToValidate(Notify type, Date lastValidated) {
465             return gc.getTime();
466         }
467     
468         @Override
469         public GregorianCalendar expiration(GregorianCalendar gc,
470                 Expiration exp, String... extra) {
471             return gc;
472         }
473     
474         @Override
475         public List<Identity> getApprovers(AuthzTrans trans, String user)
476                 throws OrganizationException {
477             return nullList;
478         }
479     
480         @Override
481         public String getApproverType() {
482             return "";
483         }
484     
485         @Override
486         public int startOfDay() {
487             return 0;
488         }
489     
490         @Override
491         public boolean canHaveMultipleCreds(String id) {
492             return false;
493         }
494     
495         @Override
496         public boolean isValidCred(final AuthzTrans trans, final String id) {
497             return false;
498         }
499     
500         @Override
501         public String validate(AuthzTrans trans, Policy policy, Executor executor, String ... vars)
502                 throws OrganizationException {
503             return "Null Organization rejects all Policies";
504         }
505     
506         @Override
507         public boolean isTestEnv() {
508             return false;
509         }
510     
511         @Override
512         public void setTestMode(boolean dryRun) {
513         }
514
515         @Override
516         public EmailWarnings emailWarningPolicy() {
517             return new EmailWarnings() {
518
519                 @Override
520                 public long credEmailInterval()
521                 {
522                     return 604800000L; // 7 days in millis 1000 * 86400 * 7
523                 }
524                 
525                 @Override
526                 public long roleEmailInterval()
527                 {
528                     return 604800000L; // 7 days in millis 1000 * 86400 * 7
529                 }
530                 
531                 @Override
532                 public long apprEmailInterval() {
533                     return 259200000L; // 3 days in millis 1000 * 86400 * 3
534                 }
535                 
536                 @Override
537                 public long  credExpirationWarning()
538                 {
539                     return( 2592000000L ); // One month, in milliseconds 1000 * 86400 * 30  in milliseconds
540                 }
541                 
542                 @Override
543                 public long roleExpirationWarning()
544                 {
545                     return( 2592000000L ); // One month, in milliseconds 1000 * 86400 * 30  in milliseconds
546                 }
547
548                 @Override
549                 public long emailUrgentWarning()
550                 {
551                     return( 1209600000L ); // Two weeks, in milliseconds 1000 * 86400 * 14  in milliseconds
552                 }
553
554             };
555             
556
557         }
558
559         @Override
560         public String[] getPasswordRules() {
561             return nullStringArray; 
562         }
563         
564         @Override
565         public boolean mayAutoDelete(AuthzTrans trans, String id) {
566                 // provide a corresponding feed that indicates that an ID has been intentionally removed from identities.dat table.
567                 return false;
568         }
569
570                 @Override
571                 public List<Identity> getIDs(AuthzTrans trans, String user, int escalate) throws OrganizationException {
572                         // TODO Auto-generated method stub
573                         return null;
574                 }
575
576     };
577 }
578
579