auth-backend: added CatalogIdentityClient

This commit is contained in:
Patrik Oldsberg
2020-09-25 15:17:59 +02:00
parent faeffa0bb1
commit c6a879838d
3 changed files with 74 additions and 0 deletions
@@ -0,0 +1,55 @@
/*
* Copyright 2020 Spotify AB
*
* 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.
*/
import fetch from 'node-fetch';
import { UserEntity } from '@backstage/catalog-model';
import { PluginEndpointDiscovery } from '@backstage/backend-common';
type UserQuery = {
annotations: Record<string, string>;
};
/**
* A catalog client tailored for reading out identity data from the catalog.
*/
export class CatalogIdentityClient {
private readonly discovery: PluginEndpointDiscovery;
constructor(options: { discovery: PluginEndpointDiscovery }) {
this.discovery = options.discovery;
}
async findUser(query: UserQuery): Promise<UserEntity[]> {
const params = new URLSearchParams();
params.append('kind', 'User');
for (const [key, value] of Object.entries(query.annotations)) {
params.append(`metadata.annotations.${key}`, value);
}
const baseUrl = await this.discovery.getBaseUrl('catalog');
const response = await fetch(`${baseUrl}/entities?${params}`);
if (!response.ok) {
const text = await response.text();
throw new Error(
`Request failed with ${response.status} ${response.statusText}, ${text}`,
);
}
return response.json();
}
}
@@ -0,0 +1,17 @@
/*
* Copyright 2020 Spotify AB
*
* 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.
*/
export { CatalogIdentityClient } from './CatalogIdentityClient';