예제 #1
0
        public async Task <IActionResult> LoginUser([FromBody] User user)
        {
            if (await IsUserLogged())
            {
                return(BadRequest("You are already logged in"));
            }
            var loginUser = await tokenRepository.LogIn(user);

            if (loginUser == null)
            {
                return(BadRequest("Invalid username or password, please try again"));
            }

            string token = TokenActions.GenerateToken(loginUser, configuration);

            Console.WriteLine(token);

            loginUser.Token = token;
            await tokenRepository.SaveToken(loginUser.IdUser, token);

            // SET COOKIES USER ID AND USER TOKEN
            Cookie cookieToken = CookieActions.SetCookie("session-id", token, 1);

            Response.Cookies.Append(cookieToken.Key, cookieToken.Value, cookieToken.Option);

            Cookie cookieUserId = CookieActions.SetCookie("user-id", loginUser.IdUser.ToString(), 1);

            Response.Cookies.Append(cookieUserId.Key, cookieUserId.Value, cookieUserId.Option);

            var userLogin = mapper.Map <UserDto>(loginUser);

            return(Ok(userLogin));
        }
예제 #2
0
        public TokenActionsFixture()
        {
            var          eventPublisher = new Mock <IEventPublisher>();
            const string scope          = "valid_scope";
            var          mock           = new Mock <IClientStore>();

            mock.Setup(x => x.GetById(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(
                new Client
            {
                JsonWebKeys =
                    "supersecretlongkey".CreateJwk(
                        JsonWebKeyUseNames.Sig,
                        KeyOperations.Sign,
                        KeyOperations.Verify)
                    .ToSet(),
                IdTokenSignedResponseAlg = SecurityAlgorithms.RsaSha256,
                ClientId = ClientId,
                Secrets  =
                    new[] { new ClientSecret {
                                Type = ClientSecretTypes.SharedSecret, Value = Clientsecret
                            } },
                AllowedScopes = new[] { scope },
                ResponseTypes = new[] { ResponseTypeNames.Token },
                GrantTypes    = new[] { GrantTypes.ClientCredentials }
            });

            _tokenActions = new TokenActions(
                new RuntimeSettings(string.Empty),
                new Mock <IAuthorizationCodeStore>().Object,
                mock.Object,
                new Mock <IScopeRepository>().Object,
                new InMemoryJwksRepository(),
                new InMemoryResourceOwnerRepository(string.Empty),
                Array.Empty <IAuthenticateResourceOwnerService>(),
                eventPublisher.Object,
                new Mock <ITokenStore>().Object,
                new Mock <IDeviceAuthorizationStore>().Object,
                new Mock <ILogger <TokenController> >().Object);
        }
예제 #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TokenController"/> class.
 /// </summary>
 /// <param name="settings">The settings.</param>
 /// <param name="authorizationCodeStore">The authorization code store.</param>
 /// <param name="clientStore">The client store.</param>
 /// <param name="scopeRepository">The scope repository.</param>
 /// <param name="resourceOwnerRepository"></param>
 /// <param name="authenticateResourceOwnerServices">The authenticate resource owner services.</param>
 /// <param name="tokenStore">The token store.</param>
 /// <param name="ticketStore">The ticket store.</param>
 /// <param name="jwksStore"></param>
 /// <param name="resourceSetRepository">The resource set repository.</param>
 /// <param name="deviceAuthorizationStore">The device authorization store.</param>
 /// <param name="eventPublisher">The event publisher.</param>
 /// <param name="logger">The logger.</param>
 public TokenController(
     RuntimeSettings settings,
     IAuthorizationCodeStore authorizationCodeStore,
     IClientStore clientStore,
     IScopeRepository scopeRepository,
     IResourceOwnerRepository resourceOwnerRepository,
     IEnumerable <IAuthenticateResourceOwnerService> authenticateResourceOwnerServices,
     ITokenStore tokenStore,
     ITicketStore ticketStore,
     IJwksStore jwksStore,
     IResourceSetRepository resourceSetRepository,
     IDeviceAuthorizationStore deviceAuthorizationStore,
     IEventPublisher eventPublisher,
     ILogger <TokenController> logger)
 {
     _logger       = logger;
     _tokenActions = new TokenActions(
         settings,
         authorizationCodeStore,
         clientStore,
         scopeRepository,
         jwksStore,
         resourceOwnerRepository,
         authenticateResourceOwnerServices,
         eventPublisher,
         tokenStore,
         deviceAuthorizationStore,
         logger);
     _umaTokenActions = new UmaTokenActions(
         ticketStore,
         settings,
         clientStore,
         scopeRepository,
         tokenStore,
         resourceSetRepository,
         jwksStore,
         eventPublisher,
         logger);
 }