Skip to content

Commit dc9b006

Browse files
committed
working auth / security
1 parent c311686 commit dc9b006

File tree

11 files changed

+141
-23
lines changed

11 files changed

+141
-23
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<!--
3+
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
21+
-->
22+
<faces-config version="2.3"
23+
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
24+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
25+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_3.xsd">
26+
<factory>
27+
<exception-handler-factory>
28+
org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory
29+
</exception-handler-factory>
30+
<!-- order is important, the following line needs to appear last -->
31+
<exception-handler-factory>org.omnifaces.exceptionhandler.ViewExpiredExceptionHandlerFactory</exception-handler-factory>
32+
</factory>
33+
<application>
34+
<resource-handler>org.omnifaces.resourcehandler.UnmappedResourceHandler</resource-handler>
35+
<el-resolver>
36+
org.primefaces.application.exceptionhandler.PrimeExceptionHandlerELResolver
37+
</el-resolver>
38+
</application>
39+
</faces-config>

emailmanager/hope-website/WEB-INF/shiro.ini

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,12 @@ authc.useRemembered = true
1111
authc.loginFailedWaitTime = 5
1212

1313
[urls]
14-
# Admin pages
15-
/shiro/adminpage* = ssl, authc, roles[admin]
16-
17-
# User pages
18-
/shiro/index* = ssl, anon
19-
/shiro/unprotected/** = ssl, anon
20-
/shiro/* = ssl, authc, perms[pages:read]
21-
2214
# Login pages
2315
/auth/login* = ssl, authc
2416
/auth/logout* = ssl, logout
2517

26-
# All other pages - fallback
27-
; +++ requireSession = false
28-
/** = anon
18+
# Resources
19+
/javax.faces.resource/** = anon
2920

30-
[roles]
31-
admin = *
32-
regular = pages:read
21+
# All other pages - fallback
22+
/** = ssl, authc

emailmanager/hope-website/WEB-INF/web.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
<param-name>org.omnifaces.FACES_VIEWS_SCAN_PATHS</param-name>
1313
<param-value>/*.xhtml</param-value>
1414
</context-param>
15+
<context-param>
16+
<param-name>org.omnifaces.EXCEPTION_TYPES_TO_IGNORE_IN_LOGGING</param-name>
17+
<param-value>javax.faces.application.ViewExpiredException, java.nio.channels.ClosedByInterruptException</param-value>
18+
</context-param>
19+
<context-param>
20+
<param-name>primefaces.CLIENT_SIDE_VALIDATION</param-name>
21+
<param-value>true</param-value>
22+
</context-param>
1523

1624
<mime-mapping>
1725
<extension>ttf</extension>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml"
3+
xmlns:jsf="http://xmlns.jcp.org/jsf"
4+
xmlns:h="http://xmlns.jcp.org/jsf/html"
5+
xmlns:p="http://primefaces.org/ui">
6+
<h:head>
7+
<title>Login Page</title>
8+
<h:outputStylesheet name="css/login-style.css"/>
9+
</h:head>
10+
<h:body id="loginFormContainer">
11+
<div jsf:rendered="#{authc.sessionExpired}" style="color: red;">
12+
Your Session Has Expired
13+
</div>
14+
<div jsf:rendered="#{authc.loginFailure}" style="color: red;">
15+
Login Failed
16+
</div>
17+
<h:form styleClass="rounded-border form-container" id="loginForm" prependId="false">
18+
<p:messages id="messages"/>
19+
<p:focus for="username"/>
20+
<div>
21+
<p:outputLabel for="username" value="User Name"/>
22+
<p:inputText id="username" required="true"/>
23+
</div>
24+
<div>
25+
<p:outputLabel for="password" value="Password"/>
26+
<p:password id="password" required="true"/>
27+
</div>
28+
<div>
29+
<p:outputLabel for="rememberMe" value="Remember Me"/>
30+
<h:selectBooleanCheckbox id="rememberMe" />
31+
</div>
32+
<p:commandButton value="Login ..." action="#{authc.login}" validateClient="true"/>
33+
</h:form>
34+
</h:body>
35+
</html>

emailmanager/hope-website/emailmanager.xhtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
</h:head>
99
<h:body>
1010
<h:form>
11-
<h1>Press <p:commandButton value="Here" actionListener="#{emailManager.eraseJunk}"/>
11+
<h1>Press <p:commandButton value="Here" actionListener="#{emailManager.eraseJunk}" update="status"/>
1212
to Erase Junk Email</h1>
13-
<h1>Press <p:commandButton value="Here" actionListener="#{emailManager.sendDrafts}"/>
13+
<h1>Press <p:commandButton value="Here" actionListener="#{emailManager.sendDrafts}" update="status"/>
1414
to Send Draft Emails</h1>
1515
</h:form>
16-
<h:form>
16+
<h:form id="status">
1717
<h3>E-Mail Operation Status: ${emailManager.emailStatus}</h3>
1818
<p:commandButton value="Log Out" action="#{authc.logout}"/>
1919
</h:form>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#loginFormContainer {
2+
background: url(../img/login-bg.png) no-repeat;
3+
width:778px;
4+
height:600px;
5+
margin:0 auto;
6+
}
7+
8+
#loginForm {
9+
padding: 90px 110px;
10+
}

emailmanager/src/main/resources/META-INF/resources/images/auth/login-bg.png renamed to emailmanager/hope-website/resources/img/login-bg.png

File renamed without changes.

emailmanager/src/main/java/com/flowlogix/website/impl/EmailManagerImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.util.Arrays;
1717
import java.util.LinkedList;
1818
import java.util.List;
19+
import java.util.Objects;
20+
import javax.annotation.PostConstruct;
1921
import javax.inject.Inject;
2022
import javax.mail.Address;
2123
import javax.mail.MailSessionDefinition;
@@ -44,6 +46,17 @@ public class EmailManagerImpl implements EmailManagerLocal {
4446
@ConfigProperty(name = "hope-smtp-password", defaultValue = "none")
4547
private String smtp_password;
4648

49+
@PostConstruct
50+
@SneakyThrows(MessagingException.class)
51+
void init() {
52+
@Cleanup var transport = mailSession.getTransport();
53+
transport.connect(smtp_host, smtp_user, smtp_password);
54+
@Cleanup var store = mailSession.getStore();
55+
UserAuth user = (UserAuth) SecurityUtils.getSubject().getPrincipal();
56+
Objects.requireNonNull(user, "not authenticated");
57+
store.connect(user.getUserName(), user.getPassword());
58+
}
59+
4760
@Override
4861
@SneakyThrows(MessagingException.class)
4962
public void eraseFolder(String folderName) {

emailmanager/src/main/java/com/flowlogix/website/security/UnixRealm.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.flowlogix.website.ui.Constants;
88
import java.util.Collection;
99
import java.util.HashSet;
10+
import java.util.Set;
1011
import lombok.Cleanup;
1112
import lombok.SneakyThrows;
1213
import org.apache.shiro.authc.AuthenticationException;
@@ -16,7 +17,6 @@
1617
import org.apache.shiro.authc.UsernamePasswordToken;
1718
import org.apache.shiro.authz.AuthorizationException;
1819
import org.apache.shiro.authz.AuthorizationInfo;
19-
import org.apache.shiro.authz.Permission;
2020
import org.apache.shiro.authz.SimpleAuthorizationInfo;
2121
import org.apache.shiro.authz.permission.WildcardPermission;
2222
import org.apache.shiro.authz.permission.WildcardPermissionResolver;
@@ -70,23 +70,21 @@ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
7070
@Override
7171
@SneakyThrows(PAMException.class)
7272
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
73-
var roles = new HashSet<String>();
74-
var permissions = new HashSet<Permission>();
75-
permissions.add(new WildcardPermission("mail:*"));
7673
Collection<UserAuth> principalsList = principals.byType(UserAuth.class);
7774

7875
if (principalsList.isEmpty()) {
7976
throw new AuthorizationException("Empty principals list!");
8077
}
8178

79+
var roles = new HashSet<String>();
8280
for (UserAuth userPrincipal : principalsList) {
8381
@Cleanup("dispose")
8482
PAM pam = getPam();
8583
UnixUser unixUser = pam.authenticate(userPrincipal.getUserName(), userPrincipal.getPassword());
8684
roles.addAll(unixUser.getGroups());
8785
}
8886
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);
89-
info.setObjectPermissions(permissions);
87+
info.setObjectPermissions(Set.of(new WildcardPermission("mail:*")));
9088

9189
return info;
9290
}

emailmanager/src/main/java/com/flowlogix/website/ui/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"fish.payara.connection-validation-method = auto-commit"})
2323
public class Constants {
2424
@Inject
25-
@ConfigProperty(name = "com.flowlogix.pam-service-name", defaultValue = "pwauth")
25+
@ConfigProperty(name = "com.flowlogix.pam-service-name", defaultValue = "login")
2626
String pamAuthServiceName;
2727
@Inject
2828
@ConfigProperty(name = "com.flowlogix.junk-folder-name", defaultValue = "Junk")

0 commit comments

Comments
 (0)