/
accounts.go
224 lines (191 loc) · 5.86 KB
/
accounts.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
package data
import (
"encoding/json"
"errors"
"net/http"
"strings"
"github.com/cozy/cozy-stack/model/account"
"github.com/cozy/cozy-stack/model/oauth"
"github.com/cozy/cozy-stack/model/permission"
"github.com/cozy/cozy-stack/pkg/consts"
"github.com/cozy/cozy-stack/pkg/couchdb"
"github.com/cozy/cozy-stack/pkg/jsonapi"
"github.com/cozy/cozy-stack/pkg/metadata"
"github.com/cozy/cozy-stack/web/middlewares"
"github.com/labstack/echo/v4"
)
// XXX: it would be better to have specific routes for managing accounts. The
// overriding of the /data/io.cozy.accounts/* routes is here mainly for
// retro-compatible reasons, but specific routes would improve the API.
func getAccount(c echo.Context) error {
instance := middlewares.GetInstance(c)
doctype := consts.Accounts
docid := c.Get("docid").(string)
if docid == "" {
return dbStatus(c)
}
var out couchdb.JSONDoc
var err error
rev := c.QueryParam("rev")
if rev != "" {
err = couchdb.GetDoc(instance, consts.SoftDeletedAccounts, docid, &out)
if err == nil && out.M["soft_deleted_rev"] != rev {
err = errors.New("invalid rev")
}
if err != nil {
err = couchdb.GetDocRev(instance, doctype, docid, rev, &out)
}
} else {
err = couchdb.GetDoc(instance, doctype, docid, &out)
}
if err != nil {
return fixErrorNoDatabaseIsWrongDoctype(err)
}
out.Type = doctype
if err = middlewares.Allow(c, permission.GET, &out); err != nil {
return err
}
if account.Encrypt(out) {
if err = couchdb.UpdateDoc(instance, &out); err != nil {
return err
}
}
perm, err := middlewares.GetPermission(c)
if err != nil {
return err
}
if perm.Type == permission.TypeKonnector ||
(c.QueryParam("include") == "credentials" && perm.Type == permission.TypeWebapp) {
// The account decryption is allowed for konnectors or for apps services
account.Decrypt(out)
}
return c.JSON(http.StatusOK, out.ToMapWithType())
}
func updateAccount(c echo.Context) error {
instance := middlewares.GetInstance(c)
docid := c.Get("docid").(string)
var doc couchdb.JSONDoc
if err := json.NewDecoder(c.Request().Body).Decode(&doc); err != nil {
return jsonapi.Errorf(http.StatusBadRequest, "%s", err)
}
doc.Type = consts.Accounts
if (doc.ID() == "") != (doc.Rev() == "") {
return jsonapi.NewError(http.StatusBadRequest,
"You must either provide an _id and _rev in document (update) or neither (create with fixed id).")
}
if doc.ID() != "" && doc.ID() != docid {
return jsonapi.NewError(http.StatusBadRequest, "document _id doesnt match url")
}
if doc.ID() == "" {
doc.SetID(docid)
return createNamedDoc(c, doc)
}
errWhole := middlewares.AllowWholeType(c, permission.PUT, doc.DocType())
if errWhole != nil {
// we cant apply to whole type, let's fetch old doc and see if it applies there
var old couchdb.JSONDoc
errFetch := couchdb.GetDoc(instance, doc.DocType(), doc.ID(), &old)
if errFetch != nil {
return errFetch
}
old.Type = doc.DocType()
// check if permissions set allows manipulating old doc
errOld := middlewares.Allow(c, permission.PUT, &old)
if errOld != nil {
return errOld
}
// also check if permissions set allows manipulating new doc
errNew := middlewares.Allow(c, permission.PUT, &doc)
if errNew != nil {
return errNew
}
}
account.Encrypt(doc)
if doc.M["cozyMetadata"] == nil {
// This is not the expected type for a JSON doc but it should work since it
// will be marshalled when saved.
doc.M["cozyMetadata"] = CozyMetadataFromClaims(c)
}
errUpdate := couchdb.UpdateDoc(instance, &doc)
if errUpdate != nil {
return fixErrorNoDatabaseIsWrongDoctype(errUpdate)
}
perm, err := middlewares.GetPermission(c)
if err != nil {
return err
}
if perm.Type == permission.TypeKonnector {
account.Decrypt(doc)
}
return c.JSON(http.StatusOK, echo.Map{
"ok": true,
"id": doc.ID(),
"rev": doc.Rev(),
"type": doc.DocType(),
"data": doc.ToMapWithType(),
})
}
func createAccount(c echo.Context) error {
doctype := consts.Accounts
instance := middlewares.GetInstance(c)
doc := couchdb.JSONDoc{Type: doctype}
if err := json.NewDecoder(c.Request().Body).Decode(&doc.M); err != nil {
return jsonapi.Errorf(http.StatusBadRequest, "%s", err)
}
if err := middlewares.Allow(c, permission.POST, &doc); err != nil {
return err
}
account.Encrypt(doc)
account.ComputeName(doc)
// This is not the expected type for a JSON doc but it should work since it
// will be marshalled when saved.
doc.M["cozyMetadata"] = CozyMetadataFromClaims(c)
if err := couchdb.CreateDoc(instance, &doc); err != nil {
return err
}
return c.JSON(http.StatusCreated, echo.Map{
"ok": true,
"id": doc.ID(),
"rev": doc.Rev(),
"type": doc.DocType(),
"data": doc.ToMapWithType(),
})
}
// CozyMetadataFromClaims returns a CozyMetadata struct, with the app fields
// filled with information from the permission claims.
func CozyMetadataFromClaims(c echo.Context) *metadata.CozyMetadata {
cm := metadata.New()
var slug, version string
if claims := c.Get("claims"); claims != nil {
cl := claims.(permission.Claims)
switch cl.AudienceString() {
case consts.AppAudience, consts.KonnectorAudience:
slug = cl.Subject
case consts.AccessTokenAudience:
if perms, err := middlewares.GetPermission(c); err == nil {
if cli, ok := perms.Client.(*oauth.Client); ok {
slug = oauth.GetLinkedAppSlug(cli.SoftwareID)
// Special case for cozy-desktop: it is an OAuth app not linked
// to a web app, so it has no slug, but we still want to keep
// in cozyMetadata its changes, so we use a fake slug.
if slug == "" && strings.Contains(cli.SoftwareID, "cozy-desktop") {
slug = "cozy-desktop"
}
version = cli.SoftwareVersion
}
}
}
}
if slug != "" {
cm.CreatedByApp = slug
cm.CreatedByAppVersion = version
cm.UpdatedByApps = []*metadata.UpdatedByAppEntry{
{
Slug: slug,
Version: version,
Date: cm.UpdatedAt,
},
}
}
return cm
}