Remove Tabs, per Jococo
[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      * Is Revoked
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 comparison of missing ID in Organization
115      * feed with a "Deleted ID" feed.  
116      * 
117      */
118     public boolean isRevoked(AuthzTrans trans, String id);
119
120
121     /**
122      * Does the ID pass Organization Standards
123      * 
124      * Return a Blank (empty) String if empty, otherwise, return a "\n" separated list of 
125      * reasons why it fails
126      * 
127      * @param id
128      * @return
129      */
130     public String isValidID(AuthzTrans trans, String id);
131
132     /**
133      * Return a Blank (empty) String if empty, otherwise, return a "\n" separated list of 
134      * reasons why it fails
135      *  
136      *  Identity is passed in to allow policies regarding passwords that are the same as user ID
137      *  
138      *  any entries for "prev" imply a reset
139      *  
140      * @param id
141      * @param password
142      * @return
143      */
144     public String isValidPassword(final AuthzTrans trans, final String id, final String password, final String ... prev);
145
146     /**
147      * Return a list of Strings denoting Organization Password Rules, suitable for posting on a WebPage with <p>
148      */
149     public String[] getPasswordRules();
150
151     /**
152      * 
153      * @param id
154      * @return
155      */
156     public boolean isValidCred(final AuthzTrans trans, final String id);
157
158     /**
159      * If response is Null, then it is valid.  Otherwise, the Organization specific reason is returned.
160      *  
161      * @param trans
162      * @param policy
163      * @param executor
164      * @param vars
165      * @return
166      * @throws OrganizationException
167      */
168     public String validate(AuthzTrans trans, Policy policy, Executor executor, String ... vars) throws OrganizationException;
169
170     /**
171      * Does your Company distinguish essential permission structures by kind of Identity?
172      * i.e. Employee, Contractor, Vendor 
173      * @return
174      */
175     public Set<String> getIdentityTypes();
176
177     public enum Notify {
178         Approval(1),
179         PasswordExpiration(2),
180         RoleExpiration(3);
181
182         final int id;
183         Notify(int id) {this.id = id;}
184         public int getValue() {return id;}
185         public static Notify from(int type) {
186             for (Notify t : Notify.values()) {
187                 if (t.id==type) {
188                     return t;
189                 }
190             }
191             return null;
192         }
193     }
194
195     public enum Response{
196         OK,
197         ERR_NotImplemented,
198         ERR_UserNotExist,
199         ERR_NotificationFailure,
200         };
201         
202     public enum Expiration {
203         Password,
204         TempPassword, 
205         Future,
206         UserInRole,
207         UserDelegate, 
208         ExtendPassword
209     }
210     
211     public enum Policy {
212         CHANGE_JOB, 
213         LEFT_COMPANY, 
214         CREATE_MECHID, 
215         CREATE_MECHID_BY_PERM_ONLY,
216         OWNS_MECHID,
217         AS_RESPONSIBLE, 
218         MAY_EXTEND_CRED_EXPIRES,
219         MAY_APPLY_DEFAULT_REALM
220     }
221     
222     /**
223      * Notify a User of Action or Info
224      * 
225      * @param type
226      * @param url
227      * @param users (separated by commas)
228      * @param ccs (separated by commas)
229      * @param summary
230      */
231
232     public Response notify(AuthzTrans trans, Notify type, String url, String ids[], String ccs[], String summary, Boolean urgent);
233
234     /**
235      * (more) generic way to send an email
236      * 
237      * @param toList
238      * @param ccList
239      * @param subject
240      * @param body
241      * @param urgent
242      */
243
244     public int sendEmail(AuthzTrans trans, List<String> toList, List<String> ccList, String subject, String body, Boolean urgent) throws OrganizationException;
245
246     /**
247      * whenToValidate
248      * 
249      * Authz support services will ask the Organization Object at startup when it should
250      * kickoff Validation processes given particular types. 
251      * 
252      * This allows the Organization to express Policy
253      * 
254      * Turn off Validation behavior by returning "null"
255      * 
256      */
257     public Date whenToValidate(Notify type, Date lastValidated);
258
259     
260     /**
261      * Expiration
262      * 
263      * Given a Calendar item of Start (or now), set the Expiration Date based on the Policy
264      * based on type.
265      * 
266      * For instance, "Passwords expire in 3 months"
267      * 
268      * The Extra Parameter is used by certain Orgs.
269      * 
270      * For Password, the extra is UserID, so it can check the User Type
271      * 
272      * @param gc
273      * @param exp
274      * @return
275      */
276     public GregorianCalendar expiration(GregorianCalendar gc, Expiration exp, String ... extra);
277     
278     /**
279      * Get Email Warning timing policies
280      * @return
281      */
282     public EmailWarnings emailWarningPolicy();
283
284     /**
285      * 
286      * @param trans
287      * @param user
288      * @return
289      */
290     public List<Identity> getApprovers(AuthzTrans trans, String user) throws OrganizationException ;
291
292     /**
293      * Get Identities for Escalation Level
294      * 1 = self
295      * 2 = expects both self and immediate responsible party
296      * 3 = expects self, immediate report and any higher that the Organization wants to escalate to in the
297      *     hierarchy.
298      *     
299      * Note: this is used to notify of imminent danger of Application's Cred or Role expirations.
300      */
301     public List<Identity> getIDs(AuthzTrans trans, String user, int escalate) throws OrganizationException ;
302     
303
304     /*
305      * 
306      * @param user
307      * @param type
308      * @param users
309      * @return
310     public Response notifyRequest(AuthzTrans trans, String user, Approval type, List<User> approvers);
311     */
312     
313     /**
314      * 
315      * @return
316      */
317     public String getApproverType();
318
319     /*
320      * startOfDay - define for company what hour of day business starts (specifically for password and other expiration which
321      *   were set by Date only.)
322      *    
323      * @return
324      */
325     public int startOfDay();
326
327     /**
328      * implement this method to support any IDs that can have multiple entries in the cred table
329      * NOTE: the combination of ID/expiration date/(encryption type when implemented) must be unique.
330      *          Since expiration date is based on startOfDay for your company, you cannot create many
331      *          creds for the same ID in the same day.
332      * @param id
333      * @return
334      */
335     public boolean canHaveMultipleCreds(String id);
336     
337     boolean isTestEnv();
338
339     public void setTestMode(boolean dryRun);
340
341     public static final Organization NULL = new Organization() 
342     {
343         private final GregorianCalendar gc = new GregorianCalendar(1900, 1, 1);
344         private final List<Identity> nullList = new ArrayList<>();
345         private final Set<String> nullStringSet = new HashSet<>();
346         private String[] nullStringArray = new String[0];
347         private final Identity nullIdentity = new Identity() {
348             List<String> nullUser = new ArrayList<>();
349             @Override
350             public String type() {
351                 return N_A;
352             }
353
354             @Override
355             public String mayOwn() {
356                 return N_A; // negative case
357             }
358             
359             @Override
360             public boolean isFound() {
361                 return false;
362             }
363             
364             @Override
365             public String id() {
366                 return N_A;
367             }
368             
369             @Override
370             public String fullID() {
371                 return N_A;
372             }
373             
374             @Override
375             public String email() {
376                 return N_A;
377             }
378             
379             @Override
380             public List<String> delegate() {
381                 return nullUser;
382             }
383             @Override
384             public String fullName() {
385                 return N_A;
386             }
387             @Override
388             public Organization org() {
389                 return NULL;
390             }
391             @Override
392             public String firstName() {
393                 return N_A;
394             }
395             @Override
396             public boolean isPerson() {
397                 return false;
398             }
399
400             @Override
401             public Identity responsibleTo() {
402                 return null;
403             }
404         };
405         @Override
406         public String getName() {
407             return N_A;
408         }
409     
410         @Override
411         public String getRealm() {
412             return N_A;
413         }
414     
415         @Override
416         public boolean supportsRealm(String r) {
417             return false;
418         }
419
420         @Override
421         public void addSupportedRealm(String r) {
422         }
423
424         @Override
425         public String getDomain() {
426             return N_A;
427         }
428     
429         @Override
430         public Identity getIdentity(AuthzTrans trans, String id) {
431             return nullIdentity;
432         }
433     
434         @Override
435         public String isValidID(final AuthzTrans trans, String id) {
436             return N_A;
437         }
438     
439         @Override
440         public String isValidPassword(final AuthzTrans trans, final String user, final String password, final String... prev) {
441             return N_A;
442         }
443     
444         @Override
445         public Set<String> getIdentityTypes() {
446             return nullStringSet;
447         }
448     
449         @Override
450         public Response notify(AuthzTrans trans, Notify type, String url,
451                 String[] users, String[] ccs, String summary, Boolean urgent) {
452             return Response.ERR_NotImplemented;
453         }
454     
455         @Override
456         public int sendEmail(AuthzTrans trans, List<String> toList, List<String> ccList,
457                 String subject, String body, Boolean urgent) throws OrganizationException {
458             return 0;
459         }
460     
461         @Override
462         public Date whenToValidate(Notify type, Date lastValidated) {
463             return gc.getTime();
464         }
465     
466         @Override
467         public GregorianCalendar expiration(GregorianCalendar gc,
468                 Expiration exp, String... extra) {
469             return gc;
470         }
471     
472         @Override
473         public List<Identity> getApprovers(AuthzTrans trans, String user)
474                 throws OrganizationException {
475             return nullList;
476         }
477     
478         @Override
479         public String getApproverType() {
480             return "";
481         }
482     
483         @Override
484         public int startOfDay() {
485             return 0;
486         }
487     
488         @Override
489         public boolean canHaveMultipleCreds(String id) {
490             return false;
491         }
492     
493         @Override
494         public boolean isValidCred(final AuthzTrans trans, final String id) {
495             return false;
496         }
497     
498         @Override
499         public String validate(AuthzTrans trans, Policy policy, Executor executor, String ... vars)
500                 throws OrganizationException {
501             return "Null Organization rejects all Policies";
502         }
503     
504         @Override
505         public boolean isTestEnv() {
506             return false;
507         }
508     
509         @Override
510         public void setTestMode(boolean dryRun) {
511         }
512
513         @Override
514         public EmailWarnings emailWarningPolicy() {
515             return new EmailWarnings() {
516
517                 @Override
518                 public long credEmailInterval()
519                 {
520                     return 604800000L; // 7 days in millis 1000 * 86400 * 7
521                 }
522                 
523                 @Override
524                 public long roleEmailInterval()
525                 {
526                     return 604800000L; // 7 days in millis 1000 * 86400 * 7
527                 }
528                 
529                 @Override
530                 public long apprEmailInterval() {
531                     return 259200000L; // 3 days in millis 1000 * 86400 * 3
532                 }
533                 
534                 @Override
535                 public long  credExpirationWarning()
536                 {
537                     return( 2592000000L ); // One month, in milliseconds 1000 * 86400 * 30  in milliseconds
538                 }
539                 
540                 @Override
541                 public long roleExpirationWarning()
542                 {
543                     return( 2592000000L ); // One month, in milliseconds 1000 * 86400 * 30  in milliseconds
544                 }
545
546                 @Override
547                 public long emailUrgentWarning()
548                 {
549                     return( 1209600000L ); // Two weeks, in milliseconds 1000 * 86400 * 14  in milliseconds
550                 }
551
552             };
553             
554
555         }
556
557         @Override
558         public String[] getPasswordRules() {
559             return nullStringArray; 
560         }
561         
562         @Override
563         public boolean isRevoked(AuthzTrans trans, String id) {
564             // provide a corresponding feed that indicates that an ID has been intentionally removed from identities.dat table.
565             return false;
566         }
567
568         @Override
569         public List<Identity> getIDs(AuthzTrans trans, String user, int escalate) throws OrganizationException {
570             // TODO Auto-generated method stub
571             return null;
572         }
573
574     };
575 }
576
577