/
identity.go
272 lines (252 loc) · 5.73 KB
/
identity.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package api
import (
"github.com/gin-gonic/gin"
"github.com/konveyor/tackle2-hub/model"
"net/http"
"strconv"
)
//
// Routes
const (
IdentitiesRoot = "/identities"
IdentityRoot = IdentitiesRoot + "/:" + ID
)
//
// Params.
const (
Decrypted = "decrypted"
AppId = "application"
)
//
// IdentityHandler handles identity resource routes.
type IdentityHandler struct {
BaseHandler
}
func (h IdentityHandler) AddRoutes(e *gin.Engine) {
routeGroup := e.Group("/")
routeGroup.Use(Required("identities"))
routeGroup.GET(IdentitiesRoot, h.setDecrypted, h.List)
routeGroup.GET(IdentitiesRoot+"/", h.setDecrypted, h.List)
routeGroup.POST(IdentitiesRoot, h.Create)
routeGroup.GET(IdentityRoot, h.setDecrypted, h.Get)
routeGroup.PUT(IdentityRoot, h.Update)
routeGroup.DELETE(IdentityRoot, h.Delete)
}
// Get godoc
// @summary Get an identity by ID.
// @description Get an identity by ID.
// @tags identities
// @produce json
// @success 200 {object} Identity
// @router /identities/{id} [get]
// @param id path int true "Identity ID"
func (h IdentityHandler) Get(ctx *gin.Context) {
id := h.pk(ctx)
m := &model.Identity{}
result := h.DB(ctx).First(m, id)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
r := Identity{}
decrypted := ctx.GetBool(Decrypted)
if decrypted {
err := m.Decrypt()
if err != nil {
h.Status(ctx, http.StatusInternalServerError)
return
}
}
r.With(m)
h.Respond(ctx, http.StatusOK, r)
}
// List godoc
// @summary List all identities.
// @description List all identities.
// @tags identities
// @produce json
// @success 200 {object} []Identity
// @router /identities [get]
func (h IdentityHandler) List(ctx *gin.Context) {
var list []model.Identity
appId := ctx.Query(AppId)
kind := ctx.Query(Kind)
db := h.DB(ctx)
if appId != "" {
db = db.Where(
"id IN (SELECT identityID from ApplicationIdentity WHERE applicationID = ?)",
appId)
}
if kind != "" {
db = db.Where(Kind, kind)
}
result := db.Find(&list)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
decrypted := ctx.GetBool(Decrypted)
resources := []Identity{}
for i := range list {
r := Identity{}
m := &list[i]
if decrypted {
err := m.Decrypt()
if err != nil {
h.Status(ctx, http.StatusInternalServerError)
return
}
}
r.With(m)
resources = append(resources, r)
}
h.Respond(ctx, http.StatusOK, resources)
}
// Create godoc
// @summary Create an identity.
// @description Create an identity.
// @tags identities
// @accept json
// @produce json
// @success 201 {object} Identity
// @router /identities [post]
// @param identity body Identity true "Identity data"
func (h IdentityHandler) Create(ctx *gin.Context) {
r := &Identity{}
err := h.Bind(ctx, r)
if err != nil {
_ = ctx.Error(err)
return
}
m := r.Model()
m.CreateUser = h.BaseHandler.CurrentUser(ctx)
ref := &model.Identity{}
err = m.Encrypt(ref)
if err != nil {
_ = ctx.Error(err)
return
}
result := h.DB(ctx).Create(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
r.With(m)
h.Respond(ctx, http.StatusCreated, r)
}
// Delete godoc
// @summary Delete an identity.
// @description Delete an identity.
// @tags identities
// @success 204
// @router /identities/{id} [delete]
// @param id path int true "Identity ID"
func (h IdentityHandler) Delete(ctx *gin.Context) {
id := h.pk(ctx)
identity := &model.Identity{}
result := h.DB(ctx).First(identity, id)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
result = h.DB(ctx).Delete(identity)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
h.Status(ctx, http.StatusNoContent)
}
// Update godoc
// @summary Update an identity.
// @description Update an identity.
// @tags identities
// @accept json
// @success 204
// @router /identities/{id} [put]
// @param id path int true "Identity ID"
// @param identity body Identity true "Identity data"
func (h IdentityHandler) Update(ctx *gin.Context) {
id := h.pk(ctx)
r := &Identity{}
err := h.Bind(ctx, r)
if err != nil {
_ = ctx.Error(err)
return
}
ref := &model.Identity{}
err = h.DB(ctx).First(ref, id).Error
if err != nil {
_ = ctx.Error(err)
return
}
m := r.Model()
err = m.Encrypt(ref)
if err != nil {
_ = ctx.Error(err)
return
}
m.ID = id
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
err = db.Updates(h.fields(m)).Error
if err != nil {
_ = ctx.Error(err)
return
}
h.Status(ctx, http.StatusNoContent)
}
//
// Set `decrypted` in the context.
// Results in 403 when the token does not have the required scope.
func (h *IdentityHandler) setDecrypted(ctx *gin.Context) {
q := ctx.Query(Decrypted)
requested, _ := strconv.ParseBool(q)
ctx.Set(Decrypted, requested)
if requested {
if !h.HasScope(ctx, "identities:decrypt") {
h.Status(ctx, http.StatusForbidden)
} else {
ctx.Next()
}
}
return
}
//
// Identity REST resource.
type Identity struct {
Resource `yaml:",inline"`
Kind string `json:"kind" binding:"required"`
Name string `json:"name" binding:"required"`
Description string `json:"description"`
User string `json:"user"`
Password string `json:"password"`
Key string `json:"key"`
Settings string `json:"settings"`
}
//
// With updates the resource with the model.
func (r *Identity) With(m *model.Identity) {
r.Resource.With(&m.Model)
r.Kind = m.Kind
r.Name = m.Name
r.Description = m.Description
r.User = m.User
r.Password = m.Password
r.Key = m.Key
r.Settings = m.Settings
}
//
// Model builds a model.
func (r *Identity) Model() (m *model.Identity) {
m = &model.Identity{
Kind: r.Kind,
Name: r.Name,
Description: r.Description,
User: r.User,
Password: r.Password,
Key: r.Key,
Settings: r.Settings,
}
m.ID = r.ID
return
}