-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #44 - adds Auto-Login servlet filter
If enabled redirects from SonarQube's login page to the IdP authentication page. This behaviour can be temporarily disabled by using the URL "<sonarServerBaseURL>/?auto-login=false" in a new browser session (without cookie from previous SonarQube login).
- Loading branch information
Showing
9 changed files
with
320 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/org/vaulttec/sonarqube/auth/oidc/AutoLoginFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* OpenID Connect Authentication for SonarQube | ||
* Copyright (c) 2021 Torsten Juergeleit | ||
* mailto:torsten AT vaulttec DOT org | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.vaulttec.sonarqube.auth.oidc; | ||
|
||
import java.io.IOException; | ||
|
||
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.web.ServletFilter; | ||
|
||
public class AutoLoginFilter extends ServletFilter { | ||
|
||
private static final String LOGIN_URL = "/sessions/new"; | ||
private static final String OIDC_URL = "/sessions/init/" + OidcIdentityProvider.KEY + "?return_to=/projects"; | ||
private static final String SKIP_REQUEST_PARAM = "auto_login=false"; | ||
|
||
private final OidcConfiguration config; | ||
|
||
public AutoLoginFilter(OidcConfiguration config) { | ||
this.config = config; | ||
} | ||
|
||
@Override | ||
public UrlPattern doGetPattern() { | ||
return UrlPattern.create(LOGIN_URL); | ||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
throws IOException, ServletException { | ||
if (config.isEnabled() && config.isAutoLogin()) { | ||
if (request instanceof HttpServletRequest) { | ||
String referrer = ((HttpServletRequest) request).getHeader("referer"); | ||
|
||
// Skip if disabled via request parameter | ||
if (referrer == null || !referrer.endsWith(SKIP_REQUEST_PARAM)) { | ||
((HttpServletResponse) response).sendRedirect(config.getBaseUrl() + OIDC_URL); | ||
return; | ||
} | ||
} | ||
} | ||
chain.doFilter(request, response); | ||
} | ||
|
||
@Override | ||
public void init(FilterConfig filterConfig) throws ServletException { | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
property.category.security.oidc=OpenID Connect | ||
property.category.security.oidc.description=In order to enable OpenID Connect authentication:<ul><li>The property 'sonar.core.serverBaseURL' must be set to a URL <strong>WITHOUT A TRAILING SLASH</strong></li></ul> | ||
property.category.security.oidc.description=In order to enable OpenID Connect authentication:<ul>\ | ||
<li>The property 'sonar.core.serverBaseURL' must be set to a URL <strong>WITHOUT A TRAILING SLASH</strong></li>\ | ||
<li>If the Auto-Login feature is enabled then the property 'sonar.forceAuthentication' must be set to 'true' as well</li>\ | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.