mirror of
				https://github.com/matrix-org/gomatrix
				synced 2025-11-04 06:18:05 +00:00 
			
		
		
		
	Switch from access_token in URL to token in header
This commit is contained in:
		
							parent
							
								
									408fff5e6a
								
							
						
					
					
						commit
						d8d51fff7e
					
				
					 2 changed files with 13 additions and 9 deletions
				
			
		
							
								
								
									
										16
									
								
								client.go
									
										
									
									
									
								
							
							
						
						
									
										16
									
								
								client.go
									
										
									
									
									
								
							| 
						 | 
					@ -53,7 +53,7 @@ func (e HTTPError) Error() string {
 | 
				
			||||||
	return fmt.Sprintf("contents=%v msg=%s code=%d wrapped=%s", e.Contents, e.Message, e.Code, wrappedErrMsg)
 | 
						return fmt.Sprintf("contents=%v msg=%s code=%d wrapped=%s", e.Contents, e.Message, e.Code, wrappedErrMsg)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// BuildURL builds a URL with the Client's homserver/prefix/access_token set already.
 | 
					// BuildURL builds a URL with the Client's homserver/prefix set already.
 | 
				
			||||||
func (cli *Client) BuildURL(urlPath ...string) string {
 | 
					func (cli *Client) BuildURL(urlPath ...string) string {
 | 
				
			||||||
	ps := []string{cli.Prefix}
 | 
						ps := []string{cli.Prefix}
 | 
				
			||||||
	for _, p := range urlPath {
 | 
						for _, p := range urlPath {
 | 
				
			||||||
| 
						 | 
					@ -62,7 +62,7 @@ func (cli *Client) BuildURL(urlPath ...string) string {
 | 
				
			||||||
	return cli.BuildBaseURL(ps...)
 | 
						return cli.BuildBaseURL(ps...)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// BuildBaseURL builds a URL with the Client's homeserver/access_token set already. You must
 | 
					// BuildBaseURL builds a URL with the Client's homeserver set already. You must
 | 
				
			||||||
// supply the prefix in the path.
 | 
					// supply the prefix in the path.
 | 
				
			||||||
func (cli *Client) BuildBaseURL(urlPath ...string) string {
 | 
					func (cli *Client) BuildBaseURL(urlPath ...string) string {
 | 
				
			||||||
	// copy the URL. Purposefully ignore error as the input is from a valid URL already
 | 
						// copy the URL. Purposefully ignore error as the input is from a valid URL already
 | 
				
			||||||
| 
						 | 
					@ -75,9 +75,6 @@ func (cli *Client) BuildBaseURL(urlPath ...string) string {
 | 
				
			||||||
		hsURL.Path = hsURL.Path + "/"
 | 
							hsURL.Path = hsURL.Path + "/"
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	query := hsURL.Query()
 | 
						query := hsURL.Query()
 | 
				
			||||||
	if cli.AccessToken != "" {
 | 
					 | 
				
			||||||
		query.Set("access_token", cli.AccessToken)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if cli.AppServiceUserID != "" {
 | 
						if cli.AppServiceUserID != "" {
 | 
				
			||||||
		query.Set("user_id", cli.AppServiceUserID)
 | 
							query.Set("user_id", cli.AppServiceUserID)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -85,7 +82,7 @@ func (cli *Client) BuildBaseURL(urlPath ...string) string {
 | 
				
			||||||
	return hsURL.String()
 | 
						return hsURL.String()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// BuildURLWithQuery builds a URL with query parameters in addition to the Client's homeserver/prefix/access_token set already.
 | 
					// BuildURLWithQuery builds a URL with query parameters in addition to the Client's homeserver/prefix set already.
 | 
				
			||||||
func (cli *Client) BuildURLWithQuery(urlPath []string, urlQuery map[string]string) string {
 | 
					func (cli *Client) BuildURLWithQuery(urlPath []string, urlQuery map[string]string) string {
 | 
				
			||||||
	u, _ := url.Parse(cli.BuildURL(urlPath...))
 | 
						u, _ := url.Parse(cli.BuildURL(urlPath...))
 | 
				
			||||||
	q := u.Query()
 | 
						q := u.Query()
 | 
				
			||||||
| 
						 | 
					@ -206,7 +203,13 @@ func (cli *Client) MakeRequest(method string, httpURL string, reqBody interface{
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	req.Header.Set("Content-Type", "application/json")
 | 
						req.Header.Set("Content-Type", "application/json")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if cli.AccessToken != "" {
 | 
				
			||||||
 | 
							req.Header.Set("Authorization", "Bearer "+cli.AccessToken)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	res, err := cli.Client.Do(req)
 | 
						res, err := cli.Client.Do(req)
 | 
				
			||||||
	if res != nil {
 | 
						if res != nil {
 | 
				
			||||||
		defer res.Body.Close()
 | 
							defer res.Body.Close()
 | 
				
			||||||
| 
						 | 
					@ -647,6 +650,7 @@ func (cli *Client) UploadToContentRepo(content io.Reader, contentType string, co
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	req.Header.Set("Content-Type", contentType)
 | 
						req.Header.Set("Content-Type", contentType)
 | 
				
			||||||
 | 
						req.Header.Set("Authorization", "Bearer "+cli.AccessToken)
 | 
				
			||||||
	req.ContentLength = contentLength
 | 
						req.ContentLength = contentLength
 | 
				
			||||||
	res, err := cli.Client.Do(req)
 | 
						res, err := cli.Client.Do(req)
 | 
				
			||||||
	if res != nil {
 | 
						if res != nil {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -55,7 +55,7 @@ func ExampleClient_BuildURLWithQuery() {
 | 
				
			||||||
		"filter_id": "5",
 | 
							"filter_id": "5",
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	fmt.Println(out)
 | 
						fmt.Println(out)
 | 
				
			||||||
	// Output: https://matrix.org/_matrix/client/r0/sync?access_token=abcdef123456&filter_id=5
 | 
						// Output: https://matrix.org/_matrix/client/r0/sync?filter_id=5
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func ExampleClient_BuildURL() {
 | 
					func ExampleClient_BuildURL() {
 | 
				
			||||||
| 
						 | 
					@ -63,7 +63,7 @@ func ExampleClient_BuildURL() {
 | 
				
			||||||
	cli, _ := NewClient("https://matrix.org", userID, "abcdef123456")
 | 
						cli, _ := NewClient("https://matrix.org", userID, "abcdef123456")
 | 
				
			||||||
	out := cli.BuildURL("user", userID, "filter")
 | 
						out := cli.BuildURL("user", userID, "filter")
 | 
				
			||||||
	fmt.Println(out)
 | 
						fmt.Println(out)
 | 
				
			||||||
	// Output: https://matrix.org/_matrix/client/r0/user/@example:matrix.org/filter?access_token=abcdef123456
 | 
						// Output: https://matrix.org/_matrix/client/r0/user/@example:matrix.org/filter
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func ExampleClient_BuildBaseURL() {
 | 
					func ExampleClient_BuildBaseURL() {
 | 
				
			||||||
| 
						 | 
					@ -71,7 +71,7 @@ func ExampleClient_BuildBaseURL() {
 | 
				
			||||||
	cli, _ := NewClient("https://matrix.org", userID, "abcdef123456")
 | 
						cli, _ := NewClient("https://matrix.org", userID, "abcdef123456")
 | 
				
			||||||
	out := cli.BuildBaseURL("_matrix", "client", "r0", "directory", "room", "#matrix:matrix.org")
 | 
						out := cli.BuildBaseURL("_matrix", "client", "r0", "directory", "room", "#matrix:matrix.org")
 | 
				
			||||||
	fmt.Println(out)
 | 
						fmt.Println(out)
 | 
				
			||||||
	// Output: https://matrix.org/_matrix/client/r0/directory/room/%23matrix:matrix.org?access_token=abcdef123456
 | 
						// Output: https://matrix.org/_matrix/client/r0/directory/room/%23matrix:matrix.org
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Retrieve the content of a m.room.name state event.
 | 
					// Retrieve the content of a m.room.name state event.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue