File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\BuildAuthorizationToken.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.model;
25
26 import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;
27 import hudson.Util;
28 import hudson.security.ACL;
29 import jenkins.model.Jenkins;
30 import org.kohsuke.stapler.StaplerRequest;
31 import org.kohsuke.stapler.StaplerResponse;
32
33 import java.io.IOException;
34 import jenkins.security.ApiTokenProperty;
35 import org.acegisecurity.AccessDeniedException;
36 import org.kohsuke.stapler.HttpResponses;
37
38 /**
39 * Authorization token to allow projects to trigger themselves under the secured environment.
40 *
41 * @author Kohsuke Kawaguchi
42 * @see BuildableItem
43 * @deprecated 2008-07-20
44 * Use {@link ACL} and {@link AbstractProject#BUILD}. This code is only here
45 * for the backward compatibility.
46 */
47 public final class BuildAuthorizationToken {
48 private final String token;
49
50 public BuildAuthorizationToken(String token) {
51 this.token = token;
52 }
53
54 public static BuildAuthorizationToken create(StaplerRequest req) {
55 if (req.getParameter("pseudoRemoteTrigger") != null) {
56 String token = Util.fixEmpty(req.getParameter("authToken"));
57 if(token!=null)
58 return new BuildAuthorizationToken(token);
59 }
60
61 return null;
62 }
63
64 public static void checkPermission(AbstractProject project, BuildAuthorizationToken token, StaplerRequest req, StaplerResponse rsp) throws IOException {
65 if (!Jenkins.getInstance().isUseSecurity())
66 return; // everyone is authorized
67
68 if(token!=null && token.token != null) {
69 //check the provided token
70 String providedToken = req.getParameter("token");
71 if (providedToken != null && providedToken.equals(token.token))
72 return;
73 if (providedToken != null)
74 throw new AccessDeniedException(Messages.BuildAuthorizationToken_InvalidTokenProvided());
75 }
76
77 project.checkPermission(AbstractProject.BUILD);
78
79 if (req.getMethod().equals("POST")) {
80 return;
81 }
82
83 if (req.getAttribute(ApiTokenProperty.class.getName()) instanceof User) {
84 return;
85 }
86
87 throw HttpResponses.forwardToView(project, "requirePOST.jelly");
88 }
89
90 public String getToken() {
91 return token;
92 }
93
94 public static final class ConverterImpl extends AbstractSingleValueConverter {
95 public boolean canConvert(Class type) {
96 return type== BuildAuthorizationToken.class;
97 }
98
99 public Object fromString(String str) {
100 return new BuildAuthorizationToken(str);
101 }
102
103 @Override
104 public String toString(Object obj) {
105 return ((BuildAuthorizationToken)obj).token;
106 }
107 }
108 }
109