markbates / goth

Package goth provides a simple, clean, and idiomatic way to write authentication packages for Go web applications.
https://blog.gobuffalo.io/goth-needs-a-new-maintainer-626cd47ca37b
MIT License
5.45k stars 587 forks source link

add providers resolver #539

Open titusgahissy opened 7 months ago

titusgahissy commented 7 months ago

Providers config (client_id, client_secret, etc) can be stored in a database and change while the application is running. The current syntax does not allow to handle this usecase.

goth.UseProviders(providers) // static data

This PR introduces a new interface:

type ProviderResolver interface {
  Get(name string) (Provider, error)
  GetAll() Providers
}

// No breaking changes

func UseProviders(viders ...Provider) {
  providers := Providers{}
  for _, p := range viders {
    providers[p.Name()] = p
  }
  resolver = DefaultProviderResolver{providers: providers}
}

func GetProviders() Providers {
  return resolver.GetAll()
}

func GetProvider(name string) (Provider, error) {
  return resolver.Get(name)
}

Now it becomes possible to do something like:


type MyCustomResolver struct{
  db *gorm.DB
}

func (r MyCustomResolver) Get(name string) (goth.Provider, error) {
   // do your magic
}

func (r MyCustomResolver) GetAll() goth.Providers {
  // do your magic
}

goth.SetProviderResolver(&MyCustomResolver{db: db})
techknowlogick commented 7 months ago

Thanks @titusgahissy, could you resolve the conflicts?