1 module hunt.framework.auth.BasicAuthRealm;
2 
3 import hunt.framework.auth.AuthRealm;
4 import hunt.framework.auth.Claim;
5 import hunt.framework.auth.ClaimTypes;
6 import hunt.framework.auth.JwtToken;
7 import hunt.framework.auth.JwtUtil;
8 import hunt.framework.auth.principal;
9 import hunt.framework.auth.UserDetails;
10 import hunt.framework.auth.UserService;
11 import hunt.framework.provider.ServiceProvider;
12 
13 import hunt.collection.ArrayList;
14 import hunt.collection.Collection;
15 import hunt.http.AuthenticationScheme;
16 import hunt.logging.ConsoleLogger;
17 import hunt.shiro;
18 import hunt.String;
19 
20 import std.format;
21 
22 
23 /**
24  * 
25  */
26 class BasicAuthRealm : AuthRealm {
27 
28     this(UserService userService) {
29         super(userService);
30     }
31 
32     override bool supports(AuthenticationToken token) {
33         version(HUNT_AUTH_DEBUG) tracef("AuthenticationToken: %s", typeid(cast(Object)token));
34         UsernamePasswordToken t = cast(UsernamePasswordToken)token;
35         return t !is null;
36     }
37 
38     override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {
39         string username = token.getPrincipal();
40         string password = cast(string)token.getCredentials();
41 
42         UserService userService = getUserService();
43         version(HUNT_AUTH_DEBUG) {
44             infof("username: %s, %s", username, typeid(cast(Object)userService));
45         }        
46 
47         // To authenticate the user with username and password
48         UserDetails user = userService.authenticate(username, password);
49         
50         if(user !is null) {
51 
52             version(HUNT_AUTH_DEBUG) infof("Realm: %s", getName());
53             PrincipalCollection pCollection = new SimplePrincipalCollection(user, getName());
54             String credentials = new String(password);
55             SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(pCollection, credentials);
56 
57             return info;
58         } else {
59             throw new IncorrectCredentialsException(username);
60         }
61     }
62 }