polycule-connect/polyculeconnect/internal/client/client.go
Melora Hugues a0849388a7
Some checks failed
/ docker-build-only (push) Failing after 37s
/ go-test (push) Failing after 1m28s
Add login workflow until callback (#48)
2024-10-06 22:11:58 +02:00

52 lines
1.3 KiB
Go

package client
import (
"context"
"fmt"
"log/slog"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger"
"github.com/google/uuid"
"github.com/zitadel/oidc/v3/pkg/client/rp"
"go.uber.org/zap"
"go.uber.org/zap/exp/zapslog"
)
type BackendOIDCConfig struct {
Issuer string
ClientID string
ClientSecret string
RedirectURI string
}
type Backend struct {
ID uuid.UUID
Name string
Config BackendOIDCConfig
}
// OIDCClient is an OIDC client which is the client used to access a registered backend
type OIDCClient struct {
backend *Backend
provider rp.RelyingParty
ctx context.Context
l *zap.SugaredLogger
}
func New(ctx context.Context, conf *Backend, l *zap.SugaredLogger) (*OIDCClient, error) {
options := []rp.Option{
rp.WithLogger(slog.New(zapslog.NewHandler(logger.L.Desugar().Core(), nil))),
}
pr, err := rp.NewRelyingPartyOIDC(ctx, conf.Config.Issuer, conf.Config.ClientID, conf.Config.ClientSecret, conf.Config.RedirectURI, []string{}, options...)
if err != nil {
return nil, fmt.Errorf("failed to init relying party provider: %w", err)
}
return &OIDCClient{ctx: ctx, backend: conf, provider: pr, l: l}, nil
}
func (c *OIDCClient) AuthorizationEndpoint() string {
url := rp.AuthURL(uuid.NewString(), c.provider)
return url
}