Exemplo n.º 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(""));
        }
Exemplo n.º 2
0
        public OAuthResponse RefreshAccessToken(string refreshToken)
        {
            HttpRequestMessage request = new HttpRequestMessage();

            request.RequestUri = new Uri("https://www.bungie.net/Platform/App/OAuth/Token/");
            request.Method     = HttpMethod.Post;
            Dictionary <string, string> dic = new Dictionary <string, string>();

            dic.Add("grant_type", "refresh_token");
            dic.Add("refresh_token", refreshToken);
            dic.Add("client_id", config["Bungie:ClientId"]);
            dic.Add("client_secret", config["Bungie:ClientSecret"]);
            request.Content = new FormUrlEncodedContent(dic);
            request.Content.Headers.Add("X-API-KEY", config["Bungie:ApiKey"]);
            HttpResponseMessage response = httpClient.SendAsync(request).Result;
            string        json           = response.Content.ReadAsStringAsync().Result;
            OAuthResponse oauthResponse  = JsonConvert.DeserializeObject <OAuthResponse>(json);

            return(oauthResponse);
        }
        public OAuthResponse AuthorizeUser(ulong discordId, string authCode)
        {
            OAuthRequest  oauthRequest  = new OAuthRequest(authCode);
            OAuthResponse oauthResponse = bungieApiService.GetOAuthAccessToken(oauthRequest);

            if (string.IsNullOrWhiteSpace(oauthResponse.AccessToken))
            {
                return(oauthResponse);
            }
            BungieAccessToken accessToken = new BungieAccessToken();

            accessToken.DiscordId    = discordId;
            accessToken.AccessToken  = oauthResponse.AccessToken;
            accessToken.RefreshToken = oauthResponse.RefreshToken;
            accessToken.AccessTokenExpiresInSeconds  = oauthResponse.AccessTokenExpiresInSeconds;
            accessToken.RefreshTokenExpiresInSeconds = oauthResponse.RefreshTokenExpiresInSeconds;
            accessToken.AccessTokenCreatedDate       = DateTimeOffset.UtcNow;
            accessToken.RefreshTokenCreatedDate      = DateTimeOffset.UtcNow;
            emissaryDao.AddOrUpdateAccessToken(accessToken);
            return(oauthResponse);
        }
        public string GetAccessToken(ulong discordId)
        {
            BungieAccessToken accessToken = emissaryDao.GetAccessTokenByDiscordId(discordId);

            if (accessToken == null)
            {
                return(null);
            }
            if (AccessTokenIsExpired(accessToken))
            {
                if (RefreshTokenIsExpired(accessToken))
                {
                    return(null);
                }
                OAuthResponse refreshResponse = bungieApiService.RefreshAccessToken(accessToken.RefreshToken);
                accessToken.AccessToken = refreshResponse.AccessToken;
                accessToken.AccessTokenExpiresInSeconds  = refreshResponse.AccessTokenExpiresInSeconds;
                accessToken.AccessTokenCreatedDate       = DateTimeOffset.UtcNow;
                accessToken.RefreshTokenExpiresInSeconds = refreshResponse.RefreshTokenExpiresInSeconds;
                emissaryDao.AddOrUpdateAccessToken(accessToken);
            }
            return(accessToken.AccessToken);
        }