Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
*/
public abstract class AbstractTeamsConversations implements TeamsConversations {

private SpringBotMicrosoftAppCredentials mac;
private SpringBotAppCredentials mac;
private BotFrameworkAdapter bfa;
private ChannelAccount botAccount;

public AbstractTeamsConversations(BotFrameworkAdapter bfa, SpringBotMicrosoftAppCredentials mac, ChannelAccount botAccount) {
public AbstractTeamsConversations(BotFrameworkAdapter bfa, SpringBotAppCredentials mac, ChannelAccount botAccount) {
super();
this.mac = mac;
this.bfa = bfa;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.finos.springbot.teams.conversations;

import com.azure.identity.ClientCertificateCredential;

public interface SpringBotAppCredentials {

String getTenantId();

String getClientId();

ClientCertificateCredential getCredential();

String getToken();

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.finos.springbot.teams.conversations;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
Expand All @@ -12,56 +9,62 @@
import com.azure.identity.ClientCertificateCredential;
import com.azure.identity.ClientCertificateCredentialBuilder;

public class SpringBotMicrosoftAppCredentials {

private String tenantId;
private String clientId;
private ClientCertificateCredential credential;
public class SpringBotMicrosoftAppCredentials implements SpringBotAppCredentials {

private String tenantId = null;
private String clientId = null;
private ClientCertificateCredential credential = null;

public SpringBotMicrosoftAppCredentials(String tenantId, String clientId, String certificate,
String certificatePassword) {
this.tenantId = tenantId;
this.clientId = clientId;
String pemContent = certificate;
// Check for file extension and illegal path characters
boolean isFilePath = (certificate != null && (certificate.endsWith(".p12")));
try {
if (!isFilePath) {
byte[] decode = Base64.getDecoder().decode(pemContent);

java.nio.file.Path tempFile = Files.createTempFile("cert", ".p12");
Files.write(tempFile, decode);
certificate = tempFile.toAbsolutePath().toString();
}
// Check for file extension and illegal path characters
boolean isFilePath = (certificate != null && (certificate.endsWith(".p12")));

if (certificate != null) {
if (!isFilePath) {
byte[] decode = Base64.getDecoder().decode(pemContent);

java.nio.file.Path tempFile = Files.createTempFile("cert", ".p12");
Files.write(tempFile, decode);
certificate = tempFile.toAbsolutePath().toString();
}

this.credential = new ClientCertificateCredentialBuilder().tenantId(tenantId).clientId(clientId)
.pemCertificate(Files.newInputStream(Paths.get(certificate)))
.clientCertificatePassword(certificatePassword).build();
this.credential = new ClientCertificateCredentialBuilder().tenantId(tenantId).clientId(clientId)
.pemCertificate(Files.newInputStream(Paths.get(certificate)))
.clientCertificatePassword(certificatePassword).build();
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Failed to create certificate", e);
}
}

}

@Override
public String getTenantId() {
return tenantId;
}

@Override
public String getClientId() {
return clientId;
}

@Override
public ClientCertificateCredential getCredential() {
return credential;
}

@Override
public String getToken() {
// SOMETHING LIKE THIS
return credential.getTokenSync(new TokenRequestContext().addScopes("https://graph.microsoft.com/.default"))
.getToken();
}

public static InputStream stringToInputStream(String content) {
return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class StateStorageBasedTeamsConversations extends AbstractTeamsConversati

protected final TeamsStateStorage tss;

public StateStorageBasedTeamsConversations(BotFrameworkAdapter bfa, SpringBotMicrosoftAppCredentials mac,
public StateStorageBasedTeamsConversations(BotFrameworkAdapter bfa, SpringBotAppCredentials mac,
ChannelAccount botAccount, TeamsStateStorage tss) {
super(bfa, mac, botAccount);
this.tss = tss;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,23 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import com.azure.identity.ClientCertificateCredential;
import com.microsoft.bot.builder.BotFrameworkAdapter;
import com.microsoft.bot.connector.authentication.MicrosoftAppCredentials;
import com.microsoft.bot.integration.AdapterWithErrorHandler;
import com.microsoft.bot.integration.BotFrameworkHttpAdapter;
import com.microsoft.bot.schema.ChannelAccount;

public class TeamsConversationsConfig extends BotDependencyConfiguration {

@Bean
@ConditionalOnMissingBean
public SpringBotMicrosoftAppCredentials microsoftCredentials(
@Value("${teams.app.tennantId}") String tennantId,
@Value("${teams.app.appId}") String appId,
@Value("${teams.app.pemCertificate}") String pemCertificate,
@Value("${teams.app.pemCertificatePassword}") String pemCertificatePassword) {
public SpringBotAppCredentials microsoftCredentials(@Value("${teams.app.tennantId}") String tennantId) {
com.microsoft.bot.integration.Configuration conf = getConfiguration();

String clientId = conf.getProperty(MicrosoftAppCredentials.MICROSOFTAPPID);
String clientId = conf
.getProperty(com.microsoft.bot.connector.authentication.MicrosoftAppCredentials.MICROSOFTAPPID);

SpringBotMicrosoftAppCredentials out = new SpringBotMicrosoftAppCredentials(
tennantId, clientId, pemCertificate,
pemCertificatePassword);
SpringBotAppCredentials out = new SpringBotMicrosoftAppCredentials(tennantId,
clientId, conf.getProperty("MicrosoftAppIdPemCertificate"),
conf.getProperty("MicrosoftAppIdPemCertificatePassword"));

// MicrosoftAppCredentials mac = new MicrosoftAppCredentials(
// conf.getProperty(MicrosoftAppCredentials.MICROSOFTAPPID),
Expand All @@ -46,7 +40,7 @@ public SpringBotMicrosoftAppCredentials microsoftCredentials(
@ConditionalOnMissingBean
public TeamsConversations teamsConversations(
BotFrameworkAdapter bfa,
SpringBotMicrosoftAppCredentials appCredentials,
SpringBotAppCredentials appCredentials,
@Value("${teams.bot.id:}") String id,
TeamsStateStorage teamsState) {
ChannelAccount botAccount = new ChannelAccount(id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.finos.springbot.teams;

import org.finos.springbot.teams.conversations.SpringBotMicrosoftAppCredentials;
import org.finos.springbot.teams.conversations.MockSpringBotMicrosoftAppCredentials;
import org.finos.springbot.teams.conversations.SpringBotAppCredentials;
import org.finos.springbot.tests.controller.OurController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

@Configuration
Expand All @@ -20,8 +22,9 @@ public OurController ourController() {
}

@Bean
public SpringBotMicrosoftAppCredentials mockMicrosoftCredentials() {
return new SpringBotMicrosoftAppCredentials("ABCD", "ABCD", "ABCD", "ABCD");
@Primary
public SpringBotAppCredentials dummyMicrosoftCredentials() {
return new MockSpringBotMicrosoftAppCredentials();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.finos.springbot.teams.conversations;

import com.azure.identity.ClientCertificateCredential;

public class MockSpringBotMicrosoftAppCredentials implements SpringBotAppCredentials {

@Override
public String getTenantId() {
return "mock-tenant-id";
}

@Override
public String getClientId() {
return "mock-client-id";
}

@Override
public ClientCertificateCredential getCredential() {
return null;
}

@Override
public String getToken() {
return "mock-token";
}

}
Loading