View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.app.web.base.login;
17  
18  import static org.codelibs.core.stream.StreamUtil.stream;
19  
20  import java.util.HashSet;
21  import java.util.Set;
22  
23  import org.codelibs.core.lang.StringUtil;
24  import org.codelibs.fess.entity.FessUser;
25  import org.codelibs.fess.helper.SystemHelper;
26  import org.codelibs.fess.sso.aad.AzureAdAuthenticator;
27  import org.codelibs.fess.util.ComponentUtil;
28  import org.lastaflute.web.login.credential.LoginCredential;
29  
30  import com.microsoft.aad.adal4j.AuthenticationResult;
31  import com.microsoft.aad.adal4j.UserInfo;
32  
33  public class AzureAdCredential implements LoginCredential, FessCredential {
34  
35      private final AuthenticationResult authResult;
36  
37      public AzureAdCredential(final AuthenticationResult authResult) {
38          this.authResult = authResult;
39      }
40  
41      @Override
42      public String getUserId() {
43          return authResult.getUserInfo().getDisplayableId();
44      }
45  
46      @Override
47      public String toString() {
48          return "{" + authResult.getUserInfo().getDisplayableId() + "}";
49      }
50  
51      public AzureAdUser getUser() {
52          return new AzureAdUser(authResult);
53      }
54  
55      public static class AzureAdUser implements FessUser {
56          private static final long serialVersionUID = 1L;
57  
58          protected String[] groups;
59  
60          protected String[] roles;
61  
62          protected String[] permissions;
63  
64          protected AuthenticationResult authResult;
65  
66          public AzureAdUser(final AuthenticationResult authResult) {
67              this.authResult = authResult;
68              final AzureAdAuthenticator authenticator = ComponentUtil.getComponent(AzureAdAuthenticator.class);
69              authenticator.updateMemberOf(this);
70          }
71  
72          @Override
73          public String getName() {
74              return authResult.getUserInfo().getDisplayableId();
75          }
76  
77          @Override
78          public String[] getRoleNames() {
79              return roles;
80          }
81  
82          @Override
83          public String[] getGroupNames() {
84              return groups;
85          }
86  
87          @Override
88          public String[] getPermissions() {
89              if (permissions == null) {
90                  final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
91                  final Set<String> permissionSet = new HashSet<>();
92                  final UserInfo userInfo = authResult.getUserInfo();
93                  permissionSet.add(systemHelper.getSearchRoleByUser(userInfo.getUniqueId()));
94                  permissionSet.add(systemHelper.getSearchRoleByUser(userInfo.getDisplayableId()));
95                  stream(groups).of(stream -> stream.forEach(s -> permissionSet.add(systemHelper.getSearchRoleByGroup(s))));
96                  stream(roles).of(stream -> stream.forEach(s -> permissionSet.add(systemHelper.getSearchRoleByRole(s))));
97                  permissions = permissionSet.stream().filter(StringUtil::isNotBlank).distinct().toArray(n -> new String[n]);
98              }
99              return permissions;
100         }
101 
102         @Override
103         public boolean refresh() {
104             if (authResult.getExpiresAfter() < System.currentTimeMillis()) {
105                 return false;
106             }
107             final AzureAdAuthenticator authenticator = ComponentUtil.getComponent(AzureAdAuthenticator.class);
108             final String refreshToken = authResult.getRefreshToken();
109             authResult = authenticator.getAccessToken(refreshToken);
110             authenticator.updateMemberOf(this);
111             permissions = null;
112             return true;
113         }
114 
115         public AuthenticationResult getAuthenticationResult() {
116             return authResult;
117         }
118 
119         public void setGroups(final String[] groups) {
120             this.groups = groups;
121         }
122 
123         public void setRoles(final String[] roles) {
124             this.roles = roles;
125         }
126     }
127 }