|
|
|
|
@ -9,6 +9,7 @@ import java.nio.charset.Charset; |
|
|
|
|
import java.security.GeneralSecurityException; |
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Objects; |
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
@ -33,11 +34,9 @@ public abstract class GoogleBaseAdapter { |
|
|
|
|
/** Application name. */ |
|
|
|
|
protected static final String APPLICATION_NAME = "ClubHelperVaadin"; |
|
|
|
|
/** Directory to store user credentials for this application. */ |
|
|
|
|
protected static final File DATA_STORE_DIR = new File( |
|
|
|
|
System.getProperty("catalina.base"), ".credentials"); |
|
|
|
|
protected static final File DATA_STORE_DIR = new File(System.getProperty("catalina.base"), ".credentials"); |
|
|
|
|
/** Global instance of the JSON factory. */ |
|
|
|
|
protected static final JsonFactory JSON_FACTORY = JacksonFactory |
|
|
|
|
.getDefaultInstance(); |
|
|
|
|
protected static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); |
|
|
|
|
/** |
|
|
|
|
* Global instance of the scopes required by this quickstart. |
|
|
|
|
* |
|
|
|
|
@ -47,8 +46,7 @@ public abstract class GoogleBaseAdapter { |
|
|
|
|
|
|
|
|
|
private static Credential credential; |
|
|
|
|
|
|
|
|
|
protected final Logger log = LoggerFactory |
|
|
|
|
.getLogger(getClass()); |
|
|
|
|
protected final Logger log = LoggerFactory.getLogger(getClass()); |
|
|
|
|
/** Global instance of the {@link FileDataStoreFactory}. */ |
|
|
|
|
protected final FileDataStoreFactory DATA_STORE_FACTORY; |
|
|
|
|
/** Global instance of the HTTP transport. */ |
|
|
|
|
@ -68,20 +66,18 @@ public abstract class GoogleBaseAdapter { |
|
|
|
|
protected void checkRefreshToken(String serverName) throws IOException { |
|
|
|
|
synchronized (SCOPES) { |
|
|
|
|
if (credential == null) { |
|
|
|
|
credential = authorize(serverName); |
|
|
|
|
credential = Objects.requireNonNull(authorize(serverName)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ((credential.getExpiresInSeconds() != null |
|
|
|
|
&& credential.getExpiresInSeconds() < 3600)) { |
|
|
|
|
if ((credential.getExpiresInSeconds() != null && credential.getExpiresInSeconds() < 3600)) { |
|
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) { |
|
|
|
|
log.debug("Security needs refresh, trying."); |
|
|
|
|
} |
|
|
|
|
boolean result = credential.refreshToken(); |
|
|
|
|
if (log.isDebugEnabled()) { |
|
|
|
|
log.debug("Token refresh " |
|
|
|
|
+ (result ? "successfull." : "failed.")); |
|
|
|
|
log.debug("Token refresh {}", (result ? "successfull." : "failed.")); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -91,8 +87,7 @@ public abstract class GoogleBaseAdapter { |
|
|
|
|
throw new IllegalStateException("credential is null, checkRefreshToken need to be called before."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return new com.google.api.services.calendar.Calendar.Builder( |
|
|
|
|
HTTP_TRANSPORT, JSON_FACTORY, credential); |
|
|
|
|
return new com.google.api.services.calendar.Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public final boolean refreshToken() throws IOException { |
|
|
|
|
@ -107,61 +102,53 @@ public abstract class GoogleBaseAdapter { |
|
|
|
|
* @return an authorized Credential object. |
|
|
|
|
* @throws IOException |
|
|
|
|
*/ |
|
|
|
|
private Credential authorize(String serverName) |
|
|
|
|
throws IOException { |
|
|
|
|
if (credential != null && (credential.getExpiresInSeconds() != null |
|
|
|
|
&& credential.getExpiresInSeconds() < 3600)) { |
|
|
|
|
private Credential authorize(String serverName) throws IOException { |
|
|
|
|
if (credential != null |
|
|
|
|
&& (credential.getExpiresInSeconds() != null && credential.getExpiresInSeconds() < 3600)) { |
|
|
|
|
credential.refreshToken(); |
|
|
|
|
return credential; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Load client secrets.
|
|
|
|
|
InputStream in = getClass().getResourceAsStream("/client_secret.json"); |
|
|
|
|
if (in == null) { |
|
|
|
|
File inHome = new File(System.getProperty("user.home"), |
|
|
|
|
"client_secret.json"); |
|
|
|
|
File inHome = new File(System.getProperty("user.home"), "client_secret.json"); |
|
|
|
|
if (inHome.exists()) { |
|
|
|
|
if (log.isInfoEnabled()) { |
|
|
|
|
log.info( |
|
|
|
|
"Google secret not found as Resource, using user Home file."); |
|
|
|
|
log.info("Google secret not found as Resource, using user Home file."); |
|
|
|
|
} |
|
|
|
|
in = new FileInputStream(inHome); |
|
|
|
|
} else { |
|
|
|
|
log.error( |
|
|
|
|
"Failed to load client_secret.json. Download from google console."); |
|
|
|
|
log.error("Failed to load client_secret.json. Download from google console."); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
GoogleClientSecrets clientSecrets = GoogleClientSecrets |
|
|
|
|
.load(JSON_FACTORY, new InputStreamReader(in, Charset.defaultCharset())); |
|
|
|
|
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, |
|
|
|
|
new InputStreamReader(in, Charset.defaultCharset())); |
|
|
|
|
if (log.isTraceEnabled()) { |
|
|
|
|
log.trace("client secret json resource loaded."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Build flow and trigger user authorization request.
|
|
|
|
|
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( |
|
|
|
|
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) |
|
|
|
|
.setDataStoreFactory(DATA_STORE_FACTORY) |
|
|
|
|
.setAccessType("offline").setApprovalPrompt("force") |
|
|
|
|
.build(); |
|
|
|
|
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, |
|
|
|
|
clientSecrets, SCOPES).setDataStoreFactory(DATA_STORE_FACTORY).setAccessType("offline") |
|
|
|
|
.setApprovalPrompt("force").build(); |
|
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) { |
|
|
|
|
log.debug("Configuring google LocalServerReceiver on " + serverName |
|
|
|
|
+ ":" + GOOGLE_SECRET_PORT); |
|
|
|
|
log.debug("Configuring google LocalServerReceiver on {}: {}", serverName, GOOGLE_SECRET_PORT); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
LocalServerReceiver localServerReceiver = new LocalServerReceiver.Builder() |
|
|
|
|
.setHost(serverName).setPort(GOOGLE_SECRET_PORT).build(); |
|
|
|
|
LocalServerReceiver localServerReceiver = new LocalServerReceiver.Builder().setHost(serverName) |
|
|
|
|
.setPort(GOOGLE_SECRET_PORT).build(); |
|
|
|
|
|
|
|
|
|
Credential credential = new AuthorizationCodeInstalledApp(flow, |
|
|
|
|
localServerReceiver).authorize("user"); |
|
|
|
|
Credential auth = new AuthorizationCodeInstalledApp(flow, localServerReceiver).authorize("user"); |
|
|
|
|
if (log.isDebugEnabled()) { |
|
|
|
|
log.debug( |
|
|
|
|
"Credentials saved to " + DATA_STORE_DIR.getAbsolutePath()); |
|
|
|
|
log.debug("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
credential.setExpiresInSeconds(Long.valueOf(691200L)); |
|
|
|
|
auth.setExpiresInSeconds(Long.valueOf(691200L)); |
|
|
|
|
|
|
|
|
|
return credential; |
|
|
|
|
return auth; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |