Run telegram auth goroutine

Fix for https://github.com/go-pkgz/auth/issues/90
This commit is contained in:
Dmitry Verkhoturov
2021-08-06 16:56:07 -05:00
committed by Umputun
parent 7b28cb9fa9
commit 9df8de511a
2 changed files with 32 additions and 24 deletions
+24 -16
View File
@@ -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
}
+8 -8
View File
@@ -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())