コード例 #1
0
ファイル: BotAuthorize.cs プロジェクト: sctigercat1/ClemBot
            public Task <Result <Model, AuthorizeStatus> > Handle(Query request,
                                                                  CancellationToken cancellationToken)
            {
                _httpContextAccessor.HttpContext !.Request.Headers.TryGetValue("Origin", out var origin);
                _logger.LogInformation($"Bot Authorize Request Initialized from Url: {origin.ToString()}");
                if (request.Key != _apiKey.Key)
                {
                    _logger.LogInformation("Bot Authorize Request Denied: Invalid Key");
                    return(Task.FromResult(AuthorizeResult <Model> .Forbidden()));
                }

                _logger.LogInformation("Bot Authorize Request Accepted");

                _logger.LogInformation($"Generating Claim: {Claims.BotApiKey}");
                var claims = new[]
                {
                    new Claim(Claims.BotApiKey, request.Key)
                };

                _logger.LogInformation("Generating JWT Access Token");
                var token = _jwtAuthManager.GenerateToken(claims, DateTime.Now);

                _logger.LogInformation("JWT Access Token Successfully Generated");

                return(Task.FromResult(AuthorizeResult <Model> .Success(new Model()
                {
                    Token = token
                })));
            }
コード例 #2
0
        public async Task <IAuthorizeResult <Model> > Handle(Query request, CancellationToken cancellationToken)
        {
            _httpContextAccessor.HttpContext !.Request.Headers.TryGetValue("Origin", out var origin);
            _logger.LogInformation("Site Login Request Initialized from Url: {Origin}", origin);

            var discordUser = await _discordAuthManager.GetDiscordUserAsync(request.Bearer);

            if (discordUser is null)
            {
                _logger.LogWarning("Site Login Request Denied: Invalid Token");
                return(AuthorizeResult <Model> .Forbidden());
            }

            _logger.LogInformation("Site Login Request Accepted");
            var claims = new List <Claim>
            {
                new(Claims.DiscordBearer, request.Bearer),
                new(Claims.DiscordUserId, discordUser.User.Id)
            };

            _logger.LogInformation("Generating JWT Access Token");
            var token = _jwtAuthManager.GenerateToken(claims, DateTime.UtcNow.AddHours(4));

            _logger.LogInformation("JWT Access Token Successfully Generated");

            return(AuthorizeResult <Model> .Success(new Model { Token = token }));
        }
コード例 #3
0
        public async Task <IAuthorizeResult <Model> > Handle(Query request, CancellationToken cancellationTokeln)
        {
            _httpContextAccessor.HttpContext !.Request.Headers.TryGetValue("Origin", out var origin);
            _logger.LogInformation("Site User Request Initialized from Url: {Origin}", origin);

            var token = _httpContextAccessor.HttpContext.User.FindFirst(Claims.DiscordBearer)?.Value;

            if (token is null)
            {
                _logger.LogWarning("Api User Request Denied: No Bearer Token Found");
                return(AuthorizeResult <Model> .Forbidden());
            }

            var discordUser = await _discordAuthManager.GetDiscordUserAsync(token);

            if (discordUser is null)
            {
                _logger.LogWarning("Api User Request Denied: Invalid Discord Token");
                return(AuthorizeResult <Model> .Forbidden());
            }

            var userGuilds = await _discordAuthManager.GetDiscordUserGuildsAsync(token);

            if (userGuilds is null)
            {
                _logger.LogWarning("Api User Guilds Request Denied: Invalid Discord Token");
                return(AuthorizeResult <Model> .Forbidden());
            }

            var addedGuilds = await _context.GuildUser
                              .Where(x => x.UserId == ulong.Parse(discordUser.User.Id))
                              .Select(y => y.GuildId.ToString())
                              .ToListAsync();

            var userClaims = await _context.Users.GetUserClaimsAsync(ulong.Parse(discordUser.User.Id));

            foreach (var guild in userGuilds)
            {
                guild.IsAdded = addedGuilds.Contains(guild.Id);

                if (userClaims.TryGetValue(ulong.Parse(guild.Id), out var claims))
                {
                    guild.Claims = claims.Select(x => x.ToString()).ToList();
                }
            }

            var siteUser = new SiteUser {
                User = discordUser, Guilds = userGuilds
            };

            _logger.LogInformation("Site Login Request Accepted");
            return(AuthorizeResult <Model> .Success(new Model { User = siteUser }));
        }