Skip to content

Commit bf31c44

Browse files
committed
more fixes
1 parent ea875b3 commit bf31c44

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

clientapi/auth/default_user_verifier.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ type DefaultUserVerifier struct {
2020
// Note: For an AS user, AS dummy device is returned.
2121
// On failure returns an JSON error response which can be sent to the client.
2222
func (d *DefaultUserVerifier) VerifyUserFromRequest(req *http.Request) (*api.Device, *util.JSONResponse) {
23-
util.GetLogger(req.Context()).Debug("Default VerifyUserFromRequest")
23+
ctx := req.Context()
24+
util.GetLogger(ctx).Debug("Default VerifyUserFromRequest")
2425
// Try to find the Application Service user
2526
token, err := ExtractAccessToken(req)
2627
if err != nil {
@@ -30,12 +31,12 @@ func (d *DefaultUserVerifier) VerifyUserFromRequest(req *http.Request) (*api.Dev
3031
}
3132
}
3233
var res api.QueryAccessTokenResponse
33-
err = d.UserAPI.QueryAccessToken(req.Context(), &api.QueryAccessTokenRequest{
34+
err = d.UserAPI.QueryAccessToken(ctx, &api.QueryAccessTokenRequest{
3435
AccessToken: token,
3536
AppServiceUserID: req.URL.Query().Get("user_id"),
3637
}, &res)
3738
if err != nil {
38-
util.GetLogger(req.Context()).WithError(err).Error("userAPI.QueryAccessToken failed")
39+
util.GetLogger(ctx).WithError(err).Error("userAPI.QueryAccessToken failed")
3940
return nil, &util.JSONResponse{
4041
Code: http.StatusInternalServerError,
4142
JSON: spec.InternalServerError{},

setup/mscs/msc3861/msc3861.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package msc3861
22

33
import (
44
"github.com/element-hq/dendrite/setup"
5+
"github.com/matrix-org/gomatrixserverlib/fclient"
56
)
67

78
func Enable(m *setup.Monolith) error {
9+
client := fclient.NewClient()
810
userVerifier, err := newMSC3861UserVerifier(
911
m.UserAPI, m.Config.Global.ServerName,
1012
m.Config.MSCs.MSC3861, !m.Config.ClientAPI.GuestsDisabled,
11-
nil,
13+
client,
1214
)
1315
if err != nil {
1416
return err

setup/mscs/msc3861/msc3861_user_verifier.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package msc3861
22

33
import (
4-
"cmp"
54
"context"
65
"database/sql"
76
"encoding/json"
@@ -15,6 +14,7 @@ import (
1514
"github.com/element-hq/dendrite/clientapi/auth"
1615
"github.com/element-hq/dendrite/setup/config"
1716
"github.com/element-hq/dendrite/userapi/api"
17+
"github.com/matrix-org/gomatrixserverlib/fclient"
1818
"github.com/matrix-org/gomatrixserverlib/spec"
1919
"github.com/matrix-org/util"
2020
)
@@ -45,7 +45,7 @@ type MSC3861UserVerifier struct {
4545
userAPI api.UserInternalAPI
4646
serverName spec.ServerName
4747
cfg *config.MSC3861
48-
httpClient *http.Client
48+
httpClient *fclient.Client
4949
openIdConfig *OpenIDConfiguration
5050
allowGuest bool
5151
}
@@ -55,13 +55,21 @@ func newMSC3861UserVerifier(
5555
serverName spec.ServerName,
5656
cfg *config.MSC3861,
5757
allowGuest bool,
58-
httpClient *http.Client,
58+
client *fclient.Client,
5959
) (*MSC3861UserVerifier, error) {
60-
client := cmp.Or(httpClient, http.DefaultClient)
60+
if cfg == nil {
61+
return nil, errors.New("unable to create MSC3861UserVerifier object as 'cfg' param is nil")
62+
}
63+
64+
if client == nil {
65+
return nil, errors.New("unable to create MSC3861UserVerifier object as 'client' param is nil")
66+
}
67+
6168
openIdConfig, err := fetchOpenIDConfiguration(client, cfg.Issuer)
6269
if err != nil {
6370
return nil, err
6471
}
72+
6573
return &MSC3861UserVerifier{
6674
userAPI: userAPI,
6775
serverName: serverName,
@@ -342,14 +350,14 @@ func (m *MSC3861UserVerifier) introspectToken(ctx context.Context, token string)
342350
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
343351
req.SetBasicAuth(m.cfg.ClientID, m.cfg.ClientSecret)
344352

345-
resp, err := m.httpClient.Do(req)
353+
resp, err := m.httpClient.DoHTTPRequest(ctx, req)
346354
if err != nil {
347355
return nil, err
348356
}
349357
body := resp.Body
350358
defer resp.Body.Close() // nolint: errcheck
351359

352-
if c := resp.StatusCode; c < 200 || c >= 300 {
360+
if c := resp.StatusCode; c/100 != 2 {
353361
return nil, errors.New(strings.Join([]string{"The introspection endpoint returned a '", resp.Status, "' response"}, ""))
354362
}
355363
var ir introspectionResponse
@@ -394,13 +402,17 @@ type OpenIDConfiguration struct {
394402
AccountManagementActionsSupported []string `json:"account_management_actions_supported"`
395403
}
396404

397-
func fetchOpenIDConfiguration(httpClient *http.Client, authHostURL string) (*OpenIDConfiguration, error) {
405+
func fetchOpenIDConfiguration(httpClient *fclient.Client, authHostURL string) (*OpenIDConfiguration, error) {
398406
u, err := url.Parse(authHostURL)
399407
if err != nil {
400408
return nil, err
401409
}
402410
u = u.JoinPath(".well-known/openid-configuration")
403-
resp, err := httpClient.Get(u.String())
411+
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
412+
if err != nil {
413+
return nil, err
414+
}
415+
resp, err := httpClient.DoHTTPRequest(context.Background(), req)
404416
if err != nil {
405417
return nil, err
406418
}

0 commit comments

Comments
 (0)