File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\security\TokenBasedRememberMeServices2.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 java.util.Date;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31 import jenkins.security.HMACConfidentialKey;
32 import org.acegisecurity.ui.rememberme.TokenBasedRememberMeServices;
33 import org.acegisecurity.userdetails.UserDetails;
34 import org.acegisecurity.Authentication;
35 import org.apache.commons.codec.binary.Base64;
36 import org.springframework.util.Assert;
37
38 /**
39 * {@link TokenBasedRememberMeServices} with modification so as not to rely
40 * on the user password being available.
41 *
42 * <p>
43 * This allows remember-me to work with security realms where the password
44 * is never available in clear text.
45 *
46 * @author Kohsuke Kawaguchi
47 */
48 public class TokenBasedRememberMeServices2 extends TokenBasedRememberMeServices {
49 @Override
50 protected String makeTokenSignature(long tokenExpiryTime, UserDetails userDetails) {
51 String expectedTokenSignature = MAC.mac(userDetails.getUsername() + ":" + tokenExpiryTime + ":"
52 + "N/A" + ":" + getKey());
53 return expectedTokenSignature;
54 }
55
56 @Override
57 protected String retrievePassword(Authentication successfulAuthentication) {
58 return "N/A";
59 }
60
61 @Override
62 public void loginSuccess(HttpServletRequest request, HttpServletResponse response,
63 Authentication successfulAuthentication) {
64 // Exit if the principal hasn't asked to be remembered
65 if (!rememberMeRequested(request, getParameter())) {
66 if (logger.isDebugEnabled()) {
67 logger.debug("Did not send remember-me cookie (principal did not set parameter '" +
68 getParameter() + "')");
69 }
70
71 return;
72 }
73
74 Assert.notNull(successfulAuthentication.getPrincipal());
75 Assert.notNull(successfulAuthentication.getCredentials());
76 Assert.isInstanceOf(UserDetails.class, successfulAuthentication.getPrincipal());
77
78 long expiryTime = System.currentTimeMillis() + (tokenValiditySeconds * 1000);
79 String username = ((UserDetails) successfulAuthentication.getPrincipal()).getUsername();
80
81 String signatureValue = makeTokenSignature(expiryTime, (UserDetails)successfulAuthentication.getPrincipal());
82 String tokenValue = username + ":" + expiryTime + ":" + signatureValue;
83 String tokenValueBase64 = new String(Base64.encodeBase64(tokenValue.getBytes()));
84 response.addCookie(makeValidCookie(tokenValueBase64, request, tokenValiditySeconds));
85
86 if (logger.isDebugEnabled()) {
87 logger.debug("Added remember-me cookie for user '" + username + "', expiry: '" + new Date(expiryTime)
88 + "'");
89 }
90 }
91
92 /**
93 * Used to compute the token signature securely.
94 */
95 private static final HMACConfidentialKey MAC = new HMACConfidentialKey(TokenBasedRememberMeServices.class,"mac");
96 }
97