Add QR encoder to an API. Add rendering of shares as QR codes to front-end.
This commit is contained in:
@@ -48,7 +48,12 @@
|
||||
<groupId>rs.in.zivanovic</groupId>
|
||||
<artifactId>sss</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.glxn.qrgen</groupId>
|
||||
<artifactId>javase</artifactId>
|
||||
<version>2.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* The MIT License
|
||||
*
|
||||
* Copyright 2014 Marko Zivanovic.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package rs.in.zivanovic.share.a.secret.api.controllers;
|
||||
|
||||
import org.springframework.validation.BindingResult;
|
||||
import rs.in.zivanovic.share.a.secret.api.dto.SasResponse;
|
||||
|
||||
/**
|
||||
* Base class for our controllers. It contains common methods used across different controllers.
|
||||
*
|
||||
* @author Marko Zivanovic <marko@zivanovic.in.rs>
|
||||
*/
|
||||
public abstract class AbstractSasController {
|
||||
|
||||
protected SasResponse processValidationErrors(BindingResult br) {
|
||||
SasResponse r = SasResponse.badRequest();
|
||||
br.getFieldErrors().stream().forEach(err -> {
|
||||
r.withInvalidParameterValueError(err.getField(), err.getRejectedValue(), err.getDefaultMessage());
|
||||
});
|
||||
return r;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* The MIT License
|
||||
*
|
||||
* Copyright 2014 Marko Zivanovic.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package rs.in.zivanovic.share.a.secret.api.controllers;
|
||||
|
||||
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import net.glxn.qrgen.core.image.ImageType;
|
||||
import net.glxn.qrgen.javase.QRCode;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import rs.in.zivanovic.share.a.secret.api.dto.SasResponse;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Marko Zivanovic <marko@zivanovic.in.rs>
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/qr")
|
||||
public class QrCodecController extends AbstractSasController {
|
||||
|
||||
private static final List<Integer> VALID_SIZES = Arrays.asList(50, 100, 200, 250);
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/encode")
|
||||
public ResponseEntity encode(
|
||||
@RequestParam(value = "text", required = true) String text,
|
||||
@RequestParam(value = "size", required = false, defaultValue = "250") int size,
|
||||
@RequestParam(value = "ecl", required = false, defaultValue = "H") ErrorCorrectionLevel ecl) {
|
||||
ResponseEntity response;
|
||||
if (!VALID_SIZES.contains(size)) {
|
||||
response = SasResponse.badRequest().
|
||||
withError(SasResponse.Error.INVALID_PARAMETER_VALUE,
|
||||
"Invalid QR code size, supported sizes: " + VALID_SIZES.toString()).build();
|
||||
} else {
|
||||
ByteArrayOutputStream baos = QRCode.from(text).to(ImageType.JPG).
|
||||
withSize(size, size).withCharset(StandardCharsets.US_ASCII.name()).
|
||||
withErrorCorrection(ecl).stream();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("Content-Type", "image/jpeg");
|
||||
response = new ResponseEntity<>(baos.toByteArray(), headers, HttpStatus.OK);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -50,7 +50,7 @@ import rs.in.zivanovic.sss.ShamirSecretSharing;
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/sas")
|
||||
public class SasController {
|
||||
public class SasController extends AbstractSasController {
|
||||
|
||||
@Value("${info.build.version:'<N/A>'}")
|
||||
private String version;
|
||||
@@ -94,14 +94,6 @@ public class SasController {
|
||||
return SasResponse.ok().withData(new VersionResponse(version, buildTime)).build();
|
||||
}
|
||||
|
||||
private SasResponse processValidationErrors(BindingResult br) {
|
||||
SasResponse r = SasResponse.badRequest();
|
||||
br.getFieldErrors().stream().forEach(err -> {
|
||||
r.withInvalidParameterValueError(err.getField(), err.getRejectedValue(), err.getDefaultMessage());
|
||||
});
|
||||
return r;
|
||||
}
|
||||
|
||||
private List<SecretShare> decodeSecretShares(JoinParameters params) {
|
||||
List<SecretShare> shares = new ArrayList<>(params.getShares().size());
|
||||
params.getShares().stream().forEach(share -> {
|
||||
|
||||
@@ -24,8 +24,8 @@
|
||||
package rs.in.zivanovic.share.a.secret.api.dto;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import org.apache.tomcat.util.codec.binary.Base64;
|
||||
import rs.in.zivanovic.sss.SasUtils;
|
||||
import rs.in.zivanovic.sss.SecretShare;
|
||||
|
||||
@@ -40,7 +40,7 @@ public class SplitResponse {
|
||||
public SplitResponse(List<SecretShare> shares) {
|
||||
this.shares.clear();
|
||||
shares.stream().forEachOrdered(share -> {
|
||||
this.shares.add(Base64.encodeBase64String(SasUtils.encodeToBinary(share)));
|
||||
this.shares.add(Base64.getUrlEncoder().encodeToString(SasUtils.encodeToBinary(share)));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -22,21 +22,22 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
var API_BASE = "https://share-a-secret-api.herokuapp.com/sas";
|
||||
//var API_BASE = "https://share-a-secret-api.herokuapp.com";
|
||||
var API_BASE = "http://localhost:8080";
|
||||
|
||||
angular.module('sasServices', []).
|
||||
factory('SasService', ['$resource', function ($resource) {
|
||||
return $resource(API_BASE, {}, {
|
||||
split: {
|
||||
url: API_BASE + "/split",
|
||||
url: API_BASE + "/sas/split",
|
||||
method: "POST"
|
||||
},
|
||||
join: {
|
||||
url: API_BASE + "/join",
|
||||
url: API_BASE + "/sas/join",
|
||||
method: "POST"
|
||||
},
|
||||
version: {
|
||||
url: API_BASE + "/version",
|
||||
url: API_BASE + "/sas/version",
|
||||
method: "GET"
|
||||
}
|
||||
});
|
||||
|
||||
@@ -32,10 +32,10 @@
|
||||
</div>
|
||||
|
||||
<div class="row" ng-show="shares.length">
|
||||
<div class="row">
|
||||
<hr/>
|
||||
<h1>Here are your secret shares ...</h1>
|
||||
</div>
|
||||
<!--<hr/>-->
|
||||
<h1>Here are your secret shares ...</h1>
|
||||
<!-- <div class="row">
|
||||
</div>-->
|
||||
|
||||
<tabset>
|
||||
<tab>
|
||||
@@ -59,6 +59,19 @@
|
||||
</div>
|
||||
</div>
|
||||
</tab>
|
||||
<tab>
|
||||
<tab-heading><i class="glyphicon glyphicon-qrcode"></i> QR codes</tab-heading>
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<p class="text-muted">Use QR encoded shares for easy scanning into mobile devices.</p>
|
||||
<div class="row">
|
||||
<div ng-repeat="share in shares">
|
||||
<div class="col-md-2"><img ng-src="/qr/encode?text={{share}}&size=200&ecl=M"/></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</tab>
|
||||
</tabset>
|
||||
</div>
|
||||
</div>
|
||||
Reference in New Issue
Block a user