Esempio n. 1
0
        /// <summary>
        /// Validate entity
        /// </summary>
        public override void Validate()
        {
            IStringLocalizer <User> localizer = ServiceActivator.GetScope().ServiceProvider.GetService <IStringLocalizer <User> >();

            if (CreatedAuthor != null)
            {
                AddNotifications(CreatedAuthor.Notifications);
            }
            if (ChangedAuthor != null)
            {
                AddNotifications(ChangedAuthor.Notifications);
            }
            AddNotifications(Name.Notifications);
            AddNotifications(Email.Notifications);
            AddNotifications(new RequiredValidationContract <string>(Email?.Address, $"Email.{nameof(Email.Address)}", localizer["EMAIL_REQUIRED"]).Contract.Notifications);
            AddNotifications(new RequiredValidationContract <UserType?>(Type, nameof(Type), localizer["USER_TYPE_REQUIRED"]).Contract.Notifications);
            AddNotifications(new PastDateValidationContract(BornDate, "Born date", localizer["BORN_DATE_REQUIRED"]).Contract.Notifications);

            if (Credential != null)
            {
                Credential.Validate();
                AddNotifications(Credential.Notifications);
            }

            if ((Type ?? UserType.User) == UserType.User)
            {
                AddNotifications(new BrasilianCpfValidationContract(Document, nameof(Document), true).Contract.Notifications);
                if (!Scopes.Any())
                {
                    AddNotification(nameof(Scopes), localizer["USER_IN_ONE_SCOPE"]);
                }
                else
                {
                    Scopes
                    .ToList()
                    .ForEach(scope =>
                    {
                        scope.Validate();
                        AddNotifications(scope.Notifications);
                    });
                }

                if (Roles != null && !Roles.Any())
                {
                    Roles
                    .ToList()
                    .ForEach(role =>
                    {
                        role.Validate();
                        AddNotifications(role.Notifications);
                    });
                }
            }
        }
        /// <summary>
        /// LoginAsync provides entry point into the MicrosoftGraphService LoginAsync
        /// </summary>
        /// <returns>A MicrosoftGraphService reference</returns>
        public async Task <bool> LoginAsync()
        {
            // check inputs
            if (string.IsNullOrEmpty(ClientId))
            {
                // error
                return(false);
            }

            if (Scopes == null || !Scopes.Any())
            {
                // error
                return(false);
            }

            // Initialize the MicrosoftGraphService
            if (!MicrosoftGraphService.Instance.Initialize(ClientId, delegatedPermissionScopes: Scopes))
            {
                // error
                return(false);
            }

            // Attempt to LoginAsync
            try
            {
                await MicrosoftGraphService.Instance.LoginAsync().ConfigureAwait(false);
            }
            catch (Exception)
            {
                // error
                return(false);
            }

            // Initialize fields from the User's information from the Microsoft Graph
            var user = await MicrosoftGraphService.Instance.GraphProvider.Me.Request().GetAsync();

            DisplayName = user.DisplayName;
            JobTitle    = user.JobTitle;
            Email       = user.Mail;

            // get the profile picture
            using (var photoStream = await MicrosoftGraphService.Instance.GraphProvider.Me.Photo.Content.Request().GetAsync().ConfigureAwait(false))
            {
                if (photoStream != null)
                {
                    Photo = System.Drawing.Image.FromStream(photoStream);
                }
            }

            // return Microsoft.Graph.GraphServiceClient from the MicrosoftGraphService.Instance.GraphProvider
            GraphServiceClient = MicrosoftGraphService.Instance.GraphProvider;
            return(true);
        }
Esempio n. 3
0
        private string BuildScope()
        {
            var sb = new StringBuilder(OAuthScope.Basic);

            if (Scopes.Any())
            {
                foreach (var scope in Scopes)
                {
                    sb.Append("+");
                    sb.Append(scope);
                }
            }

            return(sb.ToString());
        }
Esempio n. 4
0
        /// <summary>
        /// Checks whether the configuration parameters have values.
        /// </summary>
        /// <exception cref="ArgumentNullException">
        /// Returns an ArgumentNullException if the parameter is empty or has white space
        /// </exception>
        public void CheckParameters()
        {
            string message = "Parameter missing value in the config file";

            if (string.IsNullOrWhiteSpace(ClientId))
            {
                throw new ArgumentNullException(nameof(ClientId), message);
            }
            if (string.IsNullOrWhiteSpace(TenantId))
            {
                throw new ArgumentNullException(nameof(TenantId), message);
            }
            if (string.IsNullOrWhiteSpace(Instance))
            {
                throw new ArgumentNullException(nameof(Instance), message);
            }
            if (!Scopes.Any())
            {
                throw new ArgumentNullException(nameof(Scopes), message);
            }
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            var useInteractiveLogin = true;
            var ilogger             = new DefaultUsageLogger(Information, Warning, Error);

            if (Scopes == null || !Scopes.Any())
            {
                useInteractiveLogin = false;
                Scopes = new string[] { AzureADConstants.MSGraphScope };
            }

            AuthenticationSettings.PostLogoutRedirectURI = this.ResourceUri ?? AzureADConstants.GraphResourceId;
            AuthenticationSettings.MSALScopes            = Scopes;

            // Get back the Access Token and the Refresh Token
            var AzureADCache = new AzureADv2TokenCache(AuthenticationSettings, ilogger, useInteractiveLogin);

            AzureADALConnection.CurrentConnection = new AzureADALConnection(AzureADCache, AuthenticationSettings, ilogger);

            // Write Tokens to Console
            WriteObject(AzureADALConnection.CurrentConnection.AuthenticationResult);
        }
Esempio n. 6
0
        public void DynamicScopeValidation(AuthScopes requiredScope, string accessToken = null)
        {
            if (Validators.SkipAccessTokenValidation)
            {
                return;
            }
            if (Validators.SkipDynamicScopeValidation || !string.IsNullOrWhiteSpace(accessToken))
            {
                return;
            }

            if (!Scopes.Contains(requiredScope) || requiredScope == AuthScopes.Any && Scopes.Any(x => x == AuthScopes.None))
            {
                throw new InvalidCredentialException($"The current access token ({String.Join(",", Scopes)}) does not support this call. Missing required scope: {requiredScope.ToString().ToLower()}. You can skip this check by using: TwitchLib.TwitchAPI.Settings.Validators.SkipDynamicScopeValidation = true . You can also generate a new token with this scope here: https://twitchtokengenerator.com");
            }
        }