Skip to content

Commit

Permalink
Server-demo-v2: Add Server Tab about credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jun 25, 2021
1 parent abd99c2 commit 7f6e488
Show file tree
Hide file tree
Showing 10 changed files with 376 additions and 15 deletions.
4 changes: 2 additions & 2 deletions leshan-bsserver-demo/webapp2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"axios": "^0.21.1",
"core-js": "^3.6.5",
"file-saver": "^2.0.5",
"vue": "^2.6.11",
"vue": "^2.6.13",
"vue-moment": "^4.1.0",
"vue-preferences": "^3.0.0",
"vue-router": "^3.2.0",
Expand All @@ -33,7 +33,7 @@
"sass": "^1.32.0",
"sass-loader": "^10.0.0",
"vue-cli-plugin-vuetify": "~2.2.0",
"vue-template-compiler": "^2.6.11",
"vue-template-compiler": "^2.6.13",
"vuetify-loader": "^1.7.0"
},
"eslintConfig": {
Expand Down
16 changes: 8 additions & 8 deletions leshan-bsserver-demo/webapp2/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8321,10 +8321,10 @@ vue-style-loader@^4.1.0, vue-style-loader@^4.1.2:
hash-sum "^1.0.2"
loader-utils "^1.0.2"

vue-template-compiler@^2.6.11:
version "2.6.12"
resolved "https://registry.yarnpkg.com/vue-template-compiler/-/vue-template-compiler-2.6.12.tgz#947ed7196744c8a5285ebe1233fe960437fcc57e"
integrity sha512-OzzZ52zS41YUbkCBfdXShQTe69j1gQDZ9HIX8miuC9C3rBCk9wIRjLiZZLrmX9V+Ftq/YEyv1JaVr5Y/hNtByg==
vue-template-compiler@^2.6.13:
version "2.6.14"
resolved "https://registry.yarnpkg.com/vue-template-compiler/-/vue-template-compiler-2.6.14.tgz#a2f0e7d985670d42c9c9ee0d044fed7690f4f763"
integrity sha512-ODQS1SyMbjKoO1JBJZojSw6FE4qnh9rIpUZn2EUT86FKizx9uH5z6uXiIrm4/Nb/gwxTi/o17ZDEGWAXHvtC7g==
dependencies:
de-indent "^1.0.2"
he "^1.1.0"
Expand All @@ -8334,10 +8334,10 @@ vue-template-es2015-compiler@^1.9.0:
resolved "https://registry.yarnpkg.com/vue-template-es2015-compiler/-/vue-template-es2015-compiler-1.9.1.tgz#1ee3bc9a16ecbf5118be334bb15f9c46f82f5825"
integrity sha512-4gDntzrifFnCEvyoO8PqyJDmguXgVPxKiIxrBKjIowvL9l+N66196+72XVYR8BBf1Uv1Fgt3bGevJ+sEmxfZzw==

vue@^2.6.11:
version "2.6.12"
resolved "https://registry.yarnpkg.com/vue/-/vue-2.6.12.tgz#f5ebd4fa6bd2869403e29a896aed4904456c9123"
integrity sha512-uhmLFETqPPNyuLLbsKz6ioJ4q7AZHzD8ZVFNATNyICSZouqP2Sz0rotWQC8UNBF6VGSCs5abnKJoStA6JbCbfg==
vue@^2.6.13:
version "2.6.14"
resolved "https://registry.yarnpkg.com/vue/-/vue-2.6.14.tgz#e51aa5250250d569a3fbad3a8a5a687d6036e235"
integrity sha512-x2284lgYvjOMj3Za7kqzRcUSxBboHqtgRE2zlos1qWaOye5yUmHn42LB1250NJBLRwEcdrB0JRwyPTEPhfQjiQ==

vuetify-dialog@^2.0.14:
version "2.0.14"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import org.eclipse.leshan.server.demo.servlet.EventServlet;
import org.eclipse.leshan.server.demo.servlet.ObjectSpecServlet;
import org.eclipse.leshan.server.demo.servlet.SecurityServlet;
import org.eclipse.leshan.server.demo.servlet.ServerServlet;
import org.eclipse.leshan.server.demo.utils.MagicLwM2mValueConverter;
import org.eclipse.leshan.server.model.LwM2mModelProvider;
import org.eclipse.leshan.server.model.VersionedModelProvider;
Expand Down Expand Up @@ -623,6 +624,15 @@ public static void createAndStartServer(String webAddress, int webPort, String l
root.addServlet(securityServletHolder, "/api/security/*");
root.addServlet(securityServletHolder, "/v2/api/security/*");// Temporary code to be able to serve both UI

ServletHolder serverServletHolder;
if (publicKey != null) {
serverServletHolder = new ServletHolder(new ServerServlet(lwServer, publicKey));
} else {
serverServletHolder = new ServletHolder(new ServerServlet(lwServer, serverCertificateChain[0]));
}
root.addServlet(serverServletHolder, "/api/server/*");
root.addServlet(serverServletHolder, "/v2/api/server/*");// Temporary code to be able to serve both UI

ServletHolder objectSpecServletHolder = new ServletHolder(
new ObjectSpecServlet(lwServer.getModelProvider(), lwServer.getRegistrationService()));
root.addServlet(objectSpecServletHolder, "/api/objectspecs/*");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*******************************************************************************
* Copyright (c) 2017 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.server.demo.servlet;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.PublicKey;
import java.security.cert.X509Certificate;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.eclipse.leshan.server.californium.LeshanServer;
import org.eclipse.leshan.server.demo.servlet.json.PublicKeySerDes;
import org.eclipse.leshan.server.demo.servlet.json.SecuritySerializer;
import org.eclipse.leshan.server.demo.servlet.json.X509CertificateSerDes;
import org.eclipse.leshan.server.security.SecurityInfo;

import com.eclipsesource.json.JsonObject;
import com.google.gson.GsonBuilder;

public class ServerServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

private final X509CertificateSerDes certificateSerDes;
private final PublicKeySerDes publicKeySerDes;

private final LeshanServer server;
private final PublicKey publicKey;
private final X509Certificate serverCertificate;

public ServerServlet(LeshanServer server, X509Certificate serverCertificate) {
this.server = server;
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(SecurityInfo.class, new SecuritySerializer());
certificateSerDes = new X509CertificateSerDes();
publicKeySerDes = new PublicKeySerDes();

this.publicKey = null;
this.serverCertificate = serverCertificate;
}

public ServerServlet(LeshanServer server, PublicKey serverPublicKey) {
this.server = server;
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(SecurityInfo.class, new SecuritySerializer());
certificateSerDes = new X509CertificateSerDes();
publicKeySerDes = new PublicKeySerDes();
this.publicKey = serverPublicKey;
this.serverCertificate = null;
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String[] path = StringUtils.split(req.getPathInfo(), '/');

if (path.length != 1) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}

if ("security".equals(path[0])) {
JsonObject security = new JsonObject();
if (publicKey != null) {
security.add("pubkey", publicKeySerDes.jSerialize(publicKey));
} else if (serverCertificate != null) {
security.add("certificate", certificateSerDes.jSerialize(serverCertificate));
}
resp.setContentType("application/json");
resp.getOutputStream().write(security.toString().getBytes(StandardCharsets.UTF_8));
resp.setStatus(HttpServletResponse.SC_OK);
return;
}

if ("endpoint".equals(path[0])) {
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType("application/json");
resp.getOutputStream()
.write(String
.format("{ \"securedEndpointPort\":\"%s\", \"unsecuredEndpointPort\":\"%s\"}",
server.getSecuredAddress().getPort(), server.getUnsecuredAddress().getPort())
.getBytes(StandardCharsets.UTF_8));
return;
}

resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
}
}
3 changes: 2 additions & 1 deletion leshan-server-demo/webapp2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
"@mdi/font": "^5.9.55",
"axios": "^0.21.1",
"core-js": "^3.6.5",
"file-saver": "^2.0.5",
"vue": "^2.6.13",
"vue-moment": "^4.1.0",
"vue-preferences": "^3.0.0",
"vue-router": "^3.2.0",
"vue-sse": "^2.0.2",
"vuetify": "^2.5.1",
"vuetify": "^2.5.5",
"vuetify-dialog": "^2.0.14"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions leshan-server-demo/webapp2/src/components/LeshanNavBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default {
return {
pages: [
{ title: "Clients", route: "/clients", icon: "mdi-devices" },
{ title: "Server", route: "/server", icon: "mdi-server-security" },
{ title: "Security", route: "/security", icon: "mdi-shield-key" },
{ title: "About", route: "/about", icon: "mdi-information-outline" },
],
Expand Down
30 changes: 30 additions & 0 deletions leshan-server-demo/webapp2/src/js/byteutils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function toHex(byteArray){
let hex = [];
for (let i in byteArray){
hex[i] = byteArray[i].toString(16).toUpperCase();
if (hex[i].length === 1){
hex[i] = '0' + hex[i];
}
}
return hex.join('');
}

function toAscii(byteArray){
let ascii = [];
for (let i in byteArray){
ascii[i] = String.fromCharCode(byteArray[i]);
}
return ascii.join('');
}

function base64ToBytes(base64){
let byteKey = atob(base64);
let byteKeyLength = byteKey.length;
let array = new Uint8Array(new ArrayBuffer(byteKeyLength));
for(let i = 0; i < byteKeyLength; i++) {
array[i] = byteKey.charCodeAt(i);
}
return array;
}

export { toHex, base64ToBytes, toAscii};
6 changes: 6 additions & 0 deletions leshan-server-demo/webapp2/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Clients from "../views/Clients.vue";
import Client from "../views/Client.vue";
import ObjectView from "../views/ObjectView.vue";
import Security from "../views/Security.vue";
import Server from "../views/Server.vue"
import About from "../views/About.vue";

Vue.use(VueRouter);
Expand Down Expand Up @@ -33,6 +34,11 @@ const routes = [
name: "Security",
component: Security,
},
{
path: "/server",
name: "Server",
component: Server,
},
{
path: "/about",
name: "About",
Expand Down
Loading

0 comments on commit 7f6e488

Please sign in to comment.