/hudson-core/src/main/java/hudson/security/CliAuthenticator.java

http://github.com/hudson/hudson · Java · 94 lines · 12 code · 3 blank · 79 comment · 0 complexity · 257e4a1c93132d43b30ba2ef2732d0ca MD5 · raw file

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