Refactor admin service and keycloak interaction
MODIFY
src/app/components/admin/services/admin.service.ts
src/app/components/admin/services/admin.service.ts
@@ -52,20 +52,22 @@ export class AdminService {
*/
public createNewDeveloper(developer: Developer, keycloakUserId: string) {
// observer insert developer to API-Mgmt
- // response has developer object with developer id
+ // response has developer object
const url = this.apiMgmtUiRestUrl + '/developers';
const insertToApiMgmt = this.http.post(url, developer) as Observable<Developer>;
// observer to add keycloak user to developer portal group
const addDevPortalGroupToUser = this.keycloak.addDevPortalGroupToUser(keycloakUserId);
- // 2. insert developer to API-Mgmt
+ // 1. insert developer to API-Mgmt
return insertToApiMgmt.pipe(mergeMap(insertedDeveloper => {
- // 3. add devPortal group and add client role to keycloak user
- return forkJoin([addDevPortalGroupToUser])
+ // 2. add devPortal group and add client role to keycloak user
+ return addDevPortalGroupToUser
.pipe(map(() => insertedDeveloper));
}), catchError((err, caught) => {
// rollback keycloak user if developer cannot created at API-Mgmt
- // this.keycloak.deleteUser(keycloakUserToInsert.username).subscribe();
+ // we just remove the group mapping
+ // TODO REMOVE THIS COMFORT FEATURE IN FUTURE
+ this.keycloak.removeDevPortalGroupFromUser(keycloakUserId).subscribe();
throw err;
}));
}

MODIFY
src/app/components/admin/services/keycloak-interaction.service.ts
src/app/components/admin/services/keycloak-interaction.service.ts
import {Inject, Injectable} from '@angular/core';
import {KeycloakService} from 'keycloak-angular';
import KcAdminClient from 'keycloak-admin';
-import { from, Observable, of } from 'rxjs';
-import { map, mergeAll, mergeMap, share, single } from 'rxjs/operators';
-import UserRepresentation from 'keycloak-admin/lib/defs/userRepresentation';
-import {KeycloakUser} from '../../../services/api-data.service';
+import {from} from 'rxjs';
+import {map, mergeMap} from 'rxjs/operators';
import {TokenService} from '../../../services/token.service';
@Injectable({
@@ -15,8 +13,6 @@ export class KeycloakInteractionService {
private kcAdminClient: KcAdminClient;
private keycloakGroupUsers = 'API-Mgmt-Devportal-Users';
- private keycloakDevPortalClientId = 'devportal';
- private keycloakDevPortalUserRole = 'devportaluser'; // TODO REMOVE
/**
* Constructor of Keycloak Interaction Service
@@ -43,26 +39,6 @@ export class KeycloakInteractionService {
});
}
- /**
- * Get UUID of devportal client
- */
- private getDevPortalClientUUID() {
- return from(this.kcAdminClient.clients.find())
- .pipe(mergeAll())
- .pipe(single(client => client.clientId === this.keycloakDevPortalClientId), map(client => client.id))
- .pipe(share()); // fire only once and cache the result for other subscribers
- }
-
- // TODO REMOVE
- /**
- * Get the devportaluser user role
- */
- private getDevPortalUserRole() {
- return from(this.kcAdminClient.roles.findOneByName({
- name: this.keycloakDevPortalUserRole
- }));
- }
-
/**
* Get API-Mgmt-Devportal-Users Group
*/
@@ -79,126 +55,6 @@ export class KeycloakInteractionService {
return from(this.kcAdminClient.users.find({max: (Math.pow(2, 31) - 1)}));
}
- /**
- * Searchs a user from keycloak
- * @param username the keycloak username
- */
- public findUser(username: string) {
- return from(this.kcAdminClient.users.find({username}));
- }
-
- /**
- * Create user
- * @param userToCreate the user to create
- */
- private createUser(userToCreate: KeycloakUser) {
- return from(this.kcAdminClient.users.create({
- firstName: userToCreate.firstName,
- lastName: userToCreate.lastName,
- email: userToCreate.email,
- username: userToCreate.username,
- enabled: true
- }));
- }
-
- // TODO REMOVE
- /**
- * Delete user by id
- * @param userId the user id
- */
- private deleteUserById(userId: string) {
- return from(this.kcAdminClient.users.del({
- id: userId
- }));
- }
-
- // TODO REMOVE
- /**
- * Set user password
- * @param userId the user id
- * @param password the user password
- */
- public setUserPassword(userId: string, password: string) {
- return from(this.kcAdminClient.users.resetPassword({
- id: userId,
- credential: {
- temporary: true,
- type: 'password',
- value: password,
- },
- }));
- }
-
- // TODO REMOVE
- /**
- * Create client role
- * @param clientRoleName the client role name
- */
- public createClientRole(clientRoleName: string, clientRoleDescription: string) {
- return this.getDevPortalClientUUID()
- .pipe(mergeMap(clientUUID => this.kcAdminClient.clients.createRole({
- id: clientUUID,
- name: clientRoleName,
- description: clientRoleDescription
- })));
- }
-
- /**
- * Delete client role
- * @param clientRoleName the client role name
- */
- public deleteClientRole(clientRoleName: string) {
- return this.getDevPortalClientUUID()
- .pipe(mergeMap(clientId => this.kcAdminClient.clients.delRole({id: clientId, roleName: clientRoleName})));
- }
-
- /**
- * Get client role UUID
- * @param clientUUID the client UUID
- * @param clientRoleName the client role name
- */
- public getClientRoleUUID(clientUUID: string, clientRoleName: string) {
- return from(this.kcAdminClient.clients.findRole({
- id: clientUUID,
- roleName: clientRoleName
- })).pipe(map(roleObject => roleObject.id));
- }
-
- /**
- * Add client role mapping to user
- * @param userUUID user UUID
- * @param clientUUID client UUID
- * @param roleUUID role UUID
- * @param roleName role name
- */
- public addClientRoleMappingToUser(userUUID: string, clientUUID: string, roleUUID: string, roleName: string) {
- return from(this.kcAdminClient.users.addClientRoleMappings({
- id: userUUID,
- clientUniqueId: clientUUID,
- roles: [{
- id: roleUUID,
- name: roleName
- }]
- }));
- }
-
- // TODO REMOVE
- /**
- * Remove Role Mapping from user
- * @param userUUID user UUID
- * @param roleUUID role UUID
- * @param roleName role name
- */
- private removeRoleMapping(userUUID: string, roleUUID: string, roleName: string) {
- return from(this.kcAdminClient.users.delRealmRoleMappings({
- id: userUUID,
- roles: [{
- id: roleUUID,
- name: roleName
- }]
- }));
- }
-
/**
* Remove Group Mapping from user
* @param userUUID user UUID
@@ -234,69 +90,4 @@ export class KeycloakInteractionService {
// and should be skipped because of typing errors of method this.deleteUser:
.pipe(map((groups) => {}));
}
-
- /**
- * Add client role to user
- * @param userId the user id
- * @param clientRoleName the client role name
- */
- public addClientRoleToUser(userId: string, clientRoleName: string, clientRoleDescription: string) {
- return this.createClientRole(clientRoleName, clientRoleDescription)
- .pipe(mergeMap((roleObject) => this.getDevPortalClientUUID()))
- .pipe(mergeMap(clientUUID =>
- this.getClientRoleUUID(clientUUID, clientRoleName)
- .pipe(mergeMap(clientRoleUUID => this.addClientRoleMappingToUser(userId, clientUUID, clientRoleUUID, clientRoleName))))
- );
- }
-
- // TODO REMOVE
- /**
- * Find existing or create new user
- * @param user the user
- */
- // public findExistingOrCreateUser(user: KeycloakUser): Observable<UserRepresentation> {
- // return this.findUser(user.username).pipe(mergeMap(foundUsers => {
- // let observer = null;
- // if (foundUsers.length === 0) {
- // observer = this.createUser(user).pipe(mergeMap((insertedUser => {
- // return this.setUserPassword(insertedUser.id, user.password).pipe(mergeMap(() => of(insertedUser)));
- // })));
- // } else {
- // observer = of(foundUsers[0]);
- // }
- // return observer;
- // }));
- // }
-
- // TODO REMOVE
- /**
- * Determines if the user was generated from devportal admin
- * @param user Keycloak user
- */
- // private isUserGeneratedFromDevPortal(user: UserRepresentation): boolean {
- // return user.attributes
- // && user.attributes.generatedFromDevPortal
- // && user.attributes.generatedFromDevPortal.length > 0
- // && user.attributes.generatedFromDevPortal[0] === 'true';
- // }
-
- // TODO REMOVE
- /**
- * Delete dev portal user if generated from dev portal if not remove only devportaluser role from user
- * @param username Username of Developer
- */
- // public deleteUser(username: string): Observable<void> {
- // return this.findUser(username).pipe(mergeMap(users => {
- // if (users.length > 0) {
- // const userToDelete = users[0];
- // // delete user only if he was generated from dev portal
- // if (this.isUserGeneratedFromDevPortal(userToDelete)) {
- // return this.deleteUserById(userToDelete.id);
- // } else {
- // // remove only devportaluser role from user if he was not generated from dev portal
- // return this.removeDevPortalGroupFromUser(userToDelete.id);
- // }
- // }
- // }));
- // }
}

MODIFY
src/app/components/developer/api-list/api-list.component.ts
src/app/components/developer/api-list/api-list.component.ts
@@ -53,8 +53,6 @@ export class ApiListComponent implements OnInit {
static inactive = 'Inactive';
};
- // @Input('developerId') developerId;
-
constructor(private router: Router,
private apiDataService: ApiDataService,
private toasterService: ToasterService,

MODIFY
src/app/services/api-data.service.ts
src/app/services/api-data.service.ts
@@ -159,17 +159,6 @@ export interface GatewayEndpoint {
endpoint: string;
}
-/**
- * Keycloak User
- */
-export interface KeycloakUser {
- username: string;
- email: string;
- firstName: string;
- lastName: string;
- password: string;
-}
-
@Injectable({
providedIn: 'root'
})

GW-335: Refactor admin service and keycloak interaction