generated from fluffy-bunny/fluffycore-grpc-starterkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oidcflowstore.go
75 lines (65 loc) · 2.94 KB
/
oidcflowstore.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
package oidcflowstore
import (
"context"
"encoding/json"
"time"
store "github.com/eko/gocache/lib/v4/store"
di "github.com/fluffy-bunny/fluffy-dozm-di"
contracts_eko_gocache "github.com/fluffy-bunny/fluffycore-rage-identity/pkg/contracts/eko_gocache"
proto_oidc_flows "github.com/fluffy-bunny/fluffycore-rage-identity/proto/oidc/flows"
proto_oidc_models "github.com/fluffy-bunny/fluffycore-rage-identity/proto/oidc/models"
zerolog "github.com/rs/zerolog"
)
type (
service struct {
proto_oidc_flows.UnimplementedOIDCFlowStoreServer
oidcFlowCache contracts_eko_gocache.IOIDCFlowCache
}
)
var stemService = (*service)(nil)
func init() {
var _ proto_oidc_flows.IFluffyCoreOIDCFlowStoreServer = stemService
}
func (s *service) Ctor(oidcFlowCache contracts_eko_gocache.IOIDCFlowCache) (proto_oidc_flows.IFluffyCoreOIDCFlowStoreServer, error) {
return &service{
oidcFlowCache: oidcFlowCache,
}, nil
}
func AddSingletonIOIDCFlowStore(cb di.ContainerBuilder) {
di.AddSingleton[proto_oidc_flows.IFluffyCoreOIDCFlowStoreServer](cb, stemService.Ctor)
}
func (s *service) StoreAuthorizationRequestState(ctx context.Context, request *proto_oidc_flows.StoreAuthorizationRequestStateRequest) (*proto_oidc_flows.StoreAuthorizationRequestStateResponse, error) {
log := zerolog.Ctx(ctx).With().Str("state", request.State).Logger()
err := s.oidcFlowCache.Set(ctx, request.State, request.AuthorizationRequestState, store.WithExpiration(30*time.Minute))
log.Info().Err(err).Interface("request", request).Msg("StoreAuthorizationRequestState")
return &proto_oidc_flows.StoreAuthorizationRequestStateResponse{}, err
}
func (s *service) GetAuthorizationRequestState(ctx context.Context, request *proto_oidc_flows.GetAuthorizationRequestStateRequest) (*proto_oidc_flows.GetAuthorizationRequestStateResponse, error) {
log := zerolog.Ctx(ctx).With().Str("state", request.State).Logger()
mm, err := s.oidcFlowCache.Get(ctx, request.State)
if err != nil {
// redirect to error page
log.Error().Err(err).Msg("GetAuthorizationRequestState")
return nil, err
}
var value *proto_oidc_models.AuthorizationRequestState = new(proto_oidc_models.AuthorizationRequestState)
mmB, err := json.Marshal(mm)
if err != nil {
log.Error().Err(err).Msg("GetAuthorizationRequestState")
return nil, err
}
err = json.Unmarshal(mmB, value)
if err != nil {
log.Error().Err(err).Msg("GetAuthorizationRequestState")
return nil, err
}
return &proto_oidc_flows.GetAuthorizationRequestStateResponse{
AuthorizationRequestState: value,
}, nil
}
func (s *service) DeleteAuthorizationRequestState(ctx context.Context, request *proto_oidc_flows.DeleteAuthorizationRequestStateRequest) (*proto_oidc_flows.DeleteAuthorizationRequestStateResponse, error) {
log := zerolog.Ctx(ctx).With().Str("state", request.State).Logger()
err := s.oidcFlowCache.Delete(ctx, request.State)
log.Info().Err(err).Msg("DeleteAuthorizationRequestState")
return &proto_oidc_flows.DeleteAuthorizationRequestStateResponse{}, err
}