1 package org.apache.archiva.redback.struts2.interceptor;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import com.opensymphony.xwork2.ActionInvocation;
23 import com.opensymphony.xwork2.interceptor.Interceptor;
24 import org.apache.archiva.redback.authentication.PasswordBasedAuthenticationDataSource;
25 import org.apache.archiva.redback.configuration.UserConfiguration;
26 import org.apache.archiva.redback.integration.checks.security.AdminAutoCreateCheck;
27 import org.apache.archiva.redback.integration.util.AutoLoginCookies;
28 import org.apache.archiva.redback.role.RoleManager;
29 import org.apache.archiva.redback.role.RoleManagerException;
30 import org.apache.archiva.redback.system.SecuritySession;
31 import org.apache.archiva.redback.system.SecuritySystem;
32 import org.apache.archiva.redback.system.SecuritySystemConstants;
33 import org.apache.archiva.redback.users.User;
34 import org.apache.archiva.redback.users.UserManager;
35 import org.apache.archiva.redback.users.UserNotFoundException;
36 import org.apache.commons.io.IOUtils;
37 import org.apache.commons.lang.StringUtils;
38 import org.apache.struts2.ServletActionContext;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.context.annotation.Scope;
42 import org.springframework.stereotype.Controller;
44 import javax.inject.Inject;
45 import javax.inject.Named;
47 import java.io.FileInputStream;
48 import java.util.Date;
50 import java.util.Properties;
53 * EnvironmentCheckInterceptor
55 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
57 @Controller( "redbackForceAdminUserInterceptor" )
59 public class ForceAdminUserInterceptor
60 implements Interceptor
62 private Logger log = LoggerFactory.getLogger( getClass() );
64 private static final String SECURITY_ADMIN_USER_NEEDED = "security-admin-user-needed";
66 private static boolean checked = false;
69 * role-hint="configurable"
72 @Named( value = "userManager#configurable" )
73 private UserManager userManager;
79 private RoleManager roleManager;
85 private UserConfiguration config;
88 protected SecuritySystem securitySystem;
91 private AutoLoginCookies autologinCookies;
93 protected Map<String, Object> session;
105 public String intercept( ActionInvocation invocation )
110 return invocation.invoke();
115 User user = userManager.findUser( getAdminUid() );
118 user = useForceAdminFile();
121 log.info( "No admin user configured - forwarding to admin user creation page." );
122 return SECURITY_ADMIN_USER_NEEDED;
126 assignAdminRole( user );
129 log.info( "Admin user found. No need to configure admin user." );
132 catch ( UserNotFoundException e )
134 User user = useForceAdminFile();
137 assignAdminRole( user );
143 log.info( "No admin user found - forwarding to admin user creation page." );
144 return SECURITY_ADMIN_USER_NEEDED;
148 return invocation.invoke();
151 private User useForceAdminFile()
155 String forceAdminFilePath = System.getProperty( AdminAutoCreateCheck.FORCE_ADMIN_FILE_PATH );
156 if ( StringUtils.isBlank( forceAdminFilePath ) )
158 log.info( AdminAutoCreateCheck.FORCE_ADMIN_FILE_PATH
159 + " system props is empty don't use an auto creation admin " );
162 File file = new File( forceAdminFilePath );
163 if ( !file.exists() )
165 log.warn( "file set in sysprops " + AdminAutoCreateCheck.FORCE_ADMIN_FILE_PATH
166 + " not exists skip admin auto creation" );
169 Properties properties = new Properties();
170 FileInputStream fis = new FileInputStream( file );
173 properties.load( fis );
175 catch ( Exception e )
177 log.warn( "error loading properties from file " + forceAdminFilePath + " skip admin auto creation" );
182 IOUtils.closeQuietly( fis );
185 // ensure we have all properties
186 String password = properties.getProperty( AdminAutoCreateCheck.ADMIN_PASSWORD_KEY );
187 String email = properties.getProperty( AdminAutoCreateCheck.ADMIN_EMAIL_KEY );
188 String fullName = properties.getProperty( AdminAutoCreateCheck.ADMIN_FULL_NAME_KEY );
190 if ( StringUtils.isBlank( password ) )
192 log.warn( "property " + AdminAutoCreateCheck.ADMIN_PASSWORD_KEY + " not set skip auto admin creation" );
196 if ( StringUtils.isBlank( email ) )
198 log.warn( "property " + AdminAutoCreateCheck.ADMIN_EMAIL_KEY + " not set skip auto admin creation" );
202 if ( StringUtils.isBlank( fullName ) )
205 "property " + AdminAutoCreateCheck.ADMIN_FULL_NAME_KEY + " not set skip auto admin creation" );
209 User u = userManager.createUser( getAdminUid(), fullName, email );
211 u.setPassword( password );
212 u.setLocked( false );
213 u.setPasswordChangeRequired( false );
214 u.setPermanent( true );
216 u = userManager.addUser( u );
217 u.setPassword( password );
219 PasswordBasedAuthenticationDataSource authdatasource = new PasswordBasedAuthenticationDataSource();
220 authdatasource.setPrincipal( u.getUsername() );
221 authdatasource.setPassword( u.getPassword() );
222 SecuritySession securitySession = securitySystem.authenticate( authdatasource );
223 if ( securitySession.getAuthenticationResult().isAuthenticated() )
225 // good add various tokens.
226 ServletActionContext.getRequest().getSession( true ).setAttribute(
227 SecuritySystemConstants.SECURITY_SESSION_KEY, securitySession );
228 autologinCookies.setSignonCookie( authdatasource.getPrincipal(), ServletActionContext.getResponse(),
229 ServletActionContext.getRequest() );
230 u = securitySession.getUser();
231 u.setLastLoginDate( new Date() );
232 securitySystem.getUserManager().updateUser( u );
237 catch ( Exception e )
239 log.warn( "failed to automatically create an admin account " + e.getMessage(), e );
244 private String getAdminUid()
246 return config.getString( "redback.default.admin" );
249 private void assignAdminRole( User user )
250 throws RoleManagerException
252 roleManager.assignRole( "system-administrator", user.getPrincipal().toString() );