示例#1
0
        public static async Task OnCreatingTicket(OAuthCreatingTicketContext context)
        {
            var logger = GetLogger(context.HttpContext.RequestServices);

            AuthenticationLogMessages.AuthenticationTicketCreationHandling(logger);

            var userId = ulong.Parse(context.Identity.Claims
                                     .First(x => x.Type == ClaimTypes.NameIdentifier)
                                     .Value);

            var ticket = await context.HttpContext.RequestServices
                         .GetRequiredService <IAuthenticationService>()
                         .OnSignInAsync(
                userId: userId,
                username: context.Identity.Claims
                .First(x => x.Type == ClaimTypes.Name)
                .Value,
                discriminator: context.Identity.Claims
                .First(x => x.Type == ApiAuthenticationDefaults.DiscriminatorClaimType)
                .Value,
                avatarHash: context.Identity.Claims
                .First(x => x.Type == ApiAuthenticationDefaults.AvatarHashClaimType)
                .Value,
                getGuildIdsDelegate: cancellationToken => GetGuildIds(context, cancellationToken),
                context.HttpContext.RequestAborted);

            if (ticket is null)
            {
                AuthenticationLogMessages.AuthenticationTicketNotIssued(logger, userId);
                return;
            }
            AuthenticationLogMessages.AuthenticationTicketCreated(logger, ticket);

            context.Identity.AddClaim(new Claim(
                                          ApiAuthenticationDefaults.TicketIdClaimType,
                                          ticket.Id.ToString(),
                                          ClaimValueTypes.Integer64));
            AuthenticationLogMessages.AuthenticationTicketIdClaimAdded(logger, ticket.Id);

            context.Identity.AddClaim(new Claim(
                                          ApiAuthenticationDefaults.PermissionsClaimType,
                                          JsonConvert.SerializeObject(ticket.GrantedPermissions, _jsonSerializerSettings),
                                          JsonClaimValueTypes.Json));
            AuthenticationLogMessages.GrantedPermissionsClaimAdded(logger, ticket.GrantedPermissions);

            AuthenticationLogMessages.AuthenticationTicketCreationHandled(logger);
        }