private void Instance_AuthorizationRegistredEvent(UserModel userModel, UserSettingsModel userSettingsModel) { if (ServiceExists(userModel.UserId.ToString())) { return; } var token = new TokenResponse { RefreshToken = userModel.RefreshToken, AccessToken = userModel.AccessToken, ExpiresInSeconds = userModel.ExpiresIn, TokenType = userModel.TokenType, IssuedUtc = userModel.IssuedTimeUtc }; var credentials = new BotUserCredential(new GoogleAuthorizationCodeFlow( new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = _clientSecrets, Scopes = UserAccessAttribute.GetScopesValue(userSettingsModel.Access), DataStore = new DbDataStore() }), userModel, token); var serviceInitializer = new BaseClientService.Initializer { ApiKey = BotInitializer.Instance.BotSettings.GmnbApiKey, ApplicationName = BotInitializer.Instance.BotSettings.ApplicationName, HttpClientInitializer = credentials }; ServiceCollection.Add(new Service(credentials, serviceInitializer, userSettingsModel.Access)); }
public async Task SendAuthorizeLink(ISender message, AuthorizeLinks links) { try { var gmailDbContextWorker = new GmailDbContextWorker(); LogMaker.Log(Logger, $"Start authorizing user with UserId={message.From.Id}.", false); var userModel = await gmailDbContextWorker.FindUserAsync(message.From.Id) ?? await gmailDbContextWorker.AddNewUserAsync(message.From); LogMaker.Log(Logger, $"The user with id:{userModel.UserId} has requested authorization", false); var fullAccessState = Base64.Encode($"{userModel.UserId},{UserAccess.FULL}"); var notifyAccessState = Base64.Encode($"{userModel.UserId},{UserAccess.NOTIFY}"); var pendingUserModel = await gmailDbContextWorker.FindPendingUserAsync(userModel.UserId); if (pendingUserModel != null) { await gmailDbContextWorker.UpdateRecordJoinTimeAsync(pendingUserModel.Id, DateTime.UtcNow); } else { await gmailDbContextWorker.QueueAsync(userModel.UserId); } Uri notifyAccessUri = null; Uri fullAccessUri = null; switch (links) { case AuthorizeLinks.Both: notifyAccessUri = GetAuthenticationUri(notifyAccessState, UserAccessAttribute.GetScopesValue(UserAccess.NOTIFY)); fullAccessUri = GetAuthenticationUri(fullAccessState, UserAccessAttribute.GetScopesValue(UserAccess.FULL)); break; case AuthorizeLinks.Full: fullAccessUri = GetAuthenticationUri(fullAccessState, UserAccessAttribute.GetScopesValue(UserAccess.FULL)); break; case AuthorizeLinks.Notify: notifyAccessUri = GetAuthenticationUri(notifyAccessState, UserAccessAttribute.GetScopesValue(UserAccess.NOTIFY)); break; default: throw new ArgumentOutOfRangeException(nameof(links), links, null); } await _botActions.AuthorizeMessage(message.From.Id.ToString(), notifyAccessUri, fullAccessUri); } catch (Exception ex) { throw new AuthorizeException("An error occurred while trying to send the authentication link to the user", ex); } }