2018-07-17 16:36:04 +02:00
|
|
|
// Copyright 2018 New Vector Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Package api contains methods used by dendrite components in multi-process
|
|
|
|
// mode to send requests to the appservice component, typically in order to ask
|
|
|
|
// an application service for some information.
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2018-08-08 17:17:09 +02:00
|
|
|
"context"
|
2020-12-18 14:33:28 +01:00
|
|
|
"errors"
|
2018-08-08 17:17:09 +02:00
|
|
|
|
2018-08-10 16:47:14 +02:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
2022-03-24 22:45:44 +01:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2018-08-10 16:47:14 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2018-07-17 16:36:04 +02:00
|
|
|
)
|
|
|
|
|
2018-08-08 17:17:09 +02:00
|
|
|
// RoomAliasExistsRequest is a request to an application service
|
|
|
|
// about whether a room alias exists
|
|
|
|
type RoomAliasExistsRequest struct {
|
|
|
|
// Alias we want to lookup
|
|
|
|
Alias string `json:"alias"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// RoomAliasExistsResponse is a response from an application service
|
|
|
|
// about whether a room alias exists
|
|
|
|
type RoomAliasExistsResponse struct {
|
|
|
|
AliasExists bool `json:"exists"`
|
|
|
|
}
|
|
|
|
|
2018-08-10 16:47:14 +02:00
|
|
|
// UserIDExistsRequest is a request to an application service about whether a
|
|
|
|
// user ID exists
|
|
|
|
type UserIDExistsRequest struct {
|
|
|
|
// UserID we want to lookup
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserIDExistsRequestAccessToken is a request to an application service
|
|
|
|
// about whether a user ID exists. Includes an access token
|
|
|
|
type UserIDExistsRequestAccessToken struct {
|
|
|
|
// UserID we want to lookup
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserIDExistsResponse is a response from an application service about
|
|
|
|
// whether a user ID exists
|
|
|
|
type UserIDExistsResponse struct {
|
|
|
|
UserIDExists bool `json:"exists"`
|
|
|
|
}
|
|
|
|
|
2018-07-17 16:36:04 +02:00
|
|
|
// AppServiceQueryAPI is used to query user and room alias data from application
|
|
|
|
// services
|
|
|
|
type AppServiceQueryAPI interface {
|
2018-08-08 17:17:09 +02:00
|
|
|
// Check whether a room alias exists within any application service namespaces
|
|
|
|
RoomAliasExists(
|
|
|
|
ctx context.Context,
|
|
|
|
req *RoomAliasExistsRequest,
|
2018-08-10 16:47:14 +02:00
|
|
|
resp *RoomAliasExistsResponse,
|
|
|
|
) error
|
|
|
|
// Check whether a user ID exists within any application service namespaces
|
|
|
|
UserIDExists(
|
|
|
|
ctx context.Context,
|
|
|
|
req *UserIDExistsRequest,
|
|
|
|
resp *UserIDExistsResponse,
|
2018-08-08 17:17:09 +02:00
|
|
|
) error
|
2018-07-17 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 17:43:01 +02:00
|
|
|
// RetrieveUserProfile is a wrapper that queries both the local database and
|
2018-08-10 16:47:14 +02:00
|
|
|
// application services for a given user's profile
|
2020-06-04 16:43:07 +02:00
|
|
|
// TODO: Remove this, it's called from federationapi and clientapi but is a pure function
|
2019-07-12 17:43:01 +02:00
|
|
|
func RetrieveUserProfile(
|
2018-08-10 16:47:14 +02:00
|
|
|
ctx context.Context,
|
|
|
|
userID string,
|
|
|
|
asAPI AppServiceQueryAPI,
|
2022-03-24 22:45:44 +01:00
|
|
|
profileAPI userapi.UserProfileAPI,
|
2018-08-10 16:47:14 +02:00
|
|
|
) (*authtypes.Profile, error) {
|
|
|
|
localpart, _, err := gomatrixserverlib.SplitID('@', userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to query the user from the local database
|
2022-03-24 22:45:44 +01:00
|
|
|
res := &userapi.QueryProfileResponse{}
|
|
|
|
err = profileAPI.QueryProfile(ctx, &userapi.QueryProfileRequest{UserID: userID}, res)
|
|
|
|
if err != nil {
|
2018-08-10 16:47:14 +02:00
|
|
|
return nil, err
|
2022-03-24 22:45:44 +01:00
|
|
|
}
|
|
|
|
profile := &authtypes.Profile{
|
|
|
|
Localpart: localpart,
|
|
|
|
DisplayName: res.DisplayName,
|
|
|
|
AvatarURL: res.AvatarURL,
|
|
|
|
}
|
|
|
|
if res.UserExists {
|
2018-08-10 16:47:14 +02:00
|
|
|
return profile, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query the appservice component for the existence of an AS user
|
|
|
|
userReq := UserIDExistsRequest{UserID: userID}
|
|
|
|
var userResp UserIDExistsResponse
|
|
|
|
if err = asAPI.UserIDExists(ctx, &userReq, &userResp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no user exists, return
|
|
|
|
if !userResp.UserIDExists {
|
2020-12-18 14:33:28 +01:00
|
|
|
return nil, errors.New("no known profile for given user ID")
|
2018-08-10 16:47:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to query the user from the local database again
|
2022-03-24 22:45:44 +01:00
|
|
|
err = profileAPI.QueryProfile(ctx, &userapi.QueryProfileRequest{UserID: userID}, res)
|
2018-08-10 16:47:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// profile should not be nil at this point
|
2022-03-24 22:45:44 +01:00
|
|
|
return &authtypes.Profile{
|
|
|
|
Localpart: localpart,
|
|
|
|
DisplayName: res.DisplayName,
|
|
|
|
AvatarURL: res.AvatarURL,
|
|
|
|
}, nil
|
2018-08-10 16:47:14 +02:00
|
|
|
}
|