Publish an association if it has been validated and requested

This commit is contained in:
Brendan Abolivier 2017-08-31 17:57:26 +01:00
parent e0cad6dc20
commit 512c3dc787
No known key found for this signature in database
GPG key ID: 8EF1500759F70623
2 changed files with 41 additions and 0 deletions

View file

@ -88,6 +88,7 @@ func CheckAndSave3PIDAssociation(
return *reqErr return *reqErr
} }
// Check if the association has been validated
verified, address, medium, err := threepid.CheckAssociation(body.Creds) verified, address, medium, err := threepid.CheckAssociation(body.Creds)
if err != nil { if err != nil {
return httputil.LogThenError(req, err) 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) localpart, _, err := gomatrixserverlib.SplitID('@', device.UserID)
if err != nil { if err != nil {
return httputil.LogThenError(req, err) return httputil.LogThenError(req, err)

View file

@ -118,3 +118,35 @@ func CheckAssociation(creds Credentials) (bool, string, string, error) {
return true, respBody.Address, respBody.Medium, nil 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
}