/
KapuaTokenAuthenticationFilter.java
82 lines (69 loc) · 3.16 KB
/
KapuaTokenAuthenticationFilter.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
/*******************************************************************************
* 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
*******************************************************************************/
package org.eclipse.kapua.app.api.core.auth;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.web.filter.authc.AuthenticatingFilter;
import org.apache.shiro.web.util.WebUtils;
import org.eclipse.kapua.KapuaRuntimeException;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.service.authentication.AccessTokenCredentials;
import org.eclipse.kapua.service.authentication.CredentialsFactory;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class KapuaTokenAuthenticationFilter extends AuthenticatingFilter {
private static final String OPTIONS = "OPTIONS";
private static final String AUTHORIZATION_HEADER = "Authorization";
private static final String BEARER = "Bearer";
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
if (OPTIONS.equals(((HttpServletRequest) request).getMethod())) {
return true;
}
try {
return executeLogin(request, response);
} catch (AuthenticationException ae) {
return onLoginFailure(null, ae, request, response);
} catch (Exception e) {
throw KapuaRuntimeException.internalError(e);
}
}
@Override
protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) {
//
// Extract token
HttpServletRequest httpRequest = (HttpServletRequest) request;
String authorizationHeader = httpRequest.getHeader(AUTHORIZATION_HEADER);
String tokenId = null;
if (authorizationHeader != null) {
tokenId = httpRequest.getHeader(AUTHORIZATION_HEADER).replace(BEARER + " ", "");
}
//
// Build AccessToken for Shiro Auth
KapuaLocator locator = KapuaLocator.getInstance();
CredentialsFactory credentialsFactory = locator.getFactory(CredentialsFactory.class);
AccessTokenCredentials accessTokenCredentials = credentialsFactory.newAccessTokenCredentials(tokenId);
//
// Return token
return (AuthenticationToken) accessTokenCredentials;
}
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
HttpServletResponse httpResponse = WebUtils.toHttp(response);
httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
// Continue with the filter chain, because CORS headers are still needed
return true;
}
}