List of usage examples for javax.security.auth.login LoginContext LoginContext
public LoginContext(String name, Subject subject, CallbackHandler callbackHandler) throws LoginException
From source file:io.fabric8.maven.impl.MavenSecureHttpContext.java
public Subject doAuthenticate(final String username, final String password) { try {//from www. j a va2 s . com Subject subject = new Subject(); LoginContext loginContext = new LoginContext(realm, subject, new CallbackHandler() { public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (int i = 0; i < callbacks.length; i++) { if (callbacks[i] instanceof NameCallback) { ((NameCallback) callbacks[i]).setName(username); } else if (callbacks[i] instanceof PasswordCallback) { ((PasswordCallback) callbacks[i]).setPassword(password.toCharArray()); } else { throw new UnsupportedCallbackException(callbacks[i]); } } } }); loginContext.login(); if (role != null && role.length() > 0) { String clazz = "org.apache.karaf.jaas.boot.principal.RolePrincipal"; String name = role; int idx = role.indexOf(':'); if (idx > 0) { clazz = role.substring(0, idx); name = role.substring(idx + 1); } boolean found = false; for (Principal p : subject.getPrincipals()) { if (p.getClass().getName().equals(clazz) && p.getName().equals(name)) { found = true; break; } } if (!found) { throw new FailedLoginException("User does not have the required role " + role); } } return subject; } catch (AccountException e) { LOGGER.warn("Account failure", e); return null; } catch (LoginException e) { LOGGER.debug("Login failed", e); return null; } catch (GeneralSecurityException e) { LOGGER.error("General Security Exception", e); return null; } }
From source file:de.adorsys.oauth.loginmodule.DelegatingLoginModule.java
@Override public boolean login() throws LoginException { ClientID clientID = resolveClientID(); verifyClientID(clientID);/*from www . j a v a 2 s . c o m*/ loginContext = new LoginContext(clientID.getValue(), subject, callbackHandler); loginContext.login(); loginSucceded = true; return true; }
From source file:nl.nn.adapterframework.util.CredentialFactory.java
protected void getCredentialsFromAlias() { if (!gotCredentials && StringUtils.isNotEmpty(getAlias())) { try {/*from w w w .ja v a 2 s . co m*/ Set principals = new HashSet(); Set publicCredentials = new HashSet(); Set privateCredentials = new HashSet(); Principal p = new IbisPrincipal(); principals.add(p); Subject initialSubject = new Subject(false, principals, publicCredentials, privateCredentials); String loginConfiguration = AppConstants.getInstance().getProperty("PrincipalMapping", "DefaultPrincipalMapping"); LoginContext lc = new LoginContext(loginConfiguration, initialSubject, this); lc.login(); Subject s = lc.getSubject(); //showSet(s.getPrincipals(),"principals"); //showSet(s.getPublicCredentials(),"PublicCredentials"); //showSet(s.getPrivateCredentials(),"PrivateCredentials"); //Object pwcred=Subject.doAsPrivileged(s,new PasswordGetter(s),AccessController.getContext()); //Object pwcred=AccessController.doPrivileged(new PasswordGetter(s)); Object pwcred = s.getPrivateCredentials().toArray()[0]; setUsername(ClassUtils.invokeStringGetter(pwcred, "getUserName")); setPassword(invokeCharArrayGetter(pwcred, "getPassword")); gotCredentials = true; } catch (Exception e) { if (!useFallback) { NoSuchElementException nsee = new NoSuchElementException( "cannot obtain credentials from authentication alias [" + getAlias() + "]"); nsee.initCause(e); throw nsee; } log.error("exception obtaining credentials for alias [" + getAlias() + "]", e); String usernameProp = "alias." + getAlias() + ".username"; String passwordProp = "alias." + getAlias() + ".password"; log.info("trying to solve Authentication Alias from application properties [" + usernameProp + "] and [" + passwordProp + "]"); setUsername(AppConstants.getInstance().getProperty(usernameProp, username)); setPassword(AppConstants.getInstance().getProperty(passwordProp, password)); } } }
From source file:org.apache.servicemix.nmr.core.security.JaasAuthenticationService.java
public void authenticate(Subject subject, String domain, final String user, final Object credentials) throws GeneralSecurityException { if (LOG.isDebugEnabled()) { LOG.debug("Authenticating '" + user + "' with '" + credentials + "'"); }/*from www. j av a 2s. c o m*/ LoginContext loginContext = new LoginContext(domain, subject, new CallbackHandler() { public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (int i = 0; i < callbacks.length; i++) { if (callbacks[i] instanceof NameCallback) { ((NameCallback) callbacks[i]).setName(user); } else if (callbacks[i] instanceof PasswordCallback && credentials instanceof String) { ((PasswordCallback) callbacks[i]).setPassword(((String) credentials).toCharArray()); } else if (callbacks[i] instanceof CertificateCallback && credentials instanceof X509Certificate) { ((CertificateCallback) callbacks[i]).setCertificate((X509Certificate) credentials); } else { throw new UnsupportedCallbackException(callbacks[i]); } } } }); loginContext.login(); }
From source file:org.openhab.io.net.http.SecureHttpContext.java
/** * <p>Authenticates the given <code>username</code> and <code>password</code> * with respect to the given <code>realm</code> against the configured * {@link LoginModule} (see login.conf in <openhabhome>/etc to learn * more about the configured {@link LoginModule})</p> * <p><b>Note:</b>Roles aren't supported yet!</p> * //from w ww .j a v a2s . co m * @param realm the realm used by the configured {@link LoginModule}. * <i>Note:</i> the given <code>realm</code> must be same name as configured * in <code>login.conf</code> * @param username * @param password * * @return a {@link Subject} filled with username, password, realm, etc. or * <code>null</code> if the login failed * @throws UnsupportedCallbackException if a {@link Callback}-instance other * than {@link NameCallback} or {@link ObjectCallback} is going to be handled */ private Subject authenticate(final String realm, final String username, final String password) { try { logger.trace("going to authenticate user '{}', realm '{}'", username, realm); Subject subject = new Subject(); LoginContext lContext = new LoginContext(realm, subject, new CallbackHandler() { public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (int i = 0; i < callbacks.length; i++) { if (callbacks[i] instanceof NameCallback) { ((NameCallback) callbacks[i]).setName(username); } else if (callbacks[i] instanceof ObjectCallback) { ((ObjectCallback) callbacks[i]).setObject(password); } else { throw new UnsupportedCallbackException(callbacks[i]); } } } }); lContext.login(); // TODO: TEE: implement role handling here! return subject; } catch (LoginException le) { logger.warn("authentication of user '" + username + "' failed", le); return null; } }