From 512c3dc787049984c3dcfc16bfb0d5bade4624d2 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Thu, 31 Aug 2017 17:57:26 +0100 Subject: [PATCH] Publish an association if it has been validated and requested --- .../dendrite/clientapi/readers/threepid.go | 9 ++++++ .../dendrite/clientapi/threepid/threepid.go | 32 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/github.com/matrix-org/dendrite/clientapi/readers/threepid.go b/src/github.com/matrix-org/dendrite/clientapi/readers/threepid.go index bb78baf39..2eb5eb919 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/readers/threepid.go +++ b/src/github.com/matrix-org/dendrite/clientapi/readers/threepid.go @@ -88,6 +88,7 @@ func CheckAndSave3PIDAssociation( return *reqErr } + // Check if the association has been validated verified, address, medium, err := threepid.CheckAssociation(body.Creds) if err != nil { return httputil.LogThenError(req, err) @@ -103,6 +104,14 @@ func CheckAndSave3PIDAssociation( } } + if body.Bind { + // Publish the association on the identity server if requested + if err = threepid.PublishAssociation(body.Creds, device.UserID); err != nil { + return httputil.LogThenError(req, err) + } + } + + // Save the association in the database localpart, _, err := gomatrixserverlib.SplitID('@', device.UserID) if err != nil { return httputil.LogThenError(req, err) diff --git a/src/github.com/matrix-org/dendrite/clientapi/threepid/threepid.go b/src/github.com/matrix-org/dendrite/clientapi/threepid/threepid.go index 92cef2b2d..df73f75b4 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/threepid/threepid.go +++ b/src/github.com/matrix-org/dendrite/clientapi/threepid/threepid.go @@ -118,3 +118,35 @@ func CheckAssociation(creds Credentials) (bool, string, string, error) { return true, respBody.Address, respBody.Medium, nil } + +// PublishAssociation publishes a validated association between a third-party +// identifier and a Matrix ID. +// Returns an error if there was a problem sending the request or decoding the +// response, or if the identity server responded with a non-OK status. +func PublishAssociation(creds Credentials, userID string) error { + postURL := fmt.Sprintf("https://%s/_matrix/identity/api/v1/3pid/bind", creds.IDServer) + + data := url.Values{} + data.Add("sid", creds.SID) + data.Add("client_secret", creds.Secret) + data.Add("mxid", userID) + + request, err := http.NewRequest("POST", postURL, strings.NewReader(data.Encode())) + if err != nil { + return err + } + request.Header.Add("Content-Type", "application/x-www-form-urlencoded") + + client := http.Client{} + resp, err := client.Do(request) + if err != nil { + return err + } + + // Error if the status isn't OK + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("Could not publish the association on the server %s", creds.IDServer) + } + + return nil +}