Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/service/json-rpc-authprovider-client.ts
2500 views
1
/**
2
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { PartialMessage } from "@bufbuild/protobuf";
8
import { PromiseClient } from "@connectrpc/connect";
9
import { AuthProviderService } from "@gitpod/public-api/lib/gitpod/v1/authprovider_connect";
10
import {
11
CreateAuthProviderRequest,
12
CreateAuthProviderResponse,
13
DeleteAuthProviderRequest,
14
DeleteAuthProviderResponse,
15
GetAuthProviderRequest,
16
GetAuthProviderResponse,
17
ListAuthProviderDescriptionsRequest,
18
ListAuthProviderDescriptionsResponse,
19
ListAuthProvidersRequest,
20
ListAuthProvidersResponse,
21
UpdateAuthProviderRequest,
22
UpdateAuthProviderResponse,
23
} from "@gitpod/public-api/lib/gitpod/v1/authprovider_pb";
24
import { converter } from "./public-api";
25
import { getGitpodService } from "./service";
26
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
27
28
export class JsonRpcAuthProviderClient implements PromiseClient<typeof AuthProviderService> {
29
async createAuthProvider(request: PartialMessage<CreateAuthProviderRequest>): Promise<CreateAuthProviderResponse> {
30
const ownerId = request.owner?.case === "ownerId" ? request.owner.value : undefined;
31
const organizationId = request.owner?.case === "organizationId" ? request.owner.value : undefined;
32
33
if (!organizationId && !ownerId) {
34
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "organizationId or ownerId is required");
35
}
36
if (!request.type) {
37
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "type is required");
38
}
39
if (!request.host) {
40
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "host is required");
41
}
42
43
if (organizationId) {
44
const result = await getGitpodService().server.createOrgAuthProvider({
45
entry: {
46
organizationId,
47
host: request.host,
48
type: converter.fromAuthProviderType(request.type),
49
clientId: request.oauth2Config?.clientId,
50
clientSecret: request.oauth2Config?.clientSecret,
51
},
52
});
53
return new CreateAuthProviderResponse({ authProvider: converter.toAuthProvider(result) });
54
}
55
if (ownerId) {
56
const result = await getGitpodService().server.updateOwnAuthProvider({
57
entry: {
58
host: request.host,
59
ownerId,
60
type: converter.fromAuthProviderType(request.type),
61
clientId: request.oauth2Config?.clientId,
62
clientSecret: request.oauth2Config?.clientSecret,
63
},
64
});
65
return new CreateAuthProviderResponse({ authProvider: converter.toAuthProvider(result) });
66
}
67
68
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "organizationId or ownerId is required");
69
}
70
71
async getAuthProvider(request: PartialMessage<GetAuthProviderRequest>): Promise<GetAuthProviderResponse> {
72
if (!request.authProviderId) {
73
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "authProviderId is required");
74
}
75
76
const provider = await getGitpodService().server.getAuthProvider(request.authProviderId);
77
return new GetAuthProviderResponse({
78
authProvider: converter.toAuthProvider(provider),
79
});
80
}
81
82
async listAuthProviders(request: PartialMessage<ListAuthProvidersRequest>): Promise<ListAuthProvidersResponse> {
83
if (!request.id?.case) {
84
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "id is required");
85
}
86
const organizationId = request.id.case === "organizationId" ? request.id.value : undefined;
87
const userId = request.id.case === "userId" ? request.id.value : undefined;
88
89
if (!organizationId && !userId) {
90
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "organizationId or userId is required");
91
}
92
93
const authProviders = !!organizationId
94
? await getGitpodService().server.getOrgAuthProviders({
95
organizationId,
96
})
97
: await getGitpodService().server.getOwnAuthProviders();
98
const response = new ListAuthProvidersResponse({
99
authProviders: authProviders.map(converter.toAuthProvider.bind(converter)),
100
});
101
return response;
102
}
103
104
async listAuthProviderDescriptions(
105
request: PartialMessage<ListAuthProviderDescriptionsRequest>,
106
): Promise<ListAuthProviderDescriptionsResponse> {
107
const aps = await getGitpodService().server.getAuthProviders();
108
return new ListAuthProviderDescriptionsResponse({
109
descriptions: aps.map((ap) => converter.toAuthProviderDescription(ap)),
110
});
111
}
112
113
async updateAuthProvider(request: PartialMessage<UpdateAuthProviderRequest>): Promise<UpdateAuthProviderResponse> {
114
if (!request.authProviderId) {
115
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "authProviderId is required");
116
}
117
const clientId = request?.clientId || "";
118
const clientSecret = request?.clientSecret || "";
119
if (!clientId && !clientSecret) {
120
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "clientId or clientSecret are required");
121
}
122
123
const entry = await getGitpodService().server.updateAuthProvider(request.authProviderId, {
124
clientId,
125
clientSecret,
126
});
127
return new UpdateAuthProviderResponse({
128
authProvider: converter.toAuthProvider(entry),
129
});
130
}
131
132
async deleteAuthProvider(request: PartialMessage<DeleteAuthProviderRequest>): Promise<DeleteAuthProviderResponse> {
133
if (!request.authProviderId) {
134
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "authProviderId is required");
135
}
136
await getGitpodService().server.deleteAuthProvider(request.authProviderId);
137
return new DeleteAuthProviderResponse();
138
}
139
}
140
141