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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Oracle Corporation
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 hudson.cli.CLICommand;
27 import org.acegisecurity.Authentication;
28 import org.acegisecurity.AuthenticationException;
29 import org.kohsuke.args4j.Argument;
30 import org.kohsuke.args4j.CmdLineParser;
31 import org.kohsuke.args4j.Option;
32
33 import java.io.IOException;
34
35 /**
36 * Handles authentication for CLI commands.
37 *
38 * <p>
39 * {@link CliAuthenticator} is used to authenticate an invocation of the CLI command, so that
40 * the thread carries the correct {@link Authentication} that represents the user who's invoking the command.
41 *
42 * <h2>Lifecycle</h2>
43 * <p>
44 * Each time a CLI command is invoked, {@link SecurityRealm#createCliAuthenticator(CLICommand)} is called
45 * to allocate a fresh {@link CliAuthenticator} object.
46 *
47 * <p>
48 * The {@link Option} and {@link Argument} annotations on the returned {@link CliAuthenticator} instance are
49 * scanned and added into the {@link CmdLineParser}, then that parser is used to parse command line arguments.
50 * This means subtypes can define fields/setters with those annotations to define authentication-specific options
51 * to CLI commands.
52 *
53 * <p>
54 * Once the arguments and options are parsed and populated, {@link #authenticate()} method is called to
55 * perform the authentications. If the authentication succeeds, this method returns an {@link Authentication}
56 * instance that represents the user. If the authentication fails, this method throws {@link AuthenticationException}.
57 * To authenticate, the method can use parsed argument/option values, as well as interacting with the client
58 * via {@link CLICommand} by using its stdin/stdout and its channel (for example, if you want to interactively prompt
59 * a password, you can do so by using {@link CLICommand#channel}.)
60 *
61 * <p>
62 * If no explicit credential is provided, or if the {@link SecurityRealm} depends on a mode of authentication
63 * that doesn't involve in explicit password (such as Kerberos), it's also often useful to fall back to
64 * {@link CLICommand#getTransportAuthentication()}, in case the user is authenticated at the transport level.
65 *
66 * <p>
67 * Many commands do not require any authentication (for example, the "help" command), and still more commands
68 * can be run successfully with the anonymous permission. So the authenticator should normally allow unauthenticated
69 * CLI command invocations. For those, return {@link jenkins.model.Jenkins#ANONYMOUS} from the {@link #authenticate()} method.
70 *
71 * <h2>Example</h2>
72 * <p>
73 * For a complete example, see the implementation of
74 * {@link AbstractPasswordBasedSecurityRealm#createCliAuthenticator(CLICommand)}
75 *
76 * @author Kohsuke Kawaguchi
77 * @since 1.350
78 */
79 public abstract class CliAuthenticator {
80 /**
81 * Authenticates the CLI invocation. See class javadoc for the semantics.
82 *
83 * @throws AuthenticationException
84 * If the authentication failed and hence the processing shouldn't proceed.
85 * @throws IOException
86 * Can be thrown if the {@link CliAuthenticator} fails to interact with the client.
87 * This exception is treated as a failure of authentication. It's just that allowing this
88 * would often simplify the callee.
89 * @throws InterruptedException
90 * Same motivation as {@link IOException}. Treated as an authentication failure.
91 */
92 public abstract Authentication authenticate() throws AuthenticationException, IOException, InterruptedException;
93 }
94