feat: support passing client func opts
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
e5aca04280
commit
25747ed033
@ -28,6 +28,41 @@ import (
|
|||||||
|
|
||||||
func Ptr[T any](v T) *T { return &v }
|
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.
|
// Prefs stores persisted preferences.
|
||||||
type Prefs struct {
|
type Prefs struct {
|
||||||
// Instances is a map of instance names to instance preferences.
|
// 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
|
// Login authenticates the user and saves the credentials in the system
|
||||||
// keychain.
|
// keychain.
|
||||||
func Login(user string) error {
|
func Login(user string, opts ...Option) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if user == "" {
|
if user == "" {
|
||||||
@ -302,8 +337,9 @@ func Login(user string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conf := NewConfig(opts...)
|
||||||
client := clientForInstance(instance)
|
client := clientForInstance(instance)
|
||||||
clientID, clientSecret, err := ensureAppCredentials(instance, client)
|
clientID, clientSecret, err := ensureAppCredentials(instance, client, conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("OAuth2 app setup failed", "user", user, "instance", instance, "error", err)
|
slog.Error("OAuth2 app setup failed", "user", user, "instance", instance, "error", err)
|
||||||
return err
|
return err
|
||||||
@ -394,7 +430,10 @@ func clientForInstance(instance string) *apiclient.GoToSocialSwaggerDocumentatio
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ensureAppCredentials retrieves or creates and stores app credentials.
|
// 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
|
shouldCreateNewApp := false
|
||||||
|
|
||||||
clientID, err := GetInstanceClientID(instance)
|
clientID, err := GetInstanceClientID(instance)
|
||||||
@ -417,7 +456,7 @@ func ensureAppCredentials(instance string, client *apiclient.GoToSocialSwaggerDo
|
|||||||
return clientID, clientSecret, nil
|
return clientID, clientSecret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
app, err := createApp(client)
|
app, err := createApp(client, conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("couldn't create OAuth2 app", "instance", instance, "error", err)
|
slog.Error("couldn't create OAuth2 app", "instance", instance, "error", err)
|
||||||
return "", "", err
|
return "", "", err
|
||||||
@ -440,13 +479,15 @@ func ensureAppCredentials(instance string, client *apiclient.GoToSocialSwaggerDo
|
|||||||
}
|
}
|
||||||
|
|
||||||
// createApp registers a new OAuth2 application.
|
// 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(
|
resp, err := client.Apps.AppCreate(
|
||||||
&apps.AppCreateParams{
|
&apps.AppCreateParams{
|
||||||
ClientName: "gtslib",
|
ClientName: conf.Name,
|
||||||
RedirectURIs: oauthRedirect,
|
RedirectURIs: oauthRedirect,
|
||||||
Scopes: Ptr(oauthScopes),
|
Scopes: Ptr(oauthScopes),
|
||||||
Website: Ptr("https://foo.com/gtslib"),
|
Website: Ptr(conf.Website),
|
||||||
},
|
},
|
||||||
func(op *runtime.ClientOperation) {
|
func(op *runtime.ClientOperation) {
|
||||||
op.ConsumesMediaTypes = []string{"application/x-www-form-urlencoded"}
|
op.ConsumesMediaTypes = []string{"application/x-www-form-urlencoded"}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user