-
Notifications
You must be signed in to change notification settings - Fork 137
/
instance.go
778 lines (691 loc) · 23.9 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
// Package instance is for the instance model, with domain, locale, settings,
// etc.
package instance
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"path"
"strings"
"time"
"github.com/cozy/cozy-stack/model/permission"
"github.com/cozy/cozy-stack/model/vfs"
"github.com/cozy/cozy-stack/model/vfs/vfsafero"
"github.com/cozy/cozy-stack/model/vfs/vfsswift"
build "github.com/cozy/cozy-stack/pkg/config"
"github.com/cozy/cozy-stack/pkg/config/config"
"github.com/cozy/cozy-stack/pkg/consts"
"github.com/cozy/cozy-stack/pkg/couchdb"
"github.com/cozy/cozy-stack/pkg/crypto"
"github.com/cozy/cozy-stack/pkg/i18n"
"github.com/cozy/cozy-stack/pkg/jsonapi"
"github.com/cozy/cozy-stack/pkg/lock"
"github.com/cozy/cozy-stack/pkg/logger"
"github.com/cozy/cozy-stack/pkg/prefixer"
"github.com/cozy/cozy-stack/pkg/realtime"
"github.com/golang-jwt/jwt/v5"
"github.com/spf13/afero"
)
// DefaultTemplateTitle represents the default template title. It could be
// overrided by configuring it in the instance context parameters
const DefaultTemplateTitle = "Cozy"
// PBKDF2_SHA256 is the value of kdf for using PBKDF2 with SHA256 to hash the
// password on client side.
//
//lint:ignore ST1003 we prefer ALL_CAPS here
const PBKDF2_SHA256 = 0
// An Instance has the informations relatives to the logical cozy instance,
// like the domain, the locale or the access to the databases and files storage
// It is a couchdb.Doc to be persisted in couchdb.
type Instance struct {
DocID string `json:"_id,omitempty"` // couchdb _id
DocRev string `json:"_rev,omitempty"` // couchdb _rev
Domain string `json:"domain"` // The main DNS domain, like example.cozycloud.cc
DomainAliases []string `json:"domain_aliases,omitempty"`
Prefix string `json:"prefix,omitempty"` // Possible database prefix
Locale string `json:"locale"` // The locale used on the server
UUID string `json:"uuid,omitempty"` // UUID associated with the instance
OIDCID string `json:"oidc_id,omitempty"` // An identifier to check authentication from OIDC
FranceConnectID string `json:"franceconnect_id,omitempty"` // An identifier to check authentication from FranceConnect
ContextName string `json:"context,omitempty"` // The context attached to the instance
Sponsorships []string `json:"sponsorships,omitempty"` // The list of sponsorships for the instance
TOSSigned string `json:"tos,omitempty"` // Terms of Service signed version
TOSLatest string `json:"tos_latest,omitempty"` // Terms of Service latest version
AuthMode AuthMode `json:"auth_mode,omitempty"` // 2 factor authentication
MagicLink bool `json:"magic_link,omitempty"` // Authentication via a link sent by email
Deleting bool `json:"deleting,omitempty"`
Moved bool `json:"moved,omitempty"` // If the instance has been moved to a new place
Blocked bool `json:"blocked,omitempty"` // Whether or not the instance is blocked
BlockingReason string `json:"blocking_reason,omitempty"` // Why the instance is blocked
NoAutoUpdate bool `json:"no_auto_update,omitempty"` // Whether or not the instance has auto updates for its applications
OnboardingFinished bool `json:"onboarding_finished,omitempty"` // Whether or not the onboarding is complete.
PasswordDefined *bool `json:"password_defined"` // 3 possibles states: true, false, and unknown (for legacy reasons)
BytesDiskQuota int64 `json:"disk_quota,string,omitempty"` // The total size in bytes allowed to the user
IndexViewsVersion int `json:"indexes_version,omitempty"`
// Swift layout number:
// - 0 for layout v1
// - 1 for layout v2
// - 2 for layout v3
// It is called swift_cluster in CouchDB and indexed from 0 for legacy reasons.
// See model/vfs/vfsswift for more details.
SwiftLayout int `json:"swift_cluster,omitempty"`
CouchCluster int `json:"couch_cluster,omitempty"`
// PassphraseHash is a hash of a hash of the user's passphrase: the
// passphrase is first hashed in client-side to avoid sending it to the
// server as it also used for encryption on client-side, and after that,
// hashed on the server to ensure robustness. For more informations on the
// server-side hashing, see crypto.GenerateFromPassphrase.
PassphraseHash []byte `json:"passphrase_hash,omitempty"`
PassphraseResetToken []byte `json:"passphrase_reset_token,omitempty"`
PassphraseResetTime *time.Time `json:"passphrase_reset_time,omitempty"`
// Secure assets
// Register token is used on registration to prevent from stealing instances
// waiting for registration. The registerToken secret is only shared (in
// clear) with the instance's user.
RegisterToken []byte `json:"register_token,omitempty"`
// SessSecret is used to authenticate session cookies
SessSecret []byte `json:"session_secret,omitempty"`
// OAuthSecret is used to authenticate OAuth2 token
OAuthSecret []byte `json:"oauth_secret,omitempty"`
// CLISecret is used to authenticate request from the CLI
CLISecret []byte `json:"cli_secret,omitempty"`
// FeatureFlags is the feature flags that are specific to this instance
FeatureFlags map[string]interface{} `json:"feature_flags,omitempty"`
// FeatureSets is a list of feature sets from the manager
FeatureSets []string `json:"feature_sets,omitempty"`
// LastActivityFromDeletedOAuthClients is the date of the last activity for
// OAuth clients that have been deleted
LastActivityFromDeletedOAuthClients *time.Time `json:"last_activity_from_deleted_oauth_clients,omitempty"`
vfs vfs.VFS
contextualDomain string
}
// DocType implements couchdb.Doc
func (i *Instance) DocType() string { return consts.Instances }
// ID implements couchdb.Doc
func (i *Instance) ID() string { return i.DocID }
// SetID implements couchdb.Doc
func (i *Instance) SetID(v string) { i.DocID = v }
// Rev implements couchdb.Doc
func (i *Instance) Rev() string { return i.DocRev }
// SetRev implements couchdb.Doc
func (i *Instance) SetRev(v string) { i.DocRev = v }
// Clone implements couchdb.Doc
func (i *Instance) Clone() couchdb.Doc {
cloned := *i
cloned.DomainAliases = make([]string, len(i.DomainAliases))
copy(cloned.DomainAliases, i.DomainAliases)
cloned.PassphraseHash = make([]byte, len(i.PassphraseHash))
copy(cloned.PassphraseHash, i.PassphraseHash)
cloned.PassphraseResetToken = make([]byte, len(i.PassphraseResetToken))
copy(cloned.PassphraseResetToken, i.PassphraseResetToken)
if i.PassphraseResetTime != nil {
tmp := *i.PassphraseResetTime
cloned.PassphraseResetTime = &tmp
}
cloned.RegisterToken = make([]byte, len(i.RegisterToken))
copy(cloned.RegisterToken, i.RegisterToken)
cloned.SessSecret = make([]byte, len(i.SessSecret))
copy(cloned.SessSecret, i.SessSecret)
cloned.OAuthSecret = make([]byte, len(i.OAuthSecret))
copy(cloned.OAuthSecret, i.OAuthSecret)
cloned.CLISecret = make([]byte, len(i.CLISecret))
copy(cloned.CLISecret, i.CLISecret)
return &cloned
}
// DBCluster returns the index of the CouchDB cluster where the databases for
// this instance can be found.
func (i *Instance) DBCluster() int {
return i.CouchCluster
}
// DBPrefix returns the prefix to use in database naming for the
// current instance
func (i *Instance) DBPrefix() string {
if i.Prefix != "" {
return i.Prefix
}
return i.Domain
}
// DomainName returns the main domain name of the instance.
func (i *Instance) DomainName() string {
return i.Domain
}
// GetContextName returns the name of the context.
func (i *Instance) GetContextName() string {
return i.ContextName
}
// SessionSecret returns the session secret.
func (i *Instance) SessionSecret() []byte {
// The prefix is here to invalidate all the sessions that were created on
// an instance where the password was not hashed on client-side. It force
// the user to log in again and migrate its passphrase to be hashed on the
// client. It is simpler/safer and, in particular, it avoids that he/she
// can try to changed its pass in settings (which would fail).
secret := make([]byte, 2+len(i.SessSecret))
secret[0] = '2'
secret[1] = ':'
copy(secret[2:], i.SessSecret)
return secret
}
// SlugAndDomain returns the splitted slug and domain of the instance
// Ex: foobar.mycozy.cloud => ["foobar", "mycozy.cloud"]
func (i *Instance) SlugAndDomain() (string, string) {
splitted := strings.SplitN(i.Domain, ".", 2)
return splitted[0], splitted[1]
}
// Logger returns the logger associated with the instance
func (i *Instance) Logger() *logger.Entry {
return logger.WithDomain(i.Domain)
}
// VFS returns the storage provider where the binaries for the current instance
// are persisted
func (i *Instance) VFS() vfs.VFS {
if i.vfs == nil {
panic("instance: calling VFS() before makeVFS()")
}
return i.vfs
}
// MakeVFS is used to initialize the VFS linked to this instance
func (i *Instance) MakeVFS() error {
if i.vfs != nil {
return nil
}
fsURL := config.FsURL()
mutex := config.Lock().ReadWrite(i, "vfs")
index := vfs.NewCouchdbIndexer(i)
disk := vfs.DiskThresholder(i)
var err error
switch fsURL.Scheme {
case config.SchemeFile, config.SchemeMem:
i.vfs, err = vfsafero.New(i, index, disk, mutex, fsURL, i.DirName())
case config.SchemeSwift, config.SchemeSwiftSecure:
switch i.SwiftLayout {
case 2:
i.vfs, err = vfsswift.NewV3(i, index, disk, mutex)
default:
err = ErrInvalidSwiftLayout
}
default:
err = fmt.Errorf("instance: unknown storage provider %s", fsURL.Scheme)
}
return err
}
// ThumbsFS returns the hidden filesystem for storing the thumbnails of the
// photos/image
func (i *Instance) ThumbsFS() vfs.Thumbser {
fsURL := config.FsURL()
switch fsURL.Scheme {
case config.SchemeFile:
baseFS := afero.NewBasePathFs(afero.NewOsFs(),
path.Join(fsURL.Path, i.DirName(), vfs.ThumbsDirName))
return vfsafero.NewThumbsFs(baseFS)
case config.SchemeMem:
baseFS := vfsafero.GetMemFS(i.DomainName() + "-thumbs")
return vfsafero.NewThumbsFs(baseFS)
case config.SchemeSwift, config.SchemeSwiftSecure:
switch i.SwiftLayout {
case 2:
return vfsswift.NewThumbsFsV3(config.GetSwiftConnection(), i)
default:
panic(ErrInvalidSwiftLayout)
}
default:
panic(fmt.Sprintf("instance: unknown storage provider %s", fsURL.Scheme))
}
}
// EnsureSharedDrivesDir returns the Shared Drives directory, and creates it if
// it doesn't exist
func (i *Instance) EnsureSharedDrivesDir() (*vfs.DirDoc, error) {
fs := i.VFS()
dir, err := fs.DirByID(consts.SharedDrivesDirID)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
}
if dir != nil {
return dir, nil
}
name := i.Translate("Tree Shared Drives")
dir, err = vfs.NewDirDocWithPath(name, consts.RootDirID, "/", nil)
if err != nil {
return nil, err
}
dir.DocID = consts.SharedDrivesDirID
dir.CozyMetadata = vfs.NewCozyMetadata(i.PageURL("/", nil))
err = fs.CreateDir(dir)
if errors.Is(err, os.ErrExist) {
dir, err = fs.DirByPath(dir.Fullpath)
}
if err != nil {
return nil, err
}
return dir, nil
}
// NotesLock returns a mutex for the notes on this instance.
func (i *Instance) NotesLock() lock.ErrorRWLocker {
return config.Lock().ReadWrite(i, "notes")
}
func (i *Instance) SetPasswordDefined(defined bool) {
if (i.PasswordDefined == nil || !*i.PasswordDefined) && defined {
doc := couchdb.JSONDoc{
Type: consts.Settings,
M: map[string]interface{}{"_id": consts.PassphraseParametersID},
}
realtime.GetHub().Publish(i, realtime.EventCreate, &doc, nil)
}
i.PasswordDefined = &defined
}
// SettingsDocument returns the document with the settings of this instance
func (i *Instance) SettingsDocument() (*couchdb.JSONDoc, error) {
doc := &couchdb.JSONDoc{}
err := couchdb.GetDoc(i, consts.Settings, consts.InstanceSettingsID, doc)
if err != nil {
return nil, err
}
doc.Type = consts.Settings
return doc, nil
}
// SettingsEMail returns the email address defined in the settings of this
// instance.
func (i *Instance) SettingsEMail() (string, error) {
settings, err := i.SettingsDocument()
if err != nil {
return "", err
}
email, _ := settings.M["email"].(string)
return email, nil
}
// SettingsPublicName returns the public name defined in the settings of this
// instance.
func (i *Instance) SettingsPublicName() (string, error) {
settings, err := i.SettingsDocument()
if err != nil {
return "", err
}
name, _ := settings.M["public_name"].(string)
return name, nil
}
// GetFromContexts returns the parameters specific to the instance context
func (i *Instance) GetFromContexts(contexts map[string]interface{}) (interface{}, bool) {
if contexts == nil {
return nil, false
}
if i.ContextName != "" {
context, ok := contexts[i.ContextName]
if ok {
return context, true
}
}
context, ok := contexts[config.DefaultInstanceContext]
if ok && context != nil {
return context, ok
}
return nil, false
}
// SettingsContext returns the map from the config that matches the context of
// this instance
func (i *Instance) SettingsContext() (map[string]interface{}, bool) {
contexts := config.GetConfig().Contexts
context, ok := i.GetFromContexts(contexts)
if !ok {
return nil, false
}
settings := context.(map[string]interface{})
return settings, true
}
// SupportEmailAddress returns the email address that can be used to contact
// the support.
func (i *Instance) SupportEmailAddress() string {
if ctxSettings, ok := i.SettingsContext(); ok {
if email, ok := ctxSettings["support_address"].(string); ok {
return email
}
}
return "contact@cozycloud.cc"
}
// TemplateTitle returns the specific-context instance template title (if there
// is one). Otherwise, returns the default one
func (i *Instance) TemplateTitle() string {
ctxSettings, ok := i.SettingsContext()
if !ok {
return DefaultTemplateTitle
}
if title, ok := ctxSettings["templates_title"].(string); ok && title != "" {
return title
}
return DefaultTemplateTitle
}
// MoveURL returns URL for move wizard.
func (i *Instance) MoveURL() string {
moveURL := config.GetConfig().Move.URL
if settings, ok := i.SettingsContext(); ok {
if u, ok := settings["move_url"].(string); ok {
moveURL = u
}
}
return moveURL
}
// Registries returns the list of registries associated with the instance.
func (i *Instance) Registries() []*url.URL {
contexts := config.GetConfig().Registries
var context []*url.URL
var ok bool
if i.ContextName != "" {
context, ok = contexts[i.ContextName]
}
if !ok {
context, ok = contexts[config.DefaultInstanceContext]
if !ok {
context = make([]*url.URL, 0)
}
}
return context
}
// HasForcedOIDC returns true only if the instance is in a context where the
// config says that the stack shouldn't allow to authenticate with the
// password.
func (i *Instance) HasForcedOIDC() bool {
if i.ContextName == "" {
return false
}
auth, ok := config.GetConfig().Authentication[i.ContextName].(map[string]interface{})
if !ok {
return false
}
disabled, ok := auth["disable_password_authentication"].(bool)
if !ok {
return false
}
return disabled
}
// PassphraseSalt computes the salt for the client-side hashing of the master
// password. The rule for computing the salt is to create a fake email address
// "me@<domain>".
func (i *Instance) PassphraseSalt() []byte {
domain := strings.Split(i.Domain, ":")[0] // Skip the optional port
return []byte("me@" + domain)
}
// DiskQuota returns the number of bytes allowed on the disk to the user.
func (i *Instance) DiskQuota() int64 {
return i.BytesDiskQuota
}
// WithContextualDomain the current instance context with the given hostname.
func (i *Instance) WithContextualDomain(domain string) *Instance {
if i.HasDomain(domain) {
i.contextualDomain = domain
}
return i
}
// Scheme returns the scheme used for URLs. It is https by default and http
// for development instances.
func (i *Instance) Scheme() string {
if build.IsDevRelease() {
return "http"
}
return "https"
}
// ContextualDomain returns the domain with regard to the current domain
// request.
func (i *Instance) ContextualDomain() string {
if i.contextualDomain != "" {
return i.contextualDomain
}
return i.Domain
}
// HasDomain returns whether or not the given domain name is owned by this
// instance, as part of its main domain name or its aliases.
func (i *Instance) HasDomain(domain string) bool {
if domain == i.Domain {
return true
}
for _, alias := range i.DomainAliases {
if domain == alias {
return true
}
}
return false
}
// SubDomain returns the full url for a subdomain of this instance
// useful with apps slugs
func (i *Instance) SubDomain(s string) *url.URL {
domain := i.ContextualDomain()
if config.GetConfig().Subdomains == config.NestedSubdomains {
domain = s + "." + domain
} else {
parts := strings.SplitN(domain, ".", 2)
domain = parts[0] + "-" + s + "." + parts[1]
}
return &url.URL{
Scheme: i.Scheme(),
Host: domain,
Path: "/",
}
}
// ChangePasswordURL returns the URL of the settings page that can be used by
// the user to change their password.
func (i *Instance) ChangePasswordURL() string {
u := i.SubDomain(consts.SettingsSlug)
u.Fragment = "/profile/password"
return u.String()
}
// FromURL normalizes a given url with the scheme and domain of the instance.
func (i *Instance) FromURL(u *url.URL) string {
u2 := url.URL{
Scheme: i.Scheme(),
Host: i.ContextualDomain(),
Path: u.Path,
RawQuery: u.RawQuery,
Fragment: u.Fragment,
}
return u2.String()
}
// PageURL returns the full URL for a path on the cozy stack
func (i *Instance) PageURL(path string, queries url.Values) string {
var query string
if queries != nil {
query = queries.Encode()
}
u := url.URL{
Scheme: i.Scheme(),
Host: i.ContextualDomain(),
Path: path,
RawQuery: query,
}
return u.String()
}
func (i *Instance) parseRedirectAppAndRoute(redirect string) *url.URL {
splits := strings.SplitN(redirect, "#", 2)
parts := strings.SplitN(splits[0], "/", 2)
u := i.SubDomain(parts[0])
if len(parts) == 2 {
u.Path = parts[1]
}
if len(splits) == 2 {
u.Fragment = splits[1]
}
return u
}
// DefaultAppAndPath returns the default_redirection from the context, in the
// slug+path format (or use the home as the default application).
func (i *Instance) DefaultAppAndPath() string {
context, ok := i.SettingsContext()
if !ok {
return consts.HomeSlug + "/"
}
redirect, ok := context["default_redirection"].(string)
if !ok {
return consts.HomeSlug + "/"
}
return redirect
}
func (i *Instance) redirection(key, defaultSlug string) *url.URL {
context, ok := i.SettingsContext()
if !ok {
return i.SubDomain(defaultSlug)
}
redirect, ok := context[key].(string)
if !ok {
return i.SubDomain(defaultSlug)
}
return i.parseRedirectAppAndRoute(redirect)
}
// DefaultRedirection returns the URL where to redirect the user afer login
// (and in most other cases where we need a redirection URL)
func (i *Instance) DefaultRedirection() *url.URL {
if doc, err := i.SettingsDocument(); err == nil {
// XXX we had a bug where the default_redirection was filled by a full URL
// instead of slug+path, and we should ignore the bad format here.
if redirect, ok := doc.M["default_redirection"].(string); ok && !strings.HasPrefix(redirect, "http") {
return i.parseRedirectAppAndRoute(redirect)
}
}
return i.redirection("default_redirection", consts.HomeSlug)
}
// DefaultRedirectionFromContext returns the URL where to redirect the user
// after login from the context parameters. It can be overloaded by instance
// via the "default_redirection" setting.
func (i *Instance) DefaultRedirectionFromContext() *url.URL {
return i.redirection("default_redirection", consts.HomeSlug)
}
// OnboardedRedirection returns the URL where to redirect the user after
// onboarding
func (i *Instance) OnboardedRedirection() *url.URL {
return i.redirection("onboarded_redirection", consts.HomeSlug)
}
// Translate is used to translate a string to the locale used on this instance
func (i *Instance) Translate(key string, vars ...interface{}) string {
return i18n.Translate(key, i.Locale, i.ContextName, vars...)
}
// List returns the list of declared instances.
func List() ([]*Instance, error) {
var all []*Instance
err := ForeachInstances(func(doc *Instance) error {
all = append(all, doc)
return nil
})
if err != nil {
return nil, err
}
return all, nil
}
// ForeachInstances execute the given callback for each instances.
func ForeachInstances(fn func(*Instance) error) error {
return couchdb.ForeachDocsWithCustomPagination(prefixer.GlobalPrefixer, consts.Instances, 10000, func(_ string, data json.RawMessage) error {
var doc *Instance
if err := json.Unmarshal(data, &doc); err != nil {
return err
}
return fn(doc)
})
}
// PaginatedList can be used to list the instances, with pagination.
func PaginatedList(limit int, startKey string, skip int) ([]*Instance, string, error) {
var docs []*Instance
req := &couchdb.AllDocsRequest{
// Also get the following document for the next key,
// and a few more because of the design docs
Limit: limit + 10,
StartKey: startKey,
Skip: skip,
}
err := couchdb.GetAllDocs(prefixer.GlobalPrefixer, consts.Instances, req, &docs)
if err != nil {
return nil, "", err
}
if len(docs) > limit { // There are still documents to fetch
nextDoc := docs[limit]
docs = docs[:limit]
return docs, nextDoc.ID(), nil
}
return docs, "", nil
}
// PickKey choose which of the Instance keys to use depending on token audience
func (i *Instance) PickKey(audience string) ([]byte, error) {
switch audience {
case consts.AppAudience, consts.KonnectorAudience:
return i.SessionSecret(), nil
case consts.RefreshTokenAudience, consts.AccessTokenAudience, consts.ShareAudience:
return i.OAuthSecret, nil
case consts.CLIAudience:
return i.CLISecret, nil
}
return nil, permission.ErrInvalidAudience
}
// MakeJWT is a shortcut to create a JWT
func (i *Instance) MakeJWT(audience, subject, scope, sessionID string, issuedAt time.Time) (string, error) {
secret, err := i.PickKey(audience)
if err != nil {
return "", err
}
return crypto.NewJWT(secret, permission.Claims{
RegisteredClaims: jwt.RegisteredClaims{
Audience: jwt.ClaimStrings{audience},
Issuer: i.Domain,
IssuedAt: jwt.NewNumericDate(issuedAt),
Subject: subject,
},
Scope: scope,
SessionID: sessionID,
})
}
// BuildAppToken is used to build a token to identify the app for requests made
// to the stack
func (i *Instance) BuildAppToken(slug, sessionID string) string {
scope := "" // apps tokens don't have a scope
now := time.Now()
token, err := i.MakeJWT(consts.AppAudience, slug, scope, sessionID, now)
if err != nil {
return ""
}
return token
}
// BuildKonnectorToken is used to build a token to identify the konnector for
// requests made to the stack
func (i *Instance) BuildKonnectorToken(slug string) string {
scope := "" // apps tokens don't have a scope
token, err := i.MakeJWT(consts.KonnectorAudience, slug, scope, "", time.Now())
if err != nil {
return ""
}
return token
}
// CreateShareCode returns a new sharecode to put the codes field of a
// permissions document
func (i *Instance) CreateShareCode(subject string) (string, error) {
scope := ""
sessionID := ""
return i.MakeJWT(consts.ShareAudience, subject, scope, sessionID, time.Now())
}
// MovedError is used to return an error when the instance has been moved to a
// new domain/hoster.
func (i *Instance) MovedError() *jsonapi.Error {
if !i.Moved {
return nil
}
jerr := jsonapi.Error{
Status: http.StatusGone,
Title: "Cozy has been moved",
Code: "moved",
Detail: i.Translate("The Cozy has been moved to a new address"),
}
doc, err := i.SettingsDocument()
if err == nil {
if to, ok := doc.M["moved_to"].(string); ok {
jerr.Links = &jsonapi.LinksList{Related: to}
}
}
return &jerr
}
func (i *Instance) HasPremiumLinksEnabled() bool {
if ctxSettings, ok := i.SettingsContext(); ok {
if enabled, ok := ctxSettings["enable_premium_links"].(bool); ok {
return enabled
}
}
return false
}
// ensure Instance implements couchdb.Doc
var (
_ couchdb.Doc = &Instance{}
)