2018-07-17 15:36:04 +01: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 16:17:09 +01:00
|
|
|
"context"
|
2022-11-02 10:17:53 +00:00
|
|
|
"encoding/json"
|
2020-12-18 13:33:28 +00:00
|
|
|
"errors"
|
2018-08-08 16:17:09 +01:00
|
|
|
|
2018-08-10 15:47:14 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
2022-03-24 21:45:44 +00:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2018-07-17 15:36:04 +01:00
|
|
|
)
|
|
|
|
|
2022-05-06 12:39:26 +01:00
|
|
|
// AppServiceInternalAPI is used to query user and room alias data from application
|
|
|
|
// services
|
|
|
|
type AppServiceInternalAPI interface {
|
|
|
|
// Check whether a room alias exists within any application service namespaces
|
|
|
|
RoomAliasExists(
|
|
|
|
ctx context.Context,
|
|
|
|
req *RoomAliasExistsRequest,
|
|
|
|
resp *RoomAliasExistsResponse,
|
|
|
|
) error
|
|
|
|
// Check whether a user ID exists within any application service namespaces
|
|
|
|
UserIDExists(
|
|
|
|
ctx context.Context,
|
|
|
|
req *UserIDExistsRequest,
|
|
|
|
resp *UserIDExistsResponse,
|
|
|
|
) error
|
2022-11-02 10:17:53 +00:00
|
|
|
|
|
|
|
Locations(ctx context.Context, req *LocationRequest, resp *LocationResponse) error
|
|
|
|
User(ctx context.Context, request *UserRequest, response *UserResponse) error
|
|
|
|
Protocols(ctx context.Context, req *ProtocolRequest, resp *ProtocolResponse) error
|
2022-05-06 12:39:26 +01:00
|
|
|
}
|
|
|
|
|
2018-08-08 16:17:09 +01: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 15:47:14 +01: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"`
|
|
|
|
}
|
|
|
|
|
2022-11-02 10:17:53 +00:00
|
|
|
const (
|
2024-02-03 17:56:13 +00:00
|
|
|
ASProtocolLegacyPath = "/_matrix/app/unstable/thirdparty/protocol/"
|
|
|
|
ASUserLegacyPath = "/_matrix/app/unstable/thirdparty/user"
|
|
|
|
ASLocationLegacyPath = "/_matrix/app/unstable/thirdparty/location"
|
|
|
|
ASRoomAliasExistsLegacyPath = "/rooms/"
|
|
|
|
ASUserExistsLegacyPath = "/users/"
|
|
|
|
|
|
|
|
ASProtocolPath = "/_matrix/app/v1/thirdparty/protocol/"
|
|
|
|
ASUserPath = "/_matrix/app/v1/thirdparty/user"
|
|
|
|
ASLocationPath = "/_matrix/app/v1/thirdparty/location"
|
|
|
|
ASRoomAliasExistsPath = "/_matrix/app/v1/rooms/"
|
|
|
|
ASUserExistsPath = "/_matrix/app/v1/users/"
|
2022-11-02 10:17:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProtocolRequest struct {
|
|
|
|
Protocol string `json:"protocol,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProtocolResponse struct {
|
|
|
|
Protocols map[string]ASProtocolResponse `json:"protocols"`
|
|
|
|
Exists bool `json:"exists"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ASProtocolResponse struct {
|
|
|
|
FieldTypes map[string]FieldType `json:"field_types,omitempty"` // NOTSPEC: field_types is required by the spec
|
|
|
|
Icon string `json:"icon"`
|
|
|
|
Instances []ProtocolInstance `json:"instances"`
|
|
|
|
LocationFields []string `json:"location_fields"`
|
|
|
|
UserFields []string `json:"user_fields"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type FieldType struct {
|
|
|
|
Placeholder string `json:"placeholder"`
|
|
|
|
Regexp string `json:"regexp"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProtocolInstance struct {
|
|
|
|
Description string `json:"desc"`
|
|
|
|
Icon string `json:"icon,omitempty"`
|
|
|
|
NetworkID string `json:"network_id,omitempty"` // NOTSPEC: network_id is required by the spec
|
|
|
|
Fields json.RawMessage `json:"fields,omitempty"` // NOTSPEC: fields is required by the spec
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserRequest struct {
|
|
|
|
Protocol string `json:"protocol"`
|
|
|
|
Params string `json:"params"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserResponse struct {
|
|
|
|
Users []ASUserResponse `json:"users,omitempty"`
|
|
|
|
Exists bool `json:"exists,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ASUserResponse struct {
|
|
|
|
Protocol string `json:"protocol"`
|
|
|
|
UserID string `json:"userid"`
|
|
|
|
Fields json.RawMessage `json:"fields"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type LocationRequest struct {
|
|
|
|
Protocol string `json:"protocol"`
|
|
|
|
Params string `json:"params"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type LocationResponse struct {
|
|
|
|
Locations []ASLocationResponse `json:"locations,omitempty"`
|
|
|
|
Exists bool `json:"exists,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ASLocationResponse struct {
|
|
|
|
Alias string `json:"alias"`
|
|
|
|
Protocol string `json:"protocol"`
|
|
|
|
Fields json.RawMessage `json:"fields"`
|
|
|
|
}
|
|
|
|
|
2023-04-03 19:19:26 +01:00
|
|
|
// ErrProfileNotExists is returned when trying to lookup a user's profile that
|
|
|
|
// doesn't exist locally.
|
|
|
|
var ErrProfileNotExists = errors.New("no known profile for given user ID")
|
|
|
|
|
2019-07-12 16:43:01 +01:00
|
|
|
// RetrieveUserProfile is a wrapper that queries both the local database and
|
2018-08-10 15:47:14 +01:00
|
|
|
// application services for a given user's profile
|
2020-06-04 15:43:07 +01:00
|
|
|
// TODO: Remove this, it's called from federationapi and clientapi but is a pure function
|
2019-07-12 16:43:01 +01:00
|
|
|
func RetrieveUserProfile(
|
2018-08-10 15:47:14 +01:00
|
|
|
ctx context.Context,
|
|
|
|
userID string,
|
2022-05-06 12:39:26 +01:00
|
|
|
asAPI AppServiceInternalAPI,
|
2023-04-03 19:19:26 +01:00
|
|
|
profileAPI userapi.ProfileAPI,
|
2018-08-10 15:47:14 +01:00
|
|
|
) (*authtypes.Profile, error) {
|
|
|
|
// Try to query the user from the local database
|
2023-04-03 19:19:26 +01:00
|
|
|
profile, err := profileAPI.QueryProfile(ctx, userID)
|
|
|
|
if err == nil {
|
2018-08-10 15:47:14 +01: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 {
|
2023-04-03 19:19:26 +01:00
|
|
|
return nil, ErrProfileNotExists
|
2018-08-10 15:47:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to query the user from the local database again
|
2023-04-03 19:19:26 +01:00
|
|
|
profile, err = profileAPI.QueryProfile(ctx, userID)
|
2018-08-10 15:47:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// profile should not be nil at this point
|
2023-04-03 19:19:26 +01:00
|
|
|
return profile, nil
|
2018-08-10 15:47:14 +01:00
|
|
|
}
|