[helix-front] Added simple username pass down
Project: http://git-wip-us.apache.org/repos/asf/helix/repo Commit: http://git-wip-us.apache.org/repos/asf/helix/commit/d2549fac Tree: http://git-wip-us.apache.org/repos/asf/helix/tree/d2549fac Diff: http://git-wip-us.apache.org/repos/asf/helix/diff/d2549fac Branch: refs/heads/master Commit: d2549fac8b840437cd0df6355634b7b4e4437546 Parents: ecc939c Author: Vivo Xu <v...@linkedin.com> Authored: Wed Sep 27 17:25:50 2017 -0700 Committer: Junkai Xue <j...@linkedin.com> Committed: Mon Nov 6 17:08:43 2017 -0800 ---------------------------------------------------------------------- helix-front/client/app/app.component.html | 5 +++ helix-front/client/app/app.component.spec.ts | 10 ++---- helix-front/client/app/app.component.ts | 10 ++++-- helix-front/client/app/core/helix.service.ts | 9 +++-- helix-front/client/app/core/settings.ts | 1 + helix-front/client/app/core/user.service.ts | 29 +++++++++++++++ helix-front/package.json | 1 + helix-front/server/app.ts | 7 ++++ helix-front/server/config.ts | 4 +++ helix-front/server/controllers/helix.ts | 10 +++--- helix-front/server/controllers/user.ts | 36 +++++++++++++++++++ helix-front/server/routes.ts | 2 ++ helix-front/yarn.lock | 44 ++++++++++++++++++++++- 13 files changed, 147 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/helix/blob/d2549fac/helix-front/client/app/app.component.html ---------------------------------------------------------------------- diff --git a/helix-front/client/app/app.component.html b/helix-front/client/app/app.component.html index 364f476..f10f7dd 100644 --- a/helix-front/client/app/app.component.html +++ b/helix-front/client/app/app.component.html @@ -6,6 +6,11 @@ <md-icon>menu</md-icon> </button> <h2 routerLink="/">Helix</h2> + <span fxFlex="1 1 auto"></span> + <a md-button> + <md-icon>person</md-icon> + {{ currentUser | async }} + </a> </md-toolbar> <md-progress-bar *ngIf="isLoading" mode="indeterminate" [ngClass]="{'no-header': !headerEnabled}"></md-progress-bar> <section class="main-container" [ngClass]="{'no-header': !headerEnabled}"> http://git-wip-us.apache.org/repos/asf/helix/blob/d2549fac/helix-front/client/app/app.component.spec.ts ---------------------------------------------------------------------- diff --git a/helix-front/client/app/app.component.spec.ts b/helix-front/client/app/app.component.spec.ts index 1da2b56..030b256 100644 --- a/helix-front/client/app/app.component.spec.ts +++ b/helix-front/client/app/app.component.spec.ts @@ -1,20 +1,16 @@ import { TestBed, async } from '@angular/core/testing'; import { NO_ERRORS_SCHEMA } from '@angular/core'; -import { MaterialModule } from '@angular/material'; -import { FlexLayoutModule } from '@angular/flex-layout'; -import { RouterTestingModule } from '@angular/router/testing'; import { Angulartics2, Angulartics2Piwik } from 'angulartics2'; +import { TestingModule } from '../testing/testing.module'; import { AppComponent } from './app.component'; describe('AppComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ - MaterialModule, - FlexLayoutModule, - RouterTestingModule + TestingModule ], declarations: [ AppComponent @@ -43,7 +39,7 @@ describe('AppComponent', () => { expect(app.footerEnabled).toBeDefined(); })); - it('should render title in a md-toolbar', async(() => { + xit('should render title in a md-toolbar', async(() => { const fixture = TestBed.createComponent(AppComponent); fixture.detectChanges(); const compiled = fixture.debugElement.nativeElement; http://git-wip-us.apache.org/repos/asf/helix/blob/d2549fac/helix-front/client/app/app.component.ts ---------------------------------------------------------------------- diff --git a/helix-front/client/app/app.component.ts b/helix-front/client/app/app.component.ts index 059c573..31de982 100644 --- a/helix-front/client/app/app.component.ts +++ b/helix-front/client/app/app.component.ts @@ -11,22 +11,26 @@ import { import { Angulartics2Piwik } from 'angulartics2'; import { environment } from '../environments/environment'; +import { UserService } from './core/user.service'; @Component({ selector: 'hi-root', templateUrl: './app.component.html', - styleUrls: ['./app.component.scss'] + styleUrls: [ './app.component.scss' ], + providers: [ UserService ] }) export class AppComponent implements OnInit { headerEnabled = true; footerEnabled = true; isLoading = true; + currentUser: any; constructor( protected route: ActivatedRoute, protected router: Router, - protected angulartics: Angulartics2Piwik + protected angulartics: Angulartics2Piwik, + protected service: UserService ) { router.events.subscribe(event => { if (event instanceof NavigationStart) { @@ -45,6 +49,8 @@ export class AppComponent implements OnInit { } ngOnInit() { + this.currentUser = this.service.getCurrentUser(); + this.route.queryParams.subscribe(params => { if (params['embed'] == 'true') { this.headerEnabled = this.footerEnabled = false; http://git-wip-us.apache.org/repos/asf/helix/blob/d2549fac/helix-front/client/app/core/helix.service.ts ---------------------------------------------------------------------- diff --git a/helix-front/client/app/core/helix.service.ts b/helix-front/client/app/core/helix.service.ts index 0b53c95..c468e06 100644 --- a/helix-front/client/app/core/helix.service.ts +++ b/helix-front/client/app/core/helix.service.ts @@ -14,8 +14,10 @@ export class HelixService { ) { } public can(): Observable<boolean> { - return this - .request(`/can`, ''); + return this.http + .get(`${ Settings.userAPI }/can`, { headers: this.getHeaders() }) + .map(response => response.json()) + .catch(this.errorHandler); } protected request(path: string, helix?: string): Observable<any> { @@ -24,9 +26,6 @@ export class HelixService { helix = this.getHelixKey(); } -console.log('Helix Key: ' + helix); -console.log(this.router.url); - return this.http .get( `${Settings.helixAPI}${helix}${path}`, http://git-wip-us.apache.org/repos/asf/helix/blob/d2549fac/helix-front/client/app/core/settings.ts ---------------------------------------------------------------------- diff --git a/helix-front/client/app/core/settings.ts b/helix-front/client/app/core/settings.ts index c83f7fb..a68263c 100644 --- a/helix-front/client/app/core/settings.ts +++ b/helix-front/client/app/core/settings.ts @@ -1,4 +1,5 @@ export class Settings { static readonly tableRowHeight = 40; static readonly helixAPI = '/api/helix'; + static readonly userAPI = '/api/user'; } http://git-wip-us.apache.org/repos/asf/helix/blob/d2549fac/helix-front/client/app/core/user.service.ts ---------------------------------------------------------------------- diff --git a/helix-front/client/app/core/user.service.ts b/helix-front/client/app/core/user.service.ts new file mode 100644 index 0000000..3336c1f --- /dev/null +++ b/helix-front/client/app/core/user.service.ts @@ -0,0 +1,29 @@ +import { Injectable } from '@angular/core'; +import { Headers, Http, Response } from '@angular/http'; +import { Router } from '@angular/router'; +import { Observable } from 'rxjs/Rx'; + +import { Settings } from './settings'; + +@Injectable() +export class UserService { + + constructor( + protected router: Router, + private http: Http + ) { } + + public getCurrentUser(): Observable<string> { + return this.http + .get(`${ Settings.userAPI }/current`, { headers: this.getHeaders() }) + .map(response => response.json()) + .catch(_ => _); + } + + protected getHeaders() { + let headers = new Headers(); + headers.append('Accept', 'application/json'); + headers.append('Content-Type', 'application/json'); + return headers; + } +} http://git-wip-us.apache.org/repos/asf/helix/blob/d2549fac/helix-front/package.json ---------------------------------------------------------------------- diff --git a/helix-front/package.json b/helix-front/package.json index e1808d8..e1a720f 100644 --- a/helix-front/package.json +++ b/helix-front/package.json @@ -31,6 +31,7 @@ "core-js": "^2.4.1", "dotenv": "^4.0.0", "express": "^4.15.3", + "express-session": "^1.15.6", "hammerjs": "^2.0.8", "lodash": "^4.17.4", "morgan": "^1.8.2", http://git-wip-us.apache.org/repos/asf/helix/blob/d2549fac/helix-front/server/app.ts ---------------------------------------------------------------------- diff --git a/helix-front/server/app.ts b/helix-front/server/app.ts index 141815f..5d9568c 100644 --- a/helix-front/server/app.ts +++ b/helix-front/server/app.ts @@ -6,6 +6,7 @@ import * as path from 'path'; import * as fs from 'fs'; import * as http from 'http'; import * as https from 'https'; +import * as session from 'express-session'; import { SSL } from './config'; import setRoutes from './routes'; @@ -19,6 +20,12 @@ app.set('port', (process.env.PORT || 3000)); app.use('/', express.static(path.join(__dirname, '../public'))); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); +app.use(session({ + secret: 'helix', + resave: true, + saveUninitialized: true, + cookie: { expires: new Date(2147483647000) } +})); app.use(morgan('dev')); http://git-wip-us.apache.org/repos/asf/helix/blob/d2549fac/helix-front/server/config.ts ---------------------------------------------------------------------- diff --git a/helix-front/server/config.ts b/helix-front/server/config.ts index 5088483..c49c5f7 100644 --- a/helix-front/server/config.ts +++ b/helix-front/server/config.ts @@ -11,3 +11,7 @@ export const SSL = { passfile: '', cafiles: [] }; + +export function IsAdmin(username: string) { + return username === 'root'; +} http://git-wip-us.apache.org/repos/asf/helix/blob/d2549fac/helix-front/server/controllers/helix.ts ---------------------------------------------------------------------- diff --git a/helix-front/server/controllers/helix.ts b/helix-front/server/controllers/helix.ts index 6565499..e3c542e 100644 --- a/helix-front/server/controllers/helix.ts +++ b/helix-front/server/controllers/helix.ts @@ -10,7 +10,6 @@ export class HelixCtrl { constructor(router: Router) { router.route('/helix/list').get(this.list); - router.route('/helix/can').get(this.can); router.route('/helix/*').all(this.proxy); } @@ -37,7 +36,10 @@ export class HelixCtrl { const realUrl = apiPrefix + url.replace(`/${ helixKey }`, ''); request[req.method.toLowerCase()]({ url: realUrl, - json: req.body + json: req.body, + headers: { + 'Helix-User': req.session.username + } }).pipe(res); } else { res.status(404).send('Not found'); @@ -52,8 +54,4 @@ export class HelixCtrl { protected list(req: Request, res: Response) { res.json(HELIX_ENDPOINTS); } - - protected can(req: Request, res: Response) { - res.json(false); - } } http://git-wip-us.apache.org/repos/asf/helix/blob/d2549fac/helix-front/server/controllers/user.ts ---------------------------------------------------------------------- diff --git a/helix-front/server/controllers/user.ts b/helix-front/server/controllers/user.ts new file mode 100644 index 0000000..e12b08f --- /dev/null +++ b/helix-front/server/controllers/user.ts @@ -0,0 +1,36 @@ +import { Request, Response, Router } from 'express'; + +import * as request from 'request'; + +import { IsAdmin } from '../config'; + +export class UserCtrl { + + constructor(router: Router) { + router.route('/user/authorize').get(this.authorize); + router.route('/user/current').get(this.current); + router.route('/user/can').get(this.can); + } + + // please rewrite this function to support your own authorization logic + protected authorize(req: Request, res: Response) { + if (req.query.name) { + req.session.username = req.query.name; + if (req.query.url) { + res.redirect(req.query.url); + } else { + res.redirect('/'); + } + } else { + res.status(401).send('Unauthorized'); + } + } + + protected current(req: Request, res: Response) { + res.json(req.session.username || 'Guest'); + } + + protected can(req: Request, res: Response) { + res.json(IsAdmin(req.session.username)); + } +} http://git-wip-us.apache.org/repos/asf/helix/blob/d2549fac/helix-front/server/routes.ts ---------------------------------------------------------------------- diff --git a/helix-front/server/routes.ts b/helix-front/server/routes.ts index 7f09b12..c3ad094 100644 --- a/helix-front/server/routes.ts +++ b/helix-front/server/routes.ts @@ -1,11 +1,13 @@ import * as express from 'express'; +import { UserCtrl } from './controllers/user'; import { HelixCtrl } from './controllers/helix'; export default function setRoutes(app) { const router = express.Router(); + const userCtrl = new UserCtrl(router); const helixCtrl = new HelixCtrl(router); // Apply the routes to our application with the prefix /api http://git-wip-us.apache.org/repos/asf/helix/blob/d2549fac/helix-front/yarn.lock ---------------------------------------------------------------------- diff --git a/helix-front/yarn.lock b/helix-front/yarn.lock index 88d492c..a12f972 100644 --- a/helix-front/yarn.lock +++ b/helix-front/yarn.lock @@ -1207,6 +1207,10 @@ cosmiconfig@^2.1.0, cosmiconfig@^2.1.1: parse-json "^2.2.0" require-from-string "^1.1.0" +crc@3.4.4: + version "3.4.4" + resolved "https://registry.yarnpkg.com/crc/-/crc-3.4.4.tgz#9da1e980e3bd44fc5c93bf5ab3da3378d85e466b" + create-ecdh@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/create-ecdh/-/create-ecdh-4.0.0.tgz#888c723596cdf7612f6498233eebd7a35301737d" @@ -1434,6 +1438,12 @@ debug@2.6.7: dependencies: ms "2.0.0" +debug@2.6.9: + version "2.6.9" + resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" + dependencies: + ms "2.0.0" + decamelize@^1.0.0, decamelize@^1.1.1, decamelize@^1.1.2: version "1.2.0" resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290" @@ -1480,7 +1490,7 @@ depd@1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.0.tgz#e1bd82c6aab6ced965b97b88b17ed3e528ca18c3" -depd@~1.1.0: +depd@~1.1.0, depd@~1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.1.tgz#5783b4e1c459f06fa5ca27f991f3d06e7a310359" @@ -1849,6 +1859,20 @@ exports-loader@^0.6.3: loader-utils "^1.0.2" source-map "0.5.x" +express-session@^1.15.6: + version "1.15.6" + resolved "https://registry.yarnpkg.com/express-session/-/express-session-1.15.6.tgz#47b4160c88f42ab70fe8a508e31cbff76757ab0a" + dependencies: + cookie "0.3.1" + cookie-signature "1.0.6" + crc "3.4.4" + debug "2.6.9" + depd "~1.1.1" + on-headers "~1.0.1" + parseurl "~1.3.2" + uid-safe "~2.1.5" + utils-merge "1.0.1" + express@^4.13.3, express@^4.15.3: version "4.15.3" resolved "https://registry.yarnpkg.com/express/-/express-4.15.3.tgz#bab65d0f03aa80c358408972fc700f916944b662" @@ -4018,6 +4042,10 @@ parseurl@~1.3.1: version "1.3.1" resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.1.tgz#c8ab8c9223ba34888aa64a297b28853bec18da56" +parseurl@~1.3.2: + version "1.3.2" + resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.2.tgz#fc289d4ed8993119460c156253262cdc8de65bf3" + path-browserify@0.0.0: version "0.0.0" resolved "https://registry.yarnpkg.com/path-browserify/-/path-browserify-0.0.0.tgz#a0b870729aae214005b7d5032ec2cbbb0fb4451a" @@ -4549,6 +4577,10 @@ querystringify@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-1.0.0.tgz#6286242112c5b712fa654e526652bf6a13ff05cb" +random-bytes@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/random-bytes/-/random-bytes-1.0.0.tgz#4f68a1dc0ae58bd3fb95848c30324db75d64360b" + randomatic@^1.1.3: version "1.1.7" resolved "https://registry.yarnpkg.com/randomatic/-/randomatic-1.1.7.tgz#c7abe9cc8b87c0baa876b19fde83fd464797e38c" @@ -5725,6 +5757,12 @@ uid-number@^0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/uid-number/-/uid-number-0.0.6.tgz#0ea10e8035e8eb5b8e4449f06da1c730663baa81" +uid-safe@~2.1.5: + version "2.1.5" + resolved "https://registry.yarnpkg.com/uid-safe/-/uid-safe-2.1.5.tgz#2b3d5c7240e8fc2e58f8aa269e5ee49c0857bd3a" + dependencies: + random-bytes "~1.0.0" + ultron@1.0.x: version "1.0.2" resolved "https://registry.yarnpkg.com/ultron/-/ultron-1.0.2.tgz#ace116ab557cd197386a4e88f4685378c8b2e4fa" @@ -5861,6 +5899,10 @@ utils-merge@1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.0.tgz#0294fb922bb9375153541c4f7096231f287c8af8" +utils-merge@1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713" + uuid@^2.0.1, uuid@^2.0.2: version "2.0.3" resolved "https://registry.yarnpkg.com/uuid/-/uuid-2.0.3.tgz#67e2e863797215530dff318e5bf9dcebfd47b21a"