My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.deluan.shiro.gae.realm;

import com.google.appengine.api.datastore.*;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.logging.Logger;

/**
* User: deluan
* Date: Sep 21, 2010
* Time: 9:32:45 PM
*/
public class DatastoreRealm extends AuthorizingRealm {

static final String DEFAULT_USER_STORE_KIND = "ShiroUsers";

static final Logger log = Logger.getLogger("com.deluan.shiro.gae.realm.DatastoreRealm");
private DatastoreService datastoreService;
private String userStoreKind = DEFAULT_USER_STORE_KIND;

public DatastoreRealm() {
log.info("Creating a new instance of DatastoreRealm");
this.datastoreService = DatastoreServiceFactory.getDatastoreService();
}

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String username = ((UsernamePasswordToken)token).getUsername();
log.info("Attempting to authenticate " + username + " in DB realm...");

// Null username is invalid
if (username == null) {
throw new AccountException("Null usernames are not allowed by this realm.");
}

// Get the user with the given username. If the user is not
// found, then they don't have an account and we throw an
// exception.
Entity user = findByUsername(username);
if (user == null) {
throw new UnknownAccountException("No account found for user '" + username + "'");
}

log.info("Found user " + username + " in DB");

SimpleAccount account = new SimpleAccount(username, user.getProperty("passwordHash"), "DatastoreRealm");

return account;
}

private Entity findByUsername(String username) {
Query query = new Query(userStoreKind);
query.addFilter("username", Query.FilterOperator.EQUAL, username);
PreparedQuery preparedQuery = datastoreService.prepare(query);
return preparedQuery.asSingleEntity();
}

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
return null; // TODO
}

public void setUserStoreKind(String userStoreKind) {
this.userStoreKind = userStoreKind;
}

}

Change log

0860d3251129 by Deluan Q...@deluan.com.br) on Sep 22, 2010   Diff
simplified the realm, as the logic for
setting and using the credentialsMatcher
was already presented in the parent class
(AuthenticatingRealm)
Go to: 
Project members, sign in to write a code review

Older revisions

b5e1d64a6464 by Deluan Q...@deluan.com.br) on Sep 22, 2010   Diff
wasn't using the configurable
userStoreKind
f74aa2fefb2c by Deluan Q...@deluan.com.br) on Sep 22, 2010   Diff
log message when creating the realm
b025648e5269 by Deluan Q...@deluan.com.br) on Sep 22, 2010   Diff
can change UserStore kind
All revisions of this file

File info

Size: 2409 bytes, 70 lines
Powered by Google Project Hosting