Skip to content

Commit

Permalink
Merge pull request #6 from estigma88/multiple-linkedin-accounts
Browse files Browse the repository at this point in the history
Adding multiple linkedin credencials
  • Loading branch information
estigma88 authored Apr 14, 2024
2 parents aecd253 + f0a7019 commit 8c43840
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 108 deletions.
2 changes: 1 addition & 1 deletion aws-publisher/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>aws-publisher</artifactId>
<version>0.3.0-SNAPSHOT</version>
<version>0.5.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>aws-publisher</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.coderstower.socialmediapubisher.springpublisher.abstraction.security.repository.OAuth2Credentials;
import com.coderstower.socialmediapubisher.springpublisher.abstraction.security.repository.OAuth2CredentialsRepository;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class OAuth2CredentialAWSRepository implements OAuth2CredentialsRepository {
private final OAuth2CredentialDynamoRepository oauth2CredentialDynamoRepository;
Expand All @@ -27,11 +30,19 @@ public Optional<OAuth2Credentials> getCredentials(String id) {
return oauth2CredentialDynamoRepository.findById(id).map(this::convert);
}

@Override
public List<OAuth2Credentials> findAll() {
return StreamSupport.stream(oauth2CredentialDynamoRepository.findAll().spliterator(), false)
.map(this::convert)
.collect(Collectors.toList());
}

private OAuth2CredentialDynamo convert(OAuth2Credentials oAuth2Credentials) {
return OAuth2CredentialDynamo.builder()
.accessToken(oAuth2Credentials.getAccessToken())
.id(oAuth2Credentials.getId())
.expirationDate(oAuth2Credentials.getExpirationDate())
.allowedGroups(oAuth2Credentials.getAllowedGroups())
.build();
}

Expand All @@ -40,6 +51,7 @@ private OAuth2Credentials convert(OAuth2CredentialDynamo oauth2CredentialDynamo)
.accessToken(oauth2CredentialDynamo.getAccessToken())
.id(oauth2CredentialDynamo.getId())
.expirationDate(oauth2CredentialDynamo.getExpirationDate())
.allowedGroups(oauth2CredentialDynamo.getAllowedGroups())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

@Data
@DynamoDBTable(tableName = "Oauth2Credentials")
Expand All @@ -25,4 +26,6 @@ public class OAuth2CredentialDynamo {
@DynamoDBAttribute
@DynamoDBTypeConverted(converter = LocalDateTimeConverter.class)
private LocalDateTime expirationDate;
@DynamoDBAttribute
private List<String> allowedGroups;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.coderstower.socialmediapubisher.springpublisher.main.aws.repository.oauth2;

import org.socialsignin.spring.data.dynamodb.repository.EnableScan;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
@EnableScan
public interface OAuth2CredentialDynamoRepository extends CrudRepository<OAuth2CredentialDynamo, String> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void publish_credentialsExpired_unauthorizedException() throws Exception {
.with(csrf())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isUnauthorized())
.andExpect(content().string("Unauthorized. Please login again here: http://localhost:8080/oauth2/linkedin/credentials"));
.andExpect(content().string("Unauthorized for linkedin credential1. Please login again here: http://localhost:8080/oauth2/linkedin/credentials"));
}

@Test
Expand Down Expand Up @@ -154,7 +154,7 @@ public AmazonDynamoDB amazonDynamoDB() {

PutItemRequest oauth2Credentials = new PutItemRequest();
oauth2Credentials.setTableName("Oauth2Credentials");
oauth2Credentials.addItemEntry("id", new AttributeValue("linkedin"));
oauth2Credentials.addItemEntry("id", new AttributeValue("credential1"));
oauth2Credentials.addItemEntry("accessToken", new AttributeValue("access123"));
oauth2Credentials.addItemEntry("expirationDate", new AttributeValue("2020-02-01T05:05:05"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public AmazonDynamoDB amazonDynamoDB() {
oauth2Credentials.setTableName("Oauth2Credentials");
oauth2Credentials.addItemEntry("id", new AttributeValue("linkedin"));
oauth2Credentials.addItemEntry("accessToken", new AttributeValue("access123"));
oauth2Credentials.addItemEntry("allowedGroups", new AttributeValue().withL(List.of(new AttributeValue("group1"))));
oauth2Credentials.addItemEntry("expirationDate", new AttributeValue("2020-04-01T05:05:05"));

ddb.putItem(oauth2Credentials);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private boolean publishedWellOK(List<Publication> publishedPosts) {

private List<Publication> publish(List<SocialMediaPublisher> socialMediaPublishers, Post nextPost) {
return socialMediaPublishers.stream()
.map(socialMediaPublisher -> socialMediaPublisher.publish(nextPost))
.flatMap(socialMediaPublisher -> socialMediaPublisher.publish(nextPost).stream())
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Publication {
private final String id;
private final Status status;
private final String publisher;
private final String credentialId;
private final LocalDateTime publishedDate;

public enum Status{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.coderstower.socialmediapubisher.springpublisher.abstraction.post.repository.Post;

import java.util.List;

public interface SocialMediaPublisher {
String getName();
Acknowledge ping();
Publication publish(Post post);
List<Publication> publish(Post post);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;

@Data
@Builder
public class OAuth2Credentials {
private final String id;
private final String accessToken;
private final LocalDateTime expirationDate;
private final List<String> allowedGroups;

public OAuth2Credentials update(String accessToken, LocalDateTime expirationDate){
return OAuth2Credentials.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.coderstower.socialmediapubisher.springpublisher.abstraction.security.repository;

import java.util.List;
import java.util.Optional;

public interface OAuth2CredentialsRepository {
Expand All @@ -8,4 +9,5 @@ public interface OAuth2CredentialsRepository {
OAuth2Credentials update(OAuth2Credentials oAuth2Credentials);

Optional<OAuth2Credentials> getCredentials(String id);
List<OAuth2Credentials> findAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

import java.time.Clock;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
public class LinkedInPublisher implements SocialMediaPublisher {
Expand All @@ -44,75 +47,91 @@ public String getName() {

@Override
public Acknowledge ping() {
OAuth2Credentials credentials = oauth2CredentialsRepository.getCredentials(name)
.orElseThrow(() -> new IllegalArgumentException("The credentials for " + name + " doesn't exist"));

if (areCredentialsExpired(credentials)) {
throw new UnauthorizedException("Unauthorized. Please login again here: " + loginURL.expand(name));
} else {
return Acknowledge.builder()
.status(Acknowledge.Status.SUCCESS)
.build();
List<OAuth2Credentials> credentials = oauth2CredentialsRepository.findAll();

if (credentials.isEmpty()) {
throw new IllegalArgumentException("The credentials for " + name + " doesn't exist");
}

for (OAuth2Credentials credential : credentials) {
if (areCredentialsExpired(credential)) {
throw new UnauthorizedException("Unauthorized for " + name + " " + credential.getId() + ". Please login again here: " + loginURL.expand(name));
}
}

return Acknowledge.builder()
.status(Acknowledge.Status.SUCCESS)
.build();
}

@Override
public Publication publish(Post post) {
OAuth2Credentials credentials = oauth2CredentialsRepository.getCredentials(name)
.orElseThrow(() -> new IllegalArgumentException("The credentials for " + name + " doesn't exist"));

try {
Profile profile = getProfile(credentials);

LinkedInShare linkedInShare = LinkedInShare.builder()
.author("urn:li:person:" + profile.getSub())
.commentary(post.basicFormatWithoutURL())
.distribution(Distribution.builder().feedDistribution("MAIN_FEED").build())
.lifecycleState("PUBLISHED")
.content(Content.builder()
.article(ArticleContent.builder()
.description(post.getDescription())
.title(post.getName())
.source(post.getUrl().toString())
.build())
.build())
.visibility("PUBLIC")
.build();

String shareId = publish(linkedInShare, credentials);

if (Objects.nonNull(shareId)) {
return Publication.builder()
.id(shareId)
.status(Publication.Status.SUCCESS)
public List<Publication> publish(Post post) {
List<OAuth2Credentials> credentials = oauth2CredentialsRepository.findAll()
.stream()
.filter(oAuth2Credentials -> oAuth2Credentials.getAllowedGroups().contains(post.getGroup()))
.collect(Collectors.toList());

List<Publication> publications = new ArrayList<>();

for (OAuth2Credentials credential : credentials) {
try {
Profile profile = getProfile(credential);

LinkedInShare linkedInShare = LinkedInShare.builder()
.author("urn:li:person:" + profile.getSub())
.commentary(post.basicFormatWithoutURL())
.distribution(Distribution.builder().feedDistribution("MAIN_FEED").build())
.lifecycleState("PUBLISHED")
.content(Content.builder()
.article(ArticleContent.builder()
.description(post.getDescription())
.title(post.getName())
.source(post.getUrl().toString())
.build())
.build())
.visibility("PUBLIC")
.build();

String shareId = publish(linkedInShare, credential);

if (Objects.nonNull(shareId)) {
publications.add(Publication.builder()
.id(shareId)
.status(Publication.Status.SUCCESS)
.publisher(name)
.publishedDate(LocalDateTime.now(clock))
.credentialId(credential.getId())
.build());
} else {
publications.add(Publication.builder()
.status(Publication.Status.FAILURE)
.publisher(name)
.publishedDate(LocalDateTime.now(clock))
.credentialId(credential.getId())
.build());
}
} catch (HttpClientErrorException e) {
log.error("Error publishing to " + name + ", credentials " + credential.getId() + " response body: " + e.getResponseBodyAsString(), e);

publications.add(Publication.builder()
.status(Publication.Status.FAILURE)
.publisher(name)
.publishedDate(LocalDateTime.now(clock))
.build();
} else {
return Publication.builder()
.credentialId(credential.getId())
.build());

} catch (Exception e) {
log.error("Error publishing to " + name + ", credentials " + credential.getId(), e);

publications.add(Publication.builder()
.status(Publication.Status.FAILURE)
.publisher(name)
.publishedDate(LocalDateTime.now(clock))
.build();
.credentialId(credential.getId())
.build());
}
} catch (HttpClientErrorException e) {
log.error("Error publishing to " + name + ", response body: " + e.getResponseBodyAsString(), e);

return Publication.builder()
.status(Publication.Status.FAILURE)
.publisher(name)
.publishedDate(LocalDateTime.now(clock))
.build();

} catch (Exception e) {
log.error("Error publishing to " + name, e);

return Publication.builder()
.status(Publication.Status.FAILURE)
.publisher(name)
.publishedDate(LocalDateTime.now(clock))
.build();
}
return publications;
}

private String publish(LinkedInShare linkedInShare, OAuth2Credentials credentials) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public Acknowledge ping() {
}

@Override
public Publication publish(Post post) {
public List<Publication> publish(Post post) {
OAuth1Credentials credentials = oauth1CredentialsRepository.getCredentials(name)
.orElseThrow(() -> new IllegalArgumentException("The credentials for " + name + " doesn't exist"));

Expand All @@ -84,27 +84,27 @@ public Publication publish(Post post) {
Status statuses = twitter.updateStatus(post.basicFormat());

if (Objects.nonNull(statuses)) {
return Publication.builder()
return List.of(Publication.builder()
.id(String.valueOf(statuses.getId()))
.status(Publication.Status.SUCCESS)
.publisher(name)
.publishedDate(LocalDateTime.now(clock))
.build();
.build());
} else {
return Publication.builder()
return List.of(Publication.builder()
.status(Publication.Status.FAILURE)
.publisher(name)
.publishedDate(LocalDateTime.now(clock))
.build();
.build());
}
} catch (TwitterException e) {
log.error("Error publishing to " + name, e);

return Publication.builder()
return List.of(Publication.builder()
.status(Publication.Status.FAILURE)
.publisher(name)
.publishedDate(LocalDateTime.now(clock))
.build();
.build());
}
}

Expand Down
Loading

0 comments on commit 8c43840

Please sign in to comment.