|
| 1 | +using System.Diagnostics.CodeAnalysis; |
| 2 | + |
| 3 | +namespace EstateManagementUI.BlazorServer.Common; |
| 4 | + |
| 5 | +public static class AuthenticationHelpers |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Gets the security service addresses for authentication configuration. |
| 9 | + /// This method allows for port overrides to support integration testing scenarios |
| 10 | + /// where the security service may be running on different ports. |
| 11 | + /// </summary> |
| 12 | + /// <param name="authority">The base authority URL</param> |
| 13 | + /// <param name="securityServiceLocalPort">Optional local port override (defaults to 5001)</param> |
| 14 | + /// <param name="securityServicePort">Optional port override (defaults to 5001)</param> |
| 15 | + /// <returns>Tuple containing the authority address and issuer address</returns> |
| 16 | + [ExcludeFromCodeCoverage] |
| 17 | + public static (string authorityAddress, string issuerAddress) GetSecurityServiceAddresses( |
| 18 | + string authority, |
| 19 | + string? securityServiceLocalPort, |
| 20 | + string? securityServicePort) |
| 21 | + { |
| 22 | + Console.WriteLine($"authority is {authority}"); |
| 23 | + Console.WriteLine($"securityServiceLocalPort is {securityServiceLocalPort}"); |
| 24 | + Console.WriteLine($"securityServicePort is {securityServicePort}"); |
| 25 | + |
| 26 | + if (string.IsNullOrEmpty(securityServiceLocalPort)) |
| 27 | + { |
| 28 | + securityServiceLocalPort = "5001"; |
| 29 | + } |
| 30 | + |
| 31 | + if (string.IsNullOrEmpty(securityServicePort)) |
| 32 | + { |
| 33 | + securityServicePort = "5001"; |
| 34 | + } |
| 35 | + |
| 36 | + Uri u = new Uri(authority); |
| 37 | + |
| 38 | + var authorityAddress = u.Port switch |
| 39 | + { |
| 40 | + _ when u.Port.ToString() != securityServiceLocalPort => $"{u.Scheme}://{u.Host}:{securityServiceLocalPort}{u.AbsolutePath}", |
| 41 | + _ => authority |
| 42 | + }; |
| 43 | + |
| 44 | + var issuerAddress = u.Port switch |
| 45 | + { |
| 46 | + _ when u.Port.ToString() != securityServicePort => $"{u.Scheme}://{u.Host}:{securityServicePort}{u.AbsolutePath}", |
| 47 | + _ => authority |
| 48 | + }; |
| 49 | + |
| 50 | + if (authorityAddress.EndsWith("/")) |
| 51 | + { |
| 52 | + authorityAddress = authorityAddress.Substring(0, authorityAddress.Length - 1); |
| 53 | + } |
| 54 | + if (issuerAddress.EndsWith("/")) |
| 55 | + { |
| 56 | + issuerAddress = issuerAddress.Substring(0, issuerAddress.Length - 1); |
| 57 | + } |
| 58 | + |
| 59 | + Console.WriteLine($"authorityAddress is {authorityAddress}"); |
| 60 | + Console.WriteLine($"issuerAddress is {issuerAddress}"); |
| 61 | + |
| 62 | + return (authorityAddress, issuerAddress); |
| 63 | + } |
| 64 | +} |
0 commit comments