Skip to content

Commit

Permalink
Set Github token through workspace master Rest API. Added a force act… (
Browse files Browse the repository at this point in the history
#4438)

Set Github token through workspace master Rest API. Added a force activation property variable to register Github Oauth provider even without client id/secret

Signed-off-by: Sun Seng David Tan <sutan@redhat.com>
  • Loading branch information
sunix committed Jun 13, 2017
1 parent b91606c commit 5efb620
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ che.oauth.github.clientsecret=NULL
che.oauth.github.authuri= https://github.com/login/oauth/authorize
che.oauth.github.tokenuri= https://github.com/login/oauth/access_token
che.oauth.github.redirecturis= http://localhost:${SERVER_PORT}/wsmaster/api/oauth/callback

# register github even without client id and secret
che.oauth.github.forceactivation=false

### DOCKER PARAMETERS
# Docker is the default machine implementation within Che. Workspaces are powered by machines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,32 @@
import java.net.URL;

import static com.google.common.base.Strings.isNullOrEmpty;

/** OAuth authentication for github account. */
@Singleton
public class GitHubOAuthAuthenticator extends OAuthAuthenticator {
@Inject
public GitHubOAuthAuthenticator(@Nullable @Named("che.oauth.github.clientid") String clientId,
@Nullable @Named("che.oauth.github.clientsecret") String clientSecret,
@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 {
if (!isNullOrEmpty(clientId)
&& !isNullOrEmpty(clientSecret)
&& !isNullOrEmpty(authUri)
&& !isNullOrEmpty(tokenUri)
public GitHubOAuthAuthenticator(@Nullable @Named("che.oauth.github.clientid") String clientId, //
@Nullable @Named("che.oauth.github.clientsecret") String clientSecret, //
@Nullable @Named("che.oauth.github.redirecturis") String[] redirectUris, //
@Nullable @Named("che.oauth.github.authuri") String authUri, //
@Nullable @Named("che.oauth.github.tokenuri") String tokenUri, //
@Named("che.oauth.github.forceactivation") boolean forceActivation) throws IOException {
if (!isNullOrEmpty(clientId) //
&& !isNullOrEmpty(clientSecret) //
&& !isNullOrEmpty(authUri) //
&& !isNullOrEmpty(tokenUri) //
&& redirectUris != null && redirectUris.length != 0) {

configure(clientId, clientSecret, redirectUris, authUri, tokenUri, new MemoryDataStoreFactory());
return;
}

if (forceActivation && //
!isNullOrEmpty(authUri) && //
!isNullOrEmpty(tokenUri) && //
redirectUris != null && redirectUris.length != 0) {

configure("NULL", "NULL", redirectUris, authUri, tokenUri, new MemoryDataStoreFactory());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@

import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
Expand Down Expand Up @@ -203,4 +205,27 @@ protected OAuthAuthenticator getAuthenticator(String oauthProviderName) throws B
}
return oauth;
}

@POST
@Path("token")
@Consumes(MediaType.APPLICATION_JSON)
public void setToken(@Required @QueryParam("oauth_provider") String oauthProvider, //
OAuthToken token) throws ServerException {
if (token == null) {
throw new ServerException("No token provided");
}

OAuthAuthenticator provider = providers.getAuthenticator(oauthProvider);
if (provider == null) {
throw new ServerException("\"" + oauthProvider + "\" oauth provider not registered");
}

String userId = EnvironmentContext.getCurrent().getSubject().getUserId();

try {
provider.setToken(userId, token);
} catch (IOException e) {
throw new ServerException(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.google.api.client.auth.oauth2.BearerToken;
import com.google.api.client.auth.oauth2.ClientParametersAuthentication;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.auth.oauth2.StoredCredential;
import com.google.api.client.auth.oauth2.TokenResponse;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.javanet.NetHttpTransport;
Expand Down Expand Up @@ -320,4 +321,10 @@ public boolean invalidateToken(String userId) throws IOException {
public boolean isConfigured() {
return flow != null;
}

public void setToken(String userId, OAuthToken token) throws IOException {
flow.createAndStoreCredential(new TokenResponse().setAccessToken(token.getToken()) //
.setScope(token.getScope()), //
userId);
}
}

0 comments on commit 5efb620

Please sign in to comment.