-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix a lot of things about sharing #1130
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
74dde24
Fix a todo
nono efb38bf
Refactor the send mail function for sharings
nono 6af8502
Refactor a bit revoke.go
nono f938df7
Remove the button to refuse a sharing
nono 08c1e04
Allow to refresh OAuth tokens used by the recipients for a two-way sh…
nono b187fab
Implement the expires_at field on the permissions
nono 26470d7
Remove the revoked flag on sharings
nono 8e8cb0d
Fix the request to accept a sharing
nono 907878b
Fix sharer serialization in JSON
nono b6d3d3c
Display the description in the mail instructions for sharing
nono 3263774
Fix accepting a sharing
nono b7b2a35
Lint
nono File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package sharings | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/cozy/cozy-stack/client/auth" | ||
"github.com/cozy/cozy-stack/client/request" | ||
"github.com/cozy/cozy-stack/pkg/couchdb" | ||
"github.com/cozy/cozy-stack/pkg/instance" | ||
) | ||
|
||
// RecipientInfo describes the recipient information that will be transmitted to | ||
// the sharing workers. | ||
type RecipientInfo struct { | ||
Domain string | ||
Scheme string | ||
Client auth.Client | ||
AccessToken auth.AccessToken | ||
} | ||
|
||
// ExtractRecipientInfo returns a RecipientInfo from a Member | ||
func ExtractRecipientInfo(m *Member) (*RecipientInfo, error) { | ||
if m.URL == "" { | ||
return nil, ErrRecipientHasNoURL | ||
} | ||
u, err := url.Parse(m.URL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
info := RecipientInfo{ | ||
Domain: u.Host, | ||
Scheme: u.Scheme, | ||
AccessToken: m.AccessToken, | ||
Client: m.Client, | ||
} | ||
return &info, nil | ||
} | ||
|
||
// RefreshTokenAndRetry is called after an authentication failure. | ||
// It tries to renew the access_token and request again | ||
func RefreshTokenAndRetry(ins *instance.Instance, sharingID string, info *RecipientInfo, opts *request.Options) (*http.Response, error) { | ||
req := &auth.Request{ | ||
Domain: opts.Domain, | ||
Scheme: opts.Scheme, | ||
} | ||
sharing, err := FindSharing(ins, sharingID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var m *Member | ||
if sharing.Owner { | ||
m, err = sharing.GetMemberFromClientID(ins, info.Client.ClientID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
if sharing.Sharer.Client.ClientID != info.Client.ClientID { | ||
return nil, ErrRecipientDoesNotExist | ||
} | ||
m = sharing.Sharer | ||
} | ||
refreshToken := info.AccessToken.RefreshToken | ||
access, err := req.RefreshToken(&info.Client, &info.AccessToken) | ||
if err != nil { | ||
ins.Logger().Errorf("[sharing] Refresh token request failed: %v", err) | ||
return nil, err | ||
} | ||
access.RefreshToken = refreshToken | ||
m.AccessToken = *access | ||
if err = couchdb.UpdateDoc(ins, sharing); err != nil { | ||
return nil, err | ||
} | ||
opts.Headers["Authorization"] = "Bearer " + access.AccessToken | ||
res, err := request.Req(opts) | ||
return res, err | ||
} | ||
|
||
// IsAuthError returns true if the given error is an authentication one | ||
func IsAuthError(err error) bool { | ||
if v, ok := err.(*request.Error); ok { | ||
return v.Title == "Bad Request" || v.Title == "Unauthorized" | ||
} | ||
return false | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not using a
*time.Time
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. I have copied what was done here: https://github.com/cozy/cozy-stack/blob/master/pkg/oauth/client.go#L63