Exemple #1
0
        public EmissaryResult RegisterOrReauthorize(ulong discordId, string authCode)
        {
            EmissaryUser existingUser = emissaryDao.GetUserByDiscordId(discordId);

            if (existingUser != null)
            {
                // OAuthRequest refreshOAuthRequest = new OAuthRequest(authCode);
                // OAuthResponse refreshOAuthResponse = bungieApiService.GetOAuthAccessToken(refreshOAuthRequest);
                // if (string.IsNullOrWhiteSpace(refreshOAuthResponse.AccessToken)) {
                //     return EmissaryResult.FromError($"api request failed. {refreshOAuthResponse.ErrorType} ({refreshOAuthResponse.ErrorDescription})");
                // }
                OAuthResponse refreshOAuthResponse = authorizationService.AuthorizeUser(discordId, authCode);
                if (!string.IsNullOrWhiteSpace(refreshOAuthResponse.ErrorType))
                {
                    return(EmissaryResult.FromError($"unable to authorize user (discordId {discordId}). {refreshOAuthResponse.ErrorType}: {refreshOAuthResponse.ErrorDescription}"));
                }
                return(EmissaryResult.FromSuccess("successfully authorized user"));
            }
            EmissaryUser newUser = new EmissaryUser();

            newUser.DiscordId = discordId;
            OAuthResponse oauthResponse = authorizationService.AuthorizeUser(discordId, authCode);

            // OAuthRequest oauthRequest = new OAuthRequest(authCode);
            // OAuthResponse oauthResponse = bungieApiService.GetOAuthAccessToken(oauthRequest);
            if (string.IsNullOrWhiteSpace(oauthResponse.AccessToken))
            {
                return(EmissaryResult.FromError($"api request failed. {oauthResponse.ErrorType} ({oauthResponse.ErrorDescription})"));
            }
            BungieAccessToken newUsersAccessToken = new BungieAccessToken(discordId, oauthResponse.AccessToken, oauthResponse.RefreshToken, oauthResponse.AccessTokenExpiresInSeconds, oauthResponse.RefreshTokenExpiresInSeconds, DateTimeOffset.UtcNow, DateTimeOffset.UtcNow);

            emissaryDao.AddOrUpdateAccessToken(newUsersAccessToken);
            UserMembershipsRequest  membershipsRequest  = new UserMembershipsRequest(newUsersAccessToken.AccessToken);
            UserMembershipsResponse membershipsResponse = bungieApiService.GetMembershipsForUser(membershipsRequest);

            DestinyMembership mainMembership = membershipsResponse.DestinyMemberships[0];

            if (membershipsResponse.DestinyMemberships.Count > 1)
            {
                int crossSaveOverrideType = membershipsResponse.DestinyMemberships[0].CrossSaveOverride;
                mainMembership = membershipsResponse.DestinyMemberships.Where(membership => membership.MembershipType == crossSaveOverrideType).FirstOrDefault();
            }
            newUser.DestinyMembershipType = mainMembership.MembershipType;
            newUser.DestinyProfileId      = mainMembership.DestinyProfileId;

            emissaryDao.AddOrUpdateUser(newUser);
            return(EmissaryResult.FromSuccess(""));
        }
        public UserModelDTO LoginUser(UserModelDTO model)
        {
            model = _authenticationService.AuthenticateUser(model);

            if (!model.Authenticated)
            {
                return(model);
            }

            model = _authorizationService.AuthorizeUser(model);

            // If the user dosnt exsist
            if (!_userRepository.Exsist(model.UserName))
            {
                // Add the user to the database
                _userRepository.Insert(_mapper.Map <UserModelDAO>(model));
            }

            model.Id = _userRepository.GetIdByname(model.UserName);

            model = GenerateToken(model);

            model.Password = string.Empty;

            return(model);
        }
        public async Task <IActionResult> AuthorizeUser(Guid userId)
        {
            var result = await _authorizationService.AuthorizeUser(userId);

            return(Ok(result));
        }