Ejemplo n.º 1
0
        public async Task T2_CheckLoginWithToken()
        {
            // Arrange
            _factory.CreateClient(); // need to create a client for the server property to be available
            var server = _factory.Server;

            var connection1 = await StartConnectionAsync(server.CreateHandler());

            var connection2 = await StartConnectionAsync(server.CreateHandler());

            // Act
            var loginWithNameRes = await connection1.InvokeAsync <LoginResponse>("LoginWithName", "David GUETTA");

            await connection1.StopAsync();

            await Task.Delay(500);

            var jwt               = loginWithNameRes.Token;
            var parsedToken1      = JWTFactory.Parse(jwt);
            var loginWithTokenRes = await connection2.InvokeAsync <LoginResponse>("LoginWithToken", jwt);

            await connection2.StopAsync();

            // Assert
            loginWithTokenRes.Success.Should().Be(true);
            loginWithTokenRes.Name.Should().Be(parsedToken1.Name);
            var parsedToken2 = JWTFactory.Parse(loginWithTokenRes.Token);

            parsedToken2.Name.Should().Be(parsedToken1.Name);
            parsedToken2.Subject.Should().Be(parsedToken1.Subject);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Login the current client with a JWT.
        /// </summary>
        /// <param name="jwt">The JWT to login with</param>
        /// <returns>A LoginResponse with all data</returns>
        public async Task <LoginResponse> LoginWithToken(string jwt)
        {
            var userInfo = getUserInfo(Context);

            if (userInfo.Value.JwtID != null)
            {
                return(new LoginResponse {
                    Success = false
                });
            }

            if (!_jwtFactory.Verify(jwt))
            {
                return(new LoginResponse {
                    Success = false
                });
            }

            var parsedToken = JWTFactory.Parse(jwt);

            if (_revokedTokenIds.Contains(parsedToken.JwtID))
            {
                return(new LoginResponse {
                    Success = false
                });
            }

            return(await connectUser(Context, parsedToken, jwt));
        }
Ejemplo n.º 3
0
        public void T4_ParseJWT()
        {
            byte[] key     = { 0xDE, 0xAD, 0xBE, 0xEF };
            var    payload = new JWTPayload {
                Subject = "42"
            };

            var jwtFactory = new JWTFactory(key);
            var jwt        = jwtFactory.Generate(payload);

            var parsed = JWTFactory.Parse(jwt);

            parsed.Subject.Should().Be("42");
            parsed.JwtID.Should().BeNull();
        }
Ejemplo n.º 4
0
        public async Task T1_CheckLoginWithName()
        {
            // Arrange
            _factory.CreateClient(); // need to create a client for the server property to be available
            var server = _factory.Server;

            var connection1 = await StartConnectionAsync(server.CreateHandler());

            var connection2 = await StartConnectionAsync(server.CreateHandler());

            // Act
            var login11 = await connection1.InvokeAsync <LoginResponse>("LoginWithName", "David GUETTA");

            var login12 = await connection1.InvokeAsync <LoginResponse>("LoginWithName", "John DOE");

            var login21 = await connection2.InvokeAsync <LoginResponse>("LoginWithName", "Polo");

            await connection1.StopAsync();

            await connection2.StopAsync();

            // Assert
            login11.Success.Should().Be(true);
            login11.Name.Should().Be("David GUETTA");
            var parsedToken = JWTFactory.Parse(login11.Token);

            parsedToken.Name.Should().Be("David GUETTA");
            parsedToken.Subject.Should().NotBeNullOrWhiteSpace();

            // if a user is already connected, we won't accept a new login attempt
            login12.Success.Should().Be(false);

            var parsedToken2 = JWTFactory.Parse(login21.Token);

            // the subject must be different for every user
            parsedToken2.Subject.Should().NotBe(parsedToken.Subject);
        }