-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Server-demo-v2: Add Server Tab about credentials
- Loading branch information
1 parent
abd99c2
commit 7f6e488
Showing
10 changed files
with
376 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
leshan-server-demo/src/main/java/org/eclipse/leshan/server/demo/servlet/ServerServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.