-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransport.go
More file actions
142 lines (123 loc) · 5.07 KB
/
transport.go
File metadata and controls
142 lines (123 loc) · 5.07 KB
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
package goauthlib
import (
"github.com/techpro-studio/gohttplib"
"net/http"
)
type Transport struct {
useCase UseCase
}
func NewTransport(useCase UseCase) *Transport {
return &Transport{useCase: useCase}
}
func (t *Transport) withBody(w http.ResponseWriter, r *http.Request, handler func(body map[string]interface{}) (interface{}, error)) {
body, err := gohttplib.GetBody(r)
if err != nil {
gohttplib.SafeConvertToServerError(err).Write(w)
return
}
resp, err := handler(body)
gohttplib.WriteJsonOrError(w, resp, 200, err)
}
func (t *Transport) withAuthorizationEntity(w http.ResponseWriter, r *http.Request, handler func(entity AuthorizationEntity) (interface{}, error)) {
t.withBody(w, r, func(body map[string]interface{}) (i interface{}, e error) {
entity, err := GetAuthorizationEntityFromBody(body)
if err != nil {
return nil, err
}
return handler(*entity)
})
}
func (t *Transport) withOAuthPayload(w http.ResponseWriter, r *http.Request, handler func(payload SocialProviderPayload) (interface{}, error)) {
t.withBody(w, r, func(body map[string]interface{}) (i interface{}, e error) {
providerPayload, err := GetSocialProviderPayloadInfo(body)
if err != nil {
return nil, err
}
return handler(*providerPayload)
})
}
func (t *Transport) withAuthorizationEntityAndCode(w http.ResponseWriter, r *http.Request, handler func(entity AuthorizationEntity, code string) (interface{}, error)) {
t.withBody(w, r, func(body map[string]interface{}) (i interface{}, e error) {
entity, err := GetAuthorizationEntityFromBody(body)
code, err := GetCode(body)
if err != nil {
return nil, err
}
return handler(*entity, code)
})
}
func (t *Transport) CurrentUserHandler(w http.ResponseWriter, r *http.Request) {
gohttplib.WriteJson(w, GetUserFromRequestWithPanic(r), 200)
}
func (t *Transport) AuthenticateViaSocialProviderHandler(w http.ResponseWriter, r *http.Request) {
t.withOAuthPayload(w, r, func(payload SocialProviderPayload) (i interface{}, e error) {
return t.useCase.AuthenticateViaSocialProvider(r.Context(), payload)
})
}
func (t *Transport) SendCodeHandler(w http.ResponseWriter, r *http.Request) {
t.withAuthorizationEntity(w, r, func(entity AuthorizationEntity) (i interface{}, e error) {
return OK, t.useCase.SendCode(r.Context(), entity)
})
}
func (t *Transport) SendVerificationCodeHandler(writer http.ResponseWriter, request *http.Request) {
usr := GetUserFromRequestWithPanic(request)
err := t.useCase.SendVerificationCode(request.Context(), usr, deleteAccountAction)
gohttplib.WriteJsonOrError(writer, OK, 200, err)
}
func (t *Transport) VerifyDeleteHandler(writer http.ResponseWriter, request *http.Request) {
usr := GetUserFromRequestWithPanic(request)
body, err := gohttplib.GetBody(request)
if err != nil {
gohttplib.SafeConvertToServerError(err).Write(writer)
return
}
code, err := GetCode(body)
if err != nil {
gohttplib.SafeConvertToServerError(err).Write(writer)
return
}
err = t.useCase.VerifyDelete(request.Context(), usr, code)
gohttplib.WriteJsonOrError(writer, OK, 200, err)
}
func (t *Transport) ForceDeleteHandler(writer http.ResponseWriter, request *http.Request) {
usr := GetUserFromRequestWithPanic(request)
err := t.useCase.ForceDelete(request.Context(), usr)
gohttplib.WriteJsonOrError(writer, OK, 200, err)
}
func (t *Transport) AuthenticateWithCodeHandler(w http.ResponseWriter, r *http.Request) {
t.withAuthorizationEntityAndCode(w, r, func(entity AuthorizationEntity, code string) (i interface{}, e error) {
return t.useCase.AuthenticateWithCode(r.Context(), entity, code)
})
}
func (t *Transport) RemoveAuthenticationEntityHandler(w http.ResponseWriter, r *http.Request) {
t.withAuthorizationEntity(w, r, func(entity AuthorizationEntity) (i interface{}, e error) {
return OK, t.useCase.RemoveAuthenticationEntity(r.Context(), GetUserFromRequestWithPanic(r), entity)
})
}
func (t *Transport) AddSocialAuthenticationEntityHandler(w http.ResponseWriter, r *http.Request) {
t.withOAuthPayload(w, r, func(payload SocialProviderPayload) (i interface{}, e error) {
usr := GetUserFromRequestWithPanic(r)
return t.useCase.AddSocialAuthenticationEntity(r.Context(), &usr, payload)
})
}
func (t *Transport) SendCodeWithUserHandler(w http.ResponseWriter, r *http.Request) {
t.withAuthorizationEntity(w, r, func(entity AuthorizationEntity) (i interface{}, e error) {
return OK, t.useCase.SendCodeWithUser(r.Context(), GetUserFromRequestWithPanic(r), entity)
})
}
func (t *Transport) VerifyAuthenticationEntityHandler(w http.ResponseWriter, r *http.Request) {
t.withAuthorizationEntityAndCode(w, r, func(entity AuthorizationEntity, code string) (i interface{}, e error) {
usr := GetUserFromRequestWithPanic(r)
return t.useCase.VerifyAuthenticationEntity(r.Context(), &usr, entity, code)
})
}
func (t *Transport) PatchInfoHandler(w http.ResponseWriter, r *http.Request) {
usr := GetUserFromRequestWithPanic(r)
body, err := gohttplib.GetBody(r)
if err != nil {
gohttplib.SafeConvertToServerError(err).Write(w)
return
}
patched, err := t.useCase.PatchUserInfo(r.Context(), &usr, body)
gohttplib.WriteJsonOrError(w, patched, 200, err)
}