-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: github oauth with embededOAuthAPI
Signed-off-by: Pavol Baran <pbaran@redhat.com>
- Loading branch information
Showing
8 changed files
with
232 additions
and
117 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
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
103 changes: 103 additions & 0 deletions
103
...github/src/main/java/org/eclipse/che/security/oauth/GitHubOAuthAuthenticatorProvider.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,103 @@ | ||
/* | ||
* Copyright (c) 2012-2021 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.common.annotations.VisibleForTesting; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
import javax.inject.Provider; | ||
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; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@Singleton | ||
public class GitHubOAuthAuthenticatorProvider implements Provider<OAuthAuthenticator> { | ||
private static final Logger LOG = LoggerFactory.getLogger(GitHubOAuthAuthenticatorProvider.class); | ||
public static final String GITHUB_CLIENT_ID_PATH = "/che-conf/oauth/github/id"; | ||
public static final String GITHUB_CLIENT_SECRET_PATH = "/che-conf/oauth/github/secret"; | ||
|
||
private final OAuthAuthenticator authenticator; | ||
|
||
@Inject | ||
public GitHubOAuthAuthenticatorProvider( | ||
@Nullable @Named("che.oauth.github.redirecturis") String[] redirectUris, | ||
@Nullable @Named("che.oauth.github.authuri") String authUri, | ||
@Nullable @Named("che.oauth.github.tokenuri") String tokenUri) | ||
throws IOException { | ||
authenticator = | ||
getOAuthAuthenticator( | ||
redirectUris, authUri, tokenUri, GITHUB_CLIENT_ID_PATH, GITHUB_CLIENT_SECRET_PATH); | ||
|
||
} | ||
|
||
@Override | ||
public OAuthAuthenticator get() { | ||
return authenticator; | ||
} | ||
|
||
@VisibleForTesting | ||
OAuthAuthenticator getOAuthAuthenticator( | ||
String[] redirectUris, | ||
String authUri, | ||
String tokenUri, | ||
String clientIdPath, | ||
String clientSecretPath) | ||
throws IOException { | ||
|
||
if (isNullOrEmpty(authUri) | ||
|| isNullOrEmpty(tokenUri) | ||
|| Objects.isNull(redirectUris) | ||
|| redirectUris.length == 0) { | ||
return new NoopOAuthAuthenticator(); | ||
} | ||
|
||
String clientId, clientSecret; | ||
try { | ||
clientId = Files.readString(Path.of(clientIdPath)); | ||
clientSecret = Files.readString(Path.of(clientSecretPath)); | ||
} catch (IOException e) { | ||
LOG.debug( | ||
"Files with GitHub credentials cannot be accessed. NoopOAuthAuthenticator will be used."); | ||
return new NoopOAuthAuthenticator(); | ||
} | ||
|
||
if (isNullOrEmpty(clientId) || isNullOrEmpty(clientSecret)) { | ||
LOG.debug( | ||
"A file containing GitHub credentials is empty. NoopOAuthAuthenticator will be used."); | ||
return new NoopOAuthAuthenticator(); | ||
} | ||
|
||
return new GitHubOAuthAuthenticator(clientId, clientSecret, redirectUris, authUri, tokenUri); | ||
} | ||
|
||
static class NoopOAuthAuthenticator extends OAuthAuthenticator { | ||
@Override | ||
public User getUser(OAuthToken accessToken) throws OAuthAuthenticationException { | ||
throw new OAuthAuthenticationException( | ||
"The fallback noop authenticator cannot be used for GitHub authentication. Make sure OAuth is properly configured."); | ||
} | ||
|
||
@Override | ||
public String getOAuthProvider() { | ||
return "Noop"; | ||
} | ||
} | ||
} |
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.