Created
September 14, 2020 23:06
-
-
Save mlescaille/e5073eaf8cc2ee3b212f8e188667291c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.tutorial.googleeoauth | |
import com.google.api.client.auth.oauth2.StoredCredential | |
import com.google.api.client.util.store.AbstractDataStore | |
import com.google.api.client.util.store.DataStore | |
import com.tutorial.googleeoauth.domain.GoogleCredentialDetails | |
import com.tutorial.googleeoauth.exception.ResourceNotFoundException | |
import com.tutorial.googleeoauth.repository.GoogleCredentialDetailsRepository | |
import java.util.stream.Collectors | |
/** | |
* Database dependent implementation of the {@link DataStore} interface for storing authentication details related | |
* to Google OAuth authentication process. | |
* @param <V> | |
*/ | |
class DbDataStore<V extends Serializable> extends AbstractDataStore<V> { | |
GoogleCredentialDetailsRepository repository | |
/** | |
* Get the store backed by a DB to store Google's credentials. | |
* @param dataStoreFactory | |
* @param id | |
* @param repository | |
*/ | |
protected DbDataStore(DbDataStoreFactory dataStoreFactory, String id, GoogleCredentialDetailsRepository repository) { | |
super(dataStoreFactory, id) | |
this.repository = repository | |
} | |
/** | |
* Get all the keys for this store | |
* @return a list of keys this store has | |
* @throws IOException | |
*/ | |
@Override | |
Set<String> keySet() throws IOException { | |
return repository.findAllOauthClientId() | |
} | |
/** | |
* Get all the {@link StoredCredential} corresponding values for this store | |
* @return | |
* @throws IOException | |
*/ | |
@Override | |
Collection<StoredCredential> values() throws IOException { | |
return repository.findAll().stream().map(c -> { | |
c.toStoredCredential() | |
}).collect(Collectors.toList()) | |
} | |
/** | |
* Get a single value {@link StoredCredential} from the given {@code key} | |
* @param key, the client id of the credentials. | |
* @return the {@link StoredCredential} if exists, null otherwise | |
* @throws IOException | |
*/ | |
@Override | |
StoredCredential get(String key) throws IOException { | |
GoogleCredentialDetails googleCredential = repository.findByOauthClientId(key) | |
if (!googleCredential) { | |
return null | |
} | |
return googleCredential.toStoredCredential() | |
} | |
/** | |
* Set the value of the given {@code key}. | |
* @param key | |
* @param value | |
* @return | |
* @throws IOException | |
*/ | |
@Override | |
DataStore<V> set(String key, V value) throws IOException { | |
GoogleCredentialDetails googleCredentialDetails = repository.findByOauthClientId(key) | |
if (!googleCredentialDetails) { | |
googleCredentialDetails = new GoogleCredentialDetails() | |
googleCredentialDetails.uuid = UUID.randomUUID().toString() // in real projects this should be in an utility class | |
googleCredentialDetails.oauthClientId = value.key() | |
googleCredentialDetails.oauthClientSecret = value.oauthClientSecret() | |
googleCredentialDetails.accountName = value.accountName() | |
googleCredentialDetails.adServerUserProfileId = value.userProfileId() | |
} | |
googleCredentialDetails.apply(value as StoredCredential) | |
repository.save(googleCredentialDetails) | |
return this | |
} | |
/** | |
* Deletes all records in the table! | |
* @return current instance | |
* @throws IOException | |
*/ | |
@Override | |
DataStore<StoredCredential> clear() throws IOException { | |
repository.deleteAll() | |
return this | |
} | |
/** | |
* Delete the record corresponding to the given {@code key}. | |
* @param key | |
* @return | |
* @throws IOException | |
*/ | |
@Override | |
DataStore<StoredCredential> delete(String key) throws IOException { | |
GoogleCredentialDetails googleCredential = repository.findByOauthClientId(key) | |
if (!googleCredential) { | |
throw new ResourceNotFoundException("No credential found for clientId ${key}") | |
} | |
repository.delete(googleCredential) | |
return this | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment