1 package hudson.model; 2 3 import com.thoughtworks.xstream.converters.basic.AbstractBasicConverter; 4 import org.kohsuke.stapler.StaplerRequest; 5 import org.kohsuke.stapler.StaplerResponse; 6 7 import javax.servlet.ServletException ; 8 import java.io.IOException ; 9 10 16 public final class BuildAuthorizationToken { 17 private final String token; 18 19 public BuildAuthorizationToken(String token) { 20 this.token = token; 21 } 22 23 public static BuildAuthorizationToken create(StaplerRequest req) { 24 if (req.getParameter("pseudoRemoteTrigger") != null) 25 return new BuildAuthorizationToken(req.getParameter("authToken")); 26 else 27 return null; 28 } 29 30 public static void startBuildIfAuthorized(BuildAuthorizationToken token, BuildableItem job, StaplerRequest req, StaplerResponse rsp) throws IOException , ServletException { 31 if(token==null || token.authorizedToStartBuild(req,rsp)) { 32 job.scheduleBuild(); 33 rsp.forwardToPreviousPage(req); 34 } 35 } 36 37 public boolean authorizedToStartBuild(StaplerRequest req, StaplerResponse rsp) throws IOException { 38 if (!Hudson.getInstance().isUseSecurity()) 39 return true; 41 42 if(token != null) { 43 String providedToken = req.getParameter("token"); 45 if (providedToken != null && providedToken.equals(token)) 46 return true; 47 } 48 49 return Hudson.adminCheck(req, rsp); 51 } 52 53 public String getToken() { 54 return token; 55 } 56 57 public static final class ConverterImpl extends AbstractBasicConverter { 58 public boolean canConvert(Class type) { 59 return type== BuildAuthorizationToken.class; 60 } 61 62 protected Object fromString(String str) { 63 return new BuildAuthorizationToken(str); 64 } 65 66 protected String toString(Object obj) { 67 return ((BuildAuthorizationToken)obj).token; 68 } 69 } 70 } 71 | Popular Tags |