generated from fluffy-bunny/fluffycore-grpc-starterkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth2factory.go
146 lines (134 loc) · 4.98 KB
/
oauth2factory.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package oauth2factory
import (
"context"
"strings"
"sync"
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"
oauth2 "golang.org/x/oauth2"
codes "google.golang.org/grpc/codes"
)
type (
service struct {
config *contracts_config.Config
idpServiceServer proto_oidc_idp.IFluffyCoreIDPServiceServer
oauth2Configs map[string]*oauth2.Config
oauth2ProviderFactory contracts_oauth2factory.IOIDCProviderFactory
lock sync.Mutex
}
)
var stemService = (*service)(nil)
func init() {
var _ contracts_oauth2factory.IOAuth2Factory = stemService
}
func (s *service) Ctor(config *contracts_config.Config,
oauth2ProviderFactory contracts_oauth2factory.IOIDCProviderFactory,
idpServiceServer proto_oidc_idp.IFluffyCoreIDPServiceServer) (contracts_oauth2factory.IOAuth2Factory, error) {
return &service{
config: config,
idpServiceServer: idpServiceServer,
oauth2ProviderFactory: oauth2ProviderFactory,
oauth2Configs: make(map[string]*oauth2.Config),
}, nil
}
func AddSingletonIOAuth2Factory(cb di.ContainerBuilder) {
di.AddSingleton[contracts_oauth2factory.IOAuth2Factory](cb, stemService.Ctor)
}
const (
// https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps#1-request-a-users-github-identity
GithubAuthURL = "https://github.com/login/oauth/authorize"
// https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps#2-users-are-redirected-back-to-your-site-by-github
GithubTokenURL = "https://github.com/login/oauth/access_token"
GithubUserInfoEndpoint = "https://api.github.com/user"
GitHubEmailsEndpoint = "https://api.github.com/user/emails"
)
var GithubScopes = []string{"user:email"}
func (s *service) validateGetConfigRequest(request *contracts_oauth2factory.GetConfigRequest) 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) GetConfig(ctx context.Context, request *contracts_oauth2factory.GetConfigRequest) (*contracts_oauth2factory.GetConfigResponse, error) {
log := zerolog.Ctx(ctx).With().Logger()
err := s.validateGetConfigRequest(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_Github:
oauth2Config, ok := s.oauth2Configs[request.IDPHint]
if !ok {
config := oauth2.Config{
ClientID: v.Github.ClientId,
ClientSecret: v.Github.ClientSecret,
Scopes: GithubScopes,
RedirectURL: s.config.OIDCConfig.BaseUrl + s.config.OIDCConfig.OAuth2CallbackPath,
Endpoint: oauth2.Endpoint{
AuthURL: GithubAuthURL,
TokenURL: GithubTokenURL,
},
}
s.oauth2Configs[request.IDPHint] = &config
oauth2Config = &config
}
return &contracts_oauth2factory.GetConfigResponse{
Config: oauth2Config,
}, nil
case *proto_oidc_models.Protocol_Oidc:
{
getOIDCProviderResponse, err := s.oauth2ProviderFactory.GetOIDCProvider(ctx,
&contracts_oauth2factory.GetOIDCProviderRequest{
IDPHint: request.IDPHint,
})
if err != nil {
log.Error().Err(err).Msg("Failed to get oidcProvider")
return nil, err
}
oidcProvider := getOIDCProviderResponse.OIDCProvider
oauth2Config, ok := s.oauth2Configs[request.IDPHint]
if !ok {
scopes := strings.Split(v.Oidc.Scope, " ")
config := oauth2.Config{
ClientID: v.Oidc.ClientId,
ClientSecret: v.Oidc.ClientSecret,
Scopes: scopes,
RedirectURL: s.config.OIDCConfig.BaseUrl + s.config.OIDCConfig.OAuth2CallbackPath,
Endpoint: oauth2.Endpoint{
AuthURL: oidcProvider.Endpoint().AuthURL,
TokenURL: oidcProvider.Endpoint().TokenURL,
},
}
s.oauth2Configs[request.IDPHint] = &config
oauth2Config = &config
}
return &contracts_oauth2factory.GetConfigResponse{
Config: oauth2Config,
}, nil
}
}
}
return nil, status.Errorf(codes.NotFound, "no oauth2 protocol found for IDPHint: %s", request.IDPHint)
}