From 9df8de511a97d7d7eb24782d913f537e6b42e56d Mon Sep 17 00:00:00 2001 From: Dmitry Verkhoturov Date: Fri, 6 Aug 2021 23:28:31 +0200 Subject: [PATCH] Run telegram auth goroutine Fix for https://github.com/go-pkgz/auth/issues/90 --- backend/app/cmd/server.go | 40 ++++++++++++++++++++-------------- backend/app/cmd/server_test.go | 16 +++++++------- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/backend/app/cmd/server.go b/backend/app/cmd/server.go index 5dc95c27..8c0d4e31 100644 --- a/backend/app/cmd/server.go +++ b/backend/app/cmd/server.go @@ -299,7 +299,7 @@ func (s *ServerCommand) Execute(_ []string) error { cancel() }() - app, err := s.newServerApp() + app, err := s.newServerApp(ctx) if err != nil { log.Printf("[PANIC] failed to setup application, %+v", err) return err @@ -390,7 +390,7 @@ func contains(s string, a []string) bool { // newServerApp prepares application and return it with all active parts // doesn't start anything -func (s *ServerCommand) newServerApp() (*serverApp, error) { +func (s *ServerCommand) newServerApp(ctx context.Context) (*serverApp, error) { if err := makeDirs(s.BackupLocation); err != nil { return nil, errors.Wrap(err, "failed to create backup store") @@ -444,7 +444,7 @@ func (s *ServerCommand) newServerApp() (*serverApp, error) { return nil, errors.Wrap(err, "failed to make avatar store") } authRefreshCache := newAuthRefreshCache() - authenticator, err := s.makeAuthenticator(dataService, avatarStore, adminStore, authRefreshCache) + authenticator, err := s.makeAuthenticator(ctx, dataService, avatarStore, adminStore, authRefreshCache) if err != nil { _ = dataService.Close() return nil, errors.Wrap(err, "failed to make authenticator") @@ -765,7 +765,7 @@ func (s *ServerCommand) makeCache() (LoadingCache, error) { return nil, errors.Errorf("unsupported cache type %s", s.Cache.Type) } -func (s *ServerCommand) addAuthProviders(authenticator *auth.Service) error { +func (s *ServerCommand) addAuthProviders(ctx context.Context, authenticator *auth.Service) error { providers := 0 if s.Auth.Google.CID != "" && s.Auth.Google.CSEC != "" { @@ -793,16 +793,24 @@ func (s *ServerCommand) addAuthProviders(authenticator *auth.Service) error { providers++ } if s.Auth.Telegram { - authenticator.AddCustomHandler( - &provider.TelegramHandler{ - ProviderName: "telegram", - ErrorMsg: "❌ Invalid auth request. Please try clicking link again.", - SuccessMsg: "✅ You have successfully authenticated!", - Telegram: provider.NewTelegramAPI(s.Telegram.Token, &http.Client{Timeout: s.Telegram.Timeout}), - L: log.Default(), - TokenService: authenticator.TokenService(), - AvatarSaver: authenticator.AvatarProxy(), - }) + telegram := &provider.TelegramHandler{ + ProviderName: "telegram", + ErrorMsg: "❌ Invalid auth request. Please try clicking link again.", + SuccessMsg: "✅ You have successfully authenticated!", + Telegram: provider.NewTelegramAPI(s.Telegram.Token, &http.Client{Timeout: s.Telegram.Timeout}), + L: log.Default(), + TokenService: authenticator.TokenService(), + AvatarSaver: authenticator.AvatarProxy(), + } + // Run Telegram provider in the background + go func() { + err := telegram.Run(ctx) + if err != nil { + log.Printf("[ERROR] telegram auth error %+v", err) + } + }() + authenticator.AddCustomHandler(telegram) + providers++ } @@ -1004,7 +1012,7 @@ func (s *ServerCommand) makeSSLConfig() (config api.SSLConfig, err error) { return config, err } -func (s *ServerCommand) makeAuthenticator(ds *service.DataStore, avas avatar.Store, admns admin.Store, authRefreshCache *authRefreshCache) (*auth.Service, error) { +func (s *ServerCommand) makeAuthenticator(ctx context.Context, ds *service.DataStore, avas avatar.Store, admns admin.Store, authRefreshCache *authRefreshCache) (*auth.Service, error) { authenticator := auth.NewService(auth.Opts{ URL: strings.TrimSuffix(s.RemarkURL, "/"), Issuer: "remark42", @@ -1061,7 +1069,7 @@ func (s *ServerCommand) makeAuthenticator(ds *service.DataStore, avas avatar.Sto UseGravatar: true, }) - if err := s.addAuthProviders(authenticator); err != nil { + if err := s.addAuthProviders(ctx, authenticator); err != nil { return nil, err } diff --git a/backend/app/cmd/server_test.go b/backend/app/cmd/server_test.go index 34644d1d..bb31ebb5 100644 --- a/backend/app/cmd/server_test.go +++ b/backend/app/cmd/server_test.go @@ -228,7 +228,7 @@ func TestServerApp_WithSSL(t *testing.T) { require.NoError(t, err) // create app - app, err := opts.newServerApp() + app, err := opts.newServerApp(context.Background()) require.NoError(t, err) ctx, cancel := context.WithCancel(context.Background()) @@ -283,7 +283,7 @@ func TestServerApp_WithRemote(t *testing.T) { opts.BackupLocation, opts.Image.FS.Path = "/tmp", "/tmp" // create app - app, err := opts.newServerApp() + app, err := opts.newServerApp(context.Background()) require.NoError(t, err) ctx, cancel := context.WithCancel(context.Background()) @@ -312,7 +312,7 @@ func TestServerApp_Failed(t *testing.T) { // RO bolt location _, err := p.ParseArgs([]string{"--backup=/tmp", "--store.bolt.path=/dev/null", "--image.fs.path=/tmp"}) assert.NoError(t, err) - _, err = opts.newServerApp() + _, err = opts.newServerApp(context.Background()) assert.EqualError(t, err, "failed to make data store engine: failed to create bolt store: can't make directory /dev/null: mkdir /dev/null: not a directory") t.Log(err) @@ -322,7 +322,7 @@ func TestServerApp_Failed(t *testing.T) { _, err = p.ParseArgs([]string{"--store.bolt.path=/tmp", "--backup=/dev/null/not-writable"}) assert.NoError(t, err) - _, err = opts.newServerApp() + _, err = opts.newServerApp(context.Background()) assert.EqualError(t, err, "failed to create backup store: can't make directory /dev/null/not-writable: mkdir /dev/null: not a directory") t.Log(err) @@ -332,7 +332,7 @@ func TestServerApp_Failed(t *testing.T) { _, err = p.ParseArgs([]string{"--backup=/tmp", "----store.bolt.path=/tmp"}) assert.NoError(t, err) - _, err = opts.newServerApp() + _, err = opts.newServerApp(context.Background()) assert.EqualError(t, err, "invalid remark42 url demo.remark42.com") t.Log(err) @@ -343,7 +343,7 @@ func TestServerApp_Failed(t *testing.T) { assert.Error(t, err, "blah is invalid type") opts.Store.Type = "blah" - _, err = opts.newServerApp() + _, err = opts.newServerApp(context.Background()) assert.EqualError(t, err, "failed to make data store engine: unsupported store type blah") t.Log(err) @@ -353,7 +353,7 @@ func TestServerApp_Failed(t *testing.T) { p = flags.NewParser(&opts, flags.Default) _, err = p.ParseArgs([]string{"--store.bolt.path=/tmp", "--cache.type=redis_pub_sub", "--cache.redis_addr=wrong_address"}) assert.NoError(t, err) - _, err = opts.newServerApp() + _, err = opts.newServerApp(context.Background()) assert.EqualError(t, err, "failed to make cache: cache backend initialization, redis PubSub initialisation: "+ "problem subscribing to channel remark42-cache on address wrong_address: "+ @@ -699,7 +699,7 @@ func prepServerApp(t *testing.T, fn func(o ServerCommand) ServerCommand) (*serve } func createAppFromCmd(t *testing.T, cmd ServerCommand) (*serverApp, context.Context, context.CancelFunc) { - app, err := cmd.newServerApp() + app, err := cmd.newServerApp(context.Background()) require.NoError(t, err) ctx, cancel := context.WithCancel(context.Background())