private bool AccessTokenIsExpired(BungieAccessToken accessToken)
        {
            DateTimeOffset currentTime = DateTimeOffset.UtcNow;
            DateTimeOffset expireTime  = accessToken.AccessTokenCreatedDate.AddSeconds(accessToken.AccessTokenExpiresInSeconds);

            return(currentTime >= expireTime);
        }
        public void AddOrUpdateAccessToken(BungieAccessToken accessToken)
        {
            BungieAccessToken existingToken = dbContext.AccessTokens.Where(token => token.DiscordId == accessToken.DiscordId).FirstOrDefault();

            if (existingToken == null)
            {
                dbContext.AccessTokens.Add(accessToken);
            }
            else
            {
                dbContext.Entry(existingToken).CurrentValues.SetValues(accessToken);
            }
            dbContext.SaveChanges();
        }
Exemple #3
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 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);
        }