Skip to content

Commit 34d7e7f

Browse files
committed
Modified API key retrieval to first check for a configured key before retrieving from storage.
1 parent 68c8710 commit 34d7e7f

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/Umbraco.AuthorizedServices/Services/Implement/AuthorizedServiceCaller.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,25 +81,25 @@ public async Task<string> SendRequestRawAsync<TRequest>(string serviceAlias, str
8181
HttpRequestMessage requestMessage;
8282
if (serviceDetail.AuthenticationMethod == AuthenticationMethod.ApiKey)
8383
{
84-
string? key = KeyStorage.GetKey(serviceAlias);
84+
var apiKey = !string.IsNullOrEmpty(serviceDetail.ApiKey)
85+
? serviceDetail.ApiKey
86+
: KeyStorage.GetKey(serviceAlias);
8587

86-
if (key is null && string.IsNullOrEmpty(serviceDetail.ApiKey))
88+
if (string.IsNullOrWhiteSpace(apiKey))
8789
{
88-
throw new AuthorizedServiceException($"Cannot request service '{serviceAlias}' as access has not yet been authorized.");
90+
throw new AuthorizedServiceException($"Cannot request service '{serviceAlias}' as access has not yet been authorized (no API key is configured or stored).");
8991
}
9092

9193
requestMessage = _authorizedRequestBuilder.CreateRequestMessageWithApiKey(
9294
serviceDetail,
9395
path,
9496
httpMethod,
95-
!string.IsNullOrEmpty(serviceDetail.ApiKey)
96-
? serviceDetail.ApiKey
97-
: (key ?? string.Empty),
97+
apiKey,
9898
requestContent);
9999
}
100100
else
101101
{
102-
Token? token = GetAccessToken(serviceAlias) ?? throw new AuthorizedServiceException($"Cannot request service '{serviceAlias}' as access has not yet been authorized.");
102+
Token? token = GetAccessToken(serviceAlias) ?? throw new AuthorizedServiceException($"Cannot request service '{serviceAlias}' as access has not yet been authorized (no access token is available).");
103103

104104
token = await EnsureAccessToken(serviceAlias, token);
105105

@@ -126,10 +126,9 @@ public async Task<string> SendRequestRawAsync<TRequest>(string serviceAlias, str
126126
public string? GetApiKey(string serviceAlias)
127127
{
128128
ServiceDetail serviceDetail = GetServiceDetail(serviceAlias);
129-
string? key = KeyStorage.GetKey(serviceAlias);
130129
return !string.IsNullOrEmpty(serviceDetail.ApiKey)
131130
? serviceDetail.ApiKey
132-
: key;
131+
: KeyStorage.GetKey(serviceAlias);
133132
}
134133

135134
public string? GetToken(string serviceAlias)

0 commit comments

Comments
 (0)