3
3
using Microsoft . OpenApi . Models ;
4
4
using System . Net . Http ;
5
5
using System . Threading . Tasks ;
6
+ using System . Linq ;
6
7
7
8
namespace Microsoft . Azure . Management . ApiManagement . ArmTemplates
8
9
{
@@ -30,13 +31,13 @@ public async Task<APITemplate> CreateAPITemplateAsync(OpenApiDocument doc, CLICr
30
31
apiRevision = cliArguments . apiRevision ?? "" ,
31
32
apiVersionSetId = cliArguments . apiVersionSetId ?? "" ,
32
33
path = cliArguments . path ?? "" ,
34
+ authenticationSettings = CreateAuthenticationSettings ( doc ) ,
33
35
// assumptions
34
36
type = "http" ,
35
37
apiType = "http" ,
36
38
wsdlSelector = null ,
37
39
38
40
// unfinished
39
- authenticationSettings = CreateAuthenticationSettings ( doc ) ,
40
41
apiRevisionDescription = null ,
41
42
apiVersionDescription = null ,
42
43
apiVersionSet = null
@@ -58,33 +59,64 @@ public async Task<APITemplate> CreateAPITemplateAsync(OpenApiDocument doc, CLICr
58
59
59
60
public APITemplateAuthenticationSettings CreateAuthenticationSettings ( OpenApiDocument doc )
60
61
{
61
- //unfinished
62
+ // initialize subscriptionKeyRequired with value from IsSubscriptionRequired
62
63
APITemplateAuthenticationSettings authenticationSettings = new APITemplateAuthenticationSettings ( )
63
64
{
64
- subscriptionKeyRequired = false
65
+ subscriptionKeyRequired = IsSubscriptionRequired ( doc )
65
66
} ;
66
67
foreach ( OpenApiSecurityScheme securityScheme in doc . Components . SecuritySchemes . Values )
67
68
{
68
69
if ( securityScheme . Type == SecuritySchemeType . OAuth2 )
69
70
{
70
- authenticationSettings . oAuth2 = new APITemplateOAuth2 ( )
71
- {
72
- authorizationServerId = null ,
73
- scope = null
74
- } ;
71
+ authenticationSettings . oAuth2 = CreateOAuth2 ( securityScheme ) ;
75
72
}
76
73
else if ( securityScheme . Type == SecuritySchemeType . OpenIdConnect )
77
74
{
75
+ // the bearer format property only appears in Open API specs for SecuritySchemeType.Http and will never appear for OpenIDConnect
78
76
authenticationSettings . openid = new APITemplateOpenID ( )
79
77
{
80
- openidProviderId = null ,
78
+ openidProviderId = securityScheme . OpenIdConnectUrl . ToString ( ) ,
81
79
bearerTokenSendingMethods = new string [ ] { }
82
80
} ;
83
81
}
82
+ else if ( securityScheme . Type == SecuritySchemeType . ApiKey )
83
+ {
84
+ authenticationSettings . subscriptionKeyRequired = true ;
85
+ }
84
86
} ;
85
87
return authenticationSettings ;
86
88
}
87
89
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
+
88
120
public async Task < string > CreateOpenAPISpecContentsAsync ( CLICreatorArguments cliArguments )
89
121
{
90
122
if ( cliArguments . openAPISpecFile != null )
0 commit comments