View Javadoc
1   /*
2    * Copyright 2012-2019 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          } else {
58              return split(value, ",").get(stream -> stream.filter(StringUtil::isNotBlank).map(s -> s.trim()).toArray(n -> new String[n]));
59          }
60      }
61  
62      protected static String[] getDefaultRolesAsArray() {
63          final String value = ComponentUtil.getFessConfig().getSystemProperty("oic.default.roles");
64          if (StringUtil.isBlank(value)) {
65              return StringUtil.EMPTY_STRINGS;
66          } else {
67              return split(value, ",").get(stream -> stream.filter(StringUtil::isNotBlank).map(s -> s.trim()).toArray(n -> new String[n]));
68          }
69      }
70  
71      public static class OpenIdUser implements FessUser {
72  
73          private static final long serialVersionUID = 1L;
74  
75          protected final String name;
76  
77          protected String[] groups;
78  
79          protected String[] roles;
80  
81          protected String[] permissions;
82  
83          protected OpenIdUser(final String name, final String[] groups, final String[] roles) {
84              this.name = name;
85              this.groups = groups;
86              this.roles = roles;
87          }
88  
89          @Override
90          public String getName() {
91              return name;
92          }
93  
94          @Override
95          public String[] getRoleNames() {
96              return roles;
97          }
98  
99          @Override
100         public String[] getGroupNames() {
101             return groups;
102         }
103 
104         @Override
105         public String[] getPermissions() {
106             if (permissions == null) {
107                 final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
108                 final Set<String> permissionSet = new HashSet<>();
109                 permissionSet.add(systemHelper.getSearchRoleByUser(name));
110                 stream(groups).of(stream -> stream.forEach(s -> permissionSet.add(systemHelper.getSearchRoleByGroup(s))));
111                 stream(roles).of(stream -> stream.forEach(s -> permissionSet.add(systemHelper.getSearchRoleByRole(s))));
112                 permissions = permissionSet.toArray(new String[permissionSet.size()]);
113             }
114             return permissions;
115         }
116 
117     }
118 }