Search in sources :

Example 1 with UserDetails

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

the class PAMSecurityRealm method createSecurityComponents.

public SecurityComponents createSecurityComponents() {
    Binding binding = new Binding();
    binding.setVariable("instance", this);
    BeanBuilder builder = new BeanBuilder();
    builder.parse(Hudson.getInstance().servletContext.getResourceAsStream("/WEB-INF/security/PAMSecurityRealm.groovy"), binding);
    WebApplicationContext context = builder.createApplicationContext();
    return new SecurityComponents(findBean(AuthenticationManager.class, context), new UserDetailsService() {

        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
            if (!UnixUser.exists(username))
                throw new UsernameNotFoundException("No such Unix user: " + username);
            // return some dummy instance
            return new User(username, "", true, true, true, true, new GrantedAuthority[] { AUTHENTICATED_AUTHORITY });
        }
    });
}
Also used : Binding(groovy.lang.Binding) BeanBuilder(hudson.util.spring.BeanBuilder) AuthenticationManager(org.acegisecurity.AuthenticationManager) UsernameNotFoundException(org.acegisecurity.userdetails.UsernameNotFoundException) UserDetails(org.acegisecurity.userdetails.UserDetails) UnixUser(org.jvnet.libpam.UnixUser) User(org.acegisecurity.userdetails.User) GrantedAuthority(org.acegisecurity.GrantedAuthority) UserDetailsService(org.acegisecurity.userdetails.UserDetailsService) DataAccessException(org.springframework.dao.DataAccessException) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 2 with UserDetails

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

the class ClientAuthenticationCache method get.

/**
     * Gets the persisted authentication for this Hudson.
     *
     * @return {@link Hudson#ANONYMOUS} if no such credential is found, or if the stored credential is invalid.
     */
public Authentication get() {
    Hudson h = Hudson.getInstance();
    Secret userName = Secret.decrypt(props.getProperty(getPropertyKey()));
    // failed to decrypt
    if (userName == null)
        return Hudson.ANONYMOUS;
    try {
        UserDetails u = h.getSecurityRealm().loadUserByUsername(userName.toString());
        return new UsernamePasswordAuthenticationToken(u.getUsername(), u.getPassword(), u.getAuthorities());
    } catch (AuthenticationException e) {
        return Hudson.ANONYMOUS;
    } catch (DataAccessException e) {
        return Hudson.ANONYMOUS;
    }
}
Also used : Secret(hudson.util.Secret) UserDetails(org.acegisecurity.userdetails.UserDetails) AuthenticationException(org.acegisecurity.AuthenticationException) Hudson(hudson.model.Hudson) UsernamePasswordAuthenticationToken(org.acegisecurity.providers.UsernamePasswordAuthenticationToken) DataAccessException(org.springframework.dao.DataAccessException)

Example 3 with UserDetails

use of org.acegisecurity.userdetails.UserDetails in project blueocean-plugin by jenkinsci.

the class PipelineBaseTest method login.

protected User login() throws IOException {
    j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
    hudson.model.User bob = j.jenkins.getUser("bob");
    bob.setFullName("Bob Smith");
    bob.addProperty(new Mailer.UserProperty("bob@jenkins-ci.org"));
    UserDetails d = Jenkins.getInstance().getSecurityRealm().loadUserByUsername(bob.getId());
    SecurityContextHolder.getContext().setAuthentication(new PrincipalAcegiUserToken(bob.getId(), bob.getId(), bob.getId(), d.getAuthorities(), bob.getId()));
    return bob;
}
Also used : UserDetails(org.acegisecurity.userdetails.UserDetails) Mailer(hudson.tasks.Mailer) User(hudson.model.User) PrincipalAcegiUserToken(org.acegisecurity.adapters.PrincipalAcegiUserToken)

Example 4 with UserDetails

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

the class AbstractPasswordBasedSecurityRealm method doAuthenticate.

/**
     * Authenticate a login attempt.
     * This method is the heart of a {@link AbstractPasswordBasedSecurityRealm}.
     * <p/>
     * <p/>
     * If the user name and the password pair matches, retrieve the information about this user and
     * return it as a {@link UserDetails} object. {@link org.acegisecurity.userdetails.User} is a convenient
     * implementation to use, but if your backend offers additional data, you may want to use your own subtype
     * so that the rest of Hudson can use those additional information (such as e-mail address --- see
     * {@link MailAddressResolver}.)
     * <p/>
     * <p/>
     * Properties like {@link UserDetails#getPassword()} make no sense, so just return an empty value from it.
     * The only information that you need to pay real attention is {@link UserDetails#getAuthorities()}, which
     * is a list of roles/groups that the user is in. At minimum, this must contain {@link #AUTHENTICATED_AUTHORITY}
     * (which indicates that this user is authenticated and not anonymous), but if your backend supports a notion
     * of groups, you should make sure that the authorities contain one entry per one group. This enables
     * users to control authorization based on groups.
     * <p/>
     * <p/>
     * If the user name and the password pair doesn't match, throw {@link AuthenticationException} to reject the login
     * attempt.
     * If authentication was successful - HUDSON_USER environment variable will be set
     * <a href='http://issues.hudson-ci.org/browse/HUDSON-4463'>HUDSON-4463</a>
     */
protected UserDetails doAuthenticate(String username, String password) throws AuthenticationException {
    UserDetails userDetails = authenticate(username, password);
    EnvVars.setHudsonUserEnvVar(userDetails.getUsername());
    return userDetails;
}
Also used : UserDetails(org.acegisecurity.userdetails.UserDetails)

Example 5 with UserDetails

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

the class ClientAuthenticationCache method set.

/**
     * Persists the specified authentication.
     */
public void set(Authentication a) throws IOException, InterruptedException {
    Hudson h = Hudson.getInstance();
    // make sure that this security realm is capable of retrieving the authentication by name,
    // as it's not required.
    UserDetails u = h.getSecurityRealm().loadUserByUsername(a.getName());
    props.setProperty(getPropertyKey(), Secret.fromString(u.getUsername()).getEncryptedValue());
    save();
}
Also used : UserDetails(org.acegisecurity.userdetails.UserDetails) Hudson(hudson.model.Hudson)

Aggregations

UserDetails (org.acegisecurity.userdetails.UserDetails)8 User (hudson.model.User)4 Mailer (hudson.tasks.Mailer)4 PrincipalAcegiUserToken (org.acegisecurity.adapters.PrincipalAcegiUserToken)4 Hudson (hudson.model.Hudson)2 DataAccessException (org.springframework.dao.DataAccessException)2 Binding (groovy.lang.Binding)1 Secret (hudson.util.Secret)1 BeanBuilder (hudson.util.spring.BeanBuilder)1 UserImpl (io.jenkins.blueocean.service.embedded.rest.UserImpl)1 AuthenticationException (org.acegisecurity.AuthenticationException)1 AuthenticationManager (org.acegisecurity.AuthenticationManager)1 GrantedAuthority (org.acegisecurity.GrantedAuthority)1 UsernamePasswordAuthenticationToken (org.acegisecurity.providers.UsernamePasswordAuthenticationToken)1 User (org.acegisecurity.userdetails.User)1 UserDetailsService (org.acegisecurity.userdetails.UserDetailsService)1 UsernameNotFoundException (org.acegisecurity.userdetails.UsernameNotFoundException)1 Test (org.junit.Test)1 UnixUser (org.jvnet.libpam.UnixUser)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1