-
Notifications
You must be signed in to change notification settings - Fork 9
/
authorization.go
203 lines (167 loc) · 6.69 KB
/
authorization.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
package account
import (
"context"
"encoding/json"
"github.com/cloudtrust/common-service/configuration"
cs "github.com/cloudtrust/common-service"
"github.com/cloudtrust/common-service/security"
"github.com/cloudtrust/common-service/log"
api "github.com/cloudtrust/keycloak-bridge/api/account"
"github.com/cloudtrust/keycloak-bridge/internal/keycloakb"
)
// Creates constants for API method names
const (
UpdatePassword = "UpdatePassword"
GetCredentials = "GetCredentials"
GetCredentialRegistrators = "GetCredentialRegistrators"
UpdateLabelCredential = "UpdateLabelCredential"
DeleteCredential = "DeleteCredential"
MoveCredential = "MoveCredential"
GetAccount = "GetAccount"
UpdateAccount = "UpdateAccount"
DeleteAccount = "DeleteAccount"
GetConfiguration = "GetConfiguration"
infosAction = "Action"
infosCurrentRealm = "currentRealm"
)
// Tracking middleware at component level.
type authorizationComponentMW struct {
logger log.Logger
configDBModule keycloakb.ConfigurationDBModule
next Component
}
// MakeAuthorizationAccountComponentMW checks authorization and return an error if the action is not allowed.
func MakeAuthorizationAccountComponentMW(logger log.Logger, configDBModule keycloakb.ConfigurationDBModule) func(Component) Component {
return func(next Component) Component {
return &authorizationComponentMW{
logger: logger,
configDBModule: configDBModule,
next: next,
}
}
}
// authorizationComponentMW implements Component.
func (c *authorizationComponentMW) UpdatePassword(ctx context.Context, currentPassword, newPassword, confirmPassword string) error {
var action = UpdatePassword
var currentRealm = ctx.Value(cs.CtContextRealm).(string)
var config = configuration.RealmConfiguration{}
var err error
if config, err = c.configDBModule.GetConfiguration(ctx, currentRealm); err != nil {
infos, _ := json.Marshal(map[string]string{
infosCurrentRealm: currentRealm,
})
c.logger.Error(ctx, "Error", "Configuration not found", "infos", string(infos))
return err
}
if !isEnabled(config.APISelfPasswordChangeEnabled) {
infos, _ := json.Marshal(map[string]string{
infosAction: action,
infosCurrentRealm: currentRealm,
})
c.logger.Debug(ctx, "ForbiddenError", "Password change disabled", "infos", string(infos))
return security.ForbiddenError{}
}
return c.next.UpdatePassword(ctx, currentPassword, newPassword, confirmPassword)
}
// authorizationComponentMW implements Component.
func (c *authorizationComponentMW) GetCredentials(ctx context.Context) ([]api.CredentialRepresentation, error) {
// No restriction for this call
return c.next.GetCredentials(ctx)
}
func (c *authorizationComponentMW) GetCredentialRegistrators(ctx context.Context) ([]string, error) {
// No restriction for this call
return c.next.GetCredentialRegistrators(ctx)
}
func (c *authorizationComponentMW) UpdateLabelCredential(ctx context.Context, credentialID string, label string) error {
// No restriction for this call
return c.next.UpdateLabelCredential(ctx, credentialID, label)
}
func (c *authorizationComponentMW) MoveCredential(ctx context.Context, credentialID string, previousCredentialID string) error {
// No restriction for this call
return c.next.MoveCredential(ctx, credentialID, previousCredentialID)
}
func (c *authorizationComponentMW) DeleteCredential(ctx context.Context, credentialID string) error {
var action = DeleteCredential
var currentRealm = ctx.Value(cs.CtContextRealm).(string)
var config = configuration.RealmConfiguration{}
var err error
if config, err = c.configDBModule.GetConfiguration(ctx, currentRealm); err != nil {
infos, _ := json.Marshal(map[string]string{
infosCurrentRealm: currentRealm,
})
c.logger.Error(ctx, "Error", "Configuration not found", "infos", string(infos))
return err
}
if !isEnabled(config.APISelfAuthenticatorDeletionEnabled) {
infos, _ := json.Marshal(map[string]string{
infosAction: action,
infosCurrentRealm: currentRealm,
})
c.logger.Debug(ctx, "ForbiddenError", "Authenticator deletion disabled", "infos", string(infos))
return security.ForbiddenError{}
}
return c.next.DeleteCredential(ctx, credentialID)
}
func (c *authorizationComponentMW) GetAccount(ctx context.Context) (api.AccountRepresentation, error) {
// No restriction for this call
return c.next.GetAccount(ctx)
}
func (c *authorizationComponentMW) UpdateAccount(ctx context.Context, account api.UpdatableAccountRepresentation) error {
var action = UpdateAccount
var currentRealm = ctx.Value(cs.CtContextRealm).(string)
var config = configuration.RealmConfiguration{}
var err error
if config, err = c.configDBModule.GetConfiguration(ctx, currentRealm); err != nil {
infos, _ := json.Marshal(map[string]string{
infosCurrentRealm: currentRealm,
})
c.logger.Error(ctx, "Error", "Configuration not found", "infos", string(infos))
return err
}
if !isEnabled(config.APISelfAccountEditingEnabled) {
infos, _ := json.Marshal(map[string]string{
infosAction: action,
infosCurrentRealm: currentRealm,
})
c.logger.Debug(ctx, "ForbiddenError", "Account edition disabled", "infos", string(infos))
return security.ForbiddenError{}
}
return c.next.UpdateAccount(ctx, account)
}
func (c *authorizationComponentMW) DeleteAccount(ctx context.Context) error {
var action = DeleteAccount
var currentRealm = ctx.Value(cs.CtContextRealm).(string)
var err error
var config = configuration.RealmConfiguration{}
if config, err = c.configDBModule.GetConfiguration(ctx, currentRealm); err != nil {
infos, _ := json.Marshal(map[string]string{
infosCurrentRealm: currentRealm,
})
c.logger.Error(ctx, "Error", "Configuration not found", "infos", string(infos))
return err
}
if !isEnabled(config.APISelfAccountDeletionEnabled) {
infos, _ := json.Marshal(map[string]string{
infosAction: action,
infosCurrentRealm: currentRealm,
})
c.logger.Debug(ctx, "ForbiddenError", "Account deletion disabled", "infos", string(infos))
return security.ForbiddenError{}
}
return c.next.DeleteAccount(ctx)
}
func (c *authorizationComponentMW) GetConfiguration(ctx context.Context, realmIDOverride string) (api.Configuration, error) {
//No restriction for this call
return c.next.GetConfiguration(ctx, realmIDOverride)
}
func (c *authorizationComponentMW) SendVerifyEmail(ctx context.Context) error {
// No restriction for this call
return c.next.SendVerifyEmail(ctx)
}
func (c *authorizationComponentMW) SendVerifyPhoneNumber(ctx context.Context) error {
// No restriction for this call
return c.next.SendVerifyPhoneNumber(ctx)
}
func isEnabled(booleanPtr *bool) bool {
return booleanPtr != nil && *booleanPtr
}