Skip to content
This repository was archived by the owner on Nov 25, 2024. It is now read-only.

Commit 5bcaddf

Browse files
committed
Make VerifyAccessToken private, let VerifyUserFromRequest return the
device if present Signed-off-by: Anant Prakash <anantprakashjsr@gmail.com>
1 parent 9282969 commit 5bcaddf

2 files changed

Lines changed: 16 additions & 20 deletions

File tree

src/github.com/matrix-org/dendrite/clientapi/auth/auth.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,26 @@ type AccountDatabase interface {
4949
}
5050

5151
// VerifyUserFromRequest authenticates the HTTP request,
52-
// on success returns UserID of the requester.
52+
// on success returns UserID, Device of the requester.
5353
// Finds local user or an application service user.
54+
// Note: For an AS user, Device is not present.
5455
// On failure returns an JSON error response which can be sent to the client.
5556
func VerifyUserFromRequest(
5657
req *http.Request, accountDB AccountDatabase, deviceDB DeviceDatabase,
5758
applicationServices []config.ApplicationService,
58-
) (string, *util.JSONResponse) {
59+
) (string, *authtypes.Device, *util.JSONResponse) {
5960
// Try to find local user from device database
60-
dev, devErr := VerifyAccessToken(req, deviceDB)
61+
dev, devErr := verifyAccessToken(req, deviceDB)
6162

6263
if devErr == nil {
63-
return dev.UserID, nil
64+
return dev.UserID, dev, nil
6465
}
6566

6667
// Try to find the Application Service user
6768
token, err := extractAccessToken(req)
6869

6970
if err != nil {
70-
return "", &util.JSONResponse{
71+
return "", nil, &util.JSONResponse{
7172
Code: http.StatusUnauthorized,
7273
JSON: jsonerror.MissingToken(err.Error()),
7374
}
@@ -87,7 +88,7 @@ func VerifyUserFromRequest(
8788
localpart, err := userutil.ParseUsernameParam(userID, nil)
8889

8990
if err != nil {
90-
return "", &util.JSONResponse{
91+
return "", nil, &util.JSONResponse{
9192
Code: http.StatusBadRequest,
9293
JSON: jsonerror.InvalidUsername(err.Error()),
9394
}
@@ -98,25 +99,25 @@ func VerifyUserFromRequest(
9899

99100
// Verify that account exists & appServiceID matches
100101
if accountErr == nil && account.AppServiceID == appService.ID {
101-
return userID, nil
102+
return userID, nil, nil
102103
}
103104

104-
return "", &util.JSONResponse{
105+
return "", nil, &util.JSONResponse{
105106
Code: http.StatusForbidden,
106107
JSON: jsonerror.Forbidden("Application service has not registered this user"),
107108
}
108109
}
109110

110-
return "", &util.JSONResponse{
111+
return "", nil, &util.JSONResponse{
111112
Code: http.StatusUnauthorized,
112113
JSON: jsonerror.UnknownToken("Unrecognized access token"),
113114
}
114115
}
115116

116-
// VerifyAccessToken verifies that an access token was supplied in the given HTTP request
117+
// verifyAccessToken verifies that an access token was supplied in the given HTTP request
117118
// and returns the device it corresponds to. Returns resErr (an error response which can be
118119
// sent to the client) if the token is invalid or there was a problem querying the database.
119-
func VerifyAccessToken(req *http.Request, deviceDB DeviceDatabase) (device *authtypes.Device, resErr *util.JSONResponse) {
120+
func verifyAccessToken(req *http.Request, deviceDB DeviceDatabase) (device *authtypes.Device, resErr *util.JSONResponse) {
120121
token, err := extractAccessToken(req)
121122
if err != nil {
122123
resErr = &util.JSONResponse{

src/github.com/matrix-org/dendrite/common/httpapi.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,12 @@ func MakeAuthAPI(
1919
metricsName string, accountDB auth.AccountDatabase, deviceDB auth.DeviceDatabase,
2020
appServices []config.ApplicationService, f func(*http.Request, string, *authtypes.Device) util.JSONResponse) http.Handler {
2121
h := func(req *http.Request) util.JSONResponse {
22-
user, userErr := auth.VerifyUserFromRequest(req, accountDB, deviceDB, appServices)
22+
user, device, err := auth.VerifyUserFromRequest(req, accountDB, deviceDB, appServices)
2323

24-
if userErr != nil {
25-
return *userErr
26-
}
27-
device, resErr := auth.VerifyAccessToken(req, deviceDB)
28-
29-
// AS virtual users do not have a device in database
30-
if resErr != nil {
31-
return f(req, user, nil)
24+
if err != nil {
25+
return *err
3226
}
27+
// device is nil for AS virtual users, as they do not have a device in database
3328
return f(req, user, device)
3429
}
3530
return MakeExternalAPI(metricsName, h)

0 commit comments

Comments
 (0)