/
endpoint.go
68 lines (57 loc) · 2.15 KB
/
endpoint.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
package register
import (
"context"
cs "github.com/cloudtrust/common-service"
commonerrors "github.com/cloudtrust/common-service/errors"
apiregister "github.com/cloudtrust/keycloak-bridge/api/register"
msg "github.com/cloudtrust/keycloak-bridge/internal/constants"
"github.com/go-kit/kit/endpoint"
)
// Endpoints for self service
type Endpoints struct {
RegisterUser endpoint.Endpoint
RegisterCorpUser endpoint.Endpoint
GetConfiguration endpoint.Endpoint
}
// MakeRegisterUserEndpoint endpoint creation
func MakeRegisterUserEndpoint(component Component, socialRealm string) cs.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var m = req.(map[string]string)
var realm = m[prmRealm]
if realm == "" {
return nil, commonerrors.CreateBadRequestError(commonerrors.MsgErrInvalidParam + "." + msg.Realm)
}
return registerUser(ctx, component, socialRealm, realm, m[reqBody], true)
}
}
// MakeRegisterCorpUserEndpoint endpoint creation
func MakeRegisterCorpUserEndpoint(component Component) cs.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var m = req.(map[string]string)
var realm = m[prmCorpRealm]
return registerUser(ctx, component, realm, realm, m[reqBody], false)
}
}
func registerUser(ctx context.Context, component Component, corpRealm string, realm string, body string, isSocialRealm bool) (interface{}, error) {
var user, err = apiregister.UserFromJSON(body)
if err != nil {
return nil, commonerrors.CreateBadRequestError(commonerrors.MsgErrInvalidParam + "." + msg.BodyContent)
}
// Validate input request
err = user.Validate(isSocialRealm)
if err != nil {
return "", err
}
return component.RegisterUser(ctx, corpRealm, realm, user)
}
// MakeGetConfigurationEndpoint endpoint creation
func MakeGetConfigurationEndpoint(component Component) cs.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var m = req.(map[string]string)
var realm = m[prmRealm]
if realm == "" {
return nil, commonerrors.CreateBadRequestError(commonerrors.MsgErrInvalidParam + "." + msg.Realm)
}
return component.GetConfiguration(ctx, realm)
}
}