Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit b8d9447

Browse files
authored
Merge pull request #486 from microsoft/trboehre/signinlink
Fixed BotSignIn.getSignInUrl
2 parents 4ce8677 + 8058b7e commit b8d9447

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotFrameworkAdapter.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import com.fasterxml.jackson.databind.ObjectMapper;
88
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
99
import com.fasterxml.jackson.databind.node.ObjectNode;
10-
import com.google.common.io.BaseEncoding;
1110
import com.microsoft.bot.builder.integration.AdapterIntegration;
1211
import com.microsoft.bot.connector.Channels;
1312
import com.microsoft.bot.connector.ConnectorClient;
@@ -38,6 +37,7 @@
3837
import com.microsoft.bot.schema.ConversationReference;
3938
import com.microsoft.bot.schema.ConversationsResult;
4039
import com.microsoft.bot.schema.ResourceResponse;
40+
import com.microsoft.bot.schema.Serialization;
4141
import com.microsoft.bot.schema.TokenExchangeState;
4242
import com.microsoft.bot.schema.TokenResponse;
4343
import com.microsoft.bot.schema.TokenStatus;
@@ -52,6 +52,7 @@
5252
import java.util.List;
5353
import java.util.Map;
5454
import java.util.UUID;
55+
import java.util.Base64;
5556
import java.util.concurrent.CompletableFuture;
5657
import java.util.concurrent.CompletionException;
5758
import java.util.concurrent.ConcurrentHashMap;
@@ -947,10 +948,9 @@ public CompletableFuture<String> getOauthSignInLink(
947948
}
948949
};
949950

950-
ObjectMapper mapper = new ObjectMapper();
951-
String serializedState = mapper.writeValueAsString(tokenExchangeState);
952-
byte[] encodedState = serializedState.getBytes(StandardCharsets.UTF_8);
953-
String state = BaseEncoding.base64().encode(encodedState);
951+
String serializedState = Serialization.toString(tokenExchangeState);
952+
String state = Base64.getEncoder()
953+
.encodeToString(serializedState.getBytes(StandardCharsets.UTF_8));
954954

955955
return oAuthClient.getBotSignIn().getSignInUrl(state);
956956
} catch (Throwable t) {
@@ -1008,10 +1008,9 @@ public CompletableFuture<String> getOauthSignInLink(
10081008
}
10091009
};
10101010

1011-
ObjectMapper mapper = new ObjectMapper();
1012-
String serializedState = mapper.writeValueAsString(tokenExchangeState);
1013-
byte[] encodedState = serializedState.getBytes(StandardCharsets.UTF_8);
1014-
String state = BaseEncoding.base64().encode(encodedState);
1011+
String serializedState = Serialization.toString(tokenExchangeState);
1012+
String state = Base64.getEncoder()
1013+
.encodeToString(serializedState.getBytes(StandardCharsets.UTF_8));
10151014

10161015
return oAuthClient.getBotSignIn().getSignInUrl(state);
10171016
} catch (Throwable t) {

libraries/bot-connector/src/main/java/com/microsoft/bot/connector/rest/RestBotSignIn.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@
66

77
package com.microsoft.bot.connector.rest;
88

9-
import com.microsoft.bot.azure.CloudException;
109
import retrofit2.Retrofit;
1110
import com.microsoft.bot.connector.BotSignIn;
12-
import com.google.common.reflect.TypeToken;
1311
import com.microsoft.bot.rest.ServiceResponse;
14-
import java.io.IOException;
15-
import java.net.HttpURLConnection;
1612
import java.util.concurrent.CompletableFuture;
1713

1814
import okhttp3.ResponseBody;
@@ -25,6 +21,7 @@
2521
* An instance of this class provides access to all the operations defined in
2622
* BotSignIns.
2723
*/
24+
@SuppressWarnings("PMD")
2825
public class RestBotSignIn implements BotSignIn {
2926
/** The Retrofit service to perform REST calls. */
3027
private BotSignInsService service;
@@ -47,10 +44,10 @@ public RestBotSignIn(Retrofit withRetrofit, RestOAuthClient withClient) {
4744
* The interface defining all the services for BotSignIns to be used by Retrofit
4845
* to perform actually REST calls.
4946
*/
50-
@SuppressWarnings({ "checkstyle:linelength", "checkstyle:JavadocMethod" })
47+
@SuppressWarnings({"checkstyle:linelength", "checkstyle:JavadocMethod"})
5148
interface BotSignInsService {
52-
@Headers({ "Content-Type: application/json; charset=utf-8",
53-
"x-ms-logging-context: com.microsoft.bot.schema.BotSignIns getSignInUrl" })
49+
@Headers({"Content-Type: application/json; charset=utf-8",
50+
"x-ms-logging-context: com.microsoft.bot.schema.BotSignIns getSignInUrl"})
5451
@GET("api/botsignin/GetSignInUrl")
5552
CompletableFuture<Response<ResponseBody>> getSignInUrl(
5653
@Query("state") String state,
@@ -117,14 +114,11 @@ public CompletableFuture<String> getSignInUrl(
117114

118115
private ServiceResponse<String> getSignInUrlDelegate(
119116
Response<ResponseBody> response
120-
) throws CloudException, IOException, IllegalArgumentException {
117+
) throws ErrorResponseException, IllegalArgumentException {
118+
if (!response.isSuccessful()) {
119+
throw new ErrorResponseException("getSignInUrl", response);
120+
}
121121

122-
return client.restClient()
123-
.responseBuilderFactory()
124-
.<String, CloudException>newInstance(client.serializerAdapter())
125-
.register(HttpURLConnection.HTTP_OK, new TypeToken<String>() {
126-
}.getType())
127-
.registerError(CloudException.class)
128-
.build(response);
122+
return new ServiceResponse<>(response.body().source().buffer().readUtf8(), response);
129123
}
130124
}

libraries/bot-schema/src/main/java/com/microsoft/bot/schema/Serialization.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private Serialization() {
2929

3030
/**
3131
* Deserialize a value.
32-
*
32+
*
3333
* @param obj The object to deserialize.
3434
* @param classType The class type to convert to.
3535
* @param <T> The type of the return value.
@@ -45,7 +45,7 @@ public static <T> T getAs(Object obj, Class<T> classType) {
4545

4646
/**
4747
* Deserialize a value.
48-
*
48+
*
4949
* @param obj The object to deserialize.
5050
* @param classType The class type to convert to.
5151
* @param <T> The type of the return value.
@@ -64,7 +64,7 @@ public static <T> T safeGetAs(Object obj, Class<T> classType) throws JsonProcess
6464
/**
6565
* Deserializes an object to a type as a future to ease CompletableFuture
6666
* chaining.
67-
*
67+
*
6868
* @param obj The object to deserialize.
6969
* @param classType Class information to convert to.
7070
* @param <R> The return Type.
@@ -85,7 +85,7 @@ public static <R> CompletableFuture<R> futureGetAs(Object obj, Class<R> classTyp
8585

8686
/**
8787
* Converts an input object to another type.
88-
*
88+
*
8989
* @param source The object to convert.
9090
* @param toClass The class to convert to.
9191
* @param <T> Type of return value.
@@ -94,4 +94,15 @@ public static <R> CompletableFuture<R> futureGetAs(Object obj, Class<R> classTyp
9494
public static <T> T convert(Object source, Class<T> toClass) {
9595
return getAs(source, toClass);
9696
}
97+
98+
/**
99+
* Convert an object to a JSON string.
100+
*
101+
* @param source The object to convert.
102+
* @return The JSON string value.
103+
* @throws JsonProcessingException Error converting to JSON
104+
*/
105+
public static String toString(Object source) throws JsonProcessingException {
106+
return objectMapper.writeValueAsString(source);
107+
}
97108
}

0 commit comments

Comments
 (0)