2024-08-17 12:23:06 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-09-22 08:26:27 +00:00
|
|
|
"log/slog"
|
2024-08-17 12:23:06 +00:00
|
|
|
|
2024-09-22 08:26:27 +00:00
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger"
|
2024-10-06 20:11:58 +00:00
|
|
|
"github.com/google/uuid"
|
2024-08-17 12:23:06 +00:00
|
|
|
"github.com/zitadel/oidc/v3/pkg/client/rp"
|
2024-09-22 08:26:27 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/exp/zapslog"
|
2024-08-17 12:23:06 +00:00
|
|
|
)
|
|
|
|
|
2024-10-06 20:11:58 +00:00
|
|
|
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
|
2024-08-17 12:23:06 +00:00
|
|
|
type OIDCClient struct {
|
2024-10-06 20:11:58 +00:00
|
|
|
backend *Backend
|
2024-09-22 08:26:27 +00:00
|
|
|
|
2024-08-17 12:23:06 +00:00
|
|
|
provider rp.RelyingParty
|
2024-09-22 08:26:27 +00:00
|
|
|
ctx context.Context
|
|
|
|
l *zap.SugaredLogger
|
2024-08-17 12:23:06 +00:00
|
|
|
}
|
|
|
|
|
2024-10-06 20:11:58 +00:00
|
|
|
func New(ctx context.Context, conf *Backend, l *zap.SugaredLogger) (*OIDCClient, error) {
|
2024-09-22 08:26:27 +00:00
|
|
|
options := []rp.Option{
|
|
|
|
rp.WithLogger(slog.New(zapslog.NewHandler(logger.L.Desugar().Core(), nil))),
|
|
|
|
}
|
2024-10-06 20:11:58 +00:00
|
|
|
pr, err := rp.NewRelyingPartyOIDC(ctx, conf.Config.Issuer, conf.Config.ClientID, conf.Config.ClientSecret, conf.Config.RedirectURI, []string{}, options...)
|
2024-08-17 12:23:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to init relying party provider: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-10-06 20:11:58 +00:00
|
|
|
return &OIDCClient{ctx: ctx, backend: conf, provider: pr, l: l}, nil
|
2024-09-22 08:26:27 +00:00
|
|
|
}
|
|
|
|
|
2024-10-06 20:11:58 +00:00
|
|
|
func (c *OIDCClient) AuthorizationEndpoint() string {
|
|
|
|
url := rp.AuthURL(uuid.NewString(), c.provider)
|
|
|
|
return url
|
2024-08-17 12:23:06 +00:00
|
|
|
}
|