11using System . Globalization ;
22using System . Net ;
33using System . Text ;
4+ using System . Text . Encodings . Web ;
45using Altinn . App . Api . Extensions ;
56using Altinn . App . Api . Helpers . Patch ;
67using Altinn . App . Api . Helpers . RequestHandling ;
@@ -254,7 +255,13 @@ public async Task<ActionResult<InstanceResponse>> Post(
254255
255256 if (
256257 lookup == null
257- || ( lookup . PersonNumber == null && lookup . OrganisationNumber == null && lookup . PartyId == null )
258+ || (
259+ lookup . PersonNumber == null
260+ && lookup . OrganisationNumber == null
261+ && lookup . PartyId == null
262+ && lookup . ExternalIdentifier == null
263+ && lookup . Username == null
264+ )
258265 )
259266 {
260267 return BadRequest (
@@ -291,7 +298,10 @@ public async Task<ActionResult<InstanceResponse>> Post(
291298 try
292299 {
293300 party = await LookupParty ( instanceTemplate . InstanceOwner ) ?? throw new Exception ( "Unknown party" ) ;
294- instanceTemplate . InstanceOwner = InstantiationHelper . PartyToInstanceOwner ( party ) ;
301+ instanceTemplate . InstanceOwner = await InstantiationHelper . PartyToInstanceOwner (
302+ party ,
303+ _authenticationContext
304+ ) ;
295305 }
296306 catch ( Exception partyLookupException )
297307 {
@@ -479,7 +489,13 @@ public async Task<ActionResult<InstanceResponse>> PostSimplified(
479489
480490 if (
481491 lookup == null
482- || ( lookup . PersonNumber == null && lookup . OrganisationNumber == null && lookup . PartyId == null )
492+ || (
493+ lookup . PersonNumber == null
494+ && lookup . OrganisationNumber == null
495+ && lookup . PartyId == null
496+ && lookup . ExternalIdentifier == null
497+ && lookup . Username == null
498+ )
483499 )
484500 {
485501 return BadRequest (
@@ -491,7 +507,10 @@ public async Task<ActionResult<InstanceResponse>> PostSimplified(
491507 try
492508 {
493509 party = await LookupParty ( instansiationInstance . InstanceOwner ) ?? throw new Exception ( "Unknown party" ) ;
494- instansiationInstance . InstanceOwner = InstantiationHelper . PartyToInstanceOwner ( party ) ;
510+ instansiationInstance . InstanceOwner = await InstantiationHelper . PartyToInstanceOwner (
511+ party ,
512+ _authenticationContext
513+ ) ;
495514 }
496515 catch ( Exception partyLookupException )
497516 {
@@ -1132,6 +1151,18 @@ string action
11321151 string personOrOrganisationNumber = instanceOwner . PersonNumber ?? instanceOwner . OrganisationNumber ;
11331152 try
11341153 {
1154+ if ( ! string . IsNullOrEmpty ( instanceOwner . ExternalIdentifier ) )
1155+ {
1156+ var partyId = await _altinnPartyClient . GetPartyIdByUrn ( instanceOwner . ExternalIdentifier ) ;
1157+ if ( partyId == null )
1158+ {
1159+ throw new ServiceException (
1160+ HttpStatusCode . BadRequest ,
1161+ $ "Failed to lookup party by external identifier: { instanceOwner . ExternalIdentifier } . No partyId found for the provided external identifier."
1162+ ) ;
1163+ }
1164+ return await _registerClient . GetPartyUnchecked ( partyId . Value , this . HttpContext . RequestAborted ) ;
1165+ }
11351166 if ( ! string . IsNullOrEmpty ( instanceOwner . PersonNumber ) )
11361167 {
11371168 lookupNumber = "personNumber" ;
@@ -1144,6 +1175,22 @@ string action
11441175 new PartyLookup { OrgNo = instanceOwner . OrganisationNumber }
11451176 ) ;
11461177 }
1178+ else if ( ! string . IsNullOrEmpty ( instanceOwner . Username ) )
1179+ {
1180+ var email = instanceOwner . Username . StartsWith ( "epost:" , StringComparison . InvariantCultureIgnoreCase )
1181+ ? instanceOwner . Username [ 6 ..]
1182+ : instanceOwner . Username ;
1183+ var urn = $ "{ AltinnUrns . SelfIdentifiedEmail } :{ UrlEncoder . Default . Encode ( email ) } ";
1184+ var partyId = await _altinnPartyClient . GetPartyIdByUrn ( urn ) ;
1185+ if ( partyId == null )
1186+ {
1187+ throw new ServiceException (
1188+ HttpStatusCode . BadRequest ,
1189+ $ "Failed to lookup party by username: { instanceOwner . Username } . No partyId found for the provided idporten self identified email address."
1190+ ) ;
1191+ }
1192+ return await _registerClient . GetPartyUnchecked ( partyId . Value , this . HttpContext . RequestAborted ) ;
1193+ }
11471194 else
11481195 {
11491196 throw new ServiceException (
0 commit comments