/
ProfileClient.cs
111 lines (102 loc) · 4.46 KB
/
ProfileClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System.Text.Json;
using System.Text.Json.Serialization;
using Altinn.AccessManagement.UI.Core.ClientInterfaces;
using Altinn.AccessManagement.UI.Core.Extensions;
using Altinn.AccessManagement.UI.Core.Services.Interfaces;
using Altinn.AccessManagement.UI.Integration.Configuration;
using Altinn.Platform.Profile.Models;
using AltinnCore.Authentication.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Altinn.AccessManagement.UI.Integration.Clients
{
/// <summary>
/// client that integrates with the platform profile api
/// </summary>
public class ProfileClient : IProfileClient
{
private readonly ILogger _logger;
private readonly HttpClient _client;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly PlatformSettings _platformSettings;
private readonly IAccessTokenProvider _accessTokenProvider;
/// <summary>
/// Initializes a new instance of the <see cref="ProfileClient"/> class
/// </summary>
/// <param name="httpClient">the handler for httpclient service</param>
/// <param name="logger">the handler for logger service</param>
/// <param name="httpContextAccessor">the handler for httpcontextaccessor service</param>
/// <param name="platformSettings"> platform settings configuration</param>
/// <param name="accessTokenProvider">the handler for access token generator</param>
public ProfileClient(
HttpClient httpClient,
ILogger<ProfileClient> logger,
IHttpContextAccessor httpContextAccessor,
IOptions<PlatformSettings> platformSettings,
IAccessTokenProvider accessTokenProvider)
{
_logger = logger;
_platformSettings = platformSettings.Value;
httpClient.BaseAddress = new Uri(_platformSettings.ApiProfileEndpoint);
httpClient.DefaultRequestHeaders.Add(_platformSettings.SubscriptionKeyHeaderName, _platformSettings.SubscriptionKey);
_client = httpClient;
_httpContextAccessor = httpContextAccessor;
_accessTokenProvider = accessTokenProvider;
}
/// <inheritdoc/>
public async Task<UserProfile> GetUserProfile(int userId)
{
UserProfile userProfile = null;
try
{
string endpointUrl = $"users/{userId}";
userProfile = await GetUserProfileFromEndpoint(endpointUrl);
}
catch (Exception ex)
{
_logger.LogError(ex, "AccessManagement.UI // ProfileClient // GetUserProfile // Exception");
throw;
}
return userProfile;
}
/// <inheritdoc/>
public async Task<UserProfile> GetUserProfile(Guid uuid)
{
UserProfile userProfile = null;
try
{
string endpointUrl = $"users/byuuid/{uuid}";
userProfile = await GetUserProfileFromEndpoint(endpointUrl);
}
catch (Exception ex)
{
_logger.LogError(ex, "AccessManagement.UI // ProfileClient // GetUserProfile // Exception");
throw;
}
return userProfile;
}
private async Task<UserProfile> GetUserProfileFromEndpoint(string endpointUrl)
{
string token = JwtTokenUtil.GetTokenFromContext(_httpContextAccessor.HttpContext, _platformSettings.JwtCookieName);
var accessToken = await _accessTokenProvider.GetAccessToken();
HttpResponseMessage response = await _client.GetAsync(token, endpointUrl, accessToken);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string responseContent = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};
options.Converters.Add(new JsonStringEnumConverter());
UserProfile userProfile = JsonSerializer.Deserialize<UserProfile>(responseContent, options);
return userProfile;
}
else
{
_logger.LogError($"Getting user profile information from platform failed with statuscode {response.StatusCode}");
return null;
}
}
}
}