d6dfacbdb4333acd7b72b4747fd1f394170b8820
[clamp.git] / src / main / java / org / onap / clamp / util / PrincipalUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * Modifications copyright (c) 2018 Nokia
21  * ===================================================================
22  *
23  */
24
25 package org.onap.clamp.util;
26
27 import java.util.Date;
28
29 import org.onap.clamp.clds.service.DefaultUserNameHandler;
30 import org.onap.clamp.clds.service.UserNameHandler;
31 import org.onap.clamp.clds.util.LoggingUtils;
32 import org.springframework.security.core.context.SecurityContext;
33 import org.springframework.security.core.context.SecurityContextHolder;
34 import org.springframework.security.core.userdetails.UserDetails;
35
36 public class PrincipalUtils {
37     private static UserNameHandler userNameHandler = new DefaultUserNameHandler();
38     private static SecurityContext securityContext = SecurityContextHolder.getContext();
39
40     /**
41      * Private constructor to avoid creating instances of util class.
42      */
43     private PrincipalUtils(){
44     }
45
46     /**
47      * Get the Full name.
48      *
49      * @return The user name
50      */
51     public static String getUserName() {
52         String name = userNameHandler.retrieveUserName(securityContext);
53         Date startTime = new Date();
54         LoggingUtils.setTargetContext("CLDS", "getUserName");
55         LoggingUtils.setTimeContext(startTime, new Date());
56         return name;
57     }
58
59     /**
60      * Get the userId from AAF/CSP.
61      *
62      * @return The user ID
63      */
64     public static String getUserId() {
65         return getUserName();
66     }
67
68     /**
69      * Get the principal name.
70      *
71      * @return The principal name
72      */
73     public static String getPrincipalName() {
74         String principal = ((UserDetails)securityContext.getAuthentication().getPrincipal()).getUsername();
75         String name = "Not found";
76         if (principal != null) {
77             name = principal;
78         }
79         return name;
80     }
81
82     public static void setSecurityContext(SecurityContext securityContext) {
83         PrincipalUtils.securityContext = securityContext;
84     }
85
86     public static SecurityContext getSecurityContext() {
87         return securityContext;
88     }
89 }