Skip to content

Commit

Permalink
fix(ms teams): refresh token can be without client secret, fix create…
Browse files Browse the repository at this point in the history
… chat method (#2275)

* fix(ms-teams): add constraint for create chat topic and jsonAlias for principalName

* fix(ms teams): client secret optional for refresh token auth
  • Loading branch information
Oleksiivanov committed Mar 28, 2024
1 parent 955e242 commit f55483b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,8 @@
}, {
"id" : "authentication.refresh.clientSecret",
"label" : "Client secret",
"description" : "The secret value of the Azure AD application",
"optional" : false,
"constraints" : {
"notEmpty" : true
},
"description" : "The secret value of the Azure AD application; optional, depends on whether the client is public or private",
"optional" : true,
"feel" : "optional",
"group" : "authentication",
"binding" : {
Expand Down Expand Up @@ -386,6 +383,10 @@
},
"condition" : {
"allMatch" : [ {
"property" : "data.createChat.chatType",
"equals" : "group",
"type" : "simple"
}, {
"property" : "data.chatMethod",
"equals" : "createChat",
"type" : "simple"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,8 @@
}, {
"id" : "authentication.refresh.clientSecret",
"label" : "Client secret",
"description" : "The secret value of the Azure AD application",
"optional" : false,
"constraints" : {
"notEmpty" : true
},
"description" : "The secret value of the Azure AD application; optional, depends on whether the client is public or private",
"optional" : true,
"feel" : "optional",
"group" : "authentication",
"binding" : {
Expand Down Expand Up @@ -381,6 +378,10 @@
},
"condition" : {
"allMatch" : [ {
"property" : "data.createChat.chatType",
"equals" : "group",
"type" : "simple"
}, {
"property" : "data.chatMethod",
"equals" : "createChat",
"type" : "simple"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package io.camunda.connector.model;

import com.fasterxml.jackson.annotation.JsonAlias;
import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.NotNull;
import java.util.List;
Expand All @@ -15,11 +16,13 @@
public class Member {

public static final String USER_DATA_BIND = "user@odata.bind";
public static final String USER_DATA_TYPE = "@odata.type";
public static final List<String> OWNER_ROLES = List.of("owner");

private String userId;

@JsonAlias(value = {"userPrincipalName", "principalName"})
private String userPrincipalName;

@NotNull private List<String> roles;

@AssertTrue(message = "Missing one of properties : [userId, userPrincipalName]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ public record RefreshTokenAuthentication(
label = "Tenant ID",
description = "The tenant ID of the application")
String tenantId,
@NotBlank
@TemplateProperty(
@TemplateProperty(
group = "authentication",
id = "refresh.clientSecret",
label = "Client secret",
description = "The secret value of the Azure AD application")
optional = true,
description =
"The secret value of the Azure AD application; optional, depends on whether the client is public or private")
String clientSecret)
implements MSTeamsAuthentication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public record CreateChat(
id = "createChat.topic",
label = "Topic",
optional = true,
condition =
@TemplateProperty.PropertyCondition(
property = "data.createChat.chatType",
equals = "group"),
description = "Set topic of chat (optional)")
String topic,
@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Mono;

Expand Down Expand Up @@ -79,29 +79,31 @@ public GraphServiceClient buildAndGetGraphServiceClient(final String token) {

@NotNull
private Request buildRequest(final RefreshTokenAuthentication authentication) {
RequestBody formBody =

FormBody.Builder formBodyBuilder =
new FormBody.Builder()
.add(CLIENT_ID, authentication.clientId())
.add(GRANT_TYPE, REFRESH_TOKEN)
.add(CLIENT_SECRET, authentication.clientSecret())
.add(REFRESH_TOKEN, authentication.token())
.build();
.add(REFRESH_TOKEN, authentication.token());
if (StringUtils.isNoneBlank(authentication.clientSecret())) {
formBodyBuilder.add(CLIENT_SECRET, authentication.clientSecret());
}
return new Request.Builder()
.url(String.format(URL, authentication.tenantId()))
.header(CONTENT_TYPE, X_WWW_FORM_URLENCODED)
.post(formBody)
.post(formBodyBuilder.build())
.build();
}

private String getAccessToken(final Request request) {
try (Response execute = okHttpClient.newCall(request).execute()) {
if (execute.isSuccessful()) {
try (Response response = okHttpClient.newCall(request).execute()) {
if (response.isSuccessful()) {
return ObjectMapperSupplier.objectMapper()
.readTree(execute.body().string())
.readTree(response.body().string())
.get(ACCESS_TOKEN)
.asText();
} else {
throw new RuntimeException(execute.message());
throw new RuntimeException(response.message());
}
} catch (JsonProcessingException e) {
throw new RuntimeException("Error while parse refresh token response", e);
Expand Down

0 comments on commit f55483b

Please sign in to comment.