1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.codelibs.fess.util.DocumentUtil;
30 import org.lastaflute.web.login.credential.LoginCredential;
31
32
33
34
35 public class OpenIdConnectCredential implements LoginCredential, FessCredential {
36
37 private final Map<String, Object> attributes;
38
39
40
41
42
43
44 public OpenIdConnectCredential(final Map<String, Object> attributes) {
45 this.attributes = attributes;
46 }
47
48 @Override
49 public String toString() {
50 return "{" + getUserId() + "}";
51 }
52
53 @Override
54 public String getUserId() {
55 return DocumentUtil.getValue(attributes, "email", String.class);
56 }
57
58
59
60
61
62
63 public String[] getUserGroups() {
64 String[] userGroups = DocumentUtil.getValue(attributes, "groups", String[].class);
65 if (userGroups == null) {
66 userGroups = getDefaultGroupsAsArray();
67 }
68 return userGroups;
69 }
70
71
72
73
74
75
76 public OpenIdUser getUser() {
77 return new OpenIdUser(getUserId(), getUserGroups(), getDefaultRolesAsArray());
78 }
79
80
81
82
83
84
85 protected static String[] getDefaultGroupsAsArray() {
86 final String value = ComponentUtil.getFessConfig().getSystemProperty("oic.default.groups");
87 if (StringUtil.isBlank(value)) {
88 return StringUtil.EMPTY_STRINGS;
89 }
90 return split(value, ",").get(stream -> stream.filter(StringUtil::isNotBlank).map(String::trim).toArray(n -> new String[n]));
91 }
92
93
94
95
96
97
98 protected static String[] getDefaultRolesAsArray() {
99 final String value = ComponentUtil.getFessConfig().getSystemProperty("oic.default.roles");
100 if (StringUtil.isBlank(value)) {
101 return StringUtil.EMPTY_STRINGS;
102 }
103 return split(value, ",").get(stream -> stream.filter(StringUtil::isNotBlank).map(String::trim).toArray(n -> new String[n]));
104 }
105
106
107
108
109 public static class OpenIdUser implements FessUser {
110
111 private static final long serialVersionUID = 1L;
112
113
114 protected final String name;
115
116
117 protected String[] groups;
118
119
120 protected String[] roles;
121
122
123 protected String[] permissions;
124
125
126
127
128
129
130
131
132 protected OpenIdUser(final String name, final String[] groups, final String[] roles) {
133 this.name = name;
134 this.groups = groups;
135 this.roles = roles;
136 }
137
138 @Override
139 public String getName() {
140 return name;
141 }
142
143 @Override
144 public String[] getRoleNames() {
145 return roles;
146 }
147
148 @Override
149 public String[] getGroupNames() {
150 return groups;
151 }
152
153 @Override
154 public String[] getPermissions() {
155 if (permissions == null) {
156 final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
157 final Set<String> permissionSet = new HashSet<>();
158 permissionSet.add(systemHelper.getSearchRoleByUser(name));
159 stream(groups).of(stream -> stream.forEach(s -> permissionSet.add(systemHelper.getSearchRoleByGroup(s))));
160 stream(roles).of(stream -> stream.forEach(s -> permissionSet.add(systemHelper.getSearchRoleByRole(s))));
161 permissions = permissionSet.toArray(new String[permissionSet.size()]);
162 }
163 return permissions;
164 }
165
166 }
167 }