feat: support passing client func opts
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
decentral1se 2024-08-01 22:34:38 +02:00
parent e5aca04280
commit 25747ed033
Signed by: decentral1se
GPG Key ID: 03789458B3D0C410

View File

@ -28,6 +28,41 @@ import (
func Ptr[T any](v T) *T { return &v }
// Config is a client configuration.
type Config struct {
Name string // client name
Website string // client website
}
// Option with operates on a client config.
type Option func(c *Config)
// NewConfig creates a new client config.
func NewConfig(opts ...Option) *Config {
conf := &Config{
Name: "gtslib-auth-keyring",
Website: "https://doesnt.exist/gtslib-auth-keyring",
}
for _, optFunc := range opts {
optFunc(conf)
}
return conf
}
// WithName sets a client name.
func WithName(name string) Option {
return func(c *Config) {
c.Name = name
}
}
// WithWebsite sets a client website.
func WithWebsite(website string) Option {
return func(c *Config) {
c.Website = website
}
}
// Prefs stores persisted preferences.
type Prefs struct {
// Instances is a map of instance names to instance preferences.
@ -271,7 +306,7 @@ const (
// Login authenticates the user and saves the credentials in the system
// keychain.
func Login(user string) error {
func Login(user string, opts ...Option) error {
var err error
if user == "" {
@ -302,8 +337,9 @@ func Login(user string) error {
return err
}
conf := NewConfig(opts...)
client := clientForInstance(instance)
clientID, clientSecret, err := ensureAppCredentials(instance, client)
clientID, clientSecret, err := ensureAppCredentials(instance, client, conf)
if err != nil {
slog.Error("OAuth2 app setup failed", "user", user, "instance", instance, "error", err)
return err
@ -394,7 +430,10 @@ func clientForInstance(instance string) *apiclient.GoToSocialSwaggerDocumentatio
}
// ensureAppCredentials retrieves or creates and stores app credentials.
func ensureAppCredentials(instance string, client *apiclient.GoToSocialSwaggerDocumentation) (string, string, error) {
func ensureAppCredentials(
instance string,
client *apiclient.GoToSocialSwaggerDocumentation,
conf *Config) (string, string, error) {
shouldCreateNewApp := false
clientID, err := GetInstanceClientID(instance)
@ -417,7 +456,7 @@ func ensureAppCredentials(instance string, client *apiclient.GoToSocialSwaggerDo
return clientID, clientSecret, nil
}
app, err := createApp(client)
app, err := createApp(client, conf)
if err != nil {
slog.Error("couldn't create OAuth2 app", "instance", instance, "error", err)
return "", "", err
@ -440,13 +479,15 @@ func ensureAppCredentials(instance string, client *apiclient.GoToSocialSwaggerDo
}
// createApp registers a new OAuth2 application.
func createApp(client *apiclient.GoToSocialSwaggerDocumentation) (*models.Application, error) {
func createApp(
client *apiclient.GoToSocialSwaggerDocumentation,
conf *Config) (*models.Application, error) {
resp, err := client.Apps.AppCreate(
&apps.AppCreateParams{
ClientName: "gtslib",
ClientName: conf.Name,
RedirectURIs: oauthRedirect,
Scopes: Ptr(oauthScopes),
Website: Ptr("https://foo.com/gtslib"),
Website: Ptr(conf.Website),
},
func(op *runtime.ClientOperation) {
op.ConsumesMediaTypes = []string{"application/x-www-form-urlencoded"}