public void SchemeTypeKindAndOAuthTypeSetCorrectly()
        {
            // Arrange
            OAuth2ClientCredentials credentials = new OAuth2ClientCredentials();

            // Act & Assert
            Assert.Equal(SecuritySchemeType.OAuth2, credentials.SchemeType);
            Assert.Equal(OAuth2Type.ClientCredentials, credentials.OAuth2Type);
        }
        public static async void ShowOAuthView(OAuth2ClientCredentials oAuth)
        {
            var success = await oAuth.InvokeUserAuthorization();

            if (success)
            {
                OnAuthenticated?.Invoke(oAuth, new AuthenticatedEventArgs(oAuth.AccessToken.Code, oAuth.AccessToken.RefreshToken, oAuth.AccessToken.Expires));
            }
        }
        private static void AppendOAuth2(OpenApiSecurityScheme scheme, OAuthAuthorization oAuth2)
        {
            Debug.Assert(scheme != null);
            Debug.Assert(oAuth2 != null);

            scheme.Flows = new OpenApiOAuthFlows();
            OpenApiOAuthFlow flow = null;

            switch (oAuth2.OAuth2Type)
            {
            case OAuth2Type.AuthCode:     // AuthCode
                OAuth2AuthCode authCode = (OAuth2AuthCode)oAuth2;
                flow = new OpenApiOAuthFlow
                {
                    AuthorizationUrl = new Uri(authCode.AuthorizationUrl),
                    TokenUrl         = new Uri(authCode.TokenUrl)
                };
                scheme.Flows.AuthorizationCode = flow;
                break;

            case OAuth2Type.Pasword:     // Password
                OAuth2Password password = (OAuth2Password)oAuth2;
                flow = new OpenApiOAuthFlow
                {
                    TokenUrl = new Uri(password.TokenUrl)
                };
                scheme.Flows.Password = flow;
                break;

            case OAuth2Type.Implicit:     // Implicit
                OAuth2Implicit @implicit = (OAuth2Implicit)oAuth2;
                flow = new OpenApiOAuthFlow
                {
                    AuthorizationUrl = new Uri(@implicit.AuthorizationUrl)
                };
                scheme.Flows.Implicit = flow;
                break;

            case OAuth2Type.ClientCredentials:     // ClientCredentials
                OAuth2ClientCredentials credentials = (OAuth2ClientCredentials)oAuth2;
                flow = new OpenApiOAuthFlow
                {
                    TokenUrl = new Uri(credentials.TokenUrl)
                };
                scheme.Flows.ClientCredentials = flow;
                break;
            }

            Debug.Assert(flow != null);
            flow.RefreshUrl = new Uri(oAuth2.RefreshUrl);

            if (oAuth2.Scopes != null)
            {
                flow.Scopes = oAuth2.Scopes.ToDictionary(s => s.Scope, s => s.Description);
            }
        }
        public void InitializeOAuth2ClientCredentialsWithRecordSuccess()
        {
            // Arrange
            IEdmRecordExpression record = new EdmRecordExpression(
                new EdmPropertyConstructor("TokenUrl", new EdmStringConstant("http://tokenUrl")));

            OAuth2ClientCredentials credentials = new OAuth2ClientCredentials();

            Assert.Null(credentials.Name);
            Assert.Null(credentials.Description);
            Assert.Null(credentials.Scopes);
            Assert.Null(credentials.TokenUrl);

            // Act
            credentials.Initialize(record);

            // Assert
            Assert.Null(credentials.Name);
            Assert.Null(credentials.Description);
            Assert.Null(credentials.Scopes);
            Assert.Equal("http://tokenUrl", credentials.TokenUrl);
        }
        public void InitializeOAuth2ClientCredentialsWorksWithCsdl()
        {
            // Arrange
            string annotation = @"<Annotation Term=""NS.MyOAuth2ClientCredentials"">
                <Record >
                  <PropertyValue Property=""TokenUrl"" String=""http://tokenUrl"" />
                </Record>
              </Annotation>";

            IEdmModel model = GetEdmModel(annotation);

            Assert.NotNull(model); // guard
            Assert.NotNull(model.EntityContainer);

            // Act
            OAuth2ClientCredentials credentials = model.GetRecord <OAuth2ClientCredentials>(model.EntityContainer, "NS.MyOAuth2ClientCredentials");

            // Assert
            Assert.Null(credentials.Name);
            Assert.Null(credentials.Description);
            Assert.Null(credentials.Scopes);
            Assert.Equal("http://tokenUrl", credentials.TokenUrl);
        }