private IEnumerable <Claim> BuildClaims(HostContext context)
        {
            // Pass appname through jwt token to client, so that when client establishes connection with service, it will also create a corresponding AppName-connection
            yield return(new Claim(Constants.ClaimType.AppName, _appName));

            var user   = new Owin.OwinContext(context.Environment).Authentication?.User;
            var userId = UserIdProvider?.GetUserId(context.Request);

            var claims = ClaimsUtility.BuildJwtClaims(user, userId, null);

            foreach (var claim in claims)
            {
                yield return(claim);
            }
        }
        private IEnumerable <Claim> BuildClaims(IOwinContext owinContext, IRequest request)
        {
            // Pass appname through jwt token to client, so that when client establishes connection with service, it will also create a corresponding AppName-connection
            yield return(new Claim(Constants.ClaimType.AppName, _appName));

            var user   = owinContext.Authentication?.User;
            var userId = _provider?.GetUserId(request);

            var claims = ClaimsUtility.BuildJwtClaims(user, userId, GetClaimsProvider(owinContext));

            foreach (var claim in claims)
            {
                yield return(claim);
            }
        }
Пример #3
0
        private IEnumerable <Claim> BuildClaims(IOwinContext owinContext, IRequest request)
        {
            // Pass appname through jwt token to client, so that when client establishes connection with service, it will also create a corresponding AppName-connection
            yield return(new Claim(Constants.ClaimType.AppName, _appName));

            var user   = owinContext.Authentication?.User;
            var userId = _provider?.GetUserId(request);
            var claims = ClaimsUtility.BuildJwtClaims(user, userId, GetClaimsProvider(owinContext), _serverName, _mode, _enableDetailedErrors, _endpointsCount, _maxPollInterval, IsDiagnosticClient(owinContext));

            yield return(new Claim(Constants.ClaimType.Version, AssemblyVersion));

            foreach (var claim in claims)
            {
                yield return(claim);
            }
        }
Пример #4
0
        public async Task <NegotiationResponse> NegotiateAsync(string hubName, NegotiationOptions negotiationOptions, CancellationToken cancellationToken = default)
        {
            negotiationOptions ??= NegotiationOptions.Default;
            var httpContext          = negotiationOptions.HttpContext;
            var userId               = negotiationOptions.UserId;
            var claims               = negotiationOptions.Claims;
            var isDiagnosticClient   = negotiationOptions.IsDiagnosticClient;
            var enableDetailedErrors = negotiationOptions.EnableDetailedErrors;
            var lifetime             = negotiationOptions.TokenLifetime;

            try
            {
                if (cancellationToken == default && httpContext != null)
                {
                    cancellationToken = httpContext.RequestAborted;
                }

                var candidateEndpoints = _serviceEndpointManager.GetEndpoints(hubName);
                var selectedEndpoint   = _router.GetNegotiateEndpoint(httpContext, candidateEndpoints);
                var provider           = _serviceEndpointManager.GetEndpointProvider(selectedEndpoint);

                Func <IEnumerable <Claim> > claimProvider = null;
                if (claims != null)
                {
                    claimProvider = () => claims;
                }
                var claimsWithUserId = ClaimsUtility.BuildJwtClaims(httpContext?.User, userId: userId, claimProvider, enableDetailedErrors: enableDetailedErrors, isDiagnosticClient: isDiagnosticClient);

                var tokenTask = provider.GenerateClientAccessTokenAsync(hubName, claimsWithUserId, lifetime);
                await tokenTask.OrTimeout(cancellationToken, Timeout, GeneratingTokenTaskDescription);

                return(new NegotiationResponse
                {
                    Url = provider.GetClientEndpoint(hubName, null, null),
                    AccessToken = tokenTask.Result
                });
            }
            catch (Exception e) when(e is OperationCanceledException || e is TimeoutException)
            {
                throw new AzureSignalRException(ErrorMsg, e);
            }
        }
Пример #5
0
        public async Task GenerateClientEndpoint(string userId, Claim[] claims, string appName)
        {
            var endpoints  = FakeEndpointUtils.GetFakeEndpoint(3).ToArray();
            var routerMock = new Mock <IEndpointRouter>();

            routerMock.SetupSequence(router => router.GetNegotiateEndpoint(null, endpoints))
            .Returns(endpoints[0])
            .Returns(endpoints[1])
            .Returns(endpoints[2]);
            var router   = routerMock.Object;
            var provider = new ServiceCollection().AddSignalRServiceManager()
                           .Configure <ServiceManagerOptions>(o =>
            {
                o.ApplicationName  = appName;
                o.ServiceEndpoints = endpoints;
            })
                           .AddSingleton(router).BuildServiceProvider();
            var negotiateProcessor = provider.GetRequiredService <NegotiateProcessor>();

            for (int i = 0; i < 3; i++)
            {
                var negotiationResponse = await negotiateProcessor.NegotiateAsync(HubName, null, userId, claims, _tokenLifeTime);

                var tokenString = negotiationResponse.AccessToken;
                var token       = JwtTokenHelper.JwtHandler.ReadJwtToken(tokenString);

                string expectedToken = JwtTokenHelper.GenerateJwtBearer(ClientEndpointUtils.GetExpectedClientEndpoint(HubName, appName, endpoints[i].Endpoint), ClaimsUtility.BuildJwtClaims(null, userId, () => claims), token.ValidTo, token.ValidFrom, token.ValidFrom, endpoints[i].AccessKey);

                Assert.Equal(ClientEndpointUtils.GetExpectedClientEndpoint(HubName, appName, endpoints[i].Endpoint), negotiationResponse.Url);
                Assert.Equal(expectedToken, tokenString);
            }
        }