Keycloak

Форк
0
157 строк · 4.0 Кб
1
import { KeycloakContext } from "../root/KeycloakContext";
2
import { joinPath } from "../utils/joinPath";
3
import { parseResponse } from "./parse-response";
4
import {
5
  ClientRepresentation,
6
  CredentialContainer,
7
  CredentialRepresentation,
8
  DeviceRepresentation,
9
  Group,
10
  LinkedAccountRepresentation,
11
  Permission,
12
  UserRepresentation,
13
} from "./representations";
14
import { request } from "./request";
15

16
export type CallOptions = {
17
  context: KeycloakContext;
18
  signal?: AbortSignal;
19
};
20

21
export type PaginationParams = {
22
  first: number;
23
  max: number;
24
};
25

26
export async function getPersonalInfo({
27
  signal,
28
  context,
29
}: CallOptions): Promise<UserRepresentation> {
30
  const response = await request("/?userProfileMetadata=true", context, {
31
    signal,
32
  });
33
  return parseResponse<UserRepresentation>(response);
34
}
35

36
export async function getSupportedLocales({
37
  signal,
38
  context,
39
}: CallOptions): Promise<string[]> {
40
  const response = await request("/supportedLocales", context, { signal });
41
  return parseResponse<string[]>(response);
42
}
43

44
export async function savePersonalInfo(
45
  context: KeycloakContext,
46
  info: UserRepresentation,
47
): Promise<void> {
48
  const response = await request("/", context, { body: info, method: "POST" });
49
  if (!response.ok) {
50
    const { errors } = await response.json();
51
    throw errors;
52
  }
53
  return undefined;
54
}
55

56
export async function getPermissionRequests(
57
  resourceId: string,
58
  { signal, context }: CallOptions,
59
): Promise<Permission[]> {
60
  const response = await request(
61
    `/resources/${resourceId}/permissions/requests`,
62
    context,
63
    { signal },
64
  );
65

66
  return parseResponse<Permission[]>(response);
67
}
68

69
export async function getDevices({
70
  signal,
71
  context,
72
}: CallOptions): Promise<DeviceRepresentation[]> {
73
  const response = await request("/sessions/devices", context, { signal });
74
  return parseResponse<DeviceRepresentation[]>(response);
75
}
76

77
export async function getApplications({
78
  signal,
79
  context,
80
}: CallOptions): Promise<ClientRepresentation[]> {
81
  const response = await request("/applications", context, { signal });
82
  return parseResponse<ClientRepresentation[]>(response);
83
}
84

85
export async function deleteConsent(context: KeycloakContext, id: string) {
86
  return request(`/applications/${id}/consent`, context, { method: "DELETE" });
87
}
88

89
export async function deleteSession(context: KeycloakContext, id?: string) {
90
  return request(`/sessions${id ? `/${id}` : ""}`, context, {
91
    method: "DELETE",
92
  });
93
}
94

95
export async function getCredentials({ signal, context }: CallOptions) {
96
  const response = await request("/credentials", context, {
97
    signal,
98
  });
99
  return parseResponse<CredentialContainer[]>(response);
100
}
101

102
export async function deleteCredentials(
103
  context: KeycloakContext,
104
  credential: CredentialRepresentation,
105
) {
106
  return request("/credentials/" + credential.id, context, {
107
    method: "DELETE",
108
  });
109
}
110

111
export async function getLinkedAccounts({ signal, context }: CallOptions) {
112
  const response = await request("/linked-accounts", context, { signal });
113
  return parseResponse<LinkedAccountRepresentation[]>(response);
114
}
115

116
export async function unLinkAccount(
117
  context: KeycloakContext,
118
  account: LinkedAccountRepresentation,
119
) {
120
  const response = await request(
121
    "/linked-accounts/" + account.providerName,
122
    context,
123
    {
124
      method: "DELETE",
125
    },
126
  );
127
  return parseResponse(response);
128
}
129

130
export async function linkAccount(
131
  context: KeycloakContext,
132
  account: LinkedAccountRepresentation,
133
) {
134
  const redirectUri = encodeURIComponent(
135
    joinPath(
136
      context.environment.authUrl,
137
      "realms",
138
      context.environment.realm,
139
      "account",
140
    ),
141
  );
142
  const response = await request(
143
    "/linked-accounts/" + account.providerName,
144
    context,
145
    {
146
      searchParams: { providerId: account.providerName, redirectUri },
147
    },
148
  );
149
  return parseResponse<{ accountLinkUri: string }>(response);
150
}
151

152
export async function getGroups({ signal, context }: CallOptions) {
153
  const response = await request("/groups", context, {
154
    signal,
155
  });
156
  return parseResponse<Group[]>(response);
157
}
158

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.