-
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.
Add a default certificate to server-demo and display it in security tab
- Loading branch information
1 parent
f2748e3
commit 4fff2f7
Showing
13 changed files
with
365 additions
and
117 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,6 @@ bin/ | |
**/src/test/**/logback*.xml | ||
|
||
# Credentials files # | ||
**/*.der | ||
**/*.der | ||
**/*.pem | ||
!**/src/**/*.der |
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
75 changes: 75 additions & 0 deletions
75
leshan-core/src/main/java/org/eclipse/leshan/core/credentials/CredentialsReader.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,75 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2018 Sierra Wireless and others. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Eclipse Distribution License v1.0 which accompany this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* http://www.eclipse.org/legal/epl-v10.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.core.credentials; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.security.GeneralSecurityException; | ||
|
||
/** | ||
* An helper class to read credentials from various input. <br> | ||
* To be used you MUST implement at least one method between decode(byte[]) and decode(InputStream). | ||
*/ | ||
public abstract class CredentialsReader<T> { | ||
|
||
/** | ||
* Read credential from file | ||
*/ | ||
public T readFromFile(String fileName) throws IOException, GeneralSecurityException { | ||
byte[] bytes = Files.readAllBytes(Paths.get(fileName)); | ||
return decode(bytes); | ||
} | ||
|
||
/** | ||
* Read credential from resource (in a jar, war, ...) | ||
* | ||
* @see java.lang.ClassLoader#getResourceAsStream(String) | ||
*/ | ||
public T readFromResource(String resourcePath) throws IOException, GeneralSecurityException { | ||
try (InputStream in = ClassLoader.getSystemResourceAsStream(resourcePath)) { | ||
return decode(in); | ||
} | ||
} | ||
|
||
/** | ||
* Decode credential from byte array. | ||
*/ | ||
public T decode(byte[] bytes) throws IOException, GeneralSecurityException { | ||
try (ByteArrayInputStream in = new ByteArrayInputStream(bytes)) { | ||
return decode(in); | ||
} | ||
} | ||
|
||
/** | ||
* Decode credential from an InputStream. | ||
*/ | ||
public T decode(InputStream in) throws IOException, GeneralSecurityException { | ||
try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) { | ||
int nRead; | ||
byte[] data = new byte[1024]; | ||
while ((nRead = in.read(data, 0, data.length)) != -1) { | ||
buffer.write(data, 0, nRead); | ||
} | ||
buffer.flush(); | ||
|
||
return decode(buffer.toByteArray()); | ||
} | ||
} | ||
} |
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.