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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2011, CloudBees, Inc.
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 jenkins.security;
25
26 import hudson.Extension;
27 import hudson.Util;
28 import hudson.model.Descriptor.FormException;
29 import hudson.model.User;
30 import hudson.model.UserProperty;
31 import hudson.model.UserPropertyDescriptor;
32 import hudson.util.HttpResponses;
33 import hudson.util.Secret;
34 import jenkins.model.Jenkins;
35 import net.sf.json.JSONObject;
36 import org.kohsuke.stapler.AncestorInPath;
37 import org.kohsuke.stapler.DataBoundConstructor;
38 import org.kohsuke.stapler.HttpResponse;
39 import org.kohsuke.stapler.StaplerRequest;
40 import org.kohsuke.stapler.StaplerResponse;
41
42 import java.io.IOException;
43 import java.security.SecureRandom;
44
45 /**
46 * Remembers the API token for this user, that can be used like a password to login.
47 *
48 *
49 * @author Kohsuke Kawaguchi
50 * @see ApiTokenFilter
51 * @since 1.426
52 */
53 public class ApiTokenProperty extends UserProperty {
54 private volatile Secret apiToken;
55
56 @DataBoundConstructor
57 public ApiTokenProperty() {
58 _changeApiToken();
59 }
60
61 /**
62 * We don't let the external code set the API token,
63 * but for the initial value of the token we need to compute the seed by ourselves.
64 */
65 /*package*/ ApiTokenProperty(String seed) {
66 apiToken = Secret.fromString(seed);
67 }
68
69 public String getApiToken() {
70 String p = apiToken.getPlainText();
71 if (p.equals(Util.getDigestOf(Jenkins.getInstance().getSecretKey()+":"+user.getId()))) {
72 // if the current token is the initial value created by pre SECURITY-49 Jenkins, we can't use that.
73 // force using the newer value
74 apiToken = Secret.fromString(p=API_KEY_SEED.mac(user.getId()));
75 }
76 return Util.getDigestOf(p);
77 }
78
79 public boolean matchesPassword(String password) {
80 return getApiToken().equals(password);
81 }
82
83 public void changeApiToken() throws IOException {
84 _changeApiToken();
85 if (user!=null)
86 user.save();
87 }
88
89 private void _changeApiToken() {
90 byte[] random = new byte[16]; // 16x8=128bit worth of randomness, since we use md5 digest as the API token
91 RANDOM.nextBytes(random);
92 apiToken = Secret.fromString(Util.toHexString(random));
93 }
94
95 @Override
96 public UserProperty reconfigure(StaplerRequest req, JSONObject form) throws FormException {
97 return this;
98 }
99
100 @Extension
101 public static final class DescriptorImpl extends UserPropertyDescriptor {
102 public String getDisplayName() {
103 return Messages.ApiTokenProperty_DisplayName();
104 }
105
106 /**
107 * When we are creating a default {@link ApiTokenProperty} for User,
108 * we need to make sure it yields the same value for the same user,
109 * because there's no guarantee that the property is saved.
110 *
111 * But we also need to make sure that an attacker won't be able to guess
112 * the initial API token value. So we take the seed by hashing the secret + user ID.
113 */
114 public ApiTokenProperty newInstance(User user) {
115 return new ApiTokenProperty(API_KEY_SEED.mac(user.getId()));
116 }
117
118 public HttpResponse doChangeToken(@AncestorInPath User u, StaplerResponse rsp) throws IOException {
119 ApiTokenProperty p = u.getProperty(ApiTokenProperty.class);
120 if (p==null) {
121 p = newInstance(u);
122 u.addProperty(p);
123 } else {
124 p.changeApiToken();
125 }
126 rsp.setHeader("script","document.getElementById('apiToken').value='"+p.getApiToken()+"'");
127 return HttpResponses.html(Messages.ApiTokenProperty_ChangeToken_Success());
128 }
129 }
130
131 private static final SecureRandom RANDOM = new SecureRandom();
132
133 /**
134 * We don't want an API key that's too long, so cut the length to 16 (which produces 32-letter MAC code in hexdump)
135 */
136 private static final HMACConfidentialKey API_KEY_SEED = new HMACConfidentialKey(ApiTokenProperty.class,"seed",16);
137 }
138