Ejemplo n.º 1
0
 public ConfigurationValidator(AppConfiguration configuration)
 {
     this.configuration = configuration ?? throw new System.ArgumentNullException(nameof(configuration));
 }
Ejemplo n.º 2
0
        private static async Task Run(AppConfiguration configuration)
        {
            var validator = new ConfigurationValidator(configuration);

            if (!validator.Validate())
            {
                return;
            }

            using (var httpClient = new HttpClient())
                using (var tokenProviderFactory = new LogicTokenProviderFactory(configuration.TokenProvider))
                {
                    var consentClient = new ConsentClient(httpClient, tokenProviderFactory, configuration.Consent);

                    var groups = await consentClient.GetAllConsentGroupsAsync().ConfigureAwait(false);

                    if (configuration.Consent.ConsentGroupId == Guid.Empty)
                    {
                        if (groups != null && groups.Count > 0)
                        {
                            if (groups.Count > 1)
                            {
                                Log.Error("There is more than one consent group defined for this subscription");
                                return;
                            }
                            else
                            {
                                configuration.Consent.ConsentGroupId = groups[0].Id.Value;
                            }
                        }
                        else
                        {
                            var memberTitle = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(configuration.Consent.ConsentMember);

                            var scopes = string.IsNullOrEmpty(configuration.ConsentScope)
                                        ? null
                                        : new List <string> {
                                configuration.ConsentScope
                            };

                            var newGroup = await consentClient.CreateConsentGroup(
                                $"{memberTitle} Sample Group",
                                new List <ConsentGroupMemberRequest>
                            {
                                new ConsentGroupMemberRequest
                                {
                                    Key            = configuration.Consent.ConsentMember,
                                    Name           = memberTitle,
                                    SubscriptionId = configuration.Consent.SubscriptionId,
                                    Roles          = new ConsentRolesRequestResponse
                                    {
                                        CanRead   = true,
                                        CanWrite  = true,
                                        CanDelete = true,
                                    },
                                },
                            },
                                configuration.ConsentKeyFormat,
                                scopes).ConfigureAwait(false);

                            Log.Information("Created consent group {Name} with id {Id}", newGroup.Name, newGroup.Id);

                            configuration.Consent.ConsentGroupId = newGroup.Id.Value;
                        }
                    }

                    var ownedGroup = groups.FirstOrDefault(x => x.Id == configuration.Consent.ConsentGroupId);
                    if (ownedGroup != null)
                    {
                        Log.Information(
                            "Consent Group {Id}, {Name} is managed by subscription {SubscriptionId}",
                            ownedGroup.Id,
                            ownedGroup.Name,
                            configuration.Consent.SubscriptionId);

                        var groupDetails = await consentClient.GetConsentGroupAsync(configuration.Consent.ConsentGroupId).ConfigureAwait(false);

                        Log.Information("Consent group data: {@Details}", groupDetails);
                    }

                    Log.Information("Fetching consent for {Key}", configuration.ConsentKey);

                    var details = await consentClient.GetConsentAsync(configuration.ConsentKey, configuration.ConsentScope).ConfigureAwait(false);

                    Log.Information("Consent data: {@Details}", details);

                    if (details == null)
                    {
                        await consentClient.SaveConsentAsync(configuration.ConsentKey).ConfigureAwait(false);

                        Log.Information("Created new consent for key {Key}", configuration.ConsentKey);
                    }

                    if (ownedGroup != null)
                    {
                        var consentReview = await consentClient.ReviewConsentAsync(configuration.ConsentKey).ConfigureAwait(false);

                        if (consentReview != null)
                        {
                            Log.Information("Found consent definition {@Definition}", consentReview);
                        }
                        else
                        {
                            Log.Error("Unable to find consent details for key {Key}", configuration.ConsentKey);
                        }
                    }

                    var deleted = await consentClient.DeleteConsent(configuration.ConsentKey).ConfigureAwait(false);

                    if (deleted)
                    {
                        Log.Information("Consent for key {Key} was revoked", configuration.ConsentKey);
                    }
                    else
                    {
                        Log.Error("Unable to revoke consent details for key {Key}", configuration.ConsentKey);
                    }

                    if (ownedGroup != null)
                    {
                        var consentReview = await consentClient.ReviewConsentAsync(configuration.ConsentKey).ConfigureAwait(false);

                        if (consentReview != null)
                        {
                            Log.Error("Unexpectedly found consent definition {@Definition}", consentReview);
                        }
                        else
                        {
                            Log.Information("Consent details for key {Key} not longer exist, as expected", configuration.ConsentKey);
                        }
                    }
                }
        }