-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Small improvements and incorporate review comments
Signed-off-by: Kartheeswaran Kalidass <[email protected]>
- Loading branch information
Showing
5 changed files
with
133 additions
and
40 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
88 changes: 88 additions & 0 deletions
88
...pse/hono/service/management/credentials/X509CertificateCredentialWithGeneratedAuthId.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,88 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*******************************************************************************/ | ||
package org.eclipse.hono.service.management.credentials; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import org.eclipse.hono.service.management.tenant.Tenant; | ||
import org.eclipse.hono.util.IdentityTemplate; | ||
import org.eclipse.hono.util.RegistryManagementConstants; | ||
|
||
import com.fasterxml.jackson.annotation.JsonGetter; | ||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
|
||
/** | ||
* An extended {@link X509CertificateCredential} to handle the generated authentication identifier. | ||
*/ | ||
public class X509CertificateCredentialWithGeneratedAuthId extends X509CertificateCredential { | ||
|
||
/** | ||
* Creates a new credentials object from the given authentication identifier, generated authentication | ||
* identifier and secrets. | ||
* | ||
* @param authId The authentication identifier. | ||
* @param generatedAuthId the authentication identifier generated by applying the <em>auth-id-template</em> | ||
* from the tenant's trust anchor to the client certificate's subject DN. | ||
* @param secrets The credential's secret(s). | ||
* @throws NullPointerException if authentication identifier or secrets is {@code null}. | ||
*/ | ||
private X509CertificateCredentialWithGeneratedAuthId(final String authId, final String generatedAuthId, | ||
final List<X509CertificateSecret> secrets) { | ||
super(authId, generatedAuthId, secrets); | ||
} | ||
|
||
/** | ||
* Gets the authentication identifier generated by applying the <em>auth-id-template</em> from the tenant's | ||
* trust anchor to the client certificate's subject DN. | ||
* | ||
* @return The generated authentication identifier or {@code null}. | ||
*/ | ||
@JsonGetter(value = RegistryManagementConstants.FIELD_GENERATED_AUTH_ID) | ||
@Override | ||
public final String getGeneratedAuthId() { | ||
return super.getGeneratedAuthId(); | ||
} | ||
|
||
/** | ||
* Applies the <em>auth-id-template</em> from the tenant's trust anchor to the client certificate's subject DN. | ||
* <p> | ||
* It is only applicable, if a template is configured. | ||
* | ||
* @param credential The x509 certificate credential. | ||
* @param tenant The tenant information. | ||
* @return the credential with generated authentication identifier. | ||
* @throws NullPointerException if the tenant is {@code null}. | ||
*/ | ||
@JsonIgnore | ||
public static X509CertificateCredentialWithGeneratedAuthId applyAuthIdTemplate( | ||
final X509CertificateCredential credential, final Tenant tenant) { | ||
Objects.requireNonNull(credential, "credential must not be null"); | ||
Objects.requireNonNull(tenant, "tenant information must not be null"); | ||
|
||
final String generatedAuthId = Optional.ofNullable(credential.getIssuerDN()) | ||
.flatMap(tenant::getAuthIdTemplate) | ||
.map(IdentityTemplate::new) | ||
.map(t -> t.apply(credential.getAuthId())) | ||
.orElse(null); | ||
final X509CertificateCredentialWithGeneratedAuthId credWithId = new X509CertificateCredentialWithGeneratedAuthId( | ||
credential.getAuthId(), generatedAuthId, credential.getSecrets()); | ||
|
||
credWithId.setComment(credential.getComment()); | ||
credWithId.setEnabled(credential.isEnabled()); | ||
credWithId.setExtensions(credential.getExtensions()); | ||
|
||
return credWithId; | ||
} | ||
} |
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