Skip to content

Commit

Permalink
[WFLY-11587] Added test for @Inject Principal
Browse files Browse the repository at this point in the history
  • Loading branch information
nziakova committed Jan 16, 2019
1 parent 60a657c commit 9ae586a
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 0 deletions.
@@ -0,0 +1,34 @@
/*
* Copyright 2019 Red Hat, Inc.
*
* 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.test.integration.weld.builtinBeans;

import java.security.Principal;

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;

import java.io.Serializable;

@SessionScoped
public class BeanWithInjectedPrincipal implements Serializable {

@Inject
Principal principal;

public String getPrincipalName() {
return principal.getName();
}
}
@@ -0,0 +1,31 @@
/*
* Copyright 2019 Red Hat, Inc.
*
* 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.test.integration.weld.builtinBeans;

import javax.ejb.Stateless;
import javax.ejb.EJBContext;
import javax.annotation.Resource;

@Stateless
public class BeanWithPrincipalFromEJBContext {

@Resource
private EJBContext ctx;

public String getPrincipalName() {
return ctx.getCallerPrincipal().getName();
}
}
@@ -0,0 +1,42 @@
/*
* Copyright 2019 Red Hat, Inc.
*
* 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.test.integration.weld.builtinBeans;

import javax.annotation.security.RunAs;
import javax.ejb.Stateless;
import javax.inject.Inject;

import org.jboss.ejb3.annotation.RunAsPrincipal;

@Stateless
@RunAs("Admin")
@RunAsPrincipal("non-anonymous")
public class CallerWithIdentity {

@Inject
BeanWithInjectedPrincipal beanA;

@Inject
BeanWithPrincipalFromEJBContext beanB;

public String getCallerPrincipalInjected() {
return beanA.getPrincipalName();
}

public String getCallerPrincipalFromEJBContext() {
return beanB.getPrincipalName();
}
}
@@ -0,0 +1,64 @@
/*
* Copyright 2019 Red Hat, Inc.
*
* 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.test.integration.weld.builtinBeans;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.Assert;
import org.junit.runner.RunWith;


/**
* See <a href="https://issues.jboss.org/browse/WFLY-11587">WFLY-11587</a>.
*/
@RunWith(Arquillian.class)
public class InjectPrincipalTestCase {

public static final String ANONYMOUS_PRINCIPAL = "anonymous";
public static final String NON_ANONYMOUS_PRINCIPAL = "non-anonymous";

@Deployment
public static Archive<?> createTestArchive() {
return ShrinkWrap.create(WebArchive.class).addPackage(InjectPrincipalTestCase.class.getPackage())
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsWebInfResource(InjectPrincipalTestCase.class.getPackage(), "jboss-ejb3.xml", "jboss-ejb3.xml");
}

@Test
public void testAnonymousPrincipalInjected(BeanWithInjectedPrincipal beanA, BeanWithPrincipalFromEJBContext beanB) {
try {
Assert.assertEquals(ANONYMOUS_PRINCIPAL, beanA.getPrincipalName());
Assert.assertEquals(ANONYMOUS_PRINCIPAL, beanB.getPrincipalName());
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}

@Test
public void testNonAnonymousPrincipalInjected(CallerWithIdentity callerWithIdentity) throws Exception {
try {
Assert.assertEquals(NON_ANONYMOUS_PRINCIPAL, callerWithIdentity.getCallerPrincipalInjected());
Assert.assertEquals(NON_ANONYMOUS_PRINCIPAL, callerWithIdentity.getCallerPrincipalFromEJBContext());
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
}
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2019 Red Hat, Inc.
~
~ 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.
-->

<jboss:jboss
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:jboss="http://www.jboss.com/xml/ns/javaee"
xmlns:s="urn:security:1.1"
version="3.1" impl-version="2.0">

<assembly-descriptor>
<s:security>
<ejb-name>*</ejb-name>
<s:missing-method-permissions-deny-access>false</s:missing-method-permissions-deny-access>
<s:run-as-principal>non-anonymous</s:run-as-principal>
</s:security>
</assembly-descriptor>
</jboss:jboss>

0 comments on commit 9ae586a

Please sign in to comment.