Append offline_access to authorization scope when AS advertises it (SEP-2207) (#1479)
This commit is contained in:
parent
b8c4d951ec
commit
74788af04d
1 changed files with 149 additions and 1 deletions
|
|
@ -492,6 +492,7 @@ internal sealed partial class ClientOAuthProvider : McpHttpClient
|
|||
}
|
||||
|
||||
var scope = GetScopeParameter(protectedResourceMetadata);
|
||||
scope = AugmentScopeWithOfflineAccess(scope, authServerMetadata);
|
||||
if (!string.IsNullOrEmpty(scope))
|
||||
{
|
||||
queryParamsDictionary["scope"] = scope!;
|
||||
|
|
@ -726,6 +727,37 @@ internal sealed partial class ClientOAuthProvider : McpHttpClient
|
|||
return _configuredScopes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Augments the scope parameter with <c>offline_access</c> if the authorization server advertises it in
|
||||
/// <c>scopes_supported</c> and it is not already present. This signals to OIDC-flavored authorization servers
|
||||
/// that the client desires a refresh token, per SEP-2207.
|
||||
/// </summary>
|
||||
private static string? AugmentScopeWithOfflineAccess(string? scope, AuthorizationServerMetadata authServerMetadata)
|
||||
{
|
||||
const string OfflineAccess = "offline_access";
|
||||
|
||||
if (authServerMetadata.ScopesSupported?.Contains(OfflineAccess) is not true)
|
||||
{
|
||||
return scope;
|
||||
}
|
||||
|
||||
if (scope is null)
|
||||
{
|
||||
return OfflineAccess;
|
||||
}
|
||||
|
||||
// Check if offline_access is already in the scope string (space-separated tokens).
|
||||
foreach (var token in scope.Split(' '))
|
||||
{
|
||||
if (token == OfflineAccess)
|
||||
{
|
||||
return scope;
|
||||
}
|
||||
}
|
||||
|
||||
return scope + " " + OfflineAccess;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the resource URI in the metadata exactly matches the original request URL as required by the RFC.
|
||||
/// Per RFC: The resource value must be identical to the URL that the client used to make the request to the resource server.
|
||||
|
|
|
|||
Loading…
Reference in a new issue