Пример #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(""));
        }
Пример #2
0
        public UserMembershipsResponse GetMembershipsForUser(UserMembershipsRequest membershipsRequest)
        {
            HttpRequestMessage request = new HttpRequestMessage();

            request.RequestUri = new Uri("https://www.bungie.net/Platform/User/GetMembershipsForCurrentUser/");
            request.Method     = HttpMethod.Get;
            request.Headers.Add("X-API-KEY", config["Bungie:ApiKey"]);
            request.Headers.Add("Authorization", $"Bearer {membershipsRequest.AccessToken}");
            HttpResponseMessage response = httpClient.SendAsync(request).Result;

            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                throw new BungieApiException("Unauthorized: Access is denied due to invalid credentials.");
            }
            // TODO what about other exceptions? like when the token is empty string and it returns a 500 error code?
            string json = response.Content.ReadAsStringAsync().Result;
            UserMembershipsResponse membershipsResponse = JsonConvert.DeserializeObject <UserMembershipsResponse>(json);

            return(membershipsResponse);
        }