Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit cd8b72b

Browse files
committed
determine authentication settings from open api spec
1 parent 32636e0 commit cd8b72b

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

src/APIM_ARMTemplate/apimtemplate/TemplateCreators/APITemplateCreator.cs

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.OpenApi.Models;
44
using System.Net.Http;
55
using System.Threading.Tasks;
6+
using System.Linq;
67

78
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
89
{
@@ -30,13 +31,13 @@ public async Task<APITemplate> CreateAPITemplateAsync(OpenApiDocument doc, CLICr
3031
apiRevision = cliArguments.apiRevision ?? "",
3132
apiVersionSetId = cliArguments.apiVersionSetId ?? "",
3233
path = cliArguments.path ?? "",
34+
authenticationSettings = CreateAuthenticationSettings(doc),
3335
// assumptions
3436
type = "http",
3537
apiType = "http",
3638
wsdlSelector = null,
3739

3840
// unfinished
39-
authenticationSettings = CreateAuthenticationSettings(doc),
4041
apiRevisionDescription = null,
4142
apiVersionDescription = null,
4243
apiVersionSet = null
@@ -58,33 +59,64 @@ public async Task<APITemplate> CreateAPITemplateAsync(OpenApiDocument doc, CLICr
5859

5960
public APITemplateAuthenticationSettings CreateAuthenticationSettings(OpenApiDocument doc)
6061
{
61-
//unfinished
62+
// initialize subscriptionKeyRequired with value from IsSubscriptionRequired
6263
APITemplateAuthenticationSettings authenticationSettings = new APITemplateAuthenticationSettings()
6364
{
64-
subscriptionKeyRequired = false
65+
subscriptionKeyRequired = IsSubscriptionRequired(doc)
6566
};
6667
foreach (OpenApiSecurityScheme securityScheme in doc.Components.SecuritySchemes.Values)
6768
{
6869
if (securityScheme.Type == SecuritySchemeType.OAuth2)
6970
{
70-
authenticationSettings.oAuth2 = new APITemplateOAuth2()
71-
{
72-
authorizationServerId = null,
73-
scope = null
74-
};
71+
authenticationSettings.oAuth2 = CreateOAuth2(securityScheme);
7572
}
7673
else if (securityScheme.Type == SecuritySchemeType.OpenIdConnect)
7774
{
75+
// the bearer format property only appears in Open API specs for SecuritySchemeType.Http and will never appear for OpenIDConnect
7876
authenticationSettings.openid = new APITemplateOpenID()
7977
{
80-
openidProviderId = null,
78+
openidProviderId = securityScheme.OpenIdConnectUrl.ToString(),
8179
bearerTokenSendingMethods = new string[] { }
8280
};
8381
}
82+
else if (securityScheme.Type == SecuritySchemeType.ApiKey)
83+
{
84+
authenticationSettings.subscriptionKeyRequired = true;
85+
}
8486
};
8587
return authenticationSettings;
8688
}
8789

90+
public APITemplateOAuth2 CreateOAuth2(OpenApiSecurityScheme scheme)
91+
{
92+
APITemplateOAuth2 oAuth2 = new APITemplateOAuth2()
93+
{
94+
authorizationServerId = "",
95+
scope = ""
96+
};
97+
if (scheme.Flows.Implicit != null)
98+
{
99+
oAuth2.authorizationServerId = scheme.Flows.Implicit.AuthorizationUrl != null ? scheme.Flows.Implicit.AuthorizationUrl.ToString() : "";
100+
oAuth2.scope = scheme.Flows.Implicit.Scopes != null && scheme.Flows.Implicit.Scopes.Keys.FirstOrDefault() != null ? oAuth2.scope = scheme.Flows.Implicit.Scopes.Keys.FirstOrDefault() : "";
101+
}
102+
else if (scheme.Flows.AuthorizationCode != null)
103+
{
104+
oAuth2.authorizationServerId = scheme.Flows.AuthorizationCode.AuthorizationUrl != null ? scheme.Flows.AuthorizationCode.AuthorizationUrl.ToString() : "";
105+
oAuth2.scope = scheme.Flows.AuthorizationCode.Scopes != null && scheme.Flows.AuthorizationCode.Scopes.Keys.FirstOrDefault() != null ? oAuth2.scope = scheme.Flows.AuthorizationCode.Scopes.Keys.FirstOrDefault() : "";
106+
}
107+
else if (scheme.Flows.ClientCredentials != null)
108+
{
109+
oAuth2.authorizationServerId = scheme.Flows.ClientCredentials.AuthorizationUrl != null ? scheme.Flows.ClientCredentials.AuthorizationUrl.ToString() : "";
110+
oAuth2.scope = scheme.Flows.ClientCredentials.Scopes != null && scheme.Flows.ClientCredentials.Scopes.Keys.FirstOrDefault() != null ? oAuth2.scope = scheme.Flows.ClientCredentials.Scopes.Keys.FirstOrDefault() : "";
111+
}
112+
else if (scheme.Flows.Password != null)
113+
{
114+
oAuth2.authorizationServerId = scheme.Flows.Password.AuthorizationUrl != null ? scheme.Flows.Password.AuthorizationUrl.ToString() : "";
115+
oAuth2.scope = scheme.Flows.Password.Scopes != null && scheme.Flows.Password.Scopes.Keys.FirstOrDefault() != null ? oAuth2.scope = scheme.Flows.Password.Scopes.Keys.FirstOrDefault() : "";
116+
}
117+
return oAuth2;
118+
}
119+
88120
public async Task<string> CreateOpenAPISpecContentsAsync(CLICreatorArguments cliArguments)
89121
{
90122
if (cliArguments.openAPISpecFile != null)

0 commit comments

Comments
 (0)