angular2-jwt
Advanced tools
Comparing version 0.1.21 to 0.1.22
@@ -0,1 +1,2 @@ | ||
import { Provider } from '@angular/core'; | ||
import { Http, Request, RequestOptions, RequestOptionsArgs, Response } from '@angular/http'; | ||
@@ -11,3 +12,3 @@ import { Observable } from 'rxjs/Observable'; | ||
noTokenScheme?: boolean; | ||
tokenGetter: any; | ||
tokenGetter: () => string | Promise<string>; | ||
tokenName: string; | ||
@@ -24,3 +25,3 @@ } | ||
noTokenScheme: boolean; | ||
tokenGetter: any; | ||
tokenGetter: () => string | Promise<string>; | ||
tokenName: string; | ||
@@ -30,2 +31,4 @@ constructor(config?: any); | ||
} | ||
export declare class AuthHttpError extends Error { | ||
} | ||
/** | ||
@@ -42,5 +45,5 @@ * Allows for explicit authenticated HTTP requests. | ||
private requestHelper(requestArgs, additionalOptions?); | ||
private requestWithToken(req, token); | ||
setGlobalHeaders(headers: Array<Object>, request: Request | RequestOptionsArgs): void; | ||
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response>; | ||
private requestWithToken(req, token); | ||
get(url: string, options?: RequestOptionsArgs): Observable<Response>; | ||
@@ -68,3 +71,3 @@ post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>; | ||
export declare function tokenNotExpired(tokenName?: string, jwt?: string): boolean; | ||
export declare const AUTH_PROVIDERS: any; | ||
export declare function provideAuth(config?: {}): any; | ||
export declare const AUTH_PROVIDERS: Provider[]; | ||
export declare function provideAuth(config?: {}): Provider[]; |
"use strict"; | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
@@ -53,2 +58,10 @@ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
exports.AuthConfig = AuthConfig; | ||
var AuthHttpError = (function (_super) { | ||
__extends(AuthHttpError, _super); | ||
function AuthHttpError() { | ||
_super.apply(this, arguments); | ||
} | ||
return AuthHttpError; | ||
}(Error)); | ||
exports.AuthHttpError = AuthHttpError; | ||
/** | ||
@@ -82,2 +95,15 @@ * Allows for explicit authenticated HTTP requests. | ||
}; | ||
AuthHttp.prototype.requestWithToken = function (req, token) { | ||
if (!tokenNotExpired(undefined, token)) { | ||
if (!this.config.noJwtError) { | ||
return new Observable_1.Observable(function (obs) { | ||
obs.error(new AuthHttpError('No JWT present or has expired')); | ||
}); | ||
} | ||
} | ||
else { | ||
req.headers.set(this.config.headerName, this.config.headerPrefix + token); | ||
} | ||
return this.http.request(req); | ||
}; | ||
AuthHttp.prototype.setGlobalHeaders = function (headers, request) { | ||
@@ -104,5 +130,4 @@ if (!request.headers) { | ||
var token = this.config.tokenGetter(); | ||
if (token.then) { | ||
return Observable_1.Observable.fromPromise(token) | ||
.flatMap(function (jwtToken) { return _this.requestWithToken(req, jwtToken); }); | ||
if (token instanceof Promise) { | ||
return Observable_1.Observable.fromPromise(token).mergeMap(function (jwtToken) { return _this.requestWithToken(req, jwtToken); }); | ||
} | ||
@@ -113,15 +138,2 @@ else { | ||
}; | ||
AuthHttp.prototype.requestWithToken = function (req, token) { | ||
if (!tokenNotExpired(undefined, token)) { | ||
if (!this.config.noJwtError) { | ||
return new Observable_1.Observable(function (obs) { | ||
obs.error(new Error('No JWT present or has expired')); | ||
}); | ||
} | ||
} | ||
else { | ||
req.headers.set(this.config.headerName, this.config.headerPrefix + token); | ||
} | ||
return this.http.request(req); | ||
}; | ||
AuthHttp.prototype.get = function (url, options) { | ||
@@ -179,3 +191,3 @@ return this.requestHelper({ body: '', method: http_1.RequestMethod.Get, url: url }, options); | ||
} | ||
return decodeURIComponent(escape(typeof window === 'undefined' ? atob(output) : window.atob(output))); //polyfill https://github.com/davidchambers/Base64.js | ||
return decodeURIComponent(encodeURI(typeof window === 'undefined' ? atob(output) : window.atob(output))); | ||
}; | ||
@@ -197,3 +209,3 @@ JwtHelper.prototype.decodeToken = function (token) { | ||
if (!decoded.hasOwnProperty('exp')) { | ||
return new Date(); | ||
return null; | ||
} | ||
@@ -207,3 +219,3 @@ var date = new Date(0); // The 0 here is the key, which sets the date to the epoch | ||
offsetSeconds = offsetSeconds || 0; | ||
if (date.getSeconds() === new Date().getSeconds()) { | ||
if (date == null) { | ||
return false; | ||
@@ -228,20 +240,24 @@ } | ||
exports.tokenNotExpired = tokenNotExpired; | ||
exports.AUTH_PROVIDERS = { | ||
provide: AuthHttp, | ||
deps: [http_1.Http, http_1.RequestOptions], | ||
useFactory: function (http, options) { | ||
return new AuthHttp(new AuthConfig, http, options); | ||
} | ||
}; | ||
function provideAuth(config) { | ||
if (config === void 0) { config = {}; } | ||
return { | ||
exports.AUTH_PROVIDERS = [ | ||
{ | ||
provide: AuthHttp, | ||
deps: [http_1.Http, http_1.RequestOptions], | ||
useFactory: function (http, options) { | ||
return new AuthHttp(new AuthConfig(config), http, options); | ||
return new AuthHttp(new AuthConfig(), http, options); | ||
} | ||
}; | ||
} | ||
]; | ||
function provideAuth(config) { | ||
if (config === void 0) { config = {}; } | ||
return [ | ||
{ | ||
provide: AuthHttp, | ||
deps: [http_1.Http, http_1.RequestOptions], | ||
useFactory: function (http, options) { | ||
return new AuthHttp(new AuthConfig(config), http, options); | ||
} | ||
} | ||
]; | ||
} | ||
exports.provideAuth = provideAuth; | ||
//# sourceMappingURL=angular2-jwt.js.map |
@@ -1,10 +0,7 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Injectable, Provider } from '@angular/core'; | ||
import { Http, Headers, Request, RequestOptions, RequestOptionsArgs, RequestMethod, Response } from '@angular/http'; | ||
import { Observable } from 'rxjs/Observable'; | ||
import 'rxjs/add/observable/fromPromise'; | ||
import 'rxjs/add/operator/mergeMap'; | ||
import 'rxjs/add/operator/mergeMap'; | ||
// Avoid TS error "cannot find name escape" | ||
declare var escape: any; | ||
export interface IAuthConfig { | ||
@@ -16,3 +13,3 @@ globalHeaders: Array<Object>; | ||
noTokenScheme?: boolean; | ||
tokenGetter: any; | ||
tokenGetter: () => string | Promise<string>; | ||
tokenName: string; | ||
@@ -32,3 +29,3 @@ } | ||
public noTokenScheme: boolean; | ||
public tokenGetter: any; | ||
public tokenGetter: () => string | Promise<string>; | ||
public tokenName: string; | ||
@@ -48,3 +45,3 @@ | ||
this.noTokenScheme = config.noTokenScheme || false; | ||
this.tokenGetter = config.tokenGetter || (() => localStorage.getItem(this.tokenName)); | ||
this.tokenGetter = config.tokenGetter || (() => localStorage.getItem(this.tokenName) as string); | ||
this.tokenName = config.tokenName || 'id_token'; | ||
@@ -67,2 +64,5 @@ } | ||
export class AuthHttpError extends Error { | ||
} | ||
/** | ||
@@ -105,2 +105,16 @@ * Allows for explicit authenticated HTTP requests. | ||
private requestWithToken(req: Request, token: string): Observable<Response> { | ||
if (!tokenNotExpired(undefined, token)) { | ||
if (!this.config.noJwtError) { | ||
return new Observable<Response>((obs: any) => { | ||
obs.error(new AuthHttpError('No JWT present or has expired')); | ||
}); | ||
} | ||
} else { | ||
req.headers.set(this.config.headerName, this.config.headerPrefix + token); | ||
} | ||
return this.http.request(req); | ||
} | ||
public setGlobalHeaders(headers: Array<Object>, request: Request | RequestOptionsArgs) { | ||
@@ -127,6 +141,5 @@ if (!request.headers) { | ||
let req: Request = url as Request; | ||
let token: string & Promise<string> = this.config.tokenGetter(); | ||
if(token.then) { | ||
return Observable.fromPromise(token) | ||
.flatMap((jwtToken: string) => this.requestWithToken(req, jwtToken)); | ||
let token: string | Promise<string> = this.config.tokenGetter(); | ||
if (token instanceof Promise) { | ||
return Observable.fromPromise(token).mergeMap((jwtToken: string) => this.requestWithToken(req, jwtToken)); | ||
} else { | ||
@@ -137,16 +150,2 @@ return this.requestWithToken(req, token); | ||
private requestWithToken(req: Request, token: string): Observable<Response> { | ||
if (!tokenNotExpired(undefined, token)) { | ||
if (!this.config.noJwtError) { | ||
return new Observable<Response>((obs: any) => { | ||
obs.error(new Error('No JWT present or has expired')); | ||
}); | ||
} | ||
} else { | ||
req.headers.set(this.config.headerName, this.config.headerPrefix + token); | ||
} | ||
return this.http.request(req); | ||
} | ||
public get(url: string, options?: RequestOptionsArgs): Observable<Response> { | ||
@@ -199,3 +198,3 @@ return this.requestHelper({ body: '', method: RequestMethod.Get, url: url }, options); | ||
return decodeURIComponent(escape(typeof window === 'undefined' ? atob(output) : window.atob(output))); //polyfill https://github.com/davidchambers/Base64.js | ||
return decodeURIComponent(encodeURI(typeof window === 'undefined' ? atob(output) : window.atob(output))); | ||
} | ||
@@ -223,3 +222,3 @@ | ||
if (!decoded.hasOwnProperty('exp')) { | ||
return new Date(); | ||
return null; | ||
} | ||
@@ -235,5 +234,5 @@ | ||
let date = this.getTokenExpirationDate(token); | ||
offsetSeconds = offsetSeconds || 0; | ||
offsetSeconds = offsetSeconds || 0; | ||
if (date.getSeconds() === new Date().getSeconds()) { | ||
if (date == null) { | ||
return false; | ||
@@ -261,18 +260,22 @@ } | ||
export const AUTH_PROVIDERS: any = { | ||
provide: AuthHttp, | ||
deps: [Http, RequestOptions], | ||
useFactory: (http: Http, options: RequestOptions) => { | ||
return new AuthHttp(new AuthConfig, http, options); | ||
} | ||
}; | ||
export function provideAuth(config = {}): any { | ||
return { | ||
export const AUTH_PROVIDERS: Provider[] = [ | ||
{ | ||
provide: AuthHttp, | ||
deps: [Http, RequestOptions], | ||
useFactory: (http: Http, options: RequestOptions) => { | ||
return new AuthHttp(new AuthConfig(config), http, options); | ||
return new AuthHttp(new AuthConfig(), http, options); | ||
} | ||
}; | ||
} | ||
]; | ||
export function provideAuth(config = {}): Provider[] { | ||
return [ | ||
{ | ||
provide: AuthHttp, | ||
deps: [Http, RequestOptions], | ||
useFactory: (http: Http, options: RequestOptions) => { | ||
return new AuthHttp(new AuthConfig(config), http, options); | ||
} | ||
} | ||
]; | ||
} |
{ | ||
"name": "angular2-jwt", | ||
"version": "0.1.21", | ||
"version": "0.1.22", | ||
"description": "Helper library for handling JWTs in Angular 2", | ||
@@ -46,3 +46,3 @@ "repository": { | ||
"rxjs": "5.0.0-beta.11", | ||
"typescript": "^1.8.10", | ||
"typescript": "^2.0.2", | ||
"typings": "^0.7.12", | ||
@@ -49,0 +49,0 @@ "webpack": "^1.13.0", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
148833
2899