angular2-jwt
Advanced tools
Comparing version 0.1.18 to 0.1.19
import { Http, Request, RequestOptions, RequestOptionsArgs, Response } from '@angular/http'; | ||
import { Observable } from 'rxjs/Observable'; | ||
export interface IAuthConfig { | ||
globalHeaders: Array<Object>; | ||
headerName: string; | ||
headerPrefix: string; | ||
tokenName: string; | ||
tokenGetter: any; | ||
noJwtError: boolean; | ||
globalHeaders: Array<Object>; | ||
noTokenScheme?: boolean; | ||
tokenGetter: any; | ||
tokenName: string; | ||
} | ||
@@ -16,9 +16,9 @@ /** | ||
export declare class AuthConfig { | ||
globalHeaders: Array<Object>; | ||
headerName: string; | ||
headerPrefix: string; | ||
tokenName: string; | ||
tokenGetter: any; | ||
noJwtError: boolean; | ||
noTokenScheme: boolean; | ||
globalHeaders: Array<Object>; | ||
tokenGetter: any; | ||
tokenName: string; | ||
constructor(config?: any); | ||
@@ -32,16 +32,17 @@ getConfig(): IAuthConfig; | ||
private http; | ||
private _defOpts; | ||
private _config; | ||
private defOpts; | ||
private config; | ||
tokenStream: Observable<string>; | ||
constructor(options: AuthConfig, http: Http, _defOpts?: RequestOptions); | ||
constructor(options: AuthConfig, http: Http, defOpts?: RequestOptions); | ||
private mergeOptions(providedOpts, defaultOpts?); | ||
private requestHelper(requestArgs, additionalOptions?); | ||
setGlobalHeaders(headers: Array<Object>, request: Request | RequestOptionsArgs): void; | ||
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response>; | ||
private mergeOptions(defaultOpts, providedOpts); | ||
private requestHelper(requestArgs, additionalOptions); | ||
get(url: string, options?: RequestOptionsArgs): Observable<Response>; | ||
post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response>; | ||
put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response>; | ||
post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>; | ||
put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>; | ||
delete(url: string, options?: RequestOptionsArgs): Observable<Response>; | ||
patch(url: string, body: string, options?: RequestOptionsArgs): Observable<Response>; | ||
patch(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>; | ||
head(url: string, options?: RequestOptionsArgs): Observable<Response>; | ||
options(url: string, options?: RequestOptionsArgs): Observable<Response>; | ||
} | ||
@@ -48,0 +49,0 @@ /** |
@@ -21,2 +21,3 @@ "use strict"; | ||
if (config === void 0) { config = {}; } | ||
this.globalHeaders = config.globalHeaders || []; | ||
this.headerName = config.headerName || 'Authorization'; | ||
@@ -32,17 +33,16 @@ if (config.headerPrefix) { | ||
} | ||
this.tokenName = config.tokenName || 'id_token'; | ||
this.noJwtError = config.noJwtError || false; | ||
this.noTokenScheme = config.noTokenScheme || false; | ||
this.tokenGetter = config.tokenGetter || (function () { return localStorage.getItem(_this.tokenName); }); | ||
this.globalHeaders = config.globalHeaders || []; | ||
this.noTokenScheme = config.noTokenScheme || false; | ||
this.tokenName = config.tokenName || 'id_token'; | ||
} | ||
AuthConfig.prototype.getConfig = function () { | ||
return { | ||
globalHeaders: this.globalHeaders, | ||
headerName: this.headerName, | ||
headerPrefix: this.headerPrefix, | ||
tokenName: this.tokenName, | ||
tokenGetter: this.tokenGetter, | ||
noJwtError: this.noJwtError, | ||
noTokenScheme: this.noTokenScheme, | ||
globalHeaders: this.globalHeaders | ||
tokenGetter: this.tokenGetter, | ||
tokenName: this.tokenName | ||
}; | ||
@@ -57,11 +57,26 @@ }; | ||
var AuthHttp = (function () { | ||
function AuthHttp(options, http, _defOpts) { | ||
function AuthHttp(options, http, defOpts) { | ||
var _this = this; | ||
this.http = http; | ||
this._defOpts = _defOpts; | ||
this._config = options.getConfig(); | ||
this.defOpts = defOpts; | ||
this.config = options.getConfig(); | ||
this.tokenStream = new Observable_1.Observable(function (obs) { | ||
obs.next(_this._config.tokenGetter()); | ||
obs.next(_this.config.tokenGetter()); | ||
}); | ||
} | ||
AuthHttp.prototype.mergeOptions = function (providedOpts, defaultOpts) { | ||
var newOptions = defaultOpts || new http_1.RequestOptions(); | ||
if (this.config.globalHeaders) { | ||
this.setGlobalHeaders(this.config.globalHeaders, providedOpts); | ||
} | ||
newOptions = newOptions.merge(new http_1.RequestOptions(providedOpts)); | ||
return newOptions; | ||
}; | ||
AuthHttp.prototype.requestHelper = function (requestArgs, additionalOptions) { | ||
var options = new http_1.RequestOptions(requestArgs); | ||
if (additionalOptions) { | ||
options = options.merge(additionalOptions); | ||
} | ||
return this.request(new http_1.Request(this.mergeOptions(options, this.defOpts))); | ||
}; | ||
AuthHttp.prototype.setGlobalHeaders = function (headers, request) { | ||
@@ -86,4 +101,4 @@ if (!request.headers) { | ||
var req = url; | ||
if (!tokenNotExpired(null, this._config.tokenGetter())) { | ||
if (!this._config.noJwtError) { | ||
if (!tokenNotExpired(undefined, this.config.tokenGetter())) { | ||
if (!this.config.noJwtError) { | ||
return new Observable_1.Observable(function (obs) { | ||
@@ -95,39 +110,27 @@ obs.error(new Error('No JWT present or has expired')); | ||
else { | ||
req.headers.set(this._config.headerName, this._config.headerPrefix + this._config.tokenGetter()); | ||
req.headers.set(this.config.headerName, this.config.headerPrefix + this.config.tokenGetter()); | ||
} | ||
return this.http.request(req); | ||
}; | ||
AuthHttp.prototype.mergeOptions = function (defaultOpts, providedOpts) { | ||
var newOptions = defaultOpts || new http_1.RequestOptions(); | ||
if (this._config.globalHeaders) { | ||
this.setGlobalHeaders(this._config.globalHeaders, providedOpts); | ||
} | ||
newOptions = newOptions.merge(new http_1.RequestOptions(providedOpts)); | ||
return newOptions; | ||
}; | ||
AuthHttp.prototype.requestHelper = function (requestArgs, additionalOptions) { | ||
var options = new http_1.RequestOptions(requestArgs); | ||
if (additionalOptions) { | ||
options = options.merge(additionalOptions); | ||
} | ||
return this.request(new http_1.Request(this.mergeOptions(this._defOpts, options))); | ||
}; | ||
AuthHttp.prototype.get = function (url, options) { | ||
return this.requestHelper({ url: url, method: http_1.RequestMethod.Get }, options); | ||
return this.requestHelper({ body: '', method: http_1.RequestMethod.Get, url: url }, options); | ||
}; | ||
AuthHttp.prototype.post = function (url, body, options) { | ||
return this.requestHelper({ url: url, body: body, method: http_1.RequestMethod.Post }, options); | ||
return this.requestHelper({ body: body, method: http_1.RequestMethod.Post, url: url }, options); | ||
}; | ||
AuthHttp.prototype.put = function (url, body, options) { | ||
return this.requestHelper({ url: url, body: body, method: http_1.RequestMethod.Put }, options); | ||
return this.requestHelper({ body: body, method: http_1.RequestMethod.Put, url: url }, options); | ||
}; | ||
AuthHttp.prototype.delete = function (url, options) { | ||
return this.requestHelper({ url: url, method: http_1.RequestMethod.Delete }, options); | ||
return this.requestHelper({ body: '', method: http_1.RequestMethod.Delete, url: url }, options); | ||
}; | ||
AuthHttp.prototype.patch = function (url, body, options) { | ||
return this.requestHelper({ url: url, body: body, method: http_1.RequestMethod.Patch }, options); | ||
return this.requestHelper({ body: body, method: http_1.RequestMethod.Patch, url: url }, options); | ||
}; | ||
AuthHttp.prototype.head = function (url, options) { | ||
return this.requestHelper({ url: url, method: http_1.RequestMethod.Head }, options); | ||
return this.requestHelper({ body: '', method: http_1.RequestMethod.Head, url: url }, options); | ||
}; | ||
AuthHttp.prototype.options = function (url, options) { | ||
return this.requestHelper({ body: '', method: http_1.RequestMethod.Options, url: url }, options); | ||
}; | ||
AuthHttp = __decorate([ | ||
@@ -180,4 +183,4 @@ core_1.Injectable(), | ||
decoded = this.decodeToken(token); | ||
if (typeof decoded.exp === "undefined") { | ||
return null; | ||
if (typeof decoded.exp === 'undefined') { | ||
return new Date(0); | ||
} | ||
@@ -191,5 +194,2 @@ var date = new Date(0); // The 0 here is the key, which sets the date to the epoch | ||
offsetSeconds = offsetSeconds || 0; | ||
if (date === null) { | ||
return false; | ||
} | ||
// Token expired? | ||
@@ -209,3 +209,3 @@ return !(date.valueOf() > (new Date().valueOf() + (offsetSeconds * 1000))); | ||
var jwtHelper = new JwtHelper(); | ||
return token && !jwtHelper.isTokenExpired(token, null); | ||
return token != null && !jwtHelper.isTokenExpired(token); | ||
} | ||
@@ -215,6 +215,6 @@ exports.tokenNotExpired = tokenNotExpired; | ||
core_1.provide(AuthHttp, { | ||
deps: [http_1.Http, http_1.RequestOptions], | ||
useFactory: function (http, options) { | ||
return new AuthHttp(new AuthConfig(), http, options); | ||
}, | ||
deps: [http_1.Http, http_1.RequestOptions] | ||
} | ||
}) | ||
@@ -226,6 +226,6 @@ ]; | ||
core_1.provide(AuthHttp, { | ||
deps: [http_1.Http, http_1.RequestOptions], | ||
useFactory: function (http, options) { | ||
return new AuthHttp(new AuthConfig(config), http, options); | ||
}, | ||
deps: [http_1.Http, http_1.RequestOptions] | ||
} | ||
}) | ||
@@ -232,0 +232,0 @@ ]; |
@@ -1,4 +0,4 @@ | ||
import {provide, Injectable} from '@angular/core'; | ||
import {Http, Headers, Request, RequestOptions, RequestOptionsArgs, RequestMethod, Response} from '@angular/http'; | ||
import {Observable} from 'rxjs/Observable'; | ||
import { provide, Injectable } from '@angular/core'; | ||
import { Http, Headers, Request, RequestOptions, RequestOptionsArgs, RequestMethod, Response } from '@angular/http'; | ||
import { Observable } from 'rxjs/Observable'; | ||
@@ -9,9 +9,9 @@ // Avoid TS error "cannot find name escape" | ||
export interface IAuthConfig { | ||
globalHeaders: Array<Object>; | ||
headerName: string; | ||
headerPrefix: string; | ||
noJwtError: boolean; | ||
noTokenScheme?: boolean; | ||
tokenGetter: any; | ||
tokenName: string; | ||
tokenGetter: any; | ||
noJwtError: boolean; | ||
globalHeaders: Array<Object>; | ||
noTokenScheme?:boolean; | ||
} | ||
@@ -24,12 +24,13 @@ | ||
export class AuthConfig { | ||
headerName: string; | ||
headerPrefix: string; | ||
tokenName: string; | ||
tokenGetter: any; | ||
noJwtError: boolean; | ||
noTokenScheme: boolean; | ||
globalHeaders: Array<Object>; | ||
constructor(config:any={}) { | ||
public globalHeaders: Array<Object>; | ||
public headerName: string; | ||
public headerPrefix: string; | ||
public noJwtError: boolean; | ||
public noTokenScheme: boolean; | ||
public tokenGetter: any; | ||
public tokenName: string; | ||
constructor(config: any = {}) { | ||
this.globalHeaders = config.globalHeaders || []; | ||
this.headerName = config.headerName || 'Authorization'; | ||
@@ -43,19 +44,18 @@ if (config.headerPrefix) { | ||
} | ||
this.tokenName = config.tokenName || 'id_token'; | ||
this.noJwtError = config.noJwtError || false; | ||
this.noTokenScheme = config.noTokenScheme || false; | ||
this.tokenGetter = config.tokenGetter || (() => localStorage.getItem(this.tokenName)); | ||
this.globalHeaders = config.globalHeaders || []; | ||
this.noTokenScheme=config.noTokenScheme||false; | ||
this.tokenName = config.tokenName || 'id_token'; | ||
} | ||
getConfig():IAuthConfig { | ||
public getConfig(): IAuthConfig { | ||
return { | ||
globalHeaders: this.globalHeaders, | ||
headerName: this.headerName, | ||
headerPrefix: this.headerPrefix, | ||
tokenName: this.tokenName, | ||
noJwtError: this.noJwtError, | ||
noTokenScheme: this.noTokenScheme, | ||
tokenGetter: this.tokenGetter, | ||
noJwtError: this.noJwtError, | ||
noTokenScheme:this.noTokenScheme, | ||
globalHeaders: this.globalHeaders | ||
} | ||
tokenName: this.tokenName | ||
}; | ||
} | ||
@@ -72,15 +72,34 @@ | ||
private _config: IAuthConfig; | ||
private config: IAuthConfig; | ||
public tokenStream: Observable<string>; | ||
constructor(options: AuthConfig, private http: Http, private _defOpts?: RequestOptions) { | ||
this._config = options.getConfig(); | ||
constructor(options: AuthConfig, private http: Http, private defOpts?: RequestOptions) { | ||
this.config = options.getConfig(); | ||
this.tokenStream = new Observable<string>((obs: any) => { | ||
obs.next(this._config.tokenGetter()); | ||
obs.next(this.config.tokenGetter()); | ||
}); | ||
} | ||
setGlobalHeaders(headers: Array<Object>, request: Request | RequestOptionsArgs) { | ||
if ( ! request.headers ) { | ||
private mergeOptions(providedOpts: RequestOptionsArgs, defaultOpts?: RequestOptions) { | ||
let newOptions = defaultOpts || new RequestOptions(); | ||
if (this.config.globalHeaders) { | ||
this.setGlobalHeaders(this.config.globalHeaders, providedOpts); | ||
} | ||
newOptions = newOptions.merge(new RequestOptions(providedOpts)); | ||
return newOptions; | ||
} | ||
private requestHelper(requestArgs: RequestOptionsArgs, additionalOptions?: RequestOptionsArgs): Observable<Response> { | ||
let options = new RequestOptions(requestArgs); | ||
if (additionalOptions) { | ||
options = options.merge(additionalOptions); | ||
} | ||
return this.request(new Request(this.mergeOptions(options, this.defOpts))); | ||
} | ||
public setGlobalHeaders(headers: Array<Object>, request: Request | RequestOptionsArgs) { | ||
if (!request.headers) { | ||
request.headers = new Headers(); | ||
@@ -90,8 +109,8 @@ } | ||
let key: string = Object.keys(header)[0]; | ||
let headerValue: string = (<any>header)[key]; | ||
request.headers.set(key, headerValue); | ||
let headerValue: string = (header as any)[key]; | ||
(request.headers as Headers).set(key, headerValue); | ||
}); | ||
} | ||
request(url: string | Request, options?: RequestOptionsArgs) : Observable<Response> { | ||
public request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> { | ||
if (typeof url === 'string') { | ||
@@ -105,5 +124,5 @@ return this.get(url, options); // Recursion: transform url from String to Request | ||
// from this point url is always an instance of Request; | ||
let req: Request = <Request>url; | ||
if (!tokenNotExpired(null, this._config.tokenGetter())) { | ||
if (!this._config.noJwtError) { | ||
let req: Request = url as Request; | ||
if (!tokenNotExpired(undefined, this.config.tokenGetter())) { | ||
if (!this.config.noJwtError) { | ||
return new Observable<Response>((obs: any) => { | ||
@@ -114,3 +133,3 @@ obs.error(new Error('No JWT present or has expired')); | ||
} else { | ||
req.headers.set(this._config.headerName, this._config.headerPrefix + this._config.tokenGetter()); | ||
req.headers.set(this.config.headerName, this.config.headerPrefix + this.config.tokenGetter()); | ||
} | ||
@@ -120,45 +139,30 @@ return this.http.request(req); | ||
private mergeOptions(defaultOpts: RequestOptions, providedOpts: RequestOptionsArgs) { | ||
let newOptions = defaultOpts || new RequestOptions(); | ||
if (this._config.globalHeaders) { | ||
this.setGlobalHeaders(this._config.globalHeaders, providedOpts); | ||
} | ||
newOptions = newOptions.merge(new RequestOptions(providedOpts)); | ||
return newOptions; | ||
public get(url: string, options?: RequestOptionsArgs): Observable<Response> { | ||
return this.requestHelper({ body: '', method: RequestMethod.Get, url: url }, options); | ||
} | ||
private requestHelper(requestArgs: RequestOptionsArgs, additionalOptions: RequestOptionsArgs) : Observable<Response> { | ||
let options = new RequestOptions(requestArgs); | ||
if (additionalOptions) { | ||
options = options.merge(additionalOptions); | ||
} | ||
return this.request(new Request(this.mergeOptions(this._defOpts, options))); | ||
public post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> { | ||
return this.requestHelper({ body: body, method: RequestMethod.Post, url: url }, options); | ||
} | ||
get(url: string, options?: RequestOptionsArgs) : Observable<Response> { | ||
return this.requestHelper({ url: url, method: RequestMethod.Get }, options); | ||
public put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> { | ||
return this.requestHelper({ body: body, method: RequestMethod.Put, url: url }, options); | ||
} | ||
post(url: string, body: string, options?: RequestOptionsArgs) : Observable<Response> { | ||
return this.requestHelper({ url: url, body: body, method: RequestMethod.Post }, options); | ||
public delete(url: string, options?: RequestOptionsArgs): Observable<Response> { | ||
return this.requestHelper({ body: '', method: RequestMethod.Delete, url: url }, options); | ||
} | ||
put(url: string, body: string, options ?: RequestOptionsArgs) : Observable<Response> { | ||
return this.requestHelper({ url: url, body: body, method: RequestMethod.Put }, options); | ||
public patch(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> { | ||
return this.requestHelper({ body: body, method: RequestMethod.Patch, url: url }, options); | ||
} | ||
delete(url: string, options ?: RequestOptionsArgs) : Observable<Response> { | ||
return this.requestHelper({ url: url, method: RequestMethod.Delete }, options); | ||
public head(url: string, options?: RequestOptionsArgs): Observable<Response> { | ||
return this.requestHelper({ body: '', method: RequestMethod.Head, url: url }, options); | ||
} | ||
patch(url: string, body:string, options?: RequestOptionsArgs) : Observable<Response> { | ||
return this.requestHelper({ url: url, body: body, method: RequestMethod.Patch }, options); | ||
public options(url: string, options?: RequestOptionsArgs): Observable<Response> { | ||
return this.requestHelper({ body: '', method: RequestMethod.Options, url: url }, options); | ||
} | ||
head(url: string, options?: RequestOptionsArgs) : Observable<Response> { | ||
return this.requestHelper({ url: url, method: RequestMethod.Head }, options); | ||
} | ||
} | ||
@@ -172,4 +176,4 @@ | ||
public urlBase64Decode(str:string) { | ||
var output = str.replace(/-/g, '+').replace(/_/g, '/'); | ||
public urlBase64Decode(str: string): string { | ||
let output = str.replace(/-/g, '+').replace(/_/g, '/'); | ||
switch (output.length % 4) { | ||
@@ -187,4 +191,4 @@ case 0: { break; } | ||
public decodeToken(token:string) { | ||
var parts = token.split('.'); | ||
public decodeToken(token: string): any { | ||
let parts = token.split('.'); | ||
@@ -195,3 +199,3 @@ if (parts.length !== 3) { | ||
var decoded = this.urlBase64Decode(parts[1]); | ||
let decoded = this.urlBase64Decode(parts[1]); | ||
if (!decoded) { | ||
@@ -204,11 +208,11 @@ throw new Error('Cannot decode the token'); | ||
public getTokenExpirationDate(token:string) { | ||
var decoded: any; | ||
public getTokenExpirationDate(token: string): Date { | ||
let decoded: any; | ||
decoded = this.decodeToken(token); | ||
if(typeof decoded.exp === "undefined") { | ||
return null; | ||
if (typeof decoded.exp === 'undefined') { | ||
return new Date(0); | ||
} | ||
var date = new Date(0); // The 0 here is the key, which sets the date to the epoch | ||
let date = new Date(0); // The 0 here is the key, which sets the date to the epoch | ||
date.setUTCSeconds(decoded.exp); | ||
@@ -219,8 +223,5 @@ | ||
public isTokenExpired(token:string, offsetSeconds?:number) { | ||
var date = this.getTokenExpirationDate(token); | ||
public isTokenExpired(token: string, offsetSeconds?: number): boolean { | ||
let date = this.getTokenExpirationDate(token); | ||
offsetSeconds = offsetSeconds || 0; | ||
if (date === null) { | ||
return false; | ||
} | ||
@@ -237,9 +238,9 @@ // Token expired? | ||
export function tokenNotExpired(tokenName = 'id_token', jwt?:string):boolean { | ||
export function tokenNotExpired(tokenName = 'id_token', jwt?: string): boolean { | ||
const token:string = jwt || localStorage.getItem(tokenName); | ||
const token: string = jwt || localStorage.getItem(tokenName); | ||
const jwtHelper = new JwtHelper(); | ||
return token && !jwtHelper.isTokenExpired(token, null); | ||
return token != null && !jwtHelper.isTokenExpired(token); | ||
} | ||
@@ -249,6 +250,6 @@ | ||
provide(AuthHttp, { | ||
deps: [Http, RequestOptions], | ||
useFactory: (http: Http, options: RequestOptions) => { | ||
return new AuthHttp(new AuthConfig(), http, options); | ||
}, | ||
deps: [Http, RequestOptions] | ||
} | ||
}) | ||
@@ -260,8 +261,8 @@ ]; | ||
provide(AuthHttp, { | ||
deps: [Http, RequestOptions], | ||
useFactory: (http: Http, options: RequestOptions) => { | ||
return new AuthHttp(new AuthConfig(config), http, options); | ||
}, | ||
deps: [Http, RequestOptions] | ||
} | ||
}) | ||
]; | ||
} |
{ | ||
"name": "angular2-jwt", | ||
"version": "0.1.18", | ||
"version": "0.1.19", | ||
"description": "Helper library for handling JWTs in Angular 2", | ||
@@ -5,0 +5,0 @@ "repository": { |
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
130299
2616