Пример #1
0
        private async Task <User> Authenticate(Metadata headers)
        {
            var oAuth2CodeEntry = headers.FirstOrDefault((kvp) => kvp.Key == "code");

            if (oAuth2CodeEntry == null)
            {
                throw new HttpRequestException(HttpStatusCodes.Unauthorized);
            }
            var oAuth2Code = oAuth2CodeEntry.Value;
            AccessCodeResponse accessInfo = null;

            if (_flags.HasFlag(ServerFlags.RequiresDiscordOAuth2))
            {
                var specialUserToken = await UserManagerService.GetSpecialUserToken();

                if (string.IsNullOrEmpty(specialUserToken) || specialUserToken != oAuth2Code)
                {
                    var authInfo = await DiscordOAuth2.Authorize(oAuth2Code);

                    accessInfo = authInfo ?? throw new HttpRequestException(HttpStatusCodes.Unauthorized);
                }
                else
                {
                    accessInfo = new AccessCodeResponse {
                        Bypass = true
                    };
                }
            }
            var nameEntry    = headers.FirstOrDefault((kvp) => kvp.Key == "name");
            var worldIdEntry = headers.FirstOrDefault((kvp) => kvp.Key == "worldId");

            if (nameEntry == null || worldIdEntry == null || !int.TryParse(worldIdEntry.Value, out var worldId))
            {
                throw new HttpRequestException(HttpStatusCodes.BadRequest);
            }
            var name = nameEntry.Value;

            try
            {
                _ = _gameDataService.Worlds[worldId];
            }
            catch (KeyNotFoundException)
            {
                throw new HttpRequestException(HttpStatusCodes.BadRequest);
            }
            var user = new User(name, worldId)
            {
                AuthState = new DiscordOAuth2()
                {
                    AccessInformation = accessInfo,
                },
            };

            return(user);
        }
Пример #2
0
        public async Task <Tuple <User, MogmogOperationResult> > GetUser(string oAuth2Code)
        {
            if (string.IsNullOrEmpty(oAuth2Code))
            {
                return(new Tuple <User, MogmogOperationResult>(null, MogmogOperationResult.NoAuthentication));
            }
            var res = await DiscordOAuth2.Authorize(oAuth2Code);

            var userObj = await DiscordOAuth2.GetUserInfo(res);

            var id = userObj.Id;

            return(await GetUser(id));
        }