component-dbs-core
Version:
DTO objects shared among device backend
186 lines • 8.98 kB
JavaScript
;
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IdentityService = void 0;
const common_1 = require("@nestjs/common");
const graphql_tag_1 = __importDefault(require("graphql-tag"));
const deviceVerification_1 = require("../../devices/deviceVerification");
const deviceService_1 = require("../../devices/deviceService");
const identityDto_1 = require("../dtos/identityDto");
const auth0Service_1 = require("../../authzero/auth0Service");
const jwtValidator_1 = require("../../authzero/utils/jwt/jwtValidator");
const appsync_1 = require("../../common_services/appsync");
let IdentityService = class IdentityService {
constructor(auth0Service, deviceService, deviceVerification, appSyncClient) {
this.auth0Service = auth0Service;
this.deviceService = deviceService;
this.deviceVerification = deviceVerification;
this.appSyncClient = appSyncClient;
}
identitySignUp(signUpInput) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.auth0Service.createUser(signUpInput);
const loginInput = new identityDto_1.IdentityLoginInputDto(signUpInput.email, signUpInput.password, '');
const identity = yield this.auth0Service.getIdentity(loginInput);
yield this.deviceService.saveDeviceTokens(signUpInput.deviceUuid, identity.accessToken, identity.refreshToken);
yield this.deviceService.saveDeviceStatus(signUpInput.deviceUuid, "ACTIVE" /* ACTIVE */);
return Promise.resolve(identity);
}
catch (e) {
console.error(e.message);
return Promise.reject(e);
}
});
}
identityLogin(loginInput) {
return __awaiter(this, void 0, void 0, function* () {
try {
const identity = yield this.auth0Service.getIdentity(loginInput);
yield this.deviceService.saveDeviceTokens(loginInput.deviceUuid, identity.accessToken, identity.refreshToken);
yield this.deviceService.saveDeviceStatus(loginInput.deviceUuid, "ACTIVE" /* ACTIVE */);
return Promise.resolve(identity);
}
catch (e) {
console.error(e.message);
return Promise.reject(e);
}
});
}
checkAccessToken(input) {
return __awaiter(this, void 0, void 0, function* () {
try {
const jwtCheck = yield this.auth0Service.checkAccessToken(input);
// exit the function if it's a malformed or invalid accessToken
switch (jwtCheck.code) {
case jwtValidator_1.JWT_CHECK_RESULT.ERROR_MALFORMED_TOKEN.code:
case jwtValidator_1.JWT_CHECK_RESULT.ERROR_UNHANDLED.code:
return Promise.reject(new Error(jwtCheck.errorMessage));
default:
break;
}
console.info(jwtCheck);
const validDeviceToken = yield this.deviceVerification.isDeviceTokenValid(input.deviceUuid, input.accessToken, input.refreshToken);
if (!validDeviceToken) {
return Promise.reject(new Error('DEVICE_TOKEN_INVALID'));
}
switch (jwtCheck.code) {
case jwtValidator_1.JWT_CHECK_RESULT.VALID_TOKEN.code: {
return Promise.resolve(new identityDto_1.IdentityDto(true, input.accessToken, input.refreshToken));
}
case jwtValidator_1.JWT_CHECK_RESULT.ERROR_TOKEN_EXPIRED.code: {
const identity = yield this.auth0Service.getIdentity(input);
identity.refreshToken = input.refreshToken;
yield this.deviceService.saveDeviceTokens(input.deviceUuid, identity.accessToken, identity.refreshToken);
return Promise.resolve(identity);
}
default:
throw new Error('Unhandled JWT ERROR');
}
}
catch (e) {
console.error(e);
return Promise.reject(e);
}
});
}
identityForgotPassword(email) {
return this.auth0Service.forgotPassword(email);
}
identityChangePassword(changePwdInput) {
return this.auth0Service.changePassword(changePwdInput);
}
identityPhoneRegister(input) {
return __awaiter(this, void 0, void 0, function* () {
yield this.deviceService.saveDevicePhone(input.deviceUuid, input.phone);
return this.auth0Service.phoneRegister(input.phone);
});
}
identityPhoneVerify(input) {
// TODO: implement me -- identityPhone verify logic
console.log(input.code);
return Promise.resolve(new identityDto_1.PhoneVerificationDto(true));
}
identitySetPin(input) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.deviceService.saveDevicePin(input.deviceUuid, input.pin);
return Promise.resolve(true);
}
catch (e) {
console.error(e);
/* eslint-disable */
return Promise.reject(false);
}
});
}
identityVerifyPin(input) {
return this.deviceVerification.isDevicePinValid(input.pin, input.deviceUuid);
}
identityForcedLogoff(deviceUuid, reason) {
return __awaiter(this, void 0, void 0, function* () {
console.log(`${deviceUuid}, ${reason}`);
try {
const refreshToken = yield this.deviceService.getRefreshTokenByUuid(deviceUuid);
yield this.auth0Service.revokeRefreshToken(refreshToken);
yield this.deviceService.deactivateDevice(deviceUuid);
console.info("identityForcedLogoff completed");
return Promise.resolve({ deviceUuid: deviceUuid, reason: reason });
}
catch (e) {
console.error(`identityForcedLogoff falied. ${JSON.stringify(e)}`);
return Promise.reject(new Error('An error happened during forced logoff'));
}
});
}
initiateForcedLogoff(deviceUuid, reason) {
return __awaiter(this, void 0, void 0, function* () {
const mutation = graphql_tag_1.default `
mutation TriggerLogoff($_deviceUuid: ID!, $_reason: String!) {
identityForcedLogoff(deviceUuid: $_deviceUuid, reason: $_reason) {
deviceUuid
reason
}
}
`;
const request = {
mutation,
variables: {
_deviceUuid: deviceUuid,
_reason: reason,
}
};
const client = yield this.appSyncClient.getAppSyncClient();
return client.mutate(request);
});
}
};
IdentityService = __decorate([
common_1.Injectable(),
__metadata("design:paramtypes", [auth0Service_1.Auth0Service,
deviceService_1.DeviceService,
deviceVerification_1.DeviceVerification,
appsync_1.AppSyncClient])
], IdentityService);
exports.IdentityService = IdentityService;
//# sourceMappingURL=identityService.js.map