-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
OAuth2CallbackFilter.java
169 lines (147 loc) · 5.94 KB
/
OAuth2CallbackFilter.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
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.authentication;
import java.io.IOException;
import javax.annotation.CheckForNull;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.sonar.api.platform.Server;
import org.sonar.api.server.authentication.IdentityProvider;
import org.sonar.api.server.authentication.OAuth2IdentityProvider;
import org.sonar.api.server.authentication.UnauthorizedException;
import org.sonar.api.server.authentication.UserIdentity;
import org.sonar.server.authentication.event.AuthenticationEvent;
import org.sonar.server.authentication.event.AuthenticationException;
import static java.lang.String.format;
import static org.sonar.server.authentication.AuthenticationError.handleAuthenticationError;
import static org.sonar.server.authentication.AuthenticationError.handleError;
import static org.sonar.server.authentication.event.AuthenticationEvent.Source;
public class OAuth2CallbackFilter extends AuthenticationFilter {
private final OAuth2ContextFactory oAuth2ContextFactory;
private final AuthenticationEvent authenticationEvent;
public OAuth2CallbackFilter(IdentityProviderRepository identityProviderRepository, OAuth2ContextFactory oAuth2ContextFactory,
Server server, AuthenticationEvent authenticationEvent) {
super(server, identityProviderRepository);
this.oAuth2ContextFactory = oAuth2ContextFactory;
this.authenticationEvent = authenticationEvent;
}
@Override
public UrlPattern doGetPattern() {
return UrlPattern.create(CALLBACK_PATH + "*");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
IdentityProvider provider = resolveProviderOrHandleResponse(httpRequest, httpResponse, CALLBACK_PATH);
if (provider != null) {
handleProvider(httpRequest, (HttpServletResponse) response, provider);
}
}
private void handleProvider(HttpServletRequest request, HttpServletResponse response, IdentityProvider provider) {
try {
if (provider instanceof OAuth2IdentityProvider) {
handleOAuth2Provider(response, request, (OAuth2IdentityProvider) provider);
} else {
handleError(response, format("Not an OAuth2IdentityProvider: %s", provider.getClass()));
}
} catch (AuthenticationException e) {
authenticationEvent.failure(request, e);
handleAuthenticationError(e, response);
} catch (Exception e) {
handleError(e, response, format("Fail to callback authentication with '%s'", provider.getKey()));
}
}
private void handleOAuth2Provider(HttpServletResponse response, HttpServletRequest httpRequest, OAuth2IdentityProvider oAuth2Provider) {
OAuth2CallbackFilter.WrappedContext context = new OAuth2CallbackFilter.WrappedContext(oAuth2ContextFactory.newCallback(httpRequest, response, oAuth2Provider));
try {
oAuth2Provider.callback(context);
} catch (UnauthorizedException e) {
throw AuthenticationException.newBuilder()
.setSource(Source.oauth2(oAuth2Provider))
.setMessage(e.getMessage())
.setPublicMessage(e.getMessage())
.build();
}
if (context.isAuthenticated()) {
authenticationEvent.login(httpRequest, context.getLogin(), Source.oauth2(oAuth2Provider));
} else {
throw AuthenticationException.newBuilder()
.setSource(Source.oauth2(oAuth2Provider))
.setMessage("Plugin did not call authenticate")
.build();
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// Nothing to do
}
@Override
public void destroy() {
// Nothing to do
}
private static final class WrappedContext implements OAuth2IdentityProvider.CallbackContext {
private final OAuth2IdentityProvider.CallbackContext delegate;
private boolean authenticated = false;
@CheckForNull
private String login;
private WrappedContext(OAuth2IdentityProvider.CallbackContext delegate) {
this.delegate = delegate;
}
@Override
public String getCallbackUrl() {
return delegate.getCallbackUrl();
}
@Override
public HttpServletRequest getRequest() {
return delegate.getRequest();
}
@Override
public HttpServletResponse getResponse() {
return delegate.getResponse();
}
@Override
public void verifyCsrfState() {
delegate.verifyCsrfState();
}
@Override
public void redirectToRequestedPage() {
delegate.redirectToRequestedPage();
}
@Override
public void authenticate(UserIdentity userIdentity) {
delegate.authenticate(userIdentity);
this.authenticated = true;
this.login = userIdentity.getLogin();
}
public boolean isAuthenticated() {
return authenticated;
}
@CheckForNull
public String getLogin() {
return login;
}
}
}