Skip to content
This repository has been archived by the owner on May 8, 2020. It is now read-only.

Commit

Permalink
feat(remote cli): Implemented a truly ridiculously complex banner for…
Browse files Browse the repository at this point in the history
… remote login welcome
  • Loading branch information
zakhenry committed Jul 21, 2016
1 parent 96778a1 commit 33e91f1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 19 deletions.
44 changes: 44 additions & 0 deletions src/common/util/banner.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as chalk from 'chalk';
/**
* @module common
*/
Expand All @@ -20,3 +21,46 @@ export const banner = `
| |
| |
|_|`;

export function bannerBg(message: string = '$ Ubiquits Runtime CLI', bgString: string): string {

let shortMessage:string = '';
let longMessage:string = '';

message = ` ${message} `;

message.length > 36 ? longMessage = message : shortMessage = message;

shortMessage += "*".repeat(38 - shortMessage.length);

const template = `
********************************************************************************
**** /| ***** |\\ **** _ ********************************************************
*** / / ***** \\ \\ ** | | *******************************************************
** / / ******* \\ \\ * | | ************************* _______ _____ ***********
* / / ********* \\ \\ | | ************************* |__ __| | ____| ***********
* \`.\`. _______ ,'.' | |___ _ _____ _ _ _ | | ***|___ ***********
*** \`.\`.-----,'.' ** | _ | | | | _ | | | | | | | | | *******| | ***********
***** \`.\`..,',' **** | |_| | | | | |_| | | |_| | | | | | ****___| | ***********
******* \`._,' ****** |_____| |_| |___| | |_____| |_| |_| ** |_____| ***********
********* *********************** | | ***************************************
************************************ | | ***************************************
************************************ | | *${shortMessage}
************************************ |_| ***************************************
********************************************************************************
${longMessage}`;

const minReplacementLength = template.match(/\*+/g)
.join('').length;
if (bgString.length < minReplacementLength) {
bgString = bgString.repeat(minReplacementLength / bgString.length + 1);
}

return template.replace(/\*+/g, (match: string) => {
const replacement: string = bgString.substr(0, match.length);

bgString = bgString.substr(match.length);
return chalk.gray(replacement);
});

}
47 changes: 28 additions & 19 deletions src/server/services/remoteCli.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
/** End Typedoc Module Declaration */
import { Injectable, Injector } from '@angular/core';
import { banner } from '../../common/util/banner';
import { bannerBg } from '../../common/util/banner';
import { Logger } from '../../common/services/logger.service';
import { Server, RouteConfig } from '../servers/abstract.server';
import * as chalk from 'chalk';
Expand Down Expand Up @@ -102,17 +102,10 @@ export class RemoteCli extends AbstractService {
public initialize(): this {
this.vantage = new Vantage();

this.vantage.delimiter('ubiquits-runtime~$');
this.vantage.delimiter(chalk.magenta('ubiquits-runtime~$'));

this.registerAuthenticationStrategy();

let displayBanner = `Welcome to Ubiquits runtime cli. Type 'help' for commands`;
if ((<any>process.stdout).columns > 68) {
displayBanner = `${banner}\n${displayBanner}`;
}

this.vantage.banner(displayBanner);

this.logger.debug('Remote cli initialized');

return this.registerCommands();
Expand Down Expand Up @@ -189,30 +182,46 @@ export class RemoteCli extends AbstractService {
protected registerAuthenticationStrategy(): void {

this.vantage.auth((vantage: any, options: any) => {
return (args: any, cb: Function) => {

const remoteCli = this;

return function (args: {client: {jwt: string, publicKeyPath: string, columns: number}}, cb: Function) {

try {
this.logger.silly.debug('Passed client arguments: ', args);
remoteCli.logger.silly.debug('Passed client arguments: ', args);

if (!args.client.jwt) {
const token: string = args.client.jwt;
const keyPath: string = args.client.publicKeyPath;

if (!token) {
return cb("JWT was not passed in connection request", false);
}

this.logger.info(`Authenticating JSON web token against public key [${args.client.publicKeyPath}]`);
remoteCli.logger.info(`Authenticating JSON web token against public key [${keyPath}]`);

this.authService.verify(args.client.jwt, args.client.publicKeyPath)
remoteCli.authService.verify(token, keyPath)
.then((payload: any) => {
this.logger.info(`Welcome ${payload.username}, you are authenticated`);
remoteCli.logger.info(`${payload.username} has been authenticated with token`)
.debug('Token:', token);
let displayBanner = `Hi ${payload.username}, Welcome to Ubiquits runtime cli.`;
this.log('columns', args.client.columns);
if (args.client.columns > 80) {
displayBanner = bannerBg(undefined, token);
}
this.log(chalk.grey(`You were authenticated with a JSON Web token verified against the public key at ${keyPath}`));
this.log(displayBanner);
this.log(` Type 'help' for a list of available commands`);
return cb(null, true);
})
.catch(() => {
return cb("Credentials are incorrect", false);
.catch((e: Error) => {
return cb(e.message, false);
});

} catch (e) {
this.logger.error('Authentication error', e);
remoteCli.logger.error('Authentication error', e);
cb(null, false);
}
}
};
});

this.logger.debug('Registered vantage authentication strategy');
Expand Down

0 comments on commit 33e91f1

Please sign in to comment.