/
api.go
207 lines (186 loc) · 6.61 KB
/
api.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package apicommunications
import (
"encoding/json"
"github.com/cloudtrust/common-service/validation"
"github.com/cloudtrust/keycloak-bridge/internal/constants"
kc "github.com/cloudtrust/keycloak-client"
)
const (
prmEmailRecipient = "email_recipient"
prmEmailTheming = "email_theming"
prmEmailThemingSubjectKey = "email_theming_subject_key"
prmEmailThemingLocale = "email_theming_template_locale"
prmSMSMSISDN = "sms_msisdn"
prmSMSTheming = "sms_theming"
prmSMSThemingLocale = "sms_theming_locale"
regExpThemingSubjectKey = constants.RegExpDescription
)
// ActionRepresentation struct
type ActionRepresentation struct {
Name *string `json:"name"`
Scope *string `json:"scope"`
}
// EmailRepresentation struct
type EmailRepresentation struct {
Recipient *string `json:"recipient,omitempty"`
Theming *EmailThemingRepresentation `json:"theming,omitempty"`
Attachments *[]AttachementRepresentation `json:"attachments,omitempty"`
}
// EmailThemingRepresentation struct
type EmailThemingRepresentation struct {
SubjectKey *string `json:"subjectKey,omitempty"`
SubjectParameters *[]string `json:"subjectParameters,omitempty"`
Template *string `json:"template,omitempty"`
TemplateParameters *map[string]string `json:"templateParameters,omitempty"`
Locale *string `json:"locale,omitempty"`
}
// AttachementRepresentation struct
type AttachementRepresentation struct {
Filename *string `json:"filename,omitempty"`
ContentType *string `json:"contentType,omitempty"`
Content *string `json:"content,omitempty"`
}
// ExportEmailToKeycloak exports the email representation into a Keycloak email representation
func ExportEmailToKeycloak(r *EmailRepresentation) *kc.EmailRepresentation {
var kcRep kc.EmailRepresentation
if r == nil {
return nil
}
if r.Recipient != nil {
kcRep.Recipient = r.Recipient
}
if r.Theming != nil {
kcRep.Theming = &kc.EmailThemingRepresentation{}
kcRep.Theming.SubjectKey = r.Theming.SubjectKey
kcRep.Theming.SubjectParameters = r.Theming.SubjectParameters
kcRep.Theming.Template = r.Theming.Template
kcRep.Theming.TemplateParameters = r.Theming.TemplateParameters
kcRep.Theming.Locale = r.Theming.Locale
}
if r.Attachments != nil {
kcRep.Attachments = &[]kc.AttachementRepresentation{}
for _, a := range *r.Attachments {
tmp := kc.AttachementRepresentation{
Filename: a.Filename,
ContentType: a.ContentType,
Content: a.Content,
}
*kcRep.Attachments = append(*kcRep.Attachments, tmp)
}
}
return &kcRep
}
// ImportEmailFromKeycloak imports the email representation from a Keycloak email representation
func ImportEmailFromKeycloak(kcRep *kc.EmailRepresentation) *EmailRepresentation {
var r EmailRepresentation
if kcRep == nil {
return nil
}
if kcRep.Recipient != nil {
r.Recipient = kcRep.Recipient
}
if kcRep.Theming != nil {
r.Theming = &EmailThemingRepresentation{}
r.Theming.SubjectKey = kcRep.Theming.SubjectKey
r.Theming.SubjectParameters = kcRep.Theming.SubjectParameters
r.Theming.Template = kcRep.Theming.Template
r.Theming.TemplateParameters = kcRep.Theming.TemplateParameters
r.Theming.Locale = kcRep.Theming.Locale
}
if kcRep.Attachments != nil {
r.Attachments = &[]AttachementRepresentation{}
for _, a := range *kcRep.Attachments {
tmp := AttachementRepresentation{
Filename: a.Filename,
ContentType: a.ContentType,
Content: a.Content,
}
*r.Attachments = append(*r.Attachments, tmp)
}
}
return &r
}
// EmailFromJSON creates an email using its json representation
func EmailFromJSON(jsonRep string) (EmailRepresentation, error) {
var email EmailRepresentation
err := json.Unmarshal([]byte(jsonRep), &email)
return email, err
}
// EmailToJSON returns a json representation of a given Email
func (r *EmailRepresentation) EmailToJSON() string {
var bytes, _ = json.Marshal(r)
return string(bytes)
}
// SMSRepresentation struct
type SMSRepresentation struct {
MSISDN *string `json:"msisdn,omitempty"`
Theming *SMSThemingRepresentation `json:"theming,omitempty"`
}
// SMSThemingRepresentation struct
type SMSThemingRepresentation struct {
MessageKey *string `json:"messageKey,omitempty"`
MessageParameters *[]string `json:"messageParameters,omitempty"`
Locale *string `json:"locale,omitempty"`
}
// ExportSMSToKeycloak exports the SMS representation into a Keycloak SMS representation
func ExportSMSToKeycloak(r *SMSRepresentation) *kc.SMSRepresentation {
var kcRep kc.SMSRepresentation
if r == nil {
return nil
}
if r.MSISDN != nil {
kcRep.MSISDN = r.MSISDN
}
if r.Theming != nil {
kcRep.Theming = &kc.SMSThemingRepresentation{}
kcRep.Theming.MessageKey = r.Theming.MessageKey
kcRep.Theming.MessageParameters = r.Theming.MessageParameters
kcRep.Theming.Locale = r.Theming.Locale
}
return &kcRep
}
// ImportSMSFromKeycloak exports the SMS representation into a Keycloak SMS representation
func ImportSMSFromKeycloak(kcRep *kc.SMSRepresentation) *SMSRepresentation {
var r SMSRepresentation
if kcRep == nil {
return nil
}
if kcRep.MSISDN != nil {
r.MSISDN = kcRep.MSISDN
}
if kcRep.Theming != nil {
r.Theming = &SMSThemingRepresentation{}
r.Theming.MessageKey = kcRep.Theming.MessageKey
r.Theming.MessageParameters = kcRep.Theming.MessageParameters
r.Theming.Locale = kcRep.Theming.Locale
}
return &r
}
// SMSFromJSON creates an sms using its json representation
func SMSFromJSON(jsonRep string) (SMSRepresentation, error) {
var sms SMSRepresentation
err := json.Unmarshal([]byte(jsonRep), &sms)
return sms, err
}
// SMSToJSON returns a json representation of a given SMS
func (r *SMSRepresentation) SMSToJSON() string {
var bytes, _ = json.Marshal(r)
return string(bytes)
}
// Validate checks the validity of the given email
func (r *EmailRepresentation) Validate() error {
return validation.NewParameterValidator().
ValidateParameterRegExp(prmEmailRecipient, r.Recipient, constants.RegExpEmail, true).
ValidateParameterNotNil(prmEmailTheming, r.Theming).
ValidateParameterRegExp(prmEmailThemingSubjectKey, r.Theming.SubjectKey, regExpThemingSubjectKey, true).
ValidateParameterRegExp(prmEmailThemingLocale, r.Theming.Locale, constants.RegExpLocale, false).
Status()
}
// Validate checks the validity of the given sms
func (r *SMSRepresentation) Validate() error {
return validation.NewParameterValidator().
ValidateParameterPhoneNumber(prmSMSMSISDN, r.MSISDN, true).
ValidateParameterNotNil(prmSMSTheming, r.Theming).
ValidateParameterRegExp(prmSMSThemingLocale, r.Theming.Locale, constants.RegExpLocale, false).
Status()
}