/
endpoint.go
166 lines (136 loc) · 5.98 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package account
import (
"context"
"encoding/json"
cs "github.com/cloudtrust/common-service"
errrorhandler "github.com/cloudtrust/common-service/errors"
api "github.com/cloudtrust/keycloak-bridge/api/account"
msg "github.com/cloudtrust/keycloak-bridge/internal/constants"
"github.com/go-kit/kit/endpoint"
)
// Endpoints wraps a service behind a set of endpoints.
type Endpoints struct {
UpdatePassword endpoint.Endpoint
GetCredentials endpoint.Endpoint
GetCredentialRegistrators endpoint.Endpoint
UpdateLabelCredential endpoint.Endpoint
DeleteCredential endpoint.Endpoint
MoveCredential endpoint.Endpoint
GetAccount endpoint.Endpoint
UpdateAccount endpoint.Endpoint
DeleteAccount endpoint.Endpoint
GetConfiguration endpoint.Endpoint
SendVerifyEmail endpoint.Endpoint
SendVerifyPhoneNumber endpoint.Endpoint
}
// UpdatePasswordBody is the definition of the expected body content of UpdatePassword method
type UpdatePasswordBody struct {
CurrentPassword string `json:"currentPassword,omitempty"`
NewPassword string `json:"newPassword,omitempty"`
ConfirmPassword string `json:"confirmPassword,omitempty"`
}
// MakeUpdatePasswordEndpoint makes the UpdatePassword endpoint to update connected user's own password.
func MakeUpdatePasswordEndpoint(component Component) cs.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var m = req.(map[string]string)
var body api.UpdatePasswordBody
err := json.Unmarshal([]byte(m[ReqBody]), &body)
if err != nil {
return nil, errrorhandler.CreateBadRequestError(msg.MsgErrInvalidParam + "." + msg.Body)
}
if err = body.Validate(); err != nil {
return nil, err
}
return nil, component.UpdatePassword(ctx, body.CurrentPassword, body.NewPassword, body.ConfirmPassword)
}
}
// MakeGetCredentialsEndpoint makes the GetCredentials endpoint to list credentials of the current user.
func MakeGetCredentialsEndpoint(component Component) cs.Endpoint {
return func(ctx context.Context, _ interface{}) (interface{}, error) {
return component.GetCredentials(ctx)
}
}
// MakeGetCredentialRegistratorsEndpoint make the GetCredentialRegistrators endpoint to retrieve the list of possible kind of credentials.
func MakeGetCredentialRegistratorsEndpoint(component Component) cs.Endpoint {
return func(ctx context.Context, _ interface{}) (interface{}, error) {
return component.GetCredentialRegistrators(ctx)
}
}
// MakeUpdateLabelCredentialEndpoint make the UpdateLabelCredential endpoint to set a new label for a credential.
func MakeUpdateLabelCredentialEndpoint(component Component) cs.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var m = req.(map[string]string)
var body api.CredentialRepresentation
err := json.Unmarshal([]byte(m[ReqBody]), &body)
if err != nil {
return nil, errrorhandler.CreateBadRequestError(msg.MsgErrInvalidParam + "." + msg.Body)
}
if err = body.Validate(); err != nil {
return nil, err
}
if body.UserLabel == nil {
return nil, errrorhandler.CreateBadRequestError(msg.MsgErrMissingParam + "." + msg.UserLabel)
}
return nil, component.UpdateLabelCredential(ctx, m[PrmCredentialID], *body.UserLabel)
}
}
// MakeDeleteCredentialEndpoint make the DeleteCredential endpoint to delete a credential of the current user.
func MakeDeleteCredentialEndpoint(component Component) cs.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var m = req.(map[string]string)
return nil, component.DeleteCredential(ctx, m[PrmCredentialID])
}
}
// MakeMoveCredentialEndpoint make the MoveCredential endpoint to change the priority of a credential of the current user.
func MakeMoveCredentialEndpoint(component Component) cs.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var m = req.(map[string]string)
return nil, component.MoveCredential(ctx, m[PrmCredentialID], m[PrmPrevCredentialID])
}
}
// MakeGetAccountEndpoint makes the GetAccount endpoint to get connected user's info.
func MakeGetAccountEndpoint(component Component) cs.Endpoint {
return func(ctx context.Context, _ interface{}) (interface{}, error) {
return component.GetAccount(ctx)
}
}
// MakeUpdateAccountEndpoint makes the UpdateAccount endpoint to update connected user's own info.
func MakeUpdateAccountEndpoint(component Component) cs.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var m = req.(map[string]string)
var body api.UpdatableAccountRepresentation
err := json.Unmarshal([]byte(m[ReqBody]), &body)
if err != nil {
return nil, errrorhandler.CreateBadRequestError(msg.MsgErrInvalidParam + "." + msg.Body)
}
if err = body.Validate(); err != nil {
return nil, err
}
return nil, component.UpdateAccount(ctx, body)
}
}
// MakeDeleteAccountEndpoint makes the DeleteAccount endpoint to delete connected user.
func MakeDeleteAccountEndpoint(component Component) cs.Endpoint {
return func(ctx context.Context, _ interface{}) (interface{}, error) {
return nil, component.DeleteAccount(ctx)
}
}
// MakeGetConfigurationEndpoint makes the GetConfiguration endpoint to get the config for selfservice application.
func MakeGetConfigurationEndpoint(component Component) cs.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var m = req.(map[string]string)
return component.GetConfiguration(ctx, m[PrmQryRealmID])
}
}
// MakeSendVerifyEmailEndpoint makes the SendVerifyEmail endpoint
func MakeSendVerifyEmailEndpoint(component Component) cs.Endpoint {
return func(ctx context.Context, _ interface{}) (interface{}, error) {
return nil, component.SendVerifyEmail(ctx)
}
}
// MakeSendVerifyPhoneNumberEndpoint makes the SendVerifyPhoneNumber endpoint
func MakeSendVerifyPhoneNumberEndpoint(component Component) cs.Endpoint {
return func(ctx context.Context, _ interface{}) (interface{}, error) {
return nil, component.SendVerifyPhoneNumber(ctx)
}
}