/
instance.go
191 lines (158 loc) · 5.13 KB
/
instance.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
// Package settings regroups some API methods to facilitate the usage of the
// io.cozy settings documents.
package settings
import (
"encoding/json"
"net/http"
"github.com/cozy/cozy-stack/model/instance"
"github.com/cozy/cozy-stack/model/instance/lifecycle"
"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/web/middlewares"
"github.com/labstack/echo/v4"
)
type apiInstance struct {
doc *couchdb.JSONDoc
}
func (i *apiInstance) ID() string { return i.doc.ID() }
func (i *apiInstance) Rev() string { return i.doc.Rev() }
func (i *apiInstance) DocType() string { return consts.Settings }
func (i *apiInstance) Clone() couchdb.Doc { return i }
func (i *apiInstance) SetID(id string) { i.doc.SetID(id) }
func (i *apiInstance) SetRev(rev string) { i.doc.SetRev(rev) }
func (i *apiInstance) Relationships() jsonapi.RelationshipMap { return nil }
func (i *apiInstance) Included() []jsonapi.Object { return nil }
func (i *apiInstance) Links() *jsonapi.LinksList {
return &jsonapi.LinksList{Self: "/settings/instance"}
}
func (i *apiInstance) MarshalJSON() ([]byte, error) {
return json.Marshal(i.doc)
}
func getInstance(c echo.Context) error {
inst := middlewares.GetInstance(c)
doc, err := inst.SettingsDocument()
if err != nil {
return err
}
doc.M["locale"] = inst.Locale
doc.M["onboarding_finished"] = inst.OnboardingFinished
doc.M["auto_update"] = !inst.NoAutoUpdate
doc.M["auth_mode"] = instance.AuthModeToString(inst.AuthMode)
doc.M["tos"] = inst.TOSSigned
doc.M["tos_latest"] = inst.TOSLatest
doc.M["uuid"] = inst.UUID
doc.M["oidc_id"] = inst.OIDCID
doc.M["context"] = inst.ContextName
if err = middlewares.Allow(c, permission.GET, doc); err != nil {
return err
}
return jsonapi.Data(c, http.StatusOK, &apiInstance{doc}, nil)
}
func updateInstance(c echo.Context) error {
inst := middlewares.GetInstance(c)
doc := &couchdb.JSONDoc{}
obj, err := jsonapi.Bind(c.Request().Body, doc)
if err != nil {
return err
}
doc.Type = consts.Settings
doc.SetID(consts.InstanceSettingsID)
doc.SetRev(obj.Meta.Rev)
if err = middlewares.Allow(c, permission.PUT, doc); err != nil {
return err
}
pdoc, err := middlewares.GetPermission(c)
if err != nil || pdoc.Type != permission.TypeCLI {
delete(doc.M, "auth_mode")
delete(doc.M, "tos")
delete(doc.M, "tos_latest")
delete(doc.M, "uuid")
delete(doc.M, "context")
delete(doc.M, "oidc_id")
}
if err := lifecycle.Patch(inst, &lifecycle.Options{SettingsObj: doc}); err != nil {
return err
}
doc.M["locale"] = inst.Locale
doc.M["onboarding_finished"] = inst.OnboardingFinished
doc.M["auto_update"] = !inst.NoAutoUpdate
doc.M["auth_mode"] = instance.AuthModeToString(inst.AuthMode)
doc.M["tos"] = inst.TOSSigned
doc.M["tos_latest"] = inst.TOSLatest
doc.M["uuid"] = inst.UUID
doc.M["oidc_id"] = inst.OIDCID
doc.M["context"] = inst.ContextName
return jsonapi.Data(c, http.StatusOK, &apiInstance{doc}, nil)
}
func updateInstanceTOS(c echo.Context) error {
inst := middlewares.GetInstance(c)
// Allow any request from OAuth tokens to use this route
pdoc, err := middlewares.GetPermission(c)
if err != nil {
return err
}
if pdoc.Type != permission.TypeOauth && pdoc.Type != permission.TypeCLI {
return echo.NewHTTPError(http.StatusForbidden)
}
if err := lifecycle.ManagerSignTOS(inst, c.Request()); err != nil {
return err
}
return c.NoContent(http.StatusNoContent)
}
func updateInstanceAuthMode(c echo.Context) error {
inst := middlewares.GetInstance(c)
if err := middlewares.AllowWholeType(c, permission.PUT, consts.Settings); err != nil {
return err
}
args := struct {
AuthMode string `json:"auth_mode"`
TwoFactorActivationCode string `json:"two_factor_activation_code"`
}{}
if err := c.Bind(&args); err != nil {
return err
}
authMode, err := instance.StringToAuthMode(args.AuthMode)
if err != nil {
return jsonapi.BadRequest(err)
}
if inst.HasAuthMode(authMode) {
return c.NoContent(http.StatusNoContent)
}
switch authMode {
case instance.Basic:
case instance.TwoFactorMail:
if args.TwoFactorActivationCode == "" {
if err = lifecycle.SendMailConfirmationCode(inst); err != nil {
return err
}
return c.NoContent(http.StatusNoContent)
}
if ok := inst.ValidateMailConfirmationCode(args.TwoFactorActivationCode); !ok {
return c.NoContent(http.StatusUnprocessableEntity)
}
}
err = lifecycle.Patch(inst, &lifecycle.Options{AuthMode: args.AuthMode})
if err != nil {
return err
}
return c.NoContent(http.StatusNoContent)
}
func clearMovedFrom(c echo.Context) error {
if !middlewares.IsLoggedIn(c) {
return echo.NewHTTPError(http.StatusForbidden)
}
inst := middlewares.GetInstance(c)
doc, err := inst.SettingsDocument()
if err != nil {
return err
}
if doc.M["moved_from"] != nil {
delete(doc.M, "moved_from")
if err := couchdb.UpdateDoc(inst, doc); err != nil {
return err
}
}
return c.NoContent(http.StatusNoContent)
}