/
oauth.go
1068 lines (955 loc) · 32.2 KB
/
oauth.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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package auth
import (
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"html/template"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/cozy/cozy-stack/model/app"
"github.com/cozy/cozy-stack/model/bitwarden/settings"
"github.com/cozy/cozy-stack/model/feature"
"github.com/cozy/cozy-stack/model/instance"
"github.com/cozy/cozy-stack/model/instance/lifecycle"
"github.com/cozy/cozy-stack/model/move"
"github.com/cozy/cozy-stack/model/oauth"
"github.com/cozy/cozy-stack/model/permission"
"github.com/cozy/cozy-stack/model/session"
csettings "github.com/cozy/cozy-stack/model/settings"
"github.com/cozy/cozy-stack/model/sharing"
"github.com/cozy/cozy-stack/model/vfs"
"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/limits"
"github.com/cozy/cozy-stack/pkg/registry"
"github.com/cozy/cozy-stack/web/middlewares"
"github.com/labstack/echo/v4"
"github.com/mssola/user_agent"
)
type webappParams struct {
Name string
Slug string
}
type authorizeParams struct {
instance *instance.Instance
state string
clientID string
redirectURI string
scope string
resType string
challenge string
challengeMethod string
client *oauth.Client
webapp *webappParams
}
type AuthorizeHTTPHandler struct {
deprecatedApps *DeprecatedAppList
}
// NewAuthorizeHandler instantiates a new [AuthHTTPHandler].
func NewAuthorizeHandler(deprecatedAppsCfg config.DeprecatedAppsCfg) *AuthorizeHTTPHandler {
return &AuthorizeHTTPHandler{
deprecatedApps: NewDeprecatedAppList(deprecatedAppsCfg),
}
}
func (a *AuthorizeHTTPHandler) Register(router *echo.Group) {
router.GET("", a.authorizeForm)
router.POST("", a.authorize)
router.GET("/sharing", a.authorizeSharingForm)
router.POST("/sharing", a.authorizeSharing)
router.GET("/sharing/:sharing-id/cancel", a.cancelAuthorizeSharing)
router.GET("/move", a.authorizeMoveForm)
router.POST("/move", a.authorizeMove)
}
func checkAuthorizeParams(c echo.Context, params *authorizeParams) (bool, error) {
if params.state == "" {
return true, renderError(c, http.StatusBadRequest, "Error No state parameter")
}
if params.clientID == "" {
return true, renderError(c, http.StatusBadRequest, "Error No client_id parameter")
}
if params.redirectURI == "" {
return true, renderError(c, http.StatusBadRequest, "Error No redirect_uri parameter")
}
if params.resType != "code" {
return true, renderError(c, http.StatusBadRequest, "Error Invalid response type")
}
if params.challenge != "" && params.challengeMethod != "S256" {
return true, renderError(c, http.StatusBadRequest, "Error Invalid challenge code method")
}
if params.challengeMethod == "S256" && params.challenge == "" {
return true, renderError(c, http.StatusBadRequest, "Error No challenge code")
}
client, err := oauth.FindClient(params.instance, params.clientID)
if err != nil {
return true, renderError(c, http.StatusBadRequest, "Error No registered client")
}
params.client = client
if !params.client.AcceptRedirectURI(params.redirectURI) {
return true, renderError(c, http.StatusBadRequest, "Error Incorrect redirect_uri")
}
params.scope = strings.TrimSpace(params.scope)
if params.scope == "*" {
if params.challenge == "" {
return true, renderError(c, http.StatusBadRequest, "Error No challenge code")
}
instance := middlewares.GetInstance(c)
context := instance.ContextName
if context == "" {
context = config.DefaultInstanceContext
}
cfg := config.GetConfig().Flagship.Contexts[context]
skipCertification := false
if cfg, ok := cfg.(map[string]interface{}); ok {
skipCertification = cfg["skip_certification"] == true
}
if !skipCertification && !params.client.Flagship {
return true, renderConfirmFlagship(c, params.clientID)
}
return false, nil
}
if appSlug := oauth.GetLinkedAppSlug(params.client.SoftwareID); appSlug != "" {
webapp, err := registry.GetLatestVersion(appSlug, "stable", params.instance.Registries())
if err != nil {
return true, renderError(c, http.StatusBadRequest, "Cannot find application on instance registries")
}
var manifest struct {
Slug string `json:"slug"`
Name string `json:"name"`
Permissions permission.Set `json:"permissions"`
}
err = json.Unmarshal(webapp.Manifest, &manifest)
if err != nil {
return true, renderError(c, http.StatusBadRequest, "Cannot decode application manifest")
}
params.scope, err = manifest.Permissions.MarshalScopeString()
if err != nil {
return true, renderError(c, http.StatusBadRequest, "Cannot marshal scope permissions")
}
params.webapp = &webappParams{
Slug: manifest.Slug,
Name: manifest.Name,
}
}
if params.scope == "" {
return true, renderError(c, http.StatusBadRequest, "Error No scope parameter")
}
if params.scope == oauth.ScopeLogin && !params.client.AllowLoginScope {
return true, renderError(c, http.StatusBadRequest, "Error No scope parameter")
}
return false, nil
}
func (a *AuthorizeHTTPHandler) authorizeForm(c echo.Context) error {
inst := middlewares.GetInstance(c)
params := authorizeParams{
instance: inst,
state: c.QueryParam("state"),
clientID: c.QueryParam("client_id"),
redirectURI: c.QueryParam("redirect_uri"),
scope: c.QueryParam("scope"),
resType: c.QueryParam("response_type"),
challenge: c.QueryParam("code_challenge"),
challengeMethod: c.QueryParam("code_challenge_method"),
}
isLoggedIn := middlewares.IsLoggedIn(c)
if code := c.QueryParam("session_code"); code != "" {
// XXX we should always clear the session code to avoid it being
// reused, even if the user is already logged in and we don't want to
// create a new session
if checked := inst.CheckAndClearSessionCode(code); checked && !isLoggedIn {
sessionID, err := SetCookieForNewSession(c, session.ShortRun)
req := c.Request()
if err == nil {
if err = session.StoreNewLoginEntry(inst, sessionID, "", req, "session_code", false); err != nil {
inst.Logger().Errorf("Could not store session history %q: %s", sessionID, err)
}
}
redirect := req.URL
q := redirect.Query()
q.Del("session_code")
redirect.RawQuery = q.Encode()
return c.Redirect(http.StatusSeeOther, redirect.String())
}
}
if hasError, err := checkAuthorizeParams(c, ¶ms); hasError {
return err
}
if a.deprecatedApps.IsDeprecated(params.client) {
return c.Render(http.StatusOK, "new_app_available.html", a.deprecatedApps.RenderArgs(params.client, inst, c.Request().UserAgent()))
}
if !isLoggedIn {
u := inst.PageURL("/auth/login", url.Values{
"redirect": {inst.FromURL(c.Request().URL)},
})
return c.Redirect(http.StatusSeeOther, u)
}
// For a scope "login": such client is only used to transmit authentication
// for the manager. It does not require any authorization from the user, and
// generate a code without asking any permission.
if params.scope == oauth.ScopeLogin {
access, err := oauth.CreateAccessCode(params.instance, params.client, "" /* = scope */, "" /* = challenge */)
if err != nil {
return err
}
u, err := url.ParseRequestURI(params.redirectURI)
if err != nil {
return renderError(c, http.StatusBadRequest, "Error Invalid redirect_uri")
}
q := u.Query()
// We should be sending "code" only, but for compatibility reason, we keep
// the access_code parameter that we used to send in our first impl.
q.Set("access_code", access.Code)
q.Set("code", access.Code)
q.Set("state", params.state)
u.RawQuery = q.Encode()
u.Fragment = ""
return c.Redirect(http.StatusFound, u.String()+"#")
}
if !params.client.Flagship {
flags, err := feature.GetFlags(inst)
if err != nil {
return err
}
if clientsLimit, ok := flags.M["cozy.oauthclients.max"].(float64); ok && clientsLimit >= 0 {
limit := int(clientsLimit)
clients, _, err := oauth.GetConnectedUserClients(inst, 100, "")
if err != nil {
return fmt.Errorf("Could not get user OAuth clients: %w", err)
}
count := len(clients)
if count >= limit {
var manageDevicesURL, premiumURL string
connectedDevicesURL := inst.SubDomain(consts.SettingsSlug)
connectedDevicesURL.Fragment = "/connectedDevices"
manageDevicesURL = connectedDevicesURL.String()
if inst.HasPremiumLinksEnabled() {
if premiumURL, err = inst.ManagerURL(instance.ManagerPremiumURL); err != nil {
inst.Logger().Errorf("Could not get instance Premium Manager URL: %s", err.Error())
}
}
sess, _ := middlewares.GetSession(c)
settingsToken := inst.BuildAppToken(consts.SettingsSlug, sess.ID())
return c.Render(http.StatusOK, "oauth_clients_limit_exceeded.html", echo.Map{
"Domain": inst.ContextualDomain(),
"ContextName": inst.ContextName,
"Locale": inst.Locale,
"Title": inst.TemplateTitle(),
"Favicon": middlewares.Favicon(inst),
"ClientsCount": strconv.Itoa(count),
"ClientsLimit": strconv.Itoa(limit),
"OpenLinksInNewTab": true,
"ManageDevicesURL": manageDevicesURL,
"PremiumURL": premiumURL,
"SettingsToken": settingsToken,
})
}
}
}
permissions, err := permission.UnmarshalScopeString(params.scope)
if err != nil {
context := inst.ContextName
if context == "" {
context = config.DefaultInstanceContext
}
cfg := config.GetConfig().Flagship.Contexts[context]
skipCertification := false
if cfg, ok := cfg.(map[string]interface{}); ok {
skipCertification = cfg["skip_certification"] == true
}
if params.scope != "*" || (!skipCertification && !params.client.Flagship) {
return renderError(c, http.StatusBadRequest, "Error Invalid scope")
}
permissions = permission.MaximalSet()
}
readOnly := true
for _, p := range permissions {
if !p.Verbs.ReadOnly() {
readOnly = false
}
}
params.client.ClientID = params.client.CouchID
u, err := url.ParseRequestURI(params.redirectURI)
if err != nil {
return renderError(c, http.StatusBadRequest, "Error Invalid redirect_uri")
}
q := u.Query()
if params.client.CreatedAtOnboarding {
return createAccessCode(c, params, u, q)
}
q.Set("error", "access_denied")
u.RawQuery = q.Encode()
closeURI := template.URL("/")
if u.Scheme == "http" || u.Scheme == "https" || u.Scheme == "cozy" {
closeURI = template.URL(u.String())
}
var clientDomain string
clientURL, err := url.Parse(params.client.ClientURI)
if err != nil {
clientDomain = params.client.ClientURI
} else {
clientDomain = clientURL.Hostname()
}
// This Content-Security-Policy (CSP) nonce is here to allow the display of
// logos for OAuth clients on the authorize page.
if logoURI := params.client.LogoURI; logoURI != "" {
logoURL, err := url.Parse(logoURI)
if err == nil {
csp := c.Response().Header().Get(echo.HeaderContentSecurityPolicy)
if !strings.Contains(csp, "img-src") {
c.Response().Header().Set(echo.HeaderContentSecurityPolicy,
fmt.Sprintf("%simg-src 'self' https://%s;", csp, logoURL.Hostname()+logoURL.EscapedPath()))
}
}
}
slugname, instanceDomain := inst.SlugAndDomain()
return c.Render(http.StatusOK, "authorize.html", echo.Map{
"Domain": inst.ContextualDomain(),
"ContextName": inst.ContextName,
"Locale": inst.Locale,
"Title": inst.TemplateTitle(),
"Favicon": middlewares.Favicon(inst),
"InstanceSlugName": slugname,
"InstanceDomain": instanceDomain,
"ClientDomain": clientDomain,
"Client": params.client,
"State": params.state,
"RedirectURI": params.redirectURI,
"CloseURI": closeURI,
"Scope": params.scope,
"Challenge": params.challenge,
"ChallengeMethod": params.challengeMethod,
"Permissions": permissions,
"ReadOnly": readOnly,
"CSRF": c.Get("csrf"),
"Webapp": params.webapp,
})
}
func (a *AuthorizeHTTPHandler) authorize(c echo.Context) error {
instance := middlewares.GetInstance(c)
params := authorizeParams{
instance: instance,
state: c.FormValue("state"),
clientID: c.FormValue("client_id"),
redirectURI: c.FormValue("redirect_uri"),
scope: c.FormValue("scope"),
resType: c.FormValue("response_type"),
challenge: c.FormValue("code_challenge"),
challengeMethod: c.FormValue("code_challenge_method"),
}
if hasError, err := checkAuthorizeParams(c, ¶ms); hasError {
return err
}
if !middlewares.IsLoggedIn(c) {
return renderError(c, http.StatusUnauthorized, "Error Must be authenticated")
}
u, err := url.ParseRequestURI(params.redirectURI)
if err != nil {
return renderError(c, http.StatusBadRequest, "Error Invalid redirect_uri")
}
q := u.Query()
// Install the application in case of mobile client
softwareID := params.client.SoftwareID
if oauth.IsLinkedApp(softwareID) {
manifest, err := GetLinkedApp(instance, softwareID)
if err != nil {
return err
}
slug := manifest.Slug()
installer, err := app.NewInstaller(instance, app.Copier(consts.WebappType, instance), &app.InstallerOptions{
Operation: app.Install,
Type: consts.WebappType,
SourceURL: softwareID,
Slug: slug,
Registries: instance.Registries(),
})
if !errors.Is(err, app.ErrAlreadyExists) {
if err != nil {
return err
}
go installer.Run()
}
params.scope = oauth.BuildLinkedAppScope(slug)
if u.Scheme == "http" || u.Scheme == "https" {
q.Set("fallback", instance.SubDomain(slug).String())
}
}
// Fill the client_os of the OAuth client
rawUserAgent := c.Request().UserAgent()
ua := user_agent.New(rawUserAgent)
params.client.ClientOS = ua.OS()
_ = couchdb.UpdateDoc(instance, params.client)
return createAccessCode(c, params, u, q)
}
func createAccessCode(c echo.Context, params authorizeParams, u *url.URL, q url.Values) error {
q.Set("state", params.state)
access, err := oauth.CreateAccessCode(params.instance, params.client, params.scope, params.challenge)
if err != nil {
return err
}
var ip string
if forwardedFor := c.Request().Header.Get(echo.HeaderXForwardedFor); forwardedFor != "" {
ip = strings.TrimSpace(strings.SplitN(forwardedFor, ",", 2)[0])
}
if ip == "" {
ip = strings.Split(c.Request().RemoteAddr, ":")[0]
}
params.instance.Logger().WithNamespace("loginaudit").
Infof("Access code created from %s at %s with scope %s", ip, time.Now(), access.Scope)
// We should be sending "code" only, but for compatibility reason, we keep
// the access_code parameter that we used to send in our first impl.
q.Set("access_code", access.Code)
q.Set("code", access.Code)
u.RawQuery = q.Encode()
u.Fragment = ""
location := u.String() + "#"
wantsJSON := c.Request().Header.Get(echo.HeaderAccept) == echo.MIMEApplicationJSON
if wantsJSON {
return c.JSON(http.StatusOK, echo.Map{"deeplink": location})
}
return c.Redirect(http.StatusFound, location)
}
func renderConfirmFlagship(c echo.Context, clientID string) error {
inst := middlewares.GetInstance(c)
if !middlewares.IsLoggedIn(c) {
u := inst.PageURL("/auth/login", url.Values{
"redirect": {inst.FromURL(c.Request().URL)},
})
return c.Redirect(http.StatusSeeOther, u)
}
err := config.GetRateLimiter().CheckRateLimit(inst, limits.ConfirmFlagshipType)
if limits.IsLimitReachedOrExceeded(err) {
return renderError(c, http.StatusTooManyRequests, err.Error())
}
token, err := oauth.SendConfirmFlagshipCode(inst, clientID)
if err != nil {
return renderError(c, http.StatusInternalServerError, err.Error())
}
email, _ := inst.SettingsEMail()
return c.Render(http.StatusOK, "confirm_flagship.html", echo.Map{
"Domain": inst.ContextualDomain(),
"ContextName": inst.ContextName,
"Locale": inst.Locale,
"Title": inst.TemplateTitle(),
"Favicon": middlewares.Favicon(inst),
"Email": email,
"SupportEmail": inst.SupportEmailAddress(),
"Token": string(token),
"ClientID": clientID,
})
}
type authorizeSharingParams struct {
instance *instance.Instance
state string
sharingID string
}
func checkAuthorizeSharingParams(c echo.Context, params *authorizeSharingParams) (bool, error) {
if params.state == "" {
return true, renderError(c, http.StatusBadRequest, "Error No state parameter")
}
if params.sharingID == "" {
return true, renderError(c, http.StatusBadRequest, "Error No sharing_id parameter")
}
return false, nil
}
func (a *AuthorizeHTTPHandler) authorizeSharingForm(c echo.Context) error {
instance := middlewares.GetInstance(c)
params := authorizeSharingParams{
instance: instance,
state: c.QueryParam("state"),
sharingID: c.QueryParam("sharing_id"),
}
if hasError, err := checkAuthorizeSharingParams(c, ¶ms); hasError {
return err
}
if !middlewares.IsLoggedIn(c) {
u := instance.PageURL("/auth/login", url.Values{
"redirect": {instance.FromURL(c.Request().URL)},
})
return c.Redirect(http.StatusSeeOther, u)
}
s, err := sharing.FindSharing(instance, params.sharingID)
if err != nil || s.Owner || s.Active || len(s.Members) < 2 {
return renderError(c, http.StatusUnauthorized, "Error Invalid sharing")
}
hasShortcut := s.ShortcutID != ""
var sharerDomain, targetType string
sharerURL, err := url.Parse(s.Members[0].Instance)
if err != nil {
sharerDomain = s.Members[0].Instance
} else {
sharerDomain = sharerURL.Host
}
if s.Rules[0].DocType == consts.BitwardenOrganizations {
targetType = instance.Translate("Notification Sharing Type Organization")
hasShortcut = true
s.Rules[0].Mime = "organization"
if len(s.Rules) == 2 && s.Rules[1].DocType == consts.BitwardenCiphers {
s.Rules = s.Rules[:1]
}
} else if s.Rules[0].DocType != consts.Files {
targetType = instance.Translate("Notification Sharing Type Document")
} else if s.Rules[0].Mime == "" {
targetType = instance.Translate("Notification Sharing Type Directory")
} else {
targetType = instance.Translate("Notification Sharing Type File")
}
return c.Render(http.StatusOK, "authorize_sharing.html", echo.Map{
"Domain": instance.ContextualDomain(),
"ContextName": instance.ContextName,
"Locale": instance.Locale,
"Title": instance.TemplateTitle(),
"Favicon": middlewares.Favicon(instance),
"SharerDomain": sharerDomain,
"SharerName": s.Members[0].PrimaryName(),
"State": params.state,
"Sharing": s,
"CSRF": c.Get("csrf"),
"HasShortcut": hasShortcut,
"TargetType": targetType,
})
}
func (a *AuthorizeHTTPHandler) authorizeSharing(c echo.Context) error {
instance := middlewares.GetInstance(c)
params := authorizeSharingParams{
instance: instance,
state: c.FormValue("state"),
sharingID: c.FormValue("sharing_id"),
}
if hasError, err := checkAuthorizeSharingParams(c, ¶ms); hasError {
return err
}
if !middlewares.IsLoggedIn(c) {
return renderError(c, http.StatusUnauthorized, "Error Must be authenticated")
}
s, err := sharing.FindSharing(instance, params.sharingID)
if err != nil {
return err
}
if s.Owner || len(s.Members) < 2 {
return sharing.ErrInvalidSharing
}
if c.FormValue("synchronize") == "" {
if err = s.AddShortcut(instance, params.state); err != nil {
return err
}
u := instance.SubDomain(consts.DriveSlug)
u.RawQuery = "sharing=" + s.SID
u.Fragment = "/folder/" + consts.SharedWithMeDirID
return c.Redirect(http.StatusSeeOther, u.String())
}
if !s.Active {
if err = s.SendAnswer(instance, params.state); err != nil {
return err
}
}
redirect := s.RedirectAfterAuthorizeURL(instance)
return c.Redirect(http.StatusSeeOther, redirect.String())
}
func (a *AuthorizeHTTPHandler) cancelAuthorizeSharing(c echo.Context) error {
if !middlewares.IsLoggedIn(c) {
return renderError(c, http.StatusUnauthorized, "Error Must be authenticated")
}
inst := middlewares.GetInstance(c)
s, err := sharing.FindSharing(inst, c.Param("sharing-id"))
if err != nil || s.Owner || len(s.Members) < 2 {
return c.Redirect(http.StatusSeeOther, inst.SubDomain(consts.HomeSlug).String())
}
previewURL, err := s.GetPreviewURL(inst, c.QueryParam("state"))
if err != nil {
return c.Redirect(http.StatusSeeOther, inst.SubDomain(consts.HomeSlug).String())
}
return c.Redirect(http.StatusSeeOther, previewURL)
}
func (a *AuthorizeHTTPHandler) authorizeMoveForm(c echo.Context) error {
inst := middlewares.GetInstance(c)
state := c.QueryParam("state")
if state == "" {
return renderError(c, http.StatusBadRequest, "Error No state parameter")
}
clientID := c.QueryParam("client_id")
if clientID == "" {
return renderError(c, http.StatusBadRequest, "Error No client_id parameter")
}
redirectURI := c.QueryParam("redirect_uri")
if redirectURI == "" {
return renderError(c, http.StatusBadRequest, "Error No redirect_uri parameter")
}
client := oauth.Client{}
if err := couchdb.GetDoc(inst, consts.OAuthClients, clientID, &client); err != nil {
return renderError(c, http.StatusBadRequest, "Error No registered client")
}
if !client.AcceptRedirectURI(redirectURI) {
return renderError(c, http.StatusBadRequest, "Error Incorrect redirect_uri")
}
if inst.HasForcedOIDC() {
if !middlewares.IsLoggedIn(c) {
u := c.Request().URL
redirect := inst.PageURL(u.Path, u.Query())
q := url.Values{"redirect": {redirect}}
return c.Redirect(http.StatusSeeOther, inst.PageURL("/oidc/start", q))
}
twoFactorToken, err := lifecycle.SendTwoFactorPasscode(inst)
if err != nil {
return err
}
mail, _ := inst.SettingsEMail()
return c.Render(http.StatusOK, "move_delegated_auth.html", echo.Map{
"Domain": inst.ContextualDomain(),
"ContextName": inst.ContextName,
"Favicon": middlewares.Favicon(inst),
"TwoFactorToken": string(twoFactorToken),
"CredentialsError": "",
"Email": mail,
"State": state,
"ClientID": clientID,
"Redirect": redirectURI,
})
}
publicName, err := csettings.PublicName(inst)
if err != nil {
publicName = ""
}
var title string
if publicName == "" {
title = inst.Translate("Login Welcome")
} else {
title = inst.Translate("Login Welcome name", publicName)
}
help := inst.Translate("Login Password help")
iterations := 0
if settings, err := settings.Get(inst); err == nil {
iterations = settings.PassphraseKdfIterations
}
return c.Render(http.StatusOK, "authorize_move.html", echo.Map{
"TemplateTitle": inst.TemplateTitle(),
"Domain": inst.ContextualDomain(),
"ContextName": inst.ContextName,
"Locale": inst.Locale,
"Iterations": iterations,
"Salt": string(inst.PassphraseSalt()),
"Title": title,
"PasswordHelp": help,
"CSRF": c.Get("csrf"),
"Favicon": middlewares.Favicon(inst),
"BottomNavBar": middlewares.BottomNavigationBar(c),
"CryptoPolyfill": middlewares.CryptoPolyfill(c),
"State": state,
"ClientID": clientID,
"RedirectURI": redirectURI,
})
}
func (a *AuthorizeHTTPHandler) authorizeMove(c echo.Context) error {
inst := middlewares.GetInstance(c)
if inst.HasForcedOIDC() {
if !middlewares.IsLoggedIn(c) {
return renderError(c, http.StatusUnauthorized, "Error Must be authenticated")
}
token := []byte(c.FormValue("two-factor-token"))
passcode := c.FormValue("two-factor-passcode")
correctPasscode := inst.ValidateTwoFactorPasscode(token, passcode)
if !correctPasscode {
errorMessage := inst.Translate(TwoFactorErrorKey)
mail, _ := inst.SettingsEMail()
return c.Render(http.StatusOK, "move_delegated_auth.html", echo.Map{
"Domain": inst.ContextualDomain(),
"ContextName": inst.ContextName,
"Favicon": middlewares.Favicon(inst),
"TwoFactorToken": string(token),
"CredentialsError": errorMessage,
"Email": mail,
"State": c.FormValue("state"),
"ClientID": c.FormValue("client_id"),
"Redirect": c.FormValue("redirect"),
})
}
u, err := moveSuccessURI(c)
if err != nil {
return err
}
return c.Redirect(http.StatusSeeOther, u)
}
// Check passphrase
passphrase := []byte(c.FormValue("passphrase"))
if instance.CheckPassphrase(inst, passphrase) != nil {
errorMessage := inst.Translate(CredentialsErrorKey)
err := config.GetRateLimiter().CheckRateLimit(inst, limits.AuthType)
if limits.IsLimitReachedOrExceeded(err) {
if err = LoginRateExceeded(inst); err != nil {
inst.Logger().WithNamespace("auth").Warn(err.Error())
}
}
return c.JSON(http.StatusUnauthorized, echo.Map{
"error": errorMessage,
})
}
if inst.HasAuthMode(instance.TwoFactorMail) && !isTrustedDevice(c, inst) {
twoFactorToken, err := lifecycle.SendTwoFactorPasscode(inst)
if err != nil {
return err
}
v := url.Values{}
v.Add("two_factor_token", string(twoFactorToken))
v.Add("state", c.FormValue("state"))
v.Add("client_id", c.FormValue("client_id"))
v.Add("redirect", c.FormValue("redirect"))
v.Add("trusted_device_checkbox", "false")
return c.JSON(http.StatusOK, echo.Map{
"redirect": inst.PageURL("/auth/twofactor", v),
})
}
u, err := moveSuccessURI(c)
if err != nil {
return err
}
return c.JSON(http.StatusOK, echo.Map{
"redirect": u,
})
}
func moveSuccessURI(c echo.Context) (string, error) {
u, err := url.Parse(c.FormValue("redirect"))
if err != nil {
return "", echo.NewHTTPError(http.StatusBadRequest, "bad url: could not parse")
}
inst := middlewares.GetInstance(c)
vault := settings.HasVault(inst)
used, quota, err := DiskInfo(inst.VFS())
if err != nil {
return "", err
}
client, err := oauth.FindClient(inst, c.FormValue("client_id"))
if err != nil {
return "", err
}
access, err := oauth.CreateAccessCode(inst, client, move.MoveScope, "")
if err != nil {
return "", err
}
q := u.Query()
q.Set("state", c.FormValue("state"))
q.Set("code", access.Code)
q.Set("vault", strconv.FormatBool(vault))
q.Set("used", used)
if quota != "" {
q.Set("quota", quota)
}
u.RawQuery = q.Encode()
return u.String(), nil
}
// DiskInfo returns the used and quota disk space for the given VFS.
func DiskInfo(fs vfs.VFS) (string, string, error) {
versions, err := fs.VersionsUsage()
if err != nil {
return "", "", err
}
files, err := fs.FilesUsage()
if err != nil {
return "", "", err
}
used := fmt.Sprintf("%d", files+versions)
var quota string
if q := fs.DiskQuota(); q > 0 {
quota = fmt.Sprintf("%d", q)
}
return used, quota, nil
}
// AccessTokenReponse is the stuct used for serializing to JSON the response
// for an access token.
type AccessTokenReponse struct {
Type string `json:"token_type"`
Scope string `json:"scope"`
Access string `json:"access_token"`
Refresh string `json:"refresh_token,omitempty"`
}
func LockOAuthClient(inst *instance.Instance, clientID string) func() {
mu := config.Lock().ReadWrite(inst, "oauth/"+clientID)
_ = mu.Lock()
return mu.Unlock
}
func accessToken(c echo.Context) error {
grant := c.FormValue("grant_type")
clientID := c.FormValue("client_id")
clientSecret := c.FormValue("client_secret")
verifier := c.FormValue("code_verifier")
instance := middlewares.GetInstance(c)
if grant == "" {
return c.JSON(http.StatusBadRequest, echo.Map{
"error": "the grant_type parameter is mandatory",
})
}
if clientID == "" {
return c.JSON(http.StatusBadRequest, echo.Map{
"error": "the client_id parameter is mandatory",
})
}
if clientSecret == "" {
return c.JSON(http.StatusBadRequest, echo.Map{
"error": "the client_secret parameter is mandatory",
})
}
defer LockOAuthClient(instance, clientID)()
client, err := oauth.FindClient(instance, clientID)
if err != nil {
if couchErr, isCouchErr := couchdb.IsCouchError(err); isCouchErr && couchErr.StatusCode >= 500 {
return err
}
return c.JSON(http.StatusBadRequest, echo.Map{
"error": "the client must be registered",
})
}
if subtle.ConstantTimeCompare([]byte(clientSecret), []byte(client.ClientSecret)) == 0 {
return c.JSON(http.StatusBadRequest, echo.Map{
"error": "invalid client_secret",
})
}
out := AccessTokenReponse{
Type: "bearer",
}
slug := oauth.GetLinkedAppSlug(client.SoftwareID)
if slug != "" {
if err := CheckLinkedAppInstalled(instance, slug); err != nil {
return err
}
}
switch grant {
case "authorization_code":
code := c.FormValue("code")
if code == "" {
return c.JSON(http.StatusBadRequest, echo.Map{
"error": "the code parameter is mandatory",
})
}
accessCode := &oauth.AccessCode{}
if err = couchdb.GetDoc(instance, consts.OAuthAccessCodes, code, accessCode); err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{
"error": "invalid code",
})
}
if accessCode.Challenge != "" {
sum := sha256.Sum256([]byte(verifier))
challenge := base64.RawURLEncoding.EncodeToString(sum[:])
if challenge != accessCode.Challenge {
return c.JSON(http.StatusBadRequest, echo.Map{
"error": "invalid code_verifier",
})
}
}
out.Scope = accessCode.Scope
out.Refresh, err = client.CreateJWT(instance, consts.RefreshTokenAudience, out.Scope)
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{
"error": "Can't generate refresh token",
})
}
// Delete the access code, it can be used only once
err = couchdb.DeleteDoc(instance, accessCode)
if err != nil {
instance.Logger().Errorf(
"[oauth] Failed to delete the access code: %s", err)
}
case "refresh_token":
token := c.FormValue("refresh_token")
claims, ok := client.ValidToken(instance, consts.RefreshTokenAudience, token)
if !ok && client.ClientKind == "sharing" {
out.Refresh, claims, ok = sharing.TryTokenForMovedSharing(instance, client, token)
}
if !ok {
return c.JSON(http.StatusBadRequest, echo.Map{
"error": "invalid refresh token",
})
}
// Code below is used to transform an old OAuth client token scope to
// the new linked-app scope
if slug != "" {
out.Scope = oauth.BuildLinkedAppScope(slug)
} else {
out.Scope = claims.Scope
}
default:
return c.JSON(http.StatusBadRequest, echo.Map{
"error": "invalid grant type",
})
}
out.Access, err = client.CreateJWT(instance, consts.AccessTokenAudience, out.Scope)
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{
"error": "Can't generate access token",
})
}
// Update the last_refreshed_at field of the OAuth client
client.LastRefreshedAt = time.Now()
_ = couchdb.UpdateDoc(instance, client)
_ = session.RemoveLoginRegistration(instance.ContextualDomain(), clientID)
return c.JSON(http.StatusOK, out)
}
func buildKonnectorToken(c echo.Context) error {
inst := middlewares.GetInstance(c)
slug := c.Param("slug")
if err := middlewares.AllowMaximal(c); err != nil {
return c.JSON(http.StatusForbidden, err)
}