-
Notifications
You must be signed in to change notification settings - Fork 9
/
endpoint.go
82 lines (68 loc) · 2.79 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
package account
import (
"context"
"encoding/json"
cs "github.com/cloudtrust/common-service"
"github.com/cloudtrust/common-service/http"
api "github.com/cloudtrust/keycloak-bridge/api/account"
"github.com/go-kit/kit/endpoint"
)
// Endpoints wraps a service behind a set of endpoints.
type Endpoints struct {
UpdatePassword endpoint.Endpoint
GetAccount endpoint.Endpoint
UpdateAccount endpoint.Endpoint
DeleteAccount 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"`
}
// AccountComponent describes methods of the Account API
type AccountComponent interface {
UpdatePassword(ctx context.Context, currentPassword, newPassword, confirmPassword string) error
GetAccount(ctx context.Context) (api.AccountRepresentation, error)
UpdateAccount(ctx context.Context, account api.AccountRepresentation) error
DeleteAccount(ctx context.Context) error
}
// MakeUpdatePasswordEndpoint makes the UpdatePassword endpoint to update connected user's own password.
func MakeUpdatePasswordEndpoint(component AccountComponent) cs.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var m = req.(map[string]string)
var body UpdatePasswordBody
err := json.Unmarshal([]byte(m["body"]), &body)
if err != nil {
return nil, http.CreateBadRequestError("Invalid body")
}
return nil, component.UpdatePassword(ctx, body.CurrentPassword, body.NewPassword, body.ConfirmPassword)
}
}
// MakeGetAccountEndpoint makes the GetAccount endpoint to get connected user's info.
func MakeGetAccountEndpoint(component AccountComponent) cs.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
return component.GetAccount(ctx)
}
}
// MakeUpdateAccountEndpoint makes the UpdateAccount endpoint to update connected user's own info.
func MakeUpdateAccountEndpoint(component AccountComponent) cs.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var m = req.(map[string]string)
var body api.AccountRepresentation
err := json.Unmarshal([]byte(m["body"]), &body)
if err != nil {
return nil, http.CreateBadRequestError("Invalid body")
}
if err = body.Validate(); err != nil {
return nil, http.CreateBadRequestError(err.Error())
}
return nil, component.UpdateAccount(ctx, body)
}
}
// MakeDeleteAccountEndpoint makes the DeleteAccount endpoint to delete connected user.
func MakeDeleteAccountEndpoint(component AccountComponent) cs.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
return nil, component.DeleteAccount(ctx)
}
}