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.split;
19  import static org.codelibs.core.stream.StreamUtil.stream;
20  
21  import java.util.HashSet;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import org.codelibs.core.lang.StringUtil;
26  import org.codelibs.fess.entity.FessUser;
27  import org.codelibs.fess.helper.SystemHelper;
28  import org.codelibs.fess.util.ComponentUtil;
29  import org.lastaflute.web.login.credential.LoginCredential;
30  
31  public class OpenIdConnectCredential implements LoginCredential, FessCredential {
32  
33      private final Map<String, Object> attributes;
34  
35      public OpenIdConnectCredential(final Map<String, Object> attributes) {
36          this.attributes = attributes;
37      }
38  
39      @Override
40      public String toString() {
41          return "{" + getUserId() + "}";
42      }
43  
44      @Override
45      public String getUserId() {
46          return (String) attributes.get("email");
47      }
48  
49      public OpenIdUser getUser() {
50          return new OpenIdUser(getUserId(), getDefaultGroupsAsArray(), getDefaultRolesAsArray());
51      }
52  
53      protected static String[] getDefaultGroupsAsArray() {
54          final String value = ComponentUtil.getFessConfig().getSystemProperty("oic.default.groups");
55          if (StringUtil.isBlank(value)) {
56              return StringUtil.EMPTY_STRINGS;
57          }
58          return split(value, ",").get(stream -> stream.filter(StringUtil::isNotBlank).map(String::trim).toArray(n -> new String[n]));
59      }
60  
61      protected static String[] getDefaultRolesAsArray() {
62          final String value = ComponentUtil.getFessConfig().getSystemProperty("oic.default.roles");
63          if (StringUtil.isBlank(value)) {
64              return StringUtil.EMPTY_STRINGS;
65          }
66          return split(value, ",").get(stream -> stream.filter(StringUtil::isNotBlank).map(String::trim).toArray(n -> new String[n]));
67      }
68  
69      public static class OpenIdUser implements FessUser {
70  
71          private static final long serialVersionUID = 1L;
72  
73          protected final String name;
74  
75          protected String[] groups;
76  
77          protected String[] roles;
78  
79          protected String[] permissions;
80  
81          protected OpenIdUser(final String name, final String[] groups, final String[] roles) {
82              this.name = name;
83              this.groups = groups;
84              this.roles = roles;
85          }
86  
87          @Override
88          public String getName() {
89              return name;
90          }
91  
92          @Override
93          public String[] getRoleNames() {
94              return roles;
95          }
96  
97          @Override
98          public String[] getGroupNames() {
99              return groups;
100         }
101 
102         @Override
103         public String[] getPermissions() {
104             if (permissions == null) {
105                 final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
106                 final Set<String> permissionSet = new HashSet<>();
107                 permissionSet.add(systemHelper.getSearchRoleByUser(name));
108                 stream(groups).of(stream -> stream.forEach(s -> permissionSet.add(systemHelper.getSearchRoleByGroup(s))));
109                 stream(roles).of(stream -> stream.forEach(s -> permissionSet.add(systemHelper.getSearchRoleByRole(s))));
110                 permissions = permissionSet.toArray(new String[permissionSet.size()]);
111             }
112             return permissions;
113         }
114 
115     }
116 }