Search in sources :

Example 1 with TokenSecretAuthData

use of org.datatransferproject.types.transfer.auth.TokenSecretAuthData in project data-transfer-project by google.

the class OAuth1DataGenerator method generateAuthData.

@Override
public AuthData generateAuthData(String callbackBaseUrl, String authCode, String id, AuthData initialAuthData, String extra) {
    Preconditions.checkArgument(Strings.isNullOrEmpty(extra), "Extra data not expected for OAuth flow");
    Preconditions.checkArgument(initialAuthData != null, "Initial auth data expected for " + config.getServiceName());
    OAuthGetAccessToken accessTokenRequest = new OAuthGetAccessToken(config.getAccessTokenUrl());
    accessTokenRequest.transport = httpTransport;
    accessTokenRequest.temporaryToken = ((TokenSecretAuthData) initialAuthData).getToken();
    accessTokenRequest.consumerKey = clientId;
    accessTokenRequest.verifier = authCode;
    accessTokenRequest.signer = config.getAccessTokenSigner(clientSecret, ((TokenSecretAuthData) initialAuthData).getSecret());
    TokenSecretAuthData accessToken;
    try {
        OAuthCredentialsResponse response = accessTokenRequest.execute();
        accessToken = new TokenSecretAuthData(response.token, response.tokenSecret);
    } catch (IOException e) {
        monitor.severe(() -> "Error retrieving request token", e);
        return null;
    }
    return accessToken;
}
Also used : TokenSecretAuthData(org.datatransferproject.types.transfer.auth.TokenSecretAuthData) OAuthGetAccessToken(com.google.api.client.auth.oauth.OAuthGetAccessToken) IOException(java.io.IOException) OAuthCredentialsResponse(com.google.api.client.auth.oauth.OAuthCredentialsResponse)

Example 2 with TokenSecretAuthData

use of org.datatransferproject.types.transfer.auth.TokenSecretAuthData in project data-transfer-project by google.

the class FlickrUtils method getAuth.

public static Auth getAuth(AuthData authData, Flickr flickr) throws FlickrException {
    checkArgument(authData instanceof TokenSecretAuthData, "authData expected to be TokenSecretAuthData not %s", authData.getClass().getCanonicalName());
    TokenSecretAuthData tokenAuthData = (TokenSecretAuthData) authData;
    Token requestToken = new Token(tokenAuthData.getToken(), tokenAuthData.getSecret());
    return flickr.getAuthInterface().checkToken(requestToken);
}
Also used : TokenSecretAuthData(org.datatransferproject.types.transfer.auth.TokenSecretAuthData) Token(org.scribe.model.Token)

Example 3 with TokenSecretAuthData

use of org.datatransferproject.types.transfer.auth.TokenSecretAuthData in project data-transfer-project by google.

the class FlickrPhotosImporterTest method importStoresAlbumInJobStore.

@Test
public void importStoresAlbumInJobStore() throws FlickrException, Exception {
    UUID jobId = UUID.randomUUID();
    PhotosContainerResource photosContainerResource = new PhotosContainerResource(Collections.singletonList(PHOTO_ALBUM), Collections.singletonList(PHOTO_MODEL));
    // Setup Mock
    when(user.getId()).thenReturn("userId");
    when(authInterface.checkToken(any(Token.class))).thenReturn(auth);
    when(flickr.getPhotosetsInterface()).thenReturn(photosetsInterface);
    when(flickr.getUploader()).thenReturn(uploader);
    when(flickr.getAuthInterface()).thenReturn(authInterface);
    when(imageStreamProvider.get(FETCHABLE_URL)).thenReturn(bufferedInputStream);
    when(uploader.upload(any(BufferedInputStream.class), any(UploadMetaData.class))).thenReturn(FLICKR_PHOTO_ID);
    String flickrAlbumTitle = ALBUM_NAME;
    Photoset photoset = FlickrTestUtils.initializePhotoset(FLICKR_ALBUM_ID, ALBUM_DESCRIPTION, FLICKR_PHOTO_ID);
    when(photosetsInterface.create(flickrAlbumTitle, ALBUM_DESCRIPTION, FLICKR_PHOTO_ID)).thenReturn(photoset);
    // Run test
    FlickrPhotosImporter importer = new FlickrPhotosImporter(flickr, jobStore, imageStreamProvider, monitor, TransferServiceConfig.getDefaultInstance());
    ImportResult result = importer.importItem(jobId, EXECUTOR, new TokenSecretAuthData("token", "secret"), photosContainerResource);
    // Verify that the image stream provider got the correct URL and that the correct info was
    // uploaded
    verify(imageStreamProvider).get(FETCHABLE_URL);
    ArgumentCaptor<UploadMetaData> uploadMetaDataArgumentCaptor = ArgumentCaptor.forClass(UploadMetaData.class);
    verify(uploader).upload(eq(bufferedInputStream), uploadMetaDataArgumentCaptor.capture());
    UploadMetaData actualUploadMetaData = uploadMetaDataArgumentCaptor.getValue();
    assertThat(actualUploadMetaData.getTitle()).isEqualTo(PHOTO_TITLE);
    assertThat(actualUploadMetaData.getDescription()).isEqualTo(PHOTO_DESCRIPTION);
    // Verify the photosets interface got the command to create the correct album
    verify(photosetsInterface).create(flickrAlbumTitle, ALBUM_DESCRIPTION, FLICKR_PHOTO_ID);
    assertThat((String) EXECUTOR.getCachedValue(ALBUM_ID)).isEqualTo(FLICKR_ALBUM_ID);
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) TokenSecretAuthData(org.datatransferproject.types.transfer.auth.TokenSecretAuthData) BufferedInputStream(java.io.BufferedInputStream) Photoset(com.flickr4java.flickr.photosets.Photoset) Token(org.scribe.model.Token) UploadMetaData(com.flickr4java.flickr.uploader.UploadMetaData) UUID(java.util.UUID) Test(org.junit.Test)

Example 4 with TokenSecretAuthData

use of org.datatransferproject.types.transfer.auth.TokenSecretAuthData in project data-transfer-project by google.

the class SmugMugPhotosImporterTest method importEmptyAlbumName.

@Test
public void importEmptyAlbumName() throws Exception {
    UUID jobId = UUID.randomUUID();
    PhotoAlbum photoAlbum = new PhotoAlbum("albumid", "", "albumDescription");
    PhotosContainerResource photosContainerResource = new PhotosContainerResource(Collections.singletonList(photoAlbum), ImmutableList.of());
    SmugMugAlbum smugMugAlbum = new SmugMugAlbum("date", photoAlbum.getDescription(), "Untitled Album", "privacy", "albumUri1", "urlname", "weburi");
    SmugMugAlbumResponse mockAlbumResponse = new SmugMugAlbumResponse(smugMugAlbum.getUri(), "Locator", "LocatorType", smugMugAlbum);
    when(smugMugInterface.createAlbum(eq(smugMugAlbum.getName()))).thenReturn(mockAlbumResponse);
    // Run test
    SmugMugPhotosImporter importer = new SmugMugPhotosImporter(smugMugInterface, config, jobStore, new AppCredentials("key", "secret"), mock(ObjectMapper.class), monitor);
    ImportResult result = importer.importItem(jobId, EXECUTOR, new TokenSecretAuthData("token", "secret"), photosContainerResource);
    // Verify
    verify(smugMugInterface, atLeastOnce()).createAlbum(ArgumentCaptor.forClass(String.class).capture());
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) TokenSecretAuthData(org.datatransferproject.types.transfer.auth.TokenSecretAuthData) AppCredentials(org.datatransferproject.types.transfer.auth.AppCredentials) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) UUID(java.util.UUID) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 5 with TokenSecretAuthData

use of org.datatransferproject.types.transfer.auth.TokenSecretAuthData in project data-transfer-project by google.

the class SmugMugPhotosImporterTest method importStoresAlbumInJobStore.

@Test
public void importStoresAlbumInJobStore() throws Exception {
    // setup test objects
    UUID jobId = UUID.randomUUID();
    PhotoAlbum photoAlbum1 = new PhotoAlbum("albumId1", "albumName1", "albumDescription1");
    PhotoModel photoModel1 = new PhotoModel("PHOTO_TITLE", "FETCHABLE_URL", "PHOTO_DESCRIPTION", "MEDIA_TYPE", "photoId1", photoAlbum1.getId(), false);
    PhotoModel photoModel2 = new PhotoModel("PHOTO_TITLE", "FETCHABLE_URL", "PHOTO_DESCRIPTION", "MEDIA_TYPE", "photoId2", photoAlbum1.getId(), false);
    PhotoModel photoModel3 = new PhotoModel("PHOTO_TITLE", "FETCHABLE_URL", "PHOTO_DESCRIPTION", "MEDIA_TYPE", "photoId3", photoAlbum1.getId(), false);
    PhotosContainerResource photosContainerResource1 = new PhotosContainerResource(Collections.singletonList(photoAlbum1), ImmutableList.of());
    PhotosContainerResource photosContainerResource2 = new PhotosContainerResource(ImmutableList.of(), ImmutableList.of(photoModel1, photoModel2, photoModel3));
    SmugMugAlbum smugMugAlbum1 = new SmugMugAlbum("date", photoAlbum1.getDescription(), photoAlbum1.getName(), "privacy", "albumUri1", "urlname", "weburi");
    String overflowAlbumName = smugMugAlbum1.getName() + " (1)";
    SmugMugAlbum smugMugAlbum2 = new SmugMugAlbum("date", photoAlbum1.getDescription(), overflowAlbumName, "privacy", "albumUri2", "urlname", "weburi");
    SmugMugAlbumResponse mockAlbumResponse1 = new SmugMugAlbumResponse(smugMugAlbum1.getUri(), "Locator", "LocatorType", smugMugAlbum1);
    SmugMugAlbumResponse mockAlbumResponse2 = new SmugMugAlbumResponse(smugMugAlbum2.getUri(), "Locator", "LocatorType", smugMugAlbum2);
    when(smugMugInterface.createAlbum(eq(smugMugAlbum1.getName()))).thenReturn(mockAlbumResponse1);
    when(smugMugInterface.createAlbum(eq(smugMugAlbum2.getName()))).thenReturn(mockAlbumResponse2);
    SmugMugImageUploadResponse smugMugUploadImageResponse = new SmugMugImageUploadResponse("imageUri", "albumImageUri", new ImageInfo("imageUri", "albumImageUri", "statusImageReplaceUri", "url"));
    when(smugMugInterface.uploadImage(any(), any(), any())).thenReturn(smugMugUploadImageResponse);
    when(smugMugInterface.getImageAsStream(any())).thenReturn(bufferedInputStream);
    // Run test
    SmugMugPhotosImporter importer = new SmugMugPhotosImporter(smugMugInterface, config, jobStore, new AppCredentials("key", "secret"), mock(ObjectMapper.class), monitor);
    ImportResult result = importer.importItem(jobId, EXECUTOR, new TokenSecretAuthData("token", "secret"), photosContainerResource1);
    result = importer.importItem(jobId, EXECUTOR, new TokenSecretAuthData("token", "secret"), photosContainerResource2);
    // Verify
    ArgumentCaptor<String> photoUrlsCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> albumNamesCaptor = ArgumentCaptor.forClass(String.class);
    verify(smugMugInterface, atLeastOnce()).createAlbum(albumNamesCaptor.capture());
    verify(smugMugInterface, atLeastOnce()).getImageAsStream(photoUrlsCaptor.capture());
    List<String> capturedAlbumNames = albumNamesCaptor.getAllValues();
    assertTrue(capturedAlbumNames.contains(smugMugAlbum1.getName()));
    assertTrue(capturedAlbumNames.contains(smugMugAlbum2.getName()));
    List<String> capturedPhotoUrls = photoUrlsCaptor.getAllValues();
    assertTrue(capturedPhotoUrls.contains(photoModel1.getFetchableUrl()));
    assertTrue(capturedPhotoUrls.contains(photoModel2.getFetchableUrl()));
    assertTrue(capturedPhotoUrls.contains(photoModel3.getFetchableUrl()));
    String overflowAlbumId = photoAlbum1.getId() + "-overflow-1";
    assertThat((String) EXECUTOR.getCachedValue(photoAlbum1.getId())).isEqualTo(smugMugAlbum1.getUri());
    assertThat((String) EXECUTOR.getCachedValue(overflowAlbumId)).isEqualTo(smugMugAlbum2.getUri());
    SmugMugPhotoTempData tempData1 = new SmugMugPhotoTempData(photoAlbum1.getId(), smugMugAlbum1.getName(), smugMugAlbum1.getDescription(), smugMugAlbum1.getUri(), 2, overflowAlbumId);
    SmugMugPhotoTempData tempData2 = new SmugMugPhotoTempData(overflowAlbumId, smugMugAlbum2.getName(), smugMugAlbum2.getDescription(), smugMugAlbum2.getUri(), 1, null);
    assertThat(jobStore.findData(jobId, String.format(TEMP_DATA_FORMAT, photoAlbum1.getId()), SmugMugPhotoTempData.class).toString()).isEqualTo(tempData1.toString());
    assertThat(jobStore.findData(jobId, String.format(TEMP_DATA_FORMAT, overflowAlbumId), SmugMugPhotoTempData.class).toString()).isEqualTo(tempData2.toString());
}
Also used : ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) AppCredentials(org.datatransferproject.types.transfer.auth.AppCredentials) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) TokenSecretAuthData(org.datatransferproject.types.transfer.auth.TokenSecretAuthData) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) UUID(java.util.UUID) ImageInfo(org.datatransferproject.transfer.smugmug.photos.model.SmugMugImageUploadResponse.ImageInfo) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

TokenSecretAuthData (org.datatransferproject.types.transfer.auth.TokenSecretAuthData)8 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)5 Test (org.junit.Test)5 Token (org.scribe.model.Token)4 UUID (java.util.UUID)3 ImportResult (org.datatransferproject.spi.transfer.provider.ImportResult)3 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Photoset (com.flickr4java.flickr.photosets.Photoset)2 OAuthCredentialsResponse (com.google.api.client.auth.oauth.OAuthCredentialsResponse)2 IOException (java.io.IOException)2 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)2 IntPaginationToken (org.datatransferproject.types.common.IntPaginationToken)2 IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)2 AppCredentials (org.datatransferproject.types.transfer.auth.AppCredentials)2 Photo (com.flickr4java.flickr.photos.Photo)1 PhotoList (com.flickr4java.flickr.photos.PhotoList)1 Photosets (com.flickr4java.flickr.photosets.Photosets)1 UploadMetaData (com.flickr4java.flickr.uploader.UploadMetaData)1 OAuthAuthorizeTemporaryTokenUrl (com.google.api.client.auth.oauth.OAuthAuthorizeTemporaryTokenUrl)1