]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.core/src/org/argeo/security/jcr/OsJcrAuthenticationProvider.java
Remoting working
[lgpl/argeo-commons.git] / org.argeo.security.core / src / org / argeo / security / jcr / OsJcrAuthenticationProvider.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.security.jcr;
17
18 import java.util.Collection;
19
20 import javax.jcr.Node;
21 import javax.jcr.Repository;
22 import javax.jcr.RepositoryException;
23 import javax.jcr.Session;
24
25 import org.argeo.ArgeoException;
26 import org.argeo.jcr.JcrUtils;
27 import org.argeo.security.OsAuthenticationToken;
28 import org.argeo.security.SecurityUtils;
29 import org.argeo.security.core.OsAuthenticationProvider;
30 import org.springframework.security.authentication.BadCredentialsException;
31 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
32 import org.springframework.security.core.Authentication;
33 import org.springframework.security.core.AuthenticationException;
34 import org.springframework.security.core.GrantedAuthority;
35 import org.springframework.security.core.userdetails.UserDetails;
36
37 /** Relies on OS to authenticate and additionally setup JCR */
38 public class OsJcrAuthenticationProvider extends OsAuthenticationProvider {
39 private Repository repository;
40 private Session nodeSession;
41
42 private UserDetails userDetails;
43 private JcrSecurityModel jcrSecurityModel = new SimpleJcrSecurityModel();
44
45 private final static String JVM_OSUSER = System.getProperty("user.name");
46
47 public void init() {
48 try {
49 nodeSession = repository.login();
50 } catch (RepositoryException e) {
51 throw new ArgeoException("Cannot initialize", e);
52 }
53 }
54
55 public void destroy() {
56 JcrUtils.logoutQuietly(nodeSession);
57 }
58
59 public Authentication authenticate(Authentication authentication)
60 throws AuthenticationException {
61 if (authentication instanceof UsernamePasswordAuthenticationToken) {
62 // deal with remote access to internal server
63 // FIXME very primitive and unsecure at this sSession adminSession
64 // =tage
65 // consider using the keyring for username / password authentication
66 // or certificate
67 UsernamePasswordAuthenticationToken upat = (UsernamePasswordAuthenticationToken) authentication;
68 if (!upat.getPrincipal().toString().equals(JVM_OSUSER))
69 throw new BadCredentialsException("Wrong credentials");
70 UsernamePasswordAuthenticationToken authen = new UsernamePasswordAuthenticationToken(
71 authentication.getPrincipal(),
72 authentication.getCredentials(), getBaseAuthorities());
73 authen.setDetails(userDetails);
74 return authen;
75 } else if (authentication instanceof OsAuthenticationToken) {
76 OsAuthenticationToken authen = (OsAuthenticationToken) super
77 .authenticate(authentication);
78 try {
79 // WARNING: at this stage we assume that the java properties
80 // will have the same value
81 Collection<? extends GrantedAuthority> authorities = getBaseAuthorities();
82 String username = JVM_OSUSER;
83 Node userProfile = jcrSecurityModel.sync(nodeSession, username,
84 SecurityUtils.authoritiesToStringList(authorities));
85 JcrUserDetails.checkAccountStatus(userProfile);
86
87 userDetails = new JcrUserDetails(userProfile, authen
88 .getCredentials().toString(), authorities);
89 authen.setDetails(userDetails);
90 return authen;
91 } catch (RepositoryException e) {
92 JcrUtils.discardQuietly(nodeSession);
93 throw new ArgeoException(
94 "Unexpected exception when synchronizing OS and JCR security ",
95 e);
96 }
97 } else {
98 throw new ArgeoException("Unsupported authentication "
99 + authentication.getClass());
100 }
101 }
102
103 public void setRepository(Repository repository) {
104 this.repository = repository;
105 }
106
107 public void setJcrSecurityModel(JcrSecurityModel jcrSecurityModel) {
108 this.jcrSecurityModel = jcrSecurityModel;
109 }
110
111 @SuppressWarnings("rawtypes")
112 public boolean supports(Class authentication) {
113 return OsAuthenticationToken.class.isAssignableFrom(authentication)
114 || UsernamePasswordAuthenticationToken.class
115 .isAssignableFrom(authentication);
116 }
117 }