public async override Task <BitJwtToken> LocalLogin(LocalAuthenticationContext context, CancellationToken cancellationToken)
        {
            if (context.SignInMessage.TryGetValueFromAcr("x", out string x))
            {
                Logger.AddLogData("x", x);
            }

            if (context.SignInMessage.TryGetValueFromAcr("y", out string y))
            {
                Logger.AddLogData("y", y);
            }

            Logger.AddLogData("username", context.UserName);
            Logger.AddLogData("password", context.Password);

            LocalUser user = _localUsers.SingleOrDefault(u => u.UserId == context.UserName && u.Password == context.Password);

            if (user == null)
            {
                throw new DomainLogicException("LoginFailed");
            }

            BitJwtToken jwtToken = new BitJwtToken {
                UserId = user.UserId
            };

            jwtToken.CustomProps.Add("custom-data", "test");
            return(jwtToken);
        }
예제 #2
0
        public virtual async Task AuthenticateLocalAsync(LocalAuthenticationContext context, CancellationToken cancellationToken)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            try
            {
                if (context.AuthenticateResult == null)
                {
                    BitJwtToken bitJwtToken = await LocalLogin(context, cancellationToken).ConfigureAwait(false);

                    AuthenticateResult result = new AuthenticateResult(bitJwtToken.UserId, bitJwtToken.UserId,
                                                                       BuildClaimsFromBitJwtToken(bitJwtToken),
                                                                       authenticationMethod: "custom");

                    context.AuthenticateResult = result;
                }
            }
            catch (Exception ex)
            {
                ScopeStatusManager.MarkAsFailed("LocalLogin_Failed");
                if (context.AuthenticateResult == null && ExceptionToHttpErrorMapper.IsKnownError(ex))
                {
                    context.AuthenticateResult = new AuthenticateResult(ExceptionToHttpErrorMapper.GetMessage(ex));
                }
                else
                {
                    throw;
                }
            }

            await base.AuthenticateLocalAsync(context).ConfigureAwait(false);
        }
        public static void LogUserInformation(ILogger logger, IUserInformationProvider userInformationProvider)
        {
            if (userInformationProvider == null)
            {
                throw new ArgumentNullException(nameof(userInformationProvider));
            }

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            if (userInformationProvider.IsAuthenticated())
            {
                BitJwtToken bitJwtToken = userInformationProvider.GetBitJwtToken();
                logger.AddLogData("UserId", bitJwtToken.UserId);
                if (bitJwtToken.CustomProps != null)
                {
                    foreach (var keyVal in bitJwtToken.CustomProps)
                    {
                        logger.AddLogData(keyVal.Key, keyVal.Value);
                    }
                }
                logger.AddLogData("AuthenticationType", userInformationProvider.GetAuthenticationType());
                logger.AddLogData("ClientId", userInformationProvider.GetClientId());
            }
        }
예제 #4
0
        public async override Task <bool> UserIsActiveAsync(IsActiveContext context, BitJwtToken jwtToken, CancellationToken cancellationToken)
        {
            Guid userIdAsGuid = Guid.Parse(jwtToken.UserId);

            return(await(await UsersRepository.GetAllAsync(cancellationToken))
                   .AnyAsync(u => u.Id == userIdAsGuid));
        }
예제 #5
0
        public virtual Task GetProfileDataAsync(ProfileDataRequestContext context, CancellationToken cancellationToken)
        {
            BitJwtToken bitJwtToken = BitJwtToken.FromJson(context.Subject.Claims.GetClaimValue("primary_sid"));

            context.IssuedClaims = BuildClaimsFromBitJwtToken(bitJwtToken);

            return(base.GetProfileDataAsync(context));
        }
예제 #6
0
        protected virtual List <Claim> BuildClaimsFromBitJwtToken(BitJwtToken bitJwtToken)
        {
            Claim primary_sid = new Claim("primary_sid", BitJwtToken.ToJson(bitJwtToken));

            List <Claim> claims = new List <Claim>
            {
                primary_sid
            };

            return(claims);
        }
예제 #7
0
        public override Task <bool> UserIsActiveAsync(IsActiveContext context, BitJwtToken bitJwtToken, CancellationToken cancellationToken)
        {
            string activeDirectoryName = AppEnvironment.GetConfig <string>(AppEnvironment.KeyValues.IdentityServer.ActiveDirectoryName);

            using (PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, activeDirectoryName))
            {
                using (UserPrincipal user = UserPrincipal.FindByIdentity(principalContext, IdentityType.SamAccountName, context.Subject.Identity.Name))
                {
                    return(Task.FromResult(user?.Enabled != null && user.Enabled.Value));
                }
            }
        }
예제 #8
0
        public virtual Task GetProfileDataAsync(ProfileDataRequestContext context, CancellationToken cancellationToken)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            BitJwtToken bitJwtToken = BitJwtToken.FromJson(context.Subject.Claims.GetClaimValue("primary_sid") ?? throw new InvalidOperationException("primary_sid could not be found"));

            context.IssuedClaims = BuildClaimsFromBitJwtToken(bitJwtToken);

            return(base.GetProfileDataAsync(context));
        }
예제 #9
0
        public async override Task <BitJwtToken> LocalLogin(LocalAuthenticationContext context, CancellationToken cancellationToken)
        {
            LocalUser user = _localUsers.SingleOrDefault(u => u.UserId == context.UserName && u.Password == context.Password);

            if (user == null)
            {
                throw new DomainLogicException("LoginFailed");
            }

            BitJwtToken jwtToken = new BitJwtToken {
                UserId = user.UserId
            };

            jwtToken.CustomProps.Add("custom-data", "test");
            return(jwtToken);
        }
        public virtual BitJwtToken GetBitJwtToken()
        {
            if (!IsLoggedIn())
            {
                throw new InvalidOperationException("User is not logged in.");
            }

            Token token = GetCurrentToken() !;

            var handler = new JwtSecurityTokenHandler();

            var jwtToken = (JwtSecurityToken)handler.ReadToken(token.access_token);

            var primary_sid = jwtToken.Claims.First(c => c.Type == "primary_sid").Value;

            return(BitJwtToken.FromJson(primary_sid));
        }
        public virtual async Task <BitJwtToken> GetBitJwtTokenAsync(CancellationToken cancellationToken)
        {
            if (!await IsLoggedInAsync(cancellationToken).ConfigureAwait(false))
            {
                throw new InvalidOperationException("User is not logged in.");
            }

            Token token = (await GetCurrentTokenAsync(cancellationToken).ConfigureAwait(false)) !;

            var handler = new JwtSecurityTokenHandler();

            var jwtToken = (JwtSecurityToken)handler.ReadToken(token.access_token);

            var primary_sid = jwtToken.Claims.First(c => c.Type == "primary_sid").Value;

            return(BitJwtToken.FromJson(primary_sid));
        }
예제 #12
0
        public virtual async Task AuthenticateExternalAsync(ExternalAuthenticationContext context, CancellationToken cancellationToken)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.AuthenticateResult == null)
            {
                BitJwtToken jwtToken = await ExternalLogin(context, cancellationToken).ConfigureAwait(false);

                AuthenticateResult result = new AuthenticateResult(jwtToken.UserId, jwtToken.UserId,
                                                                   BuildClaimsFromBitJwtToken(jwtToken),
                                                                   authenticationMethod: context.ExternalIdentity.Provider);

                context.AuthenticateResult = result;
            }

            await base.AuthenticateExternalAsync(context).ConfigureAwait(false);
        }
예제 #13
0
 public virtual Task <bool> UserIsActiveAsync(IsActiveContext context, BitJwtToken jwtToken, CancellationToken cancellationToken)
 {
     return(Task.FromResult(true));
 }
 public override async Task <bool> UserIsActiveAsync(IsActiveContext context, BitJwtToken bitJwtToken, CancellationToken cancellationToken)
 {
     return(_localUsers.Any(u => u.UserId == bitJwtToken.UserId));
 }