-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
MultiRealmAuthManager.java
187 lines (168 loc) · 6.43 KB
/
MultiRealmAuthManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.server.security.enterprise.auth;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.authc.pam.UnsupportedTokenException;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.mgt.SubjectDAO;
import org.apache.shiro.realm.CachingRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.util.Initializable;
import java.util.Collection;
import java.util.Map;
import org.neo4j.kernel.api.security.AuthenticationResult;
import org.neo4j.kernel.api.security.exception.InvalidAuthTokenException;
import org.neo4j.kernel.enterprise.api.security.EnterpriseAuthSubject;
import org.neo4j.kernel.impl.enterprise.SecurityLog;
import org.neo4j.server.security.auth.UserManagerSupplier;
import static org.neo4j.helpers.Strings.escape;
class MultiRealmAuthManager implements EnterpriseAuthManager, UserManagerSupplier
{
private final EnterpriseUserManager userManager;
private final Collection<Realm> realms;
private final DefaultSecurityManager securityManager;
private final CacheManager cacheManager;
private final SecurityLog securityLog;
MultiRealmAuthManager( EnterpriseUserManager userManager, Collection<Realm> realms, CacheManager cacheManager,
SecurityLog securityLog )
{
this.userManager = userManager;
this.realms = realms;
this.cacheManager = cacheManager;
securityManager = new DefaultSecurityManager( realms );
this.securityLog = securityLog;
securityManager.setSubjectFactory( new ShiroSubjectFactory() );
((ModularRealmAuthenticator) securityManager.getAuthenticator())
.setAuthenticationStrategy( new ShiroAuthenticationStrategy() );
securityManager.setSubjectDAO( createSubjectDAO() );
}
private SubjectDAO createSubjectDAO()
{
DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
DefaultSessionStorageEvaluator sessionStorageEvaluator = new DefaultSessionStorageEvaluator();
sessionStorageEvaluator.setSessionStorageEnabled( false );
subjectDAO.setSessionStorageEvaluator( sessionStorageEvaluator );
return subjectDAO;
}
@Override
public boolean supports( final Map<String,Object> authToken )
{
final ShiroAuthToken token = new ShiroAuthToken( authToken );
return realms.stream().anyMatch( realm -> realm.supports( token ) );
}
@Override
public EnterpriseAuthSubject login( Map<String,Object> authToken ) throws InvalidAuthTokenException
{
EnterpriseAuthSubject subject;
ShiroAuthToken token = new ShiroAuthToken( authToken );
try
{
subject = new StandardEnterpriseAuthSubject( this, (ShiroSubject) securityManager.login( null, token ) );
securityLog.info( subject, "logged in" );
}
catch ( UnsupportedTokenException e )
{
securityLog.error( "Unknown user failed to log in: %s", e.getMessage() );
throw new InvalidAuthTokenException( e.getMessage() );
}
catch ( ExcessiveAttemptsException e )
{
// NOTE: We only get this with single (internal) realm authentication
subject = new StandardEnterpriseAuthSubject( this,
new ShiroSubject( securityManager, AuthenticationResult.TOO_MANY_ATTEMPTS ) );
securityLog.error( "[%s]: failed to log in: too many failed attempts",
escape( token.getPrincipal().toString() ) );
}
catch ( AuthenticationException e )
{
subject = new StandardEnterpriseAuthSubject( this,
new ShiroSubject( securityManager, AuthenticationResult.FAILURE ) );
securityLog.error( "[%s]: failed to log in: invalid principal or credentials",
escape( token.getPrincipal().toString() ) );
}
return subject;
}
@Override
public void init() throws Throwable
{
for ( Realm realm : realms )
{
if ( realm instanceof Initializable )
{
((Initializable) realm).init();
}
if ( realm instanceof CachingRealm )
{
((CachingRealm) realm).setCacheManager( cacheManager );
}
if ( realm instanceof RealmLifecycle )
{
((RealmLifecycle) realm).initialize();
}
}
}
@Override
public void start() throws Throwable
{
for ( Realm realm : realms )
{
if ( realm instanceof RealmLifecycle )
{
((RealmLifecycle) realm).start();
}
}
}
@Override
public void stop() throws Throwable
{
for ( Realm realm : realms )
{
if ( realm instanceof RealmLifecycle )
{
((RealmLifecycle) realm).stop();
}
}
}
@Override
public void shutdown() throws Throwable
{
for ( Realm realm : realms )
{
if ( realm instanceof CachingRealm )
{
((CachingRealm) realm).setCacheManager( null );
}
if ( realm instanceof RealmLifecycle )
{
((RealmLifecycle) realm).shutdown();
}
}
}
@Override
public EnterpriseUserManager getUserManager()
{
return userManager;
}
}