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); }
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("")); }