Initial QSfera import
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package service
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("query target not found")
|
||||
ErrBadRequest = errors.New("bad request")
|
||||
ErrMissingEmail = errors.New("missing email address")
|
||||
ErrBackend = errors.New("backend error")
|
||||
)
|
||||
@@ -0,0 +1,38 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/qsfera/server/services/invitations/pkg/invitations"
|
||||
"github.com/qsfera/server/services/invitations/pkg/metrics"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
// NewInstrument returns a service that instruments metrics.
|
||||
func NewInstrument(next Service, metrics *metrics.Metrics) Service {
|
||||
return instrument{
|
||||
next: next,
|
||||
metrics: metrics,
|
||||
}
|
||||
}
|
||||
|
||||
type instrument struct {
|
||||
next Service
|
||||
metrics *metrics.Metrics
|
||||
}
|
||||
|
||||
// Invite implements the Service interface.
|
||||
func (i instrument) Invite(ctx context.Context, invitation *invitations.Invitation) (*invitations.Invitation, error) {
|
||||
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
|
||||
us := v * 1000000
|
||||
|
||||
i.metrics.Latency.WithLabelValues().Observe(us)
|
||||
i.metrics.Duration.WithLabelValues().Observe(v)
|
||||
}))
|
||||
|
||||
defer timer.ObserveDuration()
|
||||
|
||||
i.metrics.Counter.WithLabelValues().Inc()
|
||||
|
||||
return i.next.Invite(ctx, invitation)
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/qsfera/server/pkg/log"
|
||||
"github.com/qsfera/server/services/invitations/pkg/invitations"
|
||||
)
|
||||
|
||||
// NewLogging returns a service that logs messages.
|
||||
func NewLogging(next Service, logger log.Logger) Service {
|
||||
return logging{
|
||||
next: next,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
type logging struct {
|
||||
next Service
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
// Invite implements the Service interface.
|
||||
func (l logging) Invite(ctx context.Context, invitation *invitations.Invitation) (*invitations.Invitation, error) {
|
||||
l.logger.Debug().
|
||||
Interface("invitation", invitation).
|
||||
Msg("Invite")
|
||||
|
||||
return l.next.Invite(ctx, invitation)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"github.com/qsfera/server/pkg/log"
|
||||
"github.com/qsfera/server/services/invitations/pkg/config"
|
||||
)
|
||||
|
||||
// Option defines a single option function.
|
||||
type Option func(o *Options)
|
||||
|
||||
// Options defines the available options for this package.
|
||||
type Options struct {
|
||||
Logger log.Logger
|
||||
Config *config.Config
|
||||
}
|
||||
|
||||
// newOptions initializes the available default options.
|
||||
func newOptions(opts ...Option) Options {
|
||||
opt := Options{}
|
||||
|
||||
for _, o := range opts {
|
||||
o(&opt)
|
||||
}
|
||||
|
||||
return opt
|
||||
}
|
||||
|
||||
// Logger provides a function to set the logger option.
|
||||
func Logger(val log.Logger) Option {
|
||||
return func(o *Options) {
|
||||
o.Logger = val
|
||||
}
|
||||
}
|
||||
|
||||
// Config provides a function to set the config option.
|
||||
func Config(val *config.Config) Option {
|
||||
return func(o *Options) {
|
||||
o.Config = val
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/qsfera/server/pkg/log"
|
||||
"github.com/qsfera/server/services/invitations/pkg/backends/keycloak"
|
||||
"github.com/qsfera/server/services/invitations/pkg/config"
|
||||
"github.com/qsfera/server/services/invitations/pkg/invitations"
|
||||
)
|
||||
|
||||
// Service defines the extension handlers.
|
||||
type Service interface {
|
||||
// Invite creates a new invitation. Invitation adds an external user to the organization.
|
||||
//
|
||||
// When creating a new invitation you have several options available:
|
||||
// 1. On invitation creation, Microsoft Graph can automatically send an
|
||||
// invitation email directly to the invited user, or your app can use
|
||||
// the inviteRedeemUrl returned in the creation response to craft your
|
||||
// own invitation (through your communication mechanism of choice) to
|
||||
// the invited user. If you decide to have Microsoft Graph send an
|
||||
// invitation email automatically, you can control the content and
|
||||
// language of the email using invitedUserMessageInfo.
|
||||
// 2. When the user is invited, a user entity (of userType Guest) is
|
||||
// created and can now be used to control access to resources. The
|
||||
// invited user has to go through the redemption process to access any
|
||||
// resources they have been invited to.
|
||||
Invite(ctx context.Context, invitation *invitations.Invitation) (*invitations.Invitation, error)
|
||||
}
|
||||
|
||||
// Backend defines the behaviour of a user backend.
|
||||
type Backend interface {
|
||||
// CreateUser creates a user in the backend and returns an identifier string.
|
||||
CreateUser(ctx context.Context, invitation *invitations.Invitation) (string, error)
|
||||
// CanSendMail should return true if the backend can send mail
|
||||
CanSendMail() bool
|
||||
// SendMail sends a mail to the user with details on how to reedeem the invitation.
|
||||
SendMail(ctx context.Context, identifier string) error
|
||||
}
|
||||
|
||||
// New returns a new instance of Service
|
||||
func New(opts ...Option) (Service, error) {
|
||||
options := newOptions(opts...)
|
||||
|
||||
// Harcode keycloak backend for now, but this should be configurable in the future.
|
||||
backend := keycloak.New(
|
||||
options.Logger,
|
||||
options.Config.Keycloak.BasePath,
|
||||
options.Config.Keycloak.ClientID,
|
||||
options.Config.Keycloak.ClientSecret,
|
||||
options.Config.Keycloak.ClientRealm,
|
||||
options.Config.Keycloak.UserRealm,
|
||||
options.Config.Keycloak.InsecureSkipVerify,
|
||||
)
|
||||
|
||||
return svc{
|
||||
log: options.Logger,
|
||||
config: options.Config,
|
||||
backend: backend,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type svc struct {
|
||||
config *config.Config
|
||||
log log.Logger
|
||||
backend Backend
|
||||
}
|
||||
|
||||
// Invite implements the service interface
|
||||
func (s svc) Invite(ctx context.Context, invitation *invitations.Invitation) (*invitations.Invitation, error) {
|
||||
if invitation == nil {
|
||||
return nil, ErrBadRequest
|
||||
}
|
||||
|
||||
if invitation.InvitedUserEmailAddress == "" {
|
||||
return nil, ErrMissingEmail
|
||||
}
|
||||
|
||||
id, err := s.backend.CreateUser(ctx, invitation)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %s", ErrBackend, err)
|
||||
}
|
||||
|
||||
// As we only have a single backend, and that backend supports email, we don't have
|
||||
// any code to handle mailing ourself yet.
|
||||
if s.backend.CanSendMail() {
|
||||
err := s.backend.SendMail(ctx, id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %s", ErrBackend, err)
|
||||
}
|
||||
}
|
||||
|
||||
return invitation, nil
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/qsfera/server/services/invitations/pkg/invitations"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
// NewTracing returns a service that instruments traces.
|
||||
func NewTracing(next Service, tp trace.TracerProvider) Service {
|
||||
return tracing{
|
||||
next: next,
|
||||
tp: tp,
|
||||
}
|
||||
}
|
||||
|
||||
type tracing struct {
|
||||
next Service
|
||||
tp trace.TracerProvider
|
||||
}
|
||||
|
||||
// Invite implements the Service interface.
|
||||
func (t tracing) Invite(ctx context.Context, invitation *invitations.Invitation) (*invitations.Invitation, error) {
|
||||
spanOpts := []trace.SpanStartOption{
|
||||
trace.WithSpanKind(trace.SpanKindServer),
|
||||
trace.WithAttributes(
|
||||
attribute.KeyValue{
|
||||
Key: "invitation", Value: attribute.StringValue(invitation.InvitedUserEmailAddress),
|
||||
}),
|
||||
}
|
||||
ctx, span := t.tp.Tracer("invitations").Start(ctx, "Invite", spanOpts...)
|
||||
defer span.End()
|
||||
|
||||
return t.next.Invite(ctx, invitation)
|
||||
}
|
||||
Reference in New Issue
Block a user