13
13
14
14
import org .apache .logging .log4j .Logger ;
15
15
import org .springframework .beans .factory .annotation .Autowired ;
16
+ import org .springframework .beans .factory .annotation .Qualifier ;
16
17
import org .springframework .stereotype .Component ;
17
18
18
19
import eu .unicore .util .configuration .ConfigurationException ;
19
20
import pl .edu .icm .unity .base .utils .Log ;
21
+ import pl .edu .icm .unity .engine .api .EntityManagement ;
20
22
import pl .edu .icm .unity .engine .api .authn .AuthenticatedEntity ;
21
23
import pl .edu .icm .unity .engine .api .authn .AuthenticationResult ;
22
24
import pl .edu .icm .unity .engine .api .authn .RemoteAuthenticationException ;
32
34
import pl .edu .icm .unity .engine .api .translation .in .MappedGroup ;
33
35
import pl .edu .icm .unity .engine .api .translation .in .MappedIdentity ;
34
36
import pl .edu .icm .unity .engine .api .translation .in .MappingResult ;
37
+ import pl .edu .icm .unity .engine .translation .ExecutionBreakException ;
35
38
import pl .edu .icm .unity .engine .translation .in .InputTranslationProfile ;
36
39
import pl .edu .icm .unity .engine .translation .in .InputTranslationProfileRepository ;
37
40
import pl .edu .icm .unity .exceptions .EngineException ;
38
41
import pl .edu .icm .unity .exceptions .IllegalIdentityValueException ;
39
42
import pl .edu .icm .unity .store .api .tx .Transactional ;
40
43
import pl .edu .icm .unity .types .basic .Attribute ;
44
+ import pl .edu .icm .unity .types .basic .Entity ;
41
45
import pl .edu .icm .unity .types .basic .EntityParam ;
42
46
import pl .edu .icm .unity .types .basic .IdentityParam ;
43
47
import pl .edu .icm .unity .types .basic .IdentityTaV ;
@@ -51,18 +55,20 @@ class RemoteAuthnResultTranslatorImpl implements RemoteAuthnResultTranslator
51
55
private final IdentityResolver identityResolver ;
52
56
private final InputTranslationEngine trEngine ;
53
57
private final InputTranslationActionsRegistry actionsRegistry ;
54
-
58
+ private final EntityManagement idsMan ;
55
59
56
60
@ Autowired
57
61
RemoteAuthnResultTranslatorImpl (IdentityResolver identityResolver ,
58
62
InputTranslationProfileRepository profileRepo ,
59
63
InputTranslationEngine trEngine ,
60
- InputTranslationActionsRegistry actionsRegistry )
64
+ InputTranslationActionsRegistry actionsRegistry ,
65
+ @ Qualifier ("insecure" ) EntityManagement idsMan )
61
66
{
62
67
this .identityResolver = identityResolver ;
63
68
this .inputProfileRepo = profileRepo ;
64
69
this .trEngine = trEngine ;
65
70
this .actionsRegistry = actionsRegistry ;
71
+ this .idsMan = idsMan ;
66
72
}
67
73
68
74
@ Override
@@ -72,7 +78,6 @@ public RemoteAuthenticationResult getTranslatedResult(RemotelyAuthenticatedInput
72
78
String registrationForm , boolean allowAssociation )
73
79
throws RemoteAuthenticationException
74
80
{
75
-
76
81
TranslationProfile translationProfile ;
77
82
try
78
83
{
@@ -132,7 +137,7 @@ private RemoteAuthenticationResult assembleDryRunAuthenticationResult(RemotelyAu
132
137
}
133
138
} else
134
139
{
135
- return handleUnknownUser (remotePrincipal , registrationForm , allowAssociation );
140
+ return handleUnknownUser (remotePrincipal , registrationForm , allowAssociation );
136
141
}
137
142
return RemoteAuthenticationResult .successfulPartial (remotePrincipal , authenticatedEntity );
138
143
}
@@ -221,7 +226,7 @@ public final RemotelyAuthenticatedPrincipal translateRemoteInput(RemotelyAuthent
221
226
result .addIdentity (new MappedIdentity (IdentityEffectMode .REQUIRE_MATCH ,
222
227
presetIdParam , null ));
223
228
}
224
- trEngine . preprocess (result );
229
+ setMappingToExistingEntity (result );
225
230
if (!dryRun )
226
231
trEngine .process (result );
227
232
@@ -236,6 +241,34 @@ public final RemotelyAuthenticatedPrincipal translateRemoteInput(RemotelyAuthent
236
241
ret .setCreationTime (Instant .now ());
237
242
return ret ;
238
243
}
244
+
245
+ private void setMappingToExistingEntity (MappingResult result ) throws EngineException
246
+ {
247
+ Entity existing = null ;
248
+ for (MappedIdentity checked : result .getIdentities ())
249
+ {
250
+ try
251
+ {
252
+ Entity found = idsMan .getEntity (new EntityParam (checked .getIdentity ()));
253
+ if (existing != null && !existing .getId ().equals (found .getId ()))
254
+ {
255
+ log .warn ("Identity was mapped to two different entities: " + existing + " and "
256
+ + found );
257
+ throw new ExecutionBreakException ();
258
+ }
259
+ existing = found ;
260
+ result .addAuthenticatedWith (checked .getIdentity ().getValue ());
261
+ } catch (IllegalArgumentException e )
262
+ {
263
+ log .trace ("Identity " + checked + " not found in DB, details of exception follows" , e );
264
+ }
265
+ }
266
+ if (existing != null )
267
+ {
268
+ result .setMappedToExistingEntity (new EntityParam (existing .getId ()));
269
+ }
270
+ }
271
+
239
272
private List <IdentityTaV > extractIdentities (MappingResult input )
240
273
{
241
274
List <MappedIdentity > identities = input .getIdentities ();
0 commit comments