generated from fluffy-bunny/fluffycore-grpc-starterkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oidcproviderfactory.go
95 lines (86 loc) · 3.09 KB
/
oidcproviderfactory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package oidcproviderfactory
import (
"context"
"sync"
oidc "github.com/coreos/go-oidc/v3/oidc"
di "github.com/fluffy-bunny/fluffy-dozm-di"
contracts_config "github.com/fluffy-bunny/fluffycore-rage-identity/pkg/contracts/config"
contracts_oauth2factory "github.com/fluffy-bunny/fluffycore-rage-identity/pkg/contracts/oauth2factory"
proto_oidc_idp "github.com/fluffy-bunny/fluffycore-rage-identity/proto/oidc/idp"
proto_oidc_models "github.com/fluffy-bunny/fluffycore-rage-identity/proto/oidc/models"
fluffycore_utils "github.com/fluffy-bunny/fluffycore/utils"
status "github.com/gogo/status"
zerolog "github.com/rs/zerolog"
codes "google.golang.org/grpc/codes"
)
type (
service struct {
config *contracts_config.Config
idpServiceServer proto_oidc_idp.IFluffyCoreIDPServiceServer
oidcProviders map[string]*oidc.Provider
lock sync.Mutex
}
)
var stemService = (*service)(nil)
func init() {
var _ contracts_oauth2factory.IOIDCProviderFactory = stemService
}
func (s *service) Ctor(config *contracts_config.Config, idpServiceServer proto_oidc_idp.IFluffyCoreIDPServiceServer) (contracts_oauth2factory.IOIDCProviderFactory, error) {
return &service{
config: config,
idpServiceServer: idpServiceServer,
oidcProviders: make(map[string]*oidc.Provider),
}, nil
}
func AddSingletonIOIDCProviderFactory(cb di.ContainerBuilder) {
di.AddSingleton[contracts_oauth2factory.IOIDCProviderFactory](cb, stemService.Ctor)
}
func (s *service) validateGetOIDCProviderRequest(request *contracts_oauth2factory.GetOIDCProviderRequest) error {
if request == nil {
return status.Error(codes.InvalidArgument, "request is required")
}
if fluffycore_utils.IsEmptyOrNil(request.IDPHint) {
return status.Error(codes.InvalidArgument, "IDPHint is required")
}
return nil
}
func (s *service) GetOIDCProvider(ctx context.Context, request *contracts_oauth2factory.GetOIDCProviderRequest) (*contracts_oauth2factory.GetOIDCProviderResponse, error) {
log := zerolog.Ctx(ctx).With().Logger()
err := s.validateGetOIDCProviderRequest(request)
if err != nil {
return nil, err
}
s.lock.Lock()
defer s.lock.Unlock()
getIDPBySlugResponse, err := s.idpServiceServer.GetIDPBySlug(ctx,
&proto_oidc_idp.GetIDPBySlugRequest{
Slug: request.IDPHint,
})
if err != nil {
log.Error().Err(err).Msg("GetIDPBySlug")
return nil, err
}
idp := getIDPBySlugResponse.Idp
if idp.Protocol != nil {
log.Info().Interface("getIDPBySlugResponse", getIDPBySlugResponse).Msg("getIDPBySlugResponse")
switch v := idp.Protocol.Value.(type) {
case *proto_oidc_models.Protocol_Oidc:
{
oidcProvider, ok := s.oidcProviders[request.IDPHint]
if !ok {
provider, err := oidc.NewProvider(ctx, v.Oidc.Authority)
if err != nil {
log.Error().Err(err).Msg("oidc.NewProvider")
return nil, err
}
s.oidcProviders[request.IDPHint] = provider
oidcProvider = provider
}
return &contracts_oauth2factory.GetOIDCProviderResponse{
OIDCProvider: oidcProvider,
}, nil
}
}
}
return nil, status.Errorf(codes.NotFound, "no oauth2 protocol found for IDPHint: %s", request.IDPHint)
}