in src/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration/Extensions/XElementExtensions.cs [307:388]
private static OpenApiOAuthFlow ToOAuthFlow(this XElement element, string flowType,
out IList<string> scopeNames)
{
var oAuthFlow = new OpenApiOAuthFlow();
scopeNames = new List<string>();
var authorizationUrl = element.Elements()
.FirstOrDefault(p => p.Name == KnownXmlStrings.AuthorizationUrl)?.Value;
var refreshUrl = element.Elements()
.FirstOrDefault(p => p.Name == KnownXmlStrings.RefreshUrl)?.Value;
var tokenUrl = element.Elements()
.FirstOrDefault(p => p.Name == KnownXmlStrings.TokenUrl)?.Value;
if (flowType == KnownXmlStrings.ImplicitFlow || flowType == KnownXmlStrings.AuthorizationCode)
{
if (authorizationUrl == null)
{
throw new InvalidSecurityTagException(string.Format(
SpecificationGenerationMessages.UndocumentedAuthorizationUrl,
flowType));
}
oAuthFlow.AuthorizationUrl = new Uri(authorizationUrl);
}
if (flowType == KnownXmlStrings.Password
|| flowType == KnownXmlStrings.AuthorizationCode
|| flowType == KnownXmlStrings.ClientCredentials)
{
if (tokenUrl == null)
{
throw new InvalidSecurityTagException(string.Format(
SpecificationGenerationMessages.UndocumentedTokenUrl,
flowType));
}
oAuthFlow.TokenUrl = new Uri(tokenUrl);
}
if (refreshUrl != null)
{
oAuthFlow.RefreshUrl = new Uri(refreshUrl);
}
var scopeElements = element.Elements()
.Where(p => p.Name == KnownXmlStrings.Scope);
if (!scopeElements.Any())
{
throw new InvalidSecurityTagException(string.Format(
SpecificationGenerationMessages.UndocumentedScopeForFlow,
flowType));
}
foreach (var scopeElement in scopeElements)
{
var name = scopeElement.Attribute(KnownXmlStrings.Name)?.Value;
if (string.IsNullOrWhiteSpace(name))
{
throw new InvalidSecurityTagException(string.Format(
SpecificationGenerationMessages.UndocumentedName,
KnownXmlStrings.Scope));
}
var description = scopeElement.Elements().FirstOrDefault(p => p.Name == KnownXmlStrings.Description)
?.Value;
if (string.IsNullOrWhiteSpace(description))
{
throw new InvalidSecurityTagException(string.Format(
SpecificationGenerationMessages.UndocumentedDescription,
KnownXmlStrings.Scope));
}
scopeNames.Add(name);
oAuthFlow.Scopes.Add(name, description);
}
return oAuthFlow;
}