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;
17 import hunt.shiro;
18 import hunt.String;
19 
20 import std.format;
21 import std.range;
22 
23 
24 /**
25  * 
26  */
27 class BasicAuthRealm : AuthRealm {
28 
29     this(UserService userService) {
30         super(userService);
31     }
32 
33     override bool supports(AuthenticationToken token) {
34         version(HUNT_AUTH_DEBUG) tracef("AuthenticationToken: %s", typeid(cast(Object)token));
35         UsernamePasswordToken t = cast(UsernamePasswordToken)token;
36         return t !is null;
37     }
38 
39     override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {
40         string username = token.getPrincipal();
41         string password = cast(string)token.getCredentials();
42 
43         UserService userService = getUserService();
44         version(HUNT_AUTH_DEBUG) {
45             infof("username: %s, %s", username, typeid(cast(Object)userService));
46         }
47 
48         if(username.empty) {
49             throw new IncorrectCredentialsException(username);
50         }
51 
52         // To authenticate the user with username and password
53         UserDetails user = userService.authenticate(username, password);
54         
55         if(user !is null) {
56 
57             version(HUNT_AUTH_DEBUG) infof("Realm: %s", getName());
58             PrincipalCollection pCollection = new SimplePrincipalCollection(user, getName());
59             String credentials = new String(password);
60             SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(pCollection, credentials);
61 
62             return info;
63         } else {
64             throw new IncorrectCredentialsException(username);
65         }
66     }
67 }