@shrinedev/loopback-keycloak-demo-app
Version:
Example Loopback App that integrates with Keycloak.
59 lines • 2.87 kB
JavaScript
;
// Copyright Shrine Development. 2019. All Rights Reserved.
// Node module: @shrinedev/examples
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
const rest_1 = require("@loopback/rest");
const core_1 = require("@loopback/core");
const loopback_gate_keycloak_1 = require("@shrinedev/loopback-gate-keycloak");
const sequence_1 = require("../sequence");
class TeamProfile {
}
exports.TeamProfile = TeamProfile;
exports.CURRENT_TEAM = core_1.BindingKey.create('gate.team.current');
let TeamGate = class TeamGate {
constructor(setCurrentTeam) {
this.setCurrentTeam = setCurrentTeam;
}
async gate(context, request) {
const user = context.getSync(loopback_gate_keycloak_1.KeycloakBindings.CURRENT_USER);
return new Promise(async (resolve, reject) => {
// Get user's teams and see if the one included in path is in the list
const teams = user.teams;
const route = context.getSync(sequence_1.ROUTE_BINDING);
const teamName = route.pathParams.id;
console.log('teams and team name', user, teams, teamName);
if (!teams) {
reject(new rest_1.HttpErrors.Forbidden('User has no teams'));
return;
}
// Determine team id based off path
const team = { name: teamName };
// See if authorized
if (!teams.find(t => t === team.name)) {
reject(new rest_1.HttpErrors.Forbidden('User not authorized to access this team'));
}
this.setCurrentTeam(team);
resolve(team);
});
}
};
TeamGate = __decorate([
__param(0, core_1.inject.setter(exports.CURRENT_TEAM)),
__metadata("design:paramtypes", [Function])
], TeamGate);
exports.TeamGate = TeamGate;
//# sourceMappingURL=team.gate.js.map