committed by
GitHub
34 changed files with 496 additions and 153 deletions
@ -0,0 +1,32 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.constructor.oauth2; |
|||
|
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.oauth2.OAuth2Info; |
|||
import org.thingsboard.server.gen.edge.v1.OAuth2UpdateMsg; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
|
|||
@Component |
|||
@TbCoreComponent |
|||
public class OAuth2MsgConstructor { |
|||
|
|||
public OAuth2UpdateMsg constructOAuth2UpdateMsg(OAuth2Info oAuth2Info) { |
|||
return OAuth2UpdateMsg.newBuilder().setEntity(JacksonUtil.toString(oAuth2Info)).build(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,56 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.fetch; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.EdgeUtils; |
|||
import org.thingsboard.server.common.data.edge.Edge; |
|||
import org.thingsboard.server.common.data.edge.EdgeEvent; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventType; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.oauth2.OAuth2Info; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.dao.oauth2.OAuth2Service; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
@AllArgsConstructor |
|||
@Slf4j |
|||
public class OAuth2EdgeEventFetcher implements EdgeEventFetcher { |
|||
|
|||
private final OAuth2Service oAuth2Service; |
|||
|
|||
@Override |
|||
public PageLink getPageLink(int pageSize) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public PageData<EdgeEvent> fetchEdgeEvents(TenantId tenantId, Edge edge, PageLink pageLink) { |
|||
List<EdgeEvent> result = new ArrayList<>(); |
|||
OAuth2Info oAuth2Info = oAuth2Service.findOAuth2Info(); |
|||
result.add(EdgeUtils.constructEdgeEvent(tenantId, edge.getId(), EdgeEventType.OAUTH2, |
|||
EdgeEventActionType.ADDED, null, JacksonUtil.valueToTree(oAuth2Info))); |
|||
// returns PageData object to be in sync with other fetchers
|
|||
return new PageData<>(result, 1, result.size(), false); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.processor.oauth2; |
|||
|
|||
import com.google.common.util.concurrent.Futures; |
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.EdgeUtils; |
|||
import org.thingsboard.server.common.data.edge.EdgeEvent; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventType; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.oauth2.OAuth2Info; |
|||
import org.thingsboard.server.gen.edge.v1.DownlinkMsg; |
|||
import org.thingsboard.server.gen.edge.v1.OAuth2UpdateMsg; |
|||
import org.thingsboard.server.gen.transport.TransportProtos; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.edge.rpc.processor.BaseEdgeProcessor; |
|||
|
|||
@Slf4j |
|||
@Component |
|||
@TbCoreComponent |
|||
public class OAuth2EdgeProcessor extends BaseEdgeProcessor { |
|||
|
|||
public DownlinkMsg convertOAuth2EventToDownlink(EdgeEvent edgeEvent) { |
|||
DownlinkMsg downlinkMsg = null; |
|||
OAuth2Info oAuth2Info = JacksonUtil.convertValue(edgeEvent.getBody(), OAuth2Info.class); |
|||
if (oAuth2Info != null) { |
|||
OAuth2UpdateMsg oAuth2UpdateMsg = oAuth2MsgConstructor.constructOAuth2UpdateMsg(oAuth2Info); |
|||
downlinkMsg = DownlinkMsg.newBuilder() |
|||
.setDownlinkMsgId(EdgeUtils.nextPositiveInt()) |
|||
.addOAuth2UpdateMsg(oAuth2UpdateMsg) |
|||
.build(); |
|||
} |
|||
return downlinkMsg; |
|||
} |
|||
|
|||
public ListenableFuture<Void> processOAuth2Notification(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) { |
|||
OAuth2Info oAuth2Info = JacksonUtil.fromString(edgeNotificationMsg.getBody(), OAuth2Info.class); |
|||
if (oAuth2Info == null) { |
|||
return Futures.immediateFuture(null); |
|||
} |
|||
EdgeEventType type = EdgeEventType.valueOf(edgeNotificationMsg.getType()); |
|||
EdgeEventActionType actionType = EdgeEventActionType.valueOf(edgeNotificationMsg.getAction()); |
|||
return processActionForAllEdges(tenantId, type, actionType, null, JacksonUtil.toJsonNode(edgeNotificationMsg.getBody()), null); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,113 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.edge; |
|||
|
|||
import com.google.common.collect.Lists; |
|||
import com.google.protobuf.AbstractMessage; |
|||
import org.junit.Assert; |
|||
import org.junit.Test; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.oauth2.MapperType; |
|||
import org.thingsboard.server.common.data.oauth2.OAuth2CustomMapperConfig; |
|||
import org.thingsboard.server.common.data.oauth2.OAuth2DomainInfo; |
|||
import org.thingsboard.server.common.data.oauth2.OAuth2Info; |
|||
import org.thingsboard.server.common.data.oauth2.OAuth2MapperConfig; |
|||
import org.thingsboard.server.common.data.oauth2.OAuth2ParamsInfo; |
|||
import org.thingsboard.server.common.data.oauth2.OAuth2RegistrationInfo; |
|||
import org.thingsboard.server.common.data.oauth2.SchemeType; |
|||
import org.thingsboard.server.dao.service.DaoSqlTest; |
|||
import org.thingsboard.server.gen.edge.v1.OAuth2UpdateMsg; |
|||
|
|||
import java.util.Arrays; |
|||
import java.util.Collections; |
|||
import java.util.UUID; |
|||
|
|||
@DaoSqlTest |
|||
public class OAuth2EdgeTest extends AbstractEdgeTest { |
|||
|
|||
@Test |
|||
public void testOAuth2Support() throws Exception { |
|||
loginSysAdmin(); |
|||
|
|||
// enable oauth
|
|||
edgeImitator.allowIgnoredTypes(); |
|||
edgeImitator.expectMessageAmount(1); |
|||
OAuth2Info oAuth2Info = createDefaultOAuth2Info(); |
|||
oAuth2Info = doPost("/api/oauth2/config", oAuth2Info, OAuth2Info.class); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
AbstractMessage latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof OAuth2UpdateMsg); |
|||
OAuth2UpdateMsg oAuth2UpdateMsg = (OAuth2UpdateMsg) latestMessage; |
|||
OAuth2Info result = JacksonUtil.fromString(oAuth2UpdateMsg.getEntity(), OAuth2Info.class, true); |
|||
Assert.assertEquals(oAuth2Info, result); |
|||
|
|||
// disable oauth support
|
|||
edgeImitator.expectMessageAmount(1); |
|||
oAuth2Info.setEnabled(false); |
|||
oAuth2Info.setEdgeEnabled(false); |
|||
doPost("/api/oauth2/config", oAuth2Info, OAuth2Info.class); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof OAuth2UpdateMsg); |
|||
oAuth2UpdateMsg = (OAuth2UpdateMsg) latestMessage; |
|||
result = JacksonUtil.fromString(oAuth2UpdateMsg.getEntity(), OAuth2Info.class, true); |
|||
Assert.assertEquals(oAuth2Info, result); |
|||
|
|||
edgeImitator.ignoreType(OAuth2UpdateMsg.class); |
|||
loginTenantAdmin(); |
|||
} |
|||
|
|||
private OAuth2Info createDefaultOAuth2Info() { |
|||
return new OAuth2Info(true, true, Lists.newArrayList( |
|||
OAuth2ParamsInfo.builder() |
|||
.domainInfos(Lists.newArrayList( |
|||
OAuth2DomainInfo.builder().name("domain").scheme(SchemeType.MIXED).build() |
|||
)) |
|||
.mobileInfos(Collections.emptyList()) |
|||
.clientRegistrations(Lists.newArrayList( |
|||
validRegistrationInfo() |
|||
)) |
|||
.build() |
|||
)); |
|||
} |
|||
|
|||
private OAuth2RegistrationInfo validRegistrationInfo() { |
|||
return OAuth2RegistrationInfo.builder() |
|||
.clientId(UUID.randomUUID().toString()) |
|||
.clientSecret(UUID.randomUUID().toString()) |
|||
.authorizationUri(UUID.randomUUID().toString()) |
|||
.accessTokenUri(UUID.randomUUID().toString()) |
|||
.scope(Arrays.asList(UUID.randomUUID().toString(), UUID.randomUUID().toString())) |
|||
.platforms(Collections.emptyList()) |
|||
.userInfoUri(UUID.randomUUID().toString()) |
|||
.userNameAttributeName(UUID.randomUUID().toString()) |
|||
.jwkSetUri(UUID.randomUUID().toString()) |
|||
.clientAuthenticationMethod(UUID.randomUUID().toString()) |
|||
.loginButtonLabel(UUID.randomUUID().toString()) |
|||
.mapperConfig( |
|||
OAuth2MapperConfig.builder() |
|||
.type(MapperType.CUSTOM) |
|||
.custom( |
|||
OAuth2CustomMapperConfig.builder() |
|||
.url(UUID.randomUUID().toString()) |
|||
.build() |
|||
) |
|||
.build() |
|||
) |
|||
.build(); |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue