Skip to content

Commit 509d4d2

Browse files
authored
Merge pull request #1478 from nono/sharing-redirect
Redirection after a sharing was authorized
2 parents ac90bda + d062b4b commit 509d4d2

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

docs/sharing.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ If necessary, the application can list the permissions for the token by calling
3434
The owner of a cozy instance can send and synchronize documents to others cozy
3535
users.
3636

37+
### Intents
38+
39+
When a sharing is authorized, the user is redirected to their cozy on the
40+
application that was used for the sharing (when possible). It's possible to
41+
use a specific route to do so, via the intents. The application must declare
42+
an intent in its manifest for the action `SHARING`. The doctype of the intent
43+
must be the same as the doctype of the first rule of the sharing. In the
44+
redirect URL, the query string will have a `sharing` parameter with the
45+
sharing ID (but no intent parameter).
46+
3747
### Routes
3848

3949
#### POST /sharings/

pkg/sharing/sharing.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package sharing
22

33
import (
44
"encoding/json"
5+
"net/url"
6+
"strings"
57
"time"
68

9+
"github.com/cozy/cozy-stack/pkg/apps"
710
"github.com/cozy/cozy-stack/pkg/consts"
811
"github.com/cozy/cozy-stack/pkg/contacts"
912
"github.com/cozy/cozy-stack/pkg/couchdb"
@@ -466,4 +469,49 @@ func GetSharingsByDocType(inst *instance.Instance, docType string) (map[string]*
466469
return sharings, nil
467470
}
468471

472+
func findIntentForRedirect(inst *instance.Instance, app *apps.WebappManifest, doctype string) (*apps.Intent, string) {
473+
action := "SHARING"
474+
if app != nil {
475+
if intent := app.FindIntent(action, doctype); intent != nil {
476+
return intent, app.Slug()
477+
}
478+
}
479+
var mans []apps.WebappManifest
480+
err := couchdb.GetAllDocs(inst, consts.Apps, &couchdb.AllDocsRequest{}, &mans)
481+
if err != nil {
482+
return nil, ""
483+
}
484+
for _, man := range mans {
485+
if intent := man.FindIntent(action, doctype); intent != nil {
486+
return intent, man.Slug()
487+
}
488+
}
489+
return nil, ""
490+
}
491+
492+
// RedirectAfterAuthorizeURL returns the URL for the redirection after a user
493+
// has authorized a sharing.
494+
func (s *Sharing) RedirectAfterAuthorizeURL(inst *instance.Instance) *url.URL {
495+
doctype := s.Rules[0].DocType
496+
app, _ := apps.GetWebappBySlug(inst, s.AppSlug)
497+
498+
if intent, slug := findIntentForRedirect(inst, app, doctype); intent != nil {
499+
u := inst.SubDomain(slug)
500+
parts := strings.SplitN(intent.Href, "#", 2)
501+
if len(parts[0]) > 0 {
502+
u.Path = parts[0]
503+
}
504+
if len(parts) == 2 && len(parts[1]) > 0 {
505+
u.Fragment = parts[1]
506+
}
507+
u.RawQuery = "sharing=" + s.SID
508+
return u
509+
}
510+
511+
if app == nil {
512+
return inst.DefaultRedirection()
513+
}
514+
return inst.SubDomain(app.Slug())
515+
}
516+
469517
var _ couchdb.Doc = &Sharing{}

web/auth/auth.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,8 @@ func authorizeSharing(c echo.Context) error {
722722
if err = s.SendAnswer(instance, params.state); err != nil {
723723
return err
724724
}
725-
return c.Redirect(http.StatusSeeOther, instance.DefaultRedirection().String())
725+
redirect := s.RedirectAfterAuthorizeURL(instance)
726+
return c.Redirect(http.StatusSeeOther, redirect.String())
726727
}
727728

728729
func authorizeAppForm(c echo.Context) error {

0 commit comments

Comments
 (0)