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