diff --git a/src/Project/Controllers/DataSourceController.cs b/src/Project/Controllers/DataSourceController.cs index d6fcaad09a7f3c982eb727653c978f2d2b03032a..2fdfde76a7a61b195ce5b7cfa2dcd8e78507937d 100644 --- a/src/Project/Controllers/DataSourceController.cs +++ b/src/Project/Controllers/DataSourceController.cs @@ -20,7 +20,7 @@ namespace Coscine.Api.Project.Controllers { private readonly IConfiguration _configuration; private readonly JWTHandler _jwtHandler; - private static readonly HttpClient _client = new HttpClient(); + private static readonly HttpClient Client = new HttpClient(); private readonly Authenticator _authenticator; private readonly ResourceModel _resourceModel; @@ -94,7 +94,7 @@ namespace Coscine.Api.Project.Controllers request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authHeader); // Thread safe according to msdn and HttpCompletionOption sets it to get only headers first. - var response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); + var response = await Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); if (response.IsSuccessStatusCode) { if (response.Content.Headers.Contains("Content-Disposition")) @@ -117,7 +117,7 @@ namespace Coscine.Api.Project.Controllers else if (response.StatusCode == System.Net.HttpStatusCode.Forbidden) { - return Forbid($"Not allowed to access the datasource."); + return Forbid("Not allowed to access the datasource."); } else { @@ -131,21 +131,8 @@ namespace Coscine.Api.Project.Controllers } } - private string BuildRdsAuthHeader(string bucketname) + private string BuildWaterbutlerPayload(Dictionary<string, object> auth, Dictionary<string, object> credentials, Dictionary<string, object> settings) { - var auth = new Dictionary<string, object>(); - - var credentials = new Dictionary<string, object> - { - { "access_key", _configuration.GetString("coscine/global/rds_access_key") }, - { "secret_key", _configuration.GetString("coscine/global/rds_secret_key") } - }; - - var settings = new Dictionary<string, object> - { - { "bucket", bucketname } - }; - var data = new Dictionary<string, object> { { "auth", auth }, @@ -162,6 +149,24 @@ namespace Coscine.Api.Project.Controllers return _jwtHandler.GenerateJwtToken(payload); } + private string BuildRdsAuthHeader(string bucketname) + { + var auth = new Dictionary<string, object>(); + + var credentials = new Dictionary<string, object> + { + { "access_key", _configuration.GetString("coscine/global/rds_access_key") }, + { "secret_key", _configuration.GetString("coscine/global/rds_secret_key") } + }; + + var settings = new Dictionary<string, object> + { + { "bucket", bucketname } + }; + + return BuildWaterbutlerPayload(auth, credentials, settings); + } + private string BuildGitlabAuthHeader(string externalId, string url) { @@ -180,20 +185,7 @@ namespace Coscine.Api.Project.Controllers { "host", "https://git.rwth-aachen.de"} }; - var data = new Dictionary<string, object> - { - { "auth", auth }, - { "credentials", credentials }, - { "settings", settings }, - { "callback_url", "rwth-aachen.de" } - }; - - var payload = new JwtPayload - { - { "data", data } - }; - - return _jwtHandler.GenerateJwtToken(payload); + return BuildWaterbutlerPayload(auth, credentials, settings); } } }