Skip to content
This repository has been archived by the owner on Dec 21, 2022. It is now read-only.

Commit

Permalink
Add an endpoint to print the id and refresh token
Browse files Browse the repository at this point in the history
  • Loading branch information
jebeaudet committed Nov 21, 2017
1 parent 81d6f13 commit b20fab7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ k8s:
### Command line argument
You can inject the required parameter on the command line as such :
```
java -jar -Dgoogle.clientId=something -Dgoogle.clientSecret=secret -Dgoogle.authorizeUrl=https://accounts.google.com/o/oauth2/auth -Dgoogle.tokenUrl=https://www.googleapis.com/oauth2/v4/token -Dk8s.clusterEndpoint=https://your.k8scluster.com
java -jar -Dgoogle.clientId=something -Dgoogle.clientSecret=secret -Dgoogle.authorizeUrl=https://accounts.google.com/o/oauth2/auth -Dgoogle.tokenUrl=https://www.googleapis.com/oauth2/v4/token -Dk8s.clusterEndpoint=https://your.k8scluster.com k8s-proxy-0.0.1.jar
```

### Run the jar file
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/coveo/k8sproxy/domain/TokenInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (c) 2011 - 2017, Coveo Solutions Inc.
*/
package com.coveo.k8sproxy.domain;

public class TokenInfo
{
private String idToken;
private String refreshToken;

public String getIdToken()
{
return idToken;
}

public void setIdToken(String idToken)
{
this.idToken = idToken;
}

public String getRefreshToken()
{
return refreshToken;
}

public void setRefreshToken(String refreshToken)
{
this.refreshToken = refreshToken;
}

public TokenInfo withIdToken(String idToken)
{
setIdToken(idToken);
return this;
}

public TokenInfo withRefreshToken(String refreshToken)
{
setRefreshToken(refreshToken);
return this;
}
}
28 changes: 27 additions & 1 deletion src/main/java/com/coveo/k8sproxy/proxy/K8sReverseProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.coveo.k8sproxy.domain.GoogleIdAndRefreshToken;
import com.coveo.k8sproxy.domain.JweToken;
import com.coveo.k8sproxy.domain.TokenInfo;
import com.coveo.k8sproxy.token.GoogleTokenRetriever;
import com.coveo.k8sproxy.token.JweTokenRetriever;

Expand Down Expand Up @@ -99,6 +101,28 @@ public void callback(@RequestParam String code, HttpServletRequest request, Http
response.sendRedirect(initialRedirect);
}

@RequestMapping("/get_token")
@ResponseBody
public TokenInfo getTokens(HttpServletRequest request, HttpServletResponse response)
throws ClientProtocolException,
IOException
{
if (googleToken == null || googleToken.getIdToken() == null || googleToken.getRefreshToken() == null) {
initialRedirect = request.getRequestURI().toString();
response.sendRedirect(googleTokenRetriever.getAuthorizeUrl());
return null;
}

ReadLock readLock = lock.readLock();
try {
readLock.lock();
return new TokenInfo().withIdToken(googleToken.getIdToken())
.withRefreshToken(googleToken.getRefreshToken());
} finally {
readLock.unlock();
}
}

@RequestMapping("/ui")
public void uiRedirect(HttpServletRequest request, HttpServletResponse response) throws IOException
{
Expand Down Expand Up @@ -147,7 +171,9 @@ public void reverseProxy(HttpServletRequest request, HttpServletResponse respons
response.setStatus(proxiedResponse.getStatusLine().getStatusCode());
Stream.of(proxiedResponse.getAllHeaders())
.forEach(header -> response.setHeader(header.getName(), header.getValue()));
IOUtils.copy(proxiedResponse.getEntity().getContent(), response.getOutputStream());
if (proxiedResponse.getEntity() != null) {
IOUtils.copy(proxiedResponse.getEntity().getContent(), response.getOutputStream());
}
}
}

Expand Down

0 comments on commit b20fab7

Please sign in to comment.