1
2
3
4
5
6 package pt.digitalis.dif.controller.security.managers.impl;
7
8 import java.util.HashMap;
9 import java.util.Map;
10
11 import pt.digitalis.dif.controller.security.managers.IAuthenticationManager;
12 import pt.digitalis.dif.controller.security.managers.IIdentityManager;
13 import pt.digitalis.dif.controller.security.objects.IDIFUser;
14 import pt.digitalis.dif.exception.security.AuthenticationManagerException;
15 import pt.digitalis.dif.exception.security.IdentityManagerException;
16 import pt.digitalis.dif.utils.ObjectFormatter;
17
18 import com.google.inject.Inject;
19
20
21
22
23
24
25
26
27
28 public class AuthenticationManagerStaticImpl implements IAuthenticationManager {
29
30
31 private Map<String, String> loggedClients = new HashMap<String, String>();
32
33
34 @Inject
35 private IIdentityManager theIDManager;
36
37
38
39
40 public void disconnectClient(String clientIdentifier)
41 {
42
43 logOut(clientIdentifier);
44 }
45
46
47
48
49 public IDIFUser getLoggedUser(String clientIdentifier) throws AuthenticationManagerException
50 {
51 try
52 {
53 String userID = loggedClients.get(clientIdentifier);
54
55 if (userID == null)
56 return null;
57 else
58 return theIDManager.getUser(userID);
59 }
60 catch (IdentityManagerException identityManagerException)
61 {
62 throw new AuthenticationManagerException("Could not access the identity manager for user authentication!",
63 identityManagerException);
64 }
65 }
66
67
68
69
70 public boolean isClientLogged(String clientIdentifier)
71 {
72 return loggedClients.containsKey(clientIdentifier);
73 }
74
75
76
77
78
79 public IDIFUser logIn(String clientSpecificID, String userID, String password)
80 throws AuthenticationManagerException
81 {
82 try
83 {
84 if (loggedClients.containsKey(clientSpecificID))
85 {
86
87
88 String presentUserID = loggedClients.get(clientSpecificID);
89
90 if (presentUserID.equals(userID))
91 return theIDManager.getUser(userID);
92 }
93
94
95 if (theIDManager.isIdentityValid(userID, password))
96 {
97
98
99 loggedClients.put(clientSpecificID, userID);
100
101 return theIDManager.getUser(userID);
102 }
103 else
104
105 return null;
106 }
107 catch (IdentityManagerException identityManagerException)
108 {
109 throw new AuthenticationManagerException("Could not access the identity manager for user authentication!",
110 identityManagerException);
111 }
112 }
113
114
115
116
117 public void logOut(String clientIdentifier)
118 {
119 loggedClients.remove(clientIdentifier);
120 }
121
122
123
124
125 @Override
126 public String toString()
127 {
128 ObjectFormatter formatter = new ObjectFormatter();
129 formatter.addItem("Logged Clients", loggedClients);
130
131 return formatter.getFormatedObject();
132 }
133 }