44
44
import org .slf4j .Logger ;
45
45
import org .slf4j .LoggerFactory ;
46
46
47
- public class InMemoryRegistrationStorage implements RegistrationStorage , CredentialRepository {
47
+ public class InMemoryRegistrationStorage implements CredentialRepository {
48
48
49
49
private final Cache <String , Set <CredentialRegistration >> storage =
50
50
CacheBuilder .newBuilder ().maximumSize (1000 ).expireAfterAccess (1 , TimeUnit .DAYS ).build ();
51
51
52
52
private Logger logger = LoggerFactory .getLogger (InMemoryRegistrationStorage .class );
53
53
54
- @ Override
55
- public boolean addRegistrationByUsername (String username , CredentialRegistration reg ) {
56
- try {
57
- return storage .get (username , HashSet ::new ).add (reg );
58
- } catch (ExecutionException e ) {
59
- logger .error ("Failed to add registration" , e );
60
- throw new RuntimeException (e );
61
- }
62
- }
54
+ ////////////////////////////////////////////////////////////////////////////////
55
+ // The following methods are required by the CredentialRepository interface.
56
+ ////////////////////////////////////////////////////////////////////////////////
63
57
64
58
@ Override
65
59
public Set <PublicKeyCredentialDescriptor > getCredentialIdsForUsername (String username ) {
@@ -74,6 +68,73 @@ public Set<PublicKeyCredentialDescriptor> getCredentialIdsForUsername(String use
74
68
}
75
69
76
70
@ Override
71
+ public Optional <String > getUsernameForUserHandle (ByteArray userHandle ) {
72
+ return getRegistrationsByUserHandle (userHandle ).stream ()
73
+ .findAny ()
74
+ .map (CredentialRegistration ::getUsername );
75
+ }
76
+
77
+ @ Override
78
+ public Optional <ByteArray > getUserHandleForUsername (String username ) {
79
+ return getRegistrationsByUsername (username ).stream ()
80
+ .findAny ()
81
+ .map (reg -> reg .getUserIdentity ().getId ());
82
+ }
83
+
84
+ @ Override
85
+ public Optional <RegisteredCredential > lookup (ByteArray credentialId , ByteArray userHandle ) {
86
+ Optional <CredentialRegistration > registrationMaybe =
87
+ storage .asMap ().values ().stream ()
88
+ .flatMap (Collection ::stream )
89
+ .filter (credReg -> credentialId .equals (credReg .getCredential ().getCredentialId ()))
90
+ .findAny ();
91
+
92
+ logger .debug (
93
+ "lookup credential ID: {}, user handle: {}; result: {}" ,
94
+ credentialId ,
95
+ userHandle ,
96
+ registrationMaybe );
97
+ return registrationMaybe .flatMap (
98
+ registration ->
99
+ Optional .of (
100
+ RegisteredCredential .builder ()
101
+ .credentialId (registration .getCredential ().getCredentialId ())
102
+ .userHandle (registration .getUserIdentity ().getId ())
103
+ .publicKeyCose (registration .getCredential ().getPublicKeyCose ())
104
+ .signatureCount (registration .getCredential ().getSignatureCount ())
105
+ .build ()));
106
+ }
107
+
108
+ @ Override
109
+ public Set <RegisteredCredential > lookupAll (ByteArray credentialId ) {
110
+ return CollectionUtil .immutableSet (
111
+ storage .asMap ().values ().stream ()
112
+ .flatMap (Collection ::stream )
113
+ .filter (reg -> reg .getCredential ().getCredentialId ().equals (credentialId ))
114
+ .map (
115
+ reg ->
116
+ RegisteredCredential .builder ()
117
+ .credentialId (reg .getCredential ().getCredentialId ())
118
+ .userHandle (reg .getUserIdentity ().getId ())
119
+ .publicKeyCose (reg .getCredential ().getPublicKeyCose ())
120
+ .signatureCount (reg .getCredential ().getSignatureCount ())
121
+ .build ())
122
+ .collect (Collectors .toSet ()));
123
+ }
124
+
125
+ ////////////////////////////////////////////////////////////////////////////////
126
+ // The following methods are specific to this demo application.
127
+ ////////////////////////////////////////////////////////////////////////////////
128
+
129
+ public boolean addRegistrationByUsername (String username , CredentialRegistration reg ) {
130
+ try {
131
+ return storage .get (username , HashSet ::new ).add (reg );
132
+ } catch (ExecutionException e ) {
133
+ logger .error ("Failed to add registration" , e );
134
+ throw new RuntimeException (e );
135
+ }
136
+ }
137
+
77
138
public Collection <CredentialRegistration > getRegistrationsByUsername (String username ) {
78
139
try {
79
140
return storage .get (username , HashSet ::new );
@@ -83,7 +144,6 @@ public Collection<CredentialRegistration> getRegistrationsByUsername(String user
83
144
}
84
145
}
85
146
86
- @ Override
87
147
public Collection <CredentialRegistration > getRegistrationsByUserHandle (ByteArray userHandle ) {
88
148
return storage .asMap ().values ().stream ()
89
149
.flatMap (Collection ::stream )
@@ -93,21 +153,6 @@ public Collection<CredentialRegistration> getRegistrationsByUserHandle(ByteArray
93
153
.collect (Collectors .toList ());
94
154
}
95
155
96
- @ Override
97
- public Optional <String > getUsernameForUserHandle (ByteArray userHandle ) {
98
- return getRegistrationsByUserHandle (userHandle ).stream ()
99
- .findAny ()
100
- .map (CredentialRegistration ::getUsername );
101
- }
102
-
103
- @ Override
104
- public Optional <ByteArray > getUserHandleForUsername (String username ) {
105
- return getRegistrationsByUsername (username ).stream ()
106
- .findAny ()
107
- .map (reg -> reg .getUserIdentity ().getId ());
108
- }
109
-
110
- @ Override
111
156
public void updateSignatureCount (AssertionResult result ) {
112
157
CredentialRegistration registration =
113
158
getRegistrationByUsernameAndCredentialId (result .getUsername (), result .getCredentialId ())
@@ -127,7 +172,6 @@ public void updateSignatureCount(AssertionResult result) {
127
172
.build ()));
128
173
}
129
174
130
- @ Override
131
175
public Optional <CredentialRegistration > getRegistrationByUsernameAndCredentialId (
132
176
String username , ByteArray id ) {
133
177
try {
@@ -140,7 +184,6 @@ public Optional<CredentialRegistration> getRegistrationByUsernameAndCredentialId
140
184
}
141
185
}
142
186
143
- @ Override
144
187
public boolean removeRegistrationByUsername (
145
188
String username , CredentialRegistration credentialRegistration ) {
146
189
try {
@@ -151,50 +194,12 @@ public boolean removeRegistrationByUsername(
151
194
}
152
195
}
153
196
154
- @ Override
155
197
public boolean removeAllRegistrations (String username ) {
156
198
storage .invalidate (username );
157
199
return true ;
158
200
}
159
201
160
- @ Override
161
- public Optional <RegisteredCredential > lookup (ByteArray credentialId , ByteArray userHandle ) {
162
- Optional <CredentialRegistration > registrationMaybe =
163
- storage .asMap ().values ().stream ()
164
- .flatMap (Collection ::stream )
165
- .filter (credReg -> credentialId .equals (credReg .getCredential ().getCredentialId ()))
166
- .findAny ();
167
-
168
- logger .debug (
169
- "lookup credential ID: {}, user handle: {}; result: {}" ,
170
- credentialId ,
171
- userHandle ,
172
- registrationMaybe );
173
- return registrationMaybe .flatMap (
174
- registration ->
175
- Optional .of (
176
- RegisteredCredential .builder ()
177
- .credentialId (registration .getCredential ().getCredentialId ())
178
- .userHandle (registration .getUserIdentity ().getId ())
179
- .publicKeyCose (registration .getCredential ().getPublicKeyCose ())
180
- .signatureCount (registration .getCredential ().getSignatureCount ())
181
- .build ()));
182
- }
183
-
184
- @ Override
185
- public Set <RegisteredCredential > lookupAll (ByteArray credentialId ) {
186
- return CollectionUtil .immutableSet (
187
- storage .asMap ().values ().stream ()
188
- .flatMap (Collection ::stream )
189
- .filter (reg -> reg .getCredential ().getCredentialId ().equals (credentialId ))
190
- .map (
191
- reg ->
192
- RegisteredCredential .builder ()
193
- .credentialId (reg .getCredential ().getCredentialId ())
194
- .userHandle (reg .getUserIdentity ().getId ())
195
- .publicKeyCose (reg .getCredential ().getPublicKeyCose ())
196
- .signatureCount (reg .getCredential ().getSignatureCount ())
197
- .build ())
198
- .collect (Collectors .toSet ()));
202
+ public boolean userExists (String username ) {
203
+ return !getRegistrationsByUsername (username ).isEmpty ();
199
204
}
200
205
}
0 commit comments