private async Task RevokeToken(string userId) { var typeName = typeof(TokenResponse).FullName; var context = this.DbContext; var tokens = await context.GoogleAuthData .Where(o => o.UserId == userId && o.Type == typeName) .ToArrayAsync(); foreach (var item in tokens) { try { var token = JsonConvert.DeserializeObject <TokenResponse>(item.Value); var flow = GmailApiComponent.CreateFlow(userId); var userCredential = new Google.Apis.Auth.OAuth2.UserCredential(flow, userId, token); //Refresh the access token so we can promptly destroy it. await userCredential.RefreshTokenAsync(CancellationToken.None); await userCredential.RevokeTokenAsync(CancellationToken.None); } catch { //TODO: Logging! } } }
private async Task StoreAuthToken(string userId, string userName, string refreshToken, string accessToken, long expiresIn) { var flow = GmailApiComponent.CreateFlow(userId); var token = await flow.DataStore.GetAsync <TokenResponse>(userName); if (token == null && string.IsNullOrEmpty(refreshToken)) { throw new HttpException((int)System.Net.HttpStatusCode.Unauthorized, "No refresh token provided."); } if (token == null) { token = new Google.Apis.Auth.OAuth2.Responses.TokenResponse { RefreshToken = refreshToken }; } token.AccessToken = accessToken; token.ExpiresInSeconds = expiresIn; token.Issued = flow.Clock.UtcNow; await flow.DataStore.StoreAsync(userName, token); }
public LabelsManager(string userId, string userName) { this._gmailApiComponent = new GmailApiComponent(); this._userId = userId; this._userName = userName; }