File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\security\ContainerAuthentication.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.security;
25
26 import jenkins.model.Jenkins;
27 import org.acegisecurity.Authentication;
28 import org.acegisecurity.GrantedAuthority;
29 import org.acegisecurity.GrantedAuthorityImpl;
30
31 import javax.servlet.ServletRequest;
32 import javax.servlet.http.HttpServletRequest;
33 import java.security.Principal;
34 import java.util.List;
35 import java.util.ArrayList;
36
37 /**
38 * {@link Authentication} implementation for {@link Principal}
39 * given through {@link HttpServletRequest}.
40 *
41 * <p>
42 * This is used to plug the container authentication to Acegi,
43 * for backward compatibility with Hudson &lt; 1.160.
44 *
45 * @author Kohsuke Kawaguchi
46 */
47 public final class ContainerAuthentication implements Authentication {
48 private final Principal principal;
49 private GrantedAuthority[] authorities;
50
51 /**
52 * Servlet container can tie a {@link ServletRequest} to the request handling thread,
53 * so we need to capture all the information upfront to allow {@link Authentication}
54 * to be passed to other threads, like update center does. See HUDSON-5382.
55 */
56 public ContainerAuthentication(HttpServletRequest request) {
57 this.principal = request.getUserPrincipal();
58 if (principal==null)
59 throw new IllegalStateException(); // for anonymous users, we just don't call SecurityContextHolder.getContext().setAuthentication.
60
61 // Servlet API doesn't provide a way to list up all roles the current user
62 // has, so we need to ask AuthorizationStrategy what roles it is going to check against.
63 List<GrantedAuthority> l = new ArrayList<GrantedAuthority>();
64 for( String g : Jenkins.getInstance().getAuthorizationStrategy().getGroups()) {
65 if(request.isUserInRole(g))
66 l.add(new GrantedAuthorityImpl(g));
67 }
68 l.add(SecurityRealm.AUTHENTICATED_AUTHORITY);
69 authorities = l.toArray(new GrantedAuthority[l.size()]);
70 }
71
72 public GrantedAuthority[] getAuthorities() {
73 return authorities;
74 }
75
76 public Object getCredentials() {
77 return null;
78 }
79
80 public Object getDetails() {
81 return null;
82 }
83
84 public String getPrincipal() {
85 return principal.getName();
86 }
87
88 public boolean isAuthenticated() {
89 return true;
90 }
91
92 public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
93 // noop
94 }
95
96 public String getName() {
97 return getPrincipal();
98 }
99 }
100