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 @@ -53,6 +53,7 @@ public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fie
disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
}

public String convertToString(Object value) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.servicecomb.demo.edge.authentication.encrypt;

import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@RestSchema(schemaId = "encrypt")
@RequestMapping(path = "auth/v1")
public class EncryptImpl {
@GetMapping(path = "/queryUserId")
public String queryUserId(String serviceToken) {
return serviceToken + "-userId";
}

@GetMapping(path = "/queryHcr")
public Hcr queryHcr(String hcrId) {
Hcr hcr = new Hcr();
hcr.setBodyKey("bodyKey-" + hcrId + "-");
hcr.setSignatureKey("signatureKey-" + hcrId + "-");
return hcr;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.servicecomb.demo.edge.authentication.encrypt;

public class Hcr {
private String bodyKey;

private String signatureKey;

public String getBodyKey() {
return bodyKey;
}

public void setBodyKey(String bodyKey) {
this.bodyKey = bodyKey;
}

public String getSignatureKey() {
return signatureKey;
}

public void setSignatureKey(String signatureKey) {
this.signatureKey = signatureKey;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.servicecomb.demo.edge.model.DependTypeA;
import org.apache.servicecomb.demo.edge.model.RecursiveSelfType;
import org.apache.servicecomb.demo.edge.model.ResultWithInstance;
import org.apache.servicecomb.demo.edge.model.User;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -106,4 +107,9 @@ public RecursiveSelfType recursiveSelf(@RequestBody RecursiveSelfType value) {
public DependTypeA dependType(@RequestBody DependTypeA value) {
return value;
}

@PostMapping(path = "encrypt")
public User encrypt(@RequestBody User value) {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

Expand All @@ -43,6 +44,8 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class Consumer {
Expand Down Expand Up @@ -111,6 +114,32 @@ public void run(String prefix) {
checkResult("v2/dec", decV2Result, "2.0.0");
}

public void testEncrypt() {
prepareEdge("encryptApi");
String url = edgePrefix + "/v2/encrypt";

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);

MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
form.add("name", "userName");
form.add("age", "10");
form.add("serviceToken", "serviceTokenTest");
form.add("hcrId", "hcrIdTest");
form.add("body", "bodyKey-hcrIdTest-{\"body1\":\"b1\",\"body2\":\"b2\",\"body3\":\"b3\"}");

HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(form, headers);

@SuppressWarnings("unchecked")
Map<String, Object> result = (Map<String, Object>) template.postForObject(url, entity, Map.class);
Assert.isTrue(result.containsKey("signature"), "must exist signature");
result.remove("signature");

String expected = "{name=userName, age=10, userId=serviceTokenTest-userId, body1=b1, body2=b2, body3=b3}";
Assert.isTrue(expected.equalsIgnoreCase(result.toString()),
String.format("expected: %s\nreal : %s", expected, result.toString()));
}

protected void testRecursiveSelf() {
String url = edgePrefix + "/v2/recursiveSelf";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ public static void main(String[] args) throws Exception {
Log4jUtils.init();
BeanUtils.init();

System.out.println("Running api dispater.");
new Consumer().testEncrypt();

System.out.println("Running api dispatcher.");
new Consumer().run("api");
System.out.println("Running rest dispater.");
System.out.println("Running rest dispatcher.");
new Consumer().run("rest");
System.out.println("Running url dispater.");
System.out.println("Running url dispatcher.");
new Consumer().run("url");

System.out.println("All test case finished.");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.servicecomb.demo.edge.authentication.encrypt;

public class Hcr {
private String bodyKey;

private String signatureKey;

public String getBodyKey() {
return bodyKey;
}

public void setBodyKey(String bodyKey) {
this.bodyKey = bodyKey;
}

public String getSignatureKey() {
return signatureKey;
}

public void setSignatureKey(String signatureKey) {
this.signatureKey = signatureKey;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.apache.servicecomb.demo.edge.service;/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

public interface EdgeConst {
String ENCRYPT_CONTEXT = "encryptContext";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.servicecomb.demo.edge.service.encrypt;

import java.util.concurrent.CompletableFuture;

import org.apache.servicecomb.demo.edge.authentication.encrypt.Hcr;

public interface Encrypt {
CompletableFuture<String> queryUserId(String serviceToken);

CompletableFuture<Hcr> queryHcr(String hcrId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.servicecomb.demo.edge.service.encrypt;

import org.apache.servicecomb.demo.edge.authentication.encrypt.Hcr;

public class EncryptContext {
private Hcr hcr;

private String userId;

public EncryptContext(Hcr hcr, String userId) {
this.hcr = hcr;
this.userId = userId;
}

public Hcr getHcr() {
return hcr;
}

public void setHcr(Hcr hcr) {
this.hcr = hcr;
}

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}
}
Loading