@viewdo/dxp-story-cli
Version:
DXP Story Management CLI
146 lines • 7.23 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.AuthenticationService = void 0;
const axios_1 = __importDefault(require("axios"));
const ConventionService_1 = require("./ConventionService");
const typedi_1 = require("typedi");
const ConsoleService_1 = require("./ConsoleService");
let AuthenticationService = class AuthenticationService {
constructor(console, conventions) {
this.console = console;
this.conventions = conventions;
}
_handleError(err) {
let message = "";
if (err.response) {
/*
* The request was made and the server responded with a
* status code that falls out of the range of 2xx
*/
if (err.response.status)
message = err.response.statusText;
if (err.response.data)
message = err.response.data.error;
}
else if (err.request) {
/*
* The request was made but no response was received, `error.request`
* is an instance of XMLHttpRequest in the browser and an instance
* of http.ClientRequest in Node.js
*/
message = `Unable to reach ${err.config.url}`;
}
else {
// Something happened in setting up the request and triggered an Error
message = err.message;
}
message += " (Make sure the Client ID is correct)";
return Promise.reject(message);
}
/**
* Retrieves the current cached M2M auth token from the DXP-Platform BitBucket repository
*
* @remarks
* This is not used by the login command for local tokens, see sendVerificationEmail & getTokenFromVerification
*
* @returns The auth token stored in the dxp platform as a promise
*/
getAppTokenFromPlatform(client_id, client_secret) {
return __awaiter(this, void 0, void 0, function* () {
const { auth_api_url, dxp_access_token } = this.conventions;
if (!dxp_access_token) {
this.console.error(`Unable to retrieve auth token from DXP-Platform for ${this.conventions.namespace}`);
// If cached token unavailable and auth fallback is allowed, attempt to retrieve from Auth0
return this.conventions.preventAuthFallback === '1' ? '' : yield this.getAppToken(client_id, client_secret);
}
// We have our token, now let's exchange it on the platform
let auth_path = `${auth_api_url}/accounts/exchange?token=${dxp_access_token}`;
this.console.debug(`API: POST ${auth_path}`.gray);
return yield axios_1.default.post(auth_path, {}).then((auth_response) => {
this.console.debug("API: Using App token".gray);
return auth_response.data.token;
})
.catch(this._handleError);
});
}
getAppToken() {
return __awaiter(this, arguments, void 0, function* (client_id = this.conventions.auth0_client_id, client_secret = this.conventions.auth0_client_secret) {
if (client_secret == undefined)
return Promise.resolve(null);
let { auth0_api_url, auth0_audience: audience, auth_api_url, } = this.conventions;
let auth0_path = `${auth0_api_url}/oauth/token`;
this.console.debug(`Attempting Application Authentication: ${auth0_path} for Audience: ${audience} Client ID: ${client_id} & Secret: ${client_secret.substring(0, 5) + "..."}`);
try {
const auth0_response = yield axios_1.default.post(auth0_path, {
client_id,
client_secret,
audience,
grant_type: "client_credentials",
});
let auth_path = `${auth_api_url}/accounts/exchange?token=${auth0_response.data.access_token}`;
this.console.debug(`API: POST ${auth_path}`.gray);
const auth_response = yield axios_1.default.post(auth_path, {});
this.console.debug("API: Using App token");
return auth_response.data.token;
}
catch (err) {
this._handleError(err);
}
});
}
sendVerificationEmail(client_id = this.conventions.auth0_client_id_interactive, email) {
let { auth0_api_url } = this.conventions;
let path = `${auth0_api_url}/passwordless/start`;
this.console.debug(`API: POST ${path} with client: ${client_id}`.gray);
return axios_1.default
.post(path, {
client_id,
connection: "email",
email,
send: "code",
})
.catch(this._handleError);
}
getTokenFromVerification(client_id = this.conventions.auth0_client_id_interactive, email, verification_code) {
let { auth_api_url } = this.conventions;
let path = `${auth_api_url}/accounts/confirm-passwordless`;
return axios_1.default
.post(path, {
clientId: client_id,
code: verification_code,
email,
})
.then((response) => {
return response.data.token;
})
.catch(this._handleError);
}
};
exports.AuthenticationService = AuthenticationService;
exports.AuthenticationService = AuthenticationService = __decorate([
(0, typedi_1.Service)(),
__metadata("design:paramtypes", [ConsoleService_1.ConsoleService,
ConventionService_1.ConventionService])
], AuthenticationService);
//# sourceMappingURL=AuthenticationService.js.map