-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply OpenShift OAuth provider (#15963)
- Loading branch information
Showing
11 changed files
with
229 additions
and
1 deletion.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2012-2018 Red Hat, Inc. | ||
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: | ||
Red Hat, Inc. - initial API and implementation | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<artifactId>che-master-parent</artifactId> | ||
<groupId>org.eclipse.che.core</groupId> | ||
<version>7.10.0-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>che-core-api-auth-openshift</artifactId> | ||
<packaging>jar</packaging> | ||
<name>Che Core :: API :: Authentication OpenShift</name> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.http-client</groupId> | ||
<artifactId>google-http-client</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.inject</groupId> | ||
<artifactId>guice</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.inject</groupId> | ||
<artifactId>javax.inject</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.che.core</groupId> | ||
<artifactId>che-core-api-auth</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.che.core</groupId> | ||
<artifactId>che-core-api-auth-shared</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.che.core</groupId> | ||
<artifactId>che-core-commons-annotations</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
94 changes: 94 additions & 0 deletions
94
...h-openshift/src/main/java/org/eclipse/che/security/oauth/OpenShiftOAuthAuthenticator.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,94 @@ | ||
/* | ||
* Copyright (c) 2012-2018 Red Hat, Inc. | ||
* 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: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.che.security.oauth; | ||
|
||
import static com.google.common.base.Strings.isNullOrEmpty; | ||
|
||
import com.google.api.client.util.store.MemoryDataStoreFactory; | ||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
import javax.inject.Singleton; | ||
import org.eclipse.che.api.auth.shared.dto.OAuthToken; | ||
import org.eclipse.che.commons.annotation.Nullable; | ||
import org.eclipse.che.security.oauth.shared.User; | ||
|
||
/** | ||
* OAuth authentication for OpenShift. | ||
* | ||
* @author Igor Vinokur | ||
*/ | ||
@Singleton | ||
public class OpenShiftOAuthAuthenticator extends OAuthAuthenticator { | ||
private final String verifyTokenUrl; | ||
|
||
@Inject | ||
public OpenShiftOAuthAuthenticator( | ||
@Nullable @Named("che.oauth.openshift.clientid") String clientId, | ||
@Nullable @Named("che.oauth.openshift.clientsecret") String clientSecret, | ||
@Nullable @Named("che.oauth.openshift.oauth_endpoint") String oauthEndpoint, | ||
@Nullable @Named("che.oauth.openshift.verify_token_url") String verifyTokenUrl, | ||
@Named("che.api") String apiEndpoint) | ||
throws IOException { | ||
this.verifyTokenUrl = verifyTokenUrl; | ||
String[] redirectUrl = {apiEndpoint + "/oauth/callback"}; | ||
if (!isNullOrEmpty(clientId) && !isNullOrEmpty(clientSecret) && !isNullOrEmpty(oauthEndpoint)) { | ||
oauthEndpoint = oauthEndpoint.endsWith("/") ? oauthEndpoint : oauthEndpoint + "/"; | ||
configure( | ||
clientId, | ||
clientSecret, | ||
redirectUrl, | ||
oauthEndpoint + "oauth/authorize", | ||
oauthEndpoint + "oauth/token", | ||
new MemoryDataStoreFactory()); | ||
} | ||
} | ||
|
||
@Override | ||
public User getUser(OAuthToken accessToken) throws OAuthAuthenticationException { | ||
throw new OAuthAuthenticationException("not supported"); | ||
} | ||
|
||
@Override | ||
public final String getOAuthProvider() { | ||
return "openshift"; | ||
} | ||
|
||
@Override | ||
public OAuthToken getToken(String userId) throws IOException { | ||
final OAuthToken token = super.getToken(userId); | ||
// Check if the token is valid for requests. | ||
if (!(token == null || token.getToken() == null || token.getToken().isEmpty())) { | ||
HttpURLConnection http = null; | ||
try { | ||
http = (HttpURLConnection) new URL(verifyTokenUrl).openConnection(); | ||
http.setInstanceFollowRedirects(false); | ||
http.setRequestMethod("GET"); | ||
http.setRequestProperty("Authorization", "Bearer " + token.getToken()); | ||
http.setRequestProperty("Accept", "application/json"); | ||
|
||
if (http.getResponseCode() == 401) { | ||
return null; | ||
} | ||
} finally { | ||
if (http != null) { | ||
http.disconnect(); | ||
} | ||
} | ||
|
||
return token; | ||
} | ||
return null; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...api-auth-openshift/src/main/java/org/eclipse/che/security/oauth/OpenShiftOAuthModule.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,29 @@ | ||
/* | ||
* Copyright (c) 2012-2018 Red Hat, Inc. | ||
* 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: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.che.security.oauth; | ||
|
||
import com.google.inject.AbstractModule; | ||
import com.google.inject.multibindings.Multibinder; | ||
|
||
/** | ||
* Setup OpenShiftOAuthAuthenticator in guice container. | ||
* | ||
* @author Igor Vinokur | ||
*/ | ||
public class OpenShiftOAuthModule extends AbstractModule { | ||
@Override | ||
protected void configure() { | ||
Multibinder<OAuthAuthenticator> oAuthAuthenticators = | ||
Multibinder.newSetBinder(binder(), OAuthAuthenticator.class); | ||
oAuthAuthenticators.addBinding().to(OpenShiftOAuthAuthenticator.class); | ||
} | ||
} |
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