Search in sources :

Example 11 with Authentication

use of org.acegisecurity.Authentication in project hudson-2.x by hudson.

the class CLICommand method main.

public int main(List<String> args, Locale locale, InputStream stdin, PrintStream stdout, PrintStream stderr) {
    this.stdin = new BufferedInputStream(stdin);
    this.stdout = stdout;
    this.stderr = stderr;
    this.locale = locale;
    this.channel = Channel.current();
    registerOptionHandlers();
    CmdLineParser p = new CmdLineParser(this);
    // add options from the authenticator
    SecurityContext sc = SecurityContextHolder.getContext();
    Authentication old = sc.getAuthentication();
    CliAuthenticator authenticator = Hudson.getInstance().getSecurityRealm().createCliAuthenticator(this);
    new ClassParser().parse(authenticator, p);
    try {
        p.parseArgument(args.toArray(new String[args.size()]));
        Authentication auth = authenticator.authenticate();
        if (auth == Hudson.ANONYMOUS)
            auth = loadStoredAuthentication();
        // run the CLI with the right credential
        sc.setAuthentication(auth);
        if (!(this instanceof LoginCommand || this instanceof HelpCommand))
            Hudson.getInstance().checkPermission(Hudson.READ);
        return run();
    } catch (CmdLineException e) {
        stderr.println(e.getMessage());
        printUsage(stderr, p);
        return -1;
    } catch (AbortException e) {
        // signals an error without stack trace
        stderr.println(e.getMessage());
        return -1;
    } catch (Exception e) {
        e.printStackTrace(stderr);
        return -1;
    } finally {
        // restore
        sc.setAuthentication(old);
    }
}
Also used : CliAuthenticator(hudson.security.CliAuthenticator) CmdLineParser(org.kohsuke.args4j.CmdLineParser) BufferedInputStream(java.io.BufferedInputStream) Authentication(org.acegisecurity.Authentication) SecurityContext(org.acegisecurity.context.SecurityContext) CmdLineException(org.kohsuke.args4j.CmdLineException) AbortException(hudson.AbortException) IOException(java.io.IOException) CmdLineException(org.kohsuke.args4j.CmdLineException) ClassParser(org.kohsuke.args4j.ClassParser) AbortException(hudson.AbortException)

Example 12 with Authentication

use of org.acegisecurity.Authentication in project hudson-2.x by hudson.

the class LoginCommand method run.

@Override
protected int run() throws Exception {
    Authentication a = Hudson.getAuthentication();
    if (a == Hudson.ANONYMOUS)
        // this causes CLI to show the command line options.
        throw new CmdLineException("No credentials specified.");
    ClientAuthenticationCache store = new ClientAuthenticationCache(channel);
    store.set(a);
    return 0;
}
Also used : Authentication(org.acegisecurity.Authentication) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 13 with Authentication

use of org.acegisecurity.Authentication in project hudson-2.x by hudson.

the class DefaultCrumbIssuer method issueCrumb.

/**
     * {@inheritDoc}
     */
@Override
protected String issueCrumb(ServletRequest request, String salt) {
    if (request instanceof HttpServletRequest) {
        if (md != null) {
            HttpServletRequest req = (HttpServletRequest) request;
            StringBuilder buffer = new StringBuilder();
            Authentication a = Hudson.getAuthentication();
            if (a != null) {
                buffer.append(a.getName());
            }
            buffer.append(';');
            if (!isExcludeClientIPFromCrumb()) {
                buffer.append(getClientIP(req));
            }
            md.update(buffer.toString().getBytes());
            byte[] crumbBytes = md.digest(salt.getBytes());
            StringBuilder hexString = new StringBuilder();
            for (int i = 0; i < crumbBytes.length; i++) {
                String hex = Integer.toHexString(0xFF & crumbBytes[i]);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        }
    }
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Authentication(org.acegisecurity.Authentication)

Example 14 with Authentication

use of org.acegisecurity.Authentication in project hudson-2.x by hudson.

the class HttpSessionContextIntegrationFilter2 method doFilter.

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpSession session = ((HttpServletRequest) req).getSession(false);
    if (session != null) {
        SecurityContext o = (SecurityContext) session.getAttribute(ACEGI_SECURITY_CONTEXT_KEY);
        if (o != null) {
            Authentication a = o.getAuthentication();
            if (a != null) {
                if (a.getPrincipal() instanceof InvalidatableUserDetails) {
                    InvalidatableUserDetails ud = (InvalidatableUserDetails) a.getPrincipal();
                    if (ud.isInvalid())
                        // don't let Acegi see invalid security context
                        session.setAttribute(ACEGI_SECURITY_CONTEXT_KEY, null);
                }
            }
        }
    }
    super.doFilter(req, res, chain);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) Authentication(org.acegisecurity.Authentication) SecurityContext(org.acegisecurity.context.SecurityContext)

Example 15 with Authentication

use of org.acegisecurity.Authentication in project hudson-2.x by hudson.

the class SecurityServiceImpl method callAs.

public <T> T callAs(final Authentication auth, final Callable<T> task) throws Exception {
    checkNotNull(auth);
    checkNotNull(task);
    final SecurityContext ctx = SecurityContextHolder.getContext();
    final Authentication current = ctx.getAuthentication();
    ctx.setAuthentication(auth);
    try {
        return task.call();
    } finally {
        ctx.setAuthentication(current);
    }
}
Also used : Authentication(org.acegisecurity.Authentication) SecurityContext(org.acegisecurity.context.SecurityContext)

Aggregations

Authentication (org.acegisecurity.Authentication)19 SecurityContext (org.acegisecurity.context.SecurityContext)6 User (hudson.model.User)4 ACL (hudson.security.ACL)3 Nonnull (javax.annotation.Nonnull)3 Item (hudson.model.Item)2 CliAuthenticator (hudson.security.CliAuthenticator)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpSession (javax.servlet.http.HttpSession)2 JSONObject (net.sf.json.JSONObject)2 CmdLineException (org.kohsuke.args4j.CmdLineException)2 Credentials (com.cloudbees.plugins.credentials.Credentials)1 CredentialsStore (com.cloudbees.plugins.credentials.CredentialsStore)1 IdCredentials (com.cloudbees.plugins.credentials.common.IdCredentials)1 Domain (com.cloudbees.plugins.credentials.domains.Domain)1 HashCode (com.google.common.hash.HashCode)1 AbortException (hudson.AbortException)1 ExtensionComponent (hudson.ExtensionComponent)1