-
Notifications
You must be signed in to change notification settings - Fork 160
/
JwtAuthenticatingRealm.java
181 lines (157 loc) · 6.03 KB
/
JwtAuthenticatingRealm.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
/*******************************************************************************
* Copyright (c) 2016, 2021 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
* Red Hat Inc
*******************************************************************************/
package org.eclipse.kapua.service.authentication.shiro.realm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.util.Destroyable;
import org.eclipse.kapua.KapuaRuntimeException;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.plugin.sso.openid.JwtProcessor;
import org.eclipse.kapua.plugin.sso.openid.exception.OpenIDException;
import org.eclipse.kapua.service.account.Account;
import org.eclipse.kapua.service.authentication.ApiKeyCredentials;
import org.eclipse.kapua.service.authentication.credential.Credential;
import org.eclipse.kapua.service.authentication.credential.CredentialStatus;
import org.eclipse.kapua.service.authentication.credential.CredentialType;
import org.eclipse.kapua.service.authentication.credential.shiro.CredentialImpl;
import org.eclipse.kapua.service.authentication.shiro.JwtCredentialsImpl;
import org.eclipse.kapua.service.authentication.shiro.utils.JwtProcessors;
import org.eclipse.kapua.service.user.User;
import org.eclipse.kapua.service.user.UserService;
import org.jose4j.jwt.consumer.JwtContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* {@link ApiKeyCredentials} based {@link AuthenticatingRealm} implementation.
*
* @since 1.0.0
*/
public class JwtAuthenticatingRealm extends KapuaAuthenticatingRealm implements Destroyable {
private static final Logger LOG = LoggerFactory.getLogger(JwtAuthenticatingRealm.class);
/**
* Realm name.
*/
public static final String REALM_NAME = "jwtAuthenticatingRealm";
/**
* JWT Processor.
*/
private JwtProcessor jwtProcessor;
/**
* Constructor.
*
* @since 1.0.0
*/
public JwtAuthenticatingRealm() {
setName(REALM_NAME);
}
@Override
protected void onInit() {
super.onInit();
try {
jwtProcessor = JwtProcessors.createDefault();
setCredentialsMatcher(new JwtCredentialsMatcher(jwtProcessor));
} catch (OpenIDException se) {
throw new ShiroException("Unexpected error while creating Jwt Processor!", se);
}
}
@Override
public void destroy() throws Exception {
if (jwtProcessor != null) {
jwtProcessor.close();
jwtProcessor = null;
}
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//
// Extract credentials
JwtCredentialsImpl token = (JwtCredentialsImpl) authenticationToken;
String idToken = token.getIdToken();
//
// Get Services
final KapuaLocator locator;
final UserService userService;
try {
locator = KapuaLocator.getInstance();
userService = locator.getService(UserService.class);
} catch (KapuaRuntimeException kre) {
throw new ShiroException("Unexpected error while loading KapuaServices!", kre);
}
String id = extractExternalId(idToken);
LOG.debug("JWT contains external id: {}", id);
//
// Get the associated user by external id
final User user;
try {
user = KapuaSecurityUtils.doPrivileged(() -> userService.findByExternalId(id));
} catch (AuthenticationException ae) {
throw ae;
} catch (Exception e) {
throw new ShiroException("Unexpected error while looking for the user", e);
}
//
// Check user
checkUser(user);
//
// Check account
Account account = checkAccount(user.getScopeId());
//
// Create credential
Credential credential = new CredentialImpl(user.getScopeId(), user.getId(), CredentialType.JWT, idToken, CredentialStatus.ENABLED, null);
// Build AuthenticationInfo
return new LoginAuthenticationInfo(getName(),
account,
user,
credential,
null);
}
/**
* Extract the subject information
*
* @param jwt the token to use
* @return the subject, never returns {@code null}
* @throws ShiroException in case the subject could not be extracted
*/
private String extractExternalId(String jwt) {
final String id;
try {
final JwtContext ctx = jwtProcessor.process(jwt);
id = ctx.getJwtClaims().getClaimValueAsString(jwtProcessor.getExternalIdClaimName());
} catch (Exception e) {
throw new ShiroException("Failed to parse JWT", e);
}
if (id == null || id.isEmpty()) {
throw new ShiroException("'subject' missing on JWT");
}
return id;
}
@Override
protected void assertCredentialsMatch(AuthenticationToken authcToken, AuthenticationInfo info)
throws AuthenticationException {
LoginAuthenticationInfo kapuaInfo = (LoginAuthenticationInfo) info;
super.assertCredentialsMatch(authcToken, info);
//
// Populate Session with info
populateSession(SecurityUtils.getSubject(), kapuaInfo);
}
@Override
public boolean supports(AuthenticationToken authenticationToken) {
return authenticationToken instanceof JwtCredentialsImpl;
}
}