use of org.apache.wiki.auth.WikiPrincipal in project jspwiki by apache.
the class CookieAuthenticationLoginModule method login.
/**
* @see javax.security.auth.spi.LoginModule#login()
*
* {@inheritDoc}
*/
public boolean login() throws LoginException {
// Otherwise, let's go and look for the cookie!
HttpRequestCallback hcb = new HttpRequestCallback();
// UserDatabaseCallback ucb = new UserDatabaseCallback();
WikiEngineCallback wcb = new WikiEngineCallback();
Callback[] callbacks = new Callback[] { hcb, wcb };
try {
m_handler.handle(callbacks);
HttpServletRequest request = hcb.getRequest();
String uid = getLoginCookie(request);
if (uid != null) {
WikiEngine engine = wcb.getEngine();
File cookieFile = getCookieFile(engine, uid);
if (cookieFile != null && cookieFile.exists() && cookieFile.canRead()) {
Reader in = null;
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(cookieFile), "UTF-8"));
String username = FileUtil.readContents(in);
if (log.isDebugEnabled()) {
log.debug("Logged in cookie authenticated name=" + username);
}
// If login succeeds, commit these principals/roles
m_principals.add(new WikiPrincipal(username, WikiPrincipal.LOGIN_NAME));
//
// Tag the file so that we know that it has been accessed recently.
//
cookieFile.setLastModified(System.currentTimeMillis());
return true;
} catch (IOException e) {
return false;
} finally {
if (in != null)
in.close();
}
}
}
} catch (IOException e) {
String message = "IO exception; disallowing login.";
log.error(message, e);
throw new LoginException(message);
} catch (UnsupportedCallbackException e) {
String message = "Unable to handle callback; disallowing login.";
log.error(message, e);
throw new LoginException(message);
}
return false;
}
use of org.apache.wiki.auth.WikiPrincipal in project jspwiki by apache.
the class UserDatabaseLoginModule method login.
/**
* @see javax.security.auth.spi.LoginModule#login()
*
* {@inheritDoc}
*/
public boolean login() throws LoginException {
UserDatabaseCallback ucb = new UserDatabaseCallback();
NameCallback ncb = new NameCallback("User name");
PasswordCallback pcb = new PasswordCallback("Password", false);
Callback[] callbacks = new Callback[] { ucb, ncb, pcb };
try {
m_handler.handle(callbacks);
UserDatabase db = ucb.getUserDatabase();
String username = ncb.getName();
String password = new String(pcb.getPassword());
// Look up the user and compare the password hash
if (db == null) {
throw new FailedLoginException("No user database: check the callback handler code!");
}
UserProfile profile = db.findByLoginName(username);
String storedPassword = profile.getPassword();
if (storedPassword != null && db.validatePassword(username, password)) {
if (log.isDebugEnabled()) {
log.debug("Logged in user database user " + username);
}
// If login succeeds, commit these principals/roles
m_principals.add(new WikiPrincipal(username, WikiPrincipal.LOGIN_NAME));
return true;
}
throw new FailedLoginException("The username or password is incorrect.");
} catch (IOException e) {
String message = "IO exception; disallowing login.";
log.error(message, e);
throw new LoginException(message);
} catch (UnsupportedCallbackException e) {
String message = "Unable to handle callback; disallowing login.";
log.error(message, e);
throw new LoginException(message);
} catch (NoSuchPrincipalException e) {
throw new FailedLoginException("The username or password is incorrect.");
}
}
use of org.apache.wiki.auth.WikiPrincipal in project jspwiki by apache.
the class PageManager method actionPerformed.
/**
* Listens for {@link org.apache.wiki.event.WikiSecurityEvent#PROFILE_NAME_CHANGED}
* events. If a user profile's name changes, each page ACL is inspected. If an entry contains
* a name that has changed, it is replaced with the new one. No events are emitted
* as a consequence of this method, because the page contents are still the same; it is
* only the representations of the names within the ACL that are changing.
*
* @param event The event
*/
public void actionPerformed(WikiEvent event) {
if (!(event instanceof WikiSecurityEvent)) {
return;
}
WikiSecurityEvent se = (WikiSecurityEvent) event;
if (se.getType() == WikiSecurityEvent.PROFILE_NAME_CHANGED) {
UserProfile[] profiles = (UserProfile[]) se.getTarget();
Principal[] oldPrincipals = new Principal[] { new WikiPrincipal(profiles[0].getLoginName()), new WikiPrincipal(profiles[0].getFullname()), new WikiPrincipal(profiles[0].getWikiName()) };
Principal newPrincipal = new WikiPrincipal(profiles[1].getFullname());
// Examine each page ACL
try {
int pagesChanged = 0;
Collection pages = getAllPages();
for (Iterator it = pages.iterator(); it.hasNext(); ) {
WikiPage page = (WikiPage) it.next();
boolean aclChanged = changeAcl(page, oldPrincipals, newPrincipal);
if (aclChanged) {
// If the Acl needed changing, change it now
try {
m_engine.getAclManager().setPermissions(page, page.getAcl());
} catch (WikiSecurityException e) {
log.error("Could not change page ACL for page " + page.getName() + ": " + e.getMessage(), e);
}
pagesChanged++;
}
}
log.info("Profile name change for '" + newPrincipal.toString() + "' caused " + pagesChanged + " page ACLs to change also.");
} catch (ProviderException e) {
// Oooo! This is really bad...
log.error("Could not change user name in Page ACLs because of Provider error:" + e.getMessage(), e);
}
}
}
use of org.apache.wiki.auth.WikiPrincipal in project jspwiki by apache.
the class GroupManager method actionPerformed.
/**
* Listens for {@link org.apache.wiki.event.WikiSecurityEvent#PROFILE_NAME_CHANGED}
* events. If a user profile's name changes, each group is inspected. If an entry contains
* a name that has changed, it is replaced with the new one. No group events are emitted
* as a consequence of this method, because the group memberships are still the same; it is
* only the representations of the names within that are changing.
* @param event the incoming event
*/
public void actionPerformed(WikiEvent event) {
if (!(event instanceof WikiSecurityEvent)) {
return;
}
WikiSecurityEvent se = (WikiSecurityEvent) event;
if (se.getType() == WikiSecurityEvent.PROFILE_NAME_CHANGED) {
WikiSession session = se.getSrc();
UserProfile[] profiles = (UserProfile[]) se.getTarget();
Principal[] oldPrincipals = new Principal[] { new WikiPrincipal(profiles[0].getLoginName()), new WikiPrincipal(profiles[0].getFullname()), new WikiPrincipal(profiles[0].getWikiName()) };
Principal newPrincipal = new WikiPrincipal(profiles[1].getFullname());
// Examine each group
int groupsChanged = 0;
try {
for (Group group : m_groupDatabase.groups()) {
boolean groupChanged = false;
for (Principal oldPrincipal : oldPrincipals) {
if (group.isMember(oldPrincipal)) {
group.remove(oldPrincipal);
group.add(newPrincipal);
groupChanged = true;
}
}
if (groupChanged) {
setGroup(session, group);
groupsChanged++;
}
}
} catch (WikiException e) {
// Oooo! This is really bad...
log.error("Could not change user name in Group lists because of GroupDatabase error:" + e.getMessage());
}
log.info("Profile name change for '" + newPrincipal.toString() + "' caused " + groupsChanged + " groups to change also.");
}
}
use of org.apache.wiki.auth.WikiPrincipal in project jspwiki by apache.
the class JDBCGroupDatabase method populateGroup.
/**
* Fills a Group with members.
*
* @param group the group to populate
* @return the populated Group
*/
private Group populateGroup(Group group) {
ResultSet rs = null;
PreparedStatement ps = null;
Connection conn = null;
try {
// Open the database connection
conn = m_ds.getConnection();
ps = conn.prepareStatement(m_findMembers);
ps.setString(1, group.getName());
rs = ps.executeQuery();
while (rs.next()) {
String memberName = rs.getString(m_member);
if (memberName != null) {
WikiPrincipal principal = new WikiPrincipal(memberName, WikiPrincipal.UNSPECIFIED);
group.add(principal);
}
}
} catch (SQLException e) {
// I guess that means there aren't any principals...
} finally {
closeQuietly(conn, ps, rs);
}
return group;
}
Aggregations