Skip to content

Commit

Permalink
[ELY-1721] Develop an alternative to 'wildfly-elytron-x500-deprecated'
Browse files Browse the repository at this point in the history
  • Loading branch information
fjuma committed Jan 2, 2019
1 parent 79e9910 commit fe784fa
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,11 @@
<artifactId>wildfly-elytron-x500-deprecated</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-x500-principal</artifactId>
<version>${project.version}</version>
</dependency>

<!--
External Modules
Expand Down Expand Up @@ -1225,6 +1230,7 @@
<module>wildfly-elytron-x500-cert-acme</module>
<module>wildfly-elytron-x500-cert-util</module>
<module>wildfly-elytron-x500-deprecated</module>
<module>wildfly-elytron-x500-principal</module>
<module>wildfly-elytron-tests-common</module>
<module>wildfly-elytron-tests</module>
<module>wildfly-elytron</module>
Expand Down
2 changes: 1 addition & 1 deletion wildfly-elytron-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@
</dependency>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-x500-deprecated</artifactId>
<artifactId>wildfly-elytron-x500-principal</artifactId>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@
import org.wildfly.security.auth.server.SecurityIdentity;
import org.wildfly.security.auth.server.SecurityRealm;
import org.wildfly.security.permission.PermissionVerifier;
import org.wildfly.security.x500.X500AttributePrincipalDecoder;
import org.wildfly.security.x500.cert.BasicConstraintsExtension;
import org.wildfly.security.x500.cert.SelfSignedX509CertificateAndSigningKey;
import org.wildfly.security.x500.cert.X509CertificateBuilder;
import org.wildfly.security.x500.principal.X500AttributePrincipalDecoder;

/**
* Simple test case to test authentication occurring during the establishment of an {@link SSLSession}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.junit.Test;
import org.wildfly.security.auth.server.PrincipalDecoder;
import org.wildfly.security.x500.principal.X500AttributePrincipalDecoder;

/**
* Tests for the X500AttributePrincipalDecoder.
Expand Down
4 changes: 4 additions & 0 deletions wildfly-elytron-x500-deprecated/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-x500</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-x500-principal</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* A principal decoder which decodes an attribute from an X.500 principal.
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
* @deprecated Use {@link org.wildfly.security.x500.principal.X500AttributePrincipalDecoder} instead
*/
@Deprecated
public final class X500AttributePrincipalDecoder implements PrincipalDecoder {
Expand Down
63 changes: 63 additions & 0 deletions wildfly-elytron-x500-principal/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ JBoss, Home of Professional Open Source.
~ Copyright 2018 Red Hat, Inc., and individual contributors
~ as indicated by the @author tags.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-parent</artifactId>
<version>1.8.0.Alpha1-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>wildfly-elytron-x500-principal</artifactId>

<name>WildFly Elytron - X.500 Principals</name>
<description>WildFly Security X.500 Principals</description>

<dependencies>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-auth-server</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-x500</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-processor</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.security.x500.principal;

import org.jboss.logging.BasicLogger;
import org.jboss.logging.Logger;
import org.jboss.logging.annotations.MessageLogger;

/**
* Log messages and exceptions for Elytron.
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
* @author <a href="mailto:darran.lofthouse@jboss.com">Darran Lofthouse</a>
*/
@MessageLogger(projectCode = "ELY", length = 5)
interface ElytronMessages extends BasicLogger {

ElytronMessages log = Logger.getMessageLogger(ElytronMessages.class, "org.wildfly.security");
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2015 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.security.x500.principal;

import java.security.Principal;
import java.util.Arrays;
import java.util.stream.Collectors;

import javax.security.auth.x500.X500Principal;

import org.wildfly.security.auth.server.PrincipalDecoder;
import org.wildfly.security.x500.util.X500PrincipalUtil;

/**
* A principal decoder which decodes an attribute from an X.500 principal.
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
public final class X500AttributePrincipalDecoder implements PrincipalDecoder {
private static final String[] NO_REQUIRED_OIDS = new String[0];

private final String oid;
private final String joiner;
private final int startSegment;
private final int maximumSegments;
private final boolean reverse;
private final String[] requiredOids;
private final boolean convert;

/**
* Construct a new instance. A joining string of "." is assumed.
*
* @param oid the OID of the attribute to map
*/
public X500AttributePrincipalDecoder(final String oid) {
this(oid, false);
}

/**
* Construct a new instance. A joining string of "." is assumed.
*
* @param oid the OID of the attribute to map
* @param reverse {@code true} if the attribute values should be processed and returned in reverse order
*/
public X500AttributePrincipalDecoder(final String oid, final boolean reverse) {
this(oid, ".", 0, Integer.MAX_VALUE, reverse);
}

/**
* Construct a new instance. A joining string of "." is assumed.
*
* @param oid the OID of the attribute to map
* @param maximumSegments the maximum number of occurrences of the attribute to map
*/
public X500AttributePrincipalDecoder(final String oid, final int maximumSegments) {
this(oid, maximumSegments, false);
}

/**
* Construct a new instance. A joining string of "." is assumed.
*
* @param oid the OID of the attribute to map
* @param maximumSegments the maximum number of occurrences of the attribute to map
* @param reverse {@code true} if the attribute values should be processed and returned in reverse order
*/
public X500AttributePrincipalDecoder(final String oid, final int maximumSegments, final boolean reverse) {
this(oid, ".", 0, maximumSegments, reverse);
}

/**
* Construct a new instance. A joining string of "." is assumed.
*
* @param oid the OID of the attribute to map
* @param startSegment the 0-based starting occurrence of the attribute to map
* @param maximumSegments the maximum number of occurrences of the attribute to map
*/
public X500AttributePrincipalDecoder(final String oid, final int startSegment, final int maximumSegments) {
this(oid, startSegment, maximumSegments, false);
}

/**
* Construct a new instance. A joining string of "." is assumed.
*
* @param oid the OID of the attribute to map
* @param startSegment the 0-based starting occurrence of the attribute to map
* @param maximumSegments the maximum number of occurrences of the attribute to map
* @param reverse {@code true} if the attribute values should be processed and returned in reverse order
*/
public X500AttributePrincipalDecoder(final String oid, final int startSegment, final int maximumSegments, final boolean reverse) {
this(oid, ".", startSegment, maximumSegments, reverse);
}

/**
* Construct a new instance.
*
* @param oid the OID of the attribute to map
* @param joiner the joining string
*/
public X500AttributePrincipalDecoder(final String oid, final String joiner) {
this(oid, joiner, false);
}

/**
* Construct a new instance.
*
* @param oid the OID of the attribute to map
* @param joiner the joining string
* @param reverse {@code true} if the attribute values should be processed and returned in reverse order
*/
public X500AttributePrincipalDecoder(final String oid, final String joiner, final boolean reverse) {
this(oid, joiner, 0, Integer.MAX_VALUE, reverse);
}

/**
* Construct a new instance.
*
* @param oid the OID of the attribute to map
* @param joiner the joining string
* @param maximumSegments the maximum number of occurrences of the attribute to map
*/
public X500AttributePrincipalDecoder(final String oid, final String joiner, final int maximumSegments) {
this(oid, joiner, 0, maximumSegments, false);
}

/**
* Construct a new instance.
*
* @param oid the OID of the attribute to map
* @param joiner the joining string
* @param startSegment the 0-based starting occurrence of the attribute to map
* @param maximumSegments the maximum number of occurrences of the attribute to map
* @param reverse {@code true} if the attribute values should be processed and returned in reverse order
*/
public X500AttributePrincipalDecoder(final String oid, final String joiner, final int startSegment, final int maximumSegments, final boolean reverse) {
this(oid, joiner, startSegment, maximumSegments, reverse, false, NO_REQUIRED_OIDS);
}

/**
* Construct a new instance.
*
* @param oid the OID of the attribute to map
* @param joiner the joining string
* @param startSegment the 0-based starting occurrence of the attribute to map
* @param maximumSegments the maximum number of occurrences of the attribute to map
* @param reverse {@code true} if the attribute values should be processed and returned in reverse order
* @param convert {@code true} if the Principal should be converted to {@link X500Principal} if not one already
* @param requiredOids the OIDs of the attributes that must be present
*/
public X500AttributePrincipalDecoder(final String oid, final String joiner, final int startSegment, final int maximumSegments,
final boolean reverse, final boolean convert, final String... requiredOids) {
this.oid = oid;
this.joiner = joiner;
this.startSegment = startSegment;
this.maximumSegments = maximumSegments;
this.reverse = reverse;
this.convert = convert;
this.requiredOids = requiredOids;
}

public String getName(final Principal principal) {
final X500Principal x500Principal = X500PrincipalUtil.asX500Principal(principal, convert);
if (x500Principal == null) {
return null;
}
if (requiredOids != null && requiredOids.length != 0 && ! X500PrincipalUtil.containsAllAttributes(x500Principal, requiredOids)) {
ElytronMessages.log.tracef("X500 principal [%s] was not decoded - does not contain required oids", x500Principal);
return null;
}
final String[] values = X500PrincipalUtil.getAttributeValues(x500Principal, oid, reverse);
if (values.length == 0) {
ElytronMessages.log.tracef("X500 principal [%s] was not decoded - no values of attribute [%s]", x500Principal, oid);
return null;
} else {
final String name = Arrays.stream(values).skip(startSegment).limit(maximumSegments).collect(Collectors.joining(joiner));
if (ElytronMessages.log.isTraceEnabled()) {
ElytronMessages.log.tracef("X500 principal [%s] decoded as name [%s] (attribute values: [%s])", x500Principal, name, String.join(", ", values));
}
return name;
}
}
}
4 changes: 4 additions & 0 deletions wildfly-elytron/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,10 @@
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-x500-deprecated</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-x500-principal</artifactId>
</dependency>

<dependency>
<groupId>org.wildfly.common</groupId>
Expand Down

0 comments on commit fe784fa

Please sign in to comment.