angular2-jwt
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -1,194 +0,18 @@ | ||
System.register(['angular2/angular2', 'angular2/http', '@reactivex/rxjs/dist/cjs/Rx'], function(exports_1) { | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc); | ||
switch (arguments.length) { | ||
case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target); | ||
case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0); | ||
case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc); | ||
System.register(['./src/angular2-jwt'], function(exports_1) { | ||
function exportStar_1(m) { | ||
var exports = {}; | ||
for(var n in m) { | ||
if (n !== "default") exports[n] = m[n]; | ||
} | ||
}; | ||
var __metadata = (this && this.__metadata) || function (k, v) { | ||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); | ||
}; | ||
var angular2_1, http_1, Rx_1; | ||
var AuthConfig, AuthHttp, JwtHelper; | ||
/** | ||
* Checks for presence of token and that token hasn't expired. | ||
* For use with the @CanActivate router decorator and NgIf | ||
*/ | ||
function tokenNotExpired(tokenName, jwt) { | ||
var tokenName = tokenName || 'id_token'; | ||
var token; | ||
if (token) { | ||
token = jwt; | ||
} | ||
else { | ||
token = localStorage.getItem(tokenName); | ||
} | ||
var jwtHelper = new JwtHelper(); | ||
if (!token || jwtHelper.isTokenExpired(token, null)) { | ||
return false; | ||
} | ||
else { | ||
return true; | ||
} | ||
exports_1(exports); | ||
} | ||
exports_1("tokenNotExpired", tokenNotExpired); | ||
return { | ||
setters:[ | ||
function (angular2_1_1) { | ||
angular2_1 = angular2_1_1; | ||
}, | ||
function (http_1_1) { | ||
http_1 = http_1_1; | ||
}, | ||
function (Rx_1_1) { | ||
Rx_1 = Rx_1_1; | ||
function (angular2_jwt_1_1) { | ||
exportStar_1(angular2_jwt_1_1); | ||
}], | ||
execute: function() { | ||
/** | ||
* Sets up the authentication configuration. | ||
*/ | ||
AuthConfig = (function () { | ||
function AuthConfig(config) { | ||
var _this = this; | ||
this.config = config || {}; | ||
this.headerName = this.config.headerName || 'Authorization'; | ||
this.headerPrefix = this.config.headerPrefix || 'Bearer '; | ||
this.tokenName = this.config.tokenName || 'id_token'; | ||
this.noJwtError = this.config.noJwtError || false; | ||
this.tokenGetter = this.config.tokenGetter || (function () { return localStorage.getItem(_this.tokenName); }); | ||
return { | ||
headerName: this.headerName, | ||
headerPrefix: this.headerPrefix, | ||
tokenName: this.tokenName, | ||
tokenGetter: this.tokenGetter, | ||
noJwtError: this.noJwtError | ||
}; | ||
} | ||
return AuthConfig; | ||
})(); | ||
exports_1("AuthConfig", AuthConfig); | ||
/** | ||
* Allows for explicit authenticated HTTP requests. | ||
*/ | ||
AuthHttp = (function () { | ||
function AuthHttp(config) { | ||
var _this = this; | ||
this._config = new AuthConfig(config); | ||
var injector = angular2_1.Injector.resolveAndCreate([http_1.HTTP_PROVIDERS]); | ||
this.http = injector.get(http_1.Http); | ||
this.tokenStream = new Rx_1.Observable(function (obs) { | ||
obs.next(_this._config.tokenGetter()); | ||
}); | ||
} | ||
AuthHttp.prototype.request = function (method, url, body) { | ||
if (!tokenNotExpired(null, this._config.tokenGetter())) { | ||
if (this._config.noJwtError) { | ||
return this.http.request(new http_1.Request({ | ||
method: method, | ||
url: url, | ||
body: body | ||
})); | ||
} | ||
throw 'Invalid JWT'; | ||
} | ||
var authHeader = new http_1.Headers(); | ||
authHeader.append(this._config.headerName, this._config.headerPrefix + this._config.tokenGetter()); | ||
return this.http.request(new http_1.Request({ | ||
method: method, | ||
url: url, | ||
body: body, | ||
headers: authHeader | ||
})); | ||
}; | ||
AuthHttp.prototype.get = function (url) { | ||
return this.request(http_1.RequestMethods.Get, url); | ||
}; | ||
AuthHttp.prototype.post = function (url, body) { | ||
return this.request(http_1.RequestMethods.Post, url, body); | ||
}; | ||
AuthHttp.prototype.put = function (url, body) { | ||
return this.request(http_1.RequestMethods.Put, url, body); | ||
}; | ||
AuthHttp.prototype.delete = function (url, body) { | ||
return this.request(http_1.RequestMethods.Delete, url, body); | ||
}; | ||
AuthHttp.prototype.options = function (url, body) { | ||
return this.request(http_1.RequestMethods.Options, url, body); | ||
}; | ||
AuthHttp.prototype.head = function (url, body) { | ||
return this.request(http_1.RequestMethods.Head, url, body); | ||
}; | ||
AuthHttp.prototype.patch = function (url, body) { | ||
return this.request(http_1.RequestMethods.Patch, url, body); | ||
}; | ||
AuthHttp = __decorate([ | ||
angular2_1.Injectable(), | ||
__metadata('design:paramtypes', [Object]) | ||
], AuthHttp); | ||
return AuthHttp; | ||
})(); | ||
exports_1("AuthHttp", AuthHttp); | ||
/** | ||
* Helper class to decode and find JWT expiration. | ||
*/ | ||
JwtHelper = (function () { | ||
function JwtHelper() { | ||
} | ||
JwtHelper.prototype.urlBase64Decode = function (str) { | ||
var output = str.replace(/-/g, '+').replace(/_/g, '/'); | ||
switch (output.length % 4) { | ||
case 0: { | ||
break; | ||
} | ||
case 2: { | ||
output += '=='; | ||
break; | ||
} | ||
case 3: { | ||
output += '='; | ||
break; | ||
} | ||
default: { | ||
throw 'Illegal base64url string!'; | ||
} | ||
} | ||
return decodeURIComponent(escape(window.atob(output))); //polifyll https://github.com/davidchambers/Base64.js | ||
}; | ||
JwtHelper.prototype.decodeToken = function (token) { | ||
var parts = token.split('.'); | ||
if (parts.length !== 3) { | ||
throw new Error('JWT must have 3 parts'); | ||
} | ||
var decoded = this.urlBase64Decode(parts[1]); | ||
if (!decoded) { | ||
throw new Error('Cannot decode the token'); | ||
} | ||
return JSON.parse(decoded); | ||
}; | ||
JwtHelper.prototype.getTokenExpirationDate = function (token) { | ||
var decoded; | ||
decoded = this.decodeToken(token); | ||
if (typeof decoded.exp === "undefined") { | ||
return null; | ||
} | ||
var date = new Date(0); // The 0 here is the key, which sets the date to the epoch | ||
date.setUTCSeconds(decoded.exp); | ||
return date; | ||
}; | ||
JwtHelper.prototype.isTokenExpired = function (token, offsetSeconds) { | ||
var date = this.getTokenExpirationDate(token); | ||
offsetSeconds = offsetSeconds || 0; | ||
if (date === null) { | ||
return false; | ||
} | ||
// Token expired? | ||
return !(date.valueOf() > (new Date().valueOf() + (offsetSeconds * 1000))); | ||
}; | ||
return JwtHelper; | ||
})(); | ||
exports_1("JwtHelper", JwtHelper); | ||
} | ||
} | ||
}); | ||
//# sourceMappingURL=angular2-jwt.js.map |
{ | ||
"name": "angular2-jwt", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Helper library for handling JWTs in Angular 2", | ||
@@ -9,2 +9,5 @@ "repository": { | ||
}, | ||
"scripts": { | ||
"dev": "tsc --watch" | ||
}, | ||
"keywords": [ | ||
@@ -25,12 +28,7 @@ "angular", | ||
"devDependencies": { | ||
"del": "^1.2.0", | ||
"gulp": "^3.9.0", | ||
"gulp-typescript": "^2.8.0", | ||
"gulp-uglify": "^1.4.2", | ||
"typescript": "^1.6.2" | ||
}, | ||
"dependencies": { | ||
"angular2": "2.0.0-alpha.45", | ||
"@reactivex/rxjs": "5.0.0-alpha.7" | ||
} | ||
} |
@@ -7,5 +7,6 @@ # angular2-jwt | ||
* Send a JWT on a per-request basis using the **explicit `AuthHttp`** class | ||
* **Decode a JWT** from your Angular 2 app | ||
* Check the **expiration date** of the JWT | ||
* Send a JWT on a per-request basis using the **explicit `AuthHttp`** class | ||
* Conditionally allow **route navigation** based on JWT status | ||
@@ -25,3 +26,3 @@ ## Installation | ||
If you wish to only send a JWT on a specific HTTP requests, you can use the `AuthHttp` class. | ||
If you wish to only send a JWT on a specific HTTP request, you can use the `AuthHttp` class. | ||
@@ -68,3 +69,3 @@ ```js | ||
* Token Getter Function: `(() => localStorage.getItem(tokenName))` | ||
* Error thrown if no JWT is saved: `true` | ||
* Supress error and continue with regular HTTP request if no JWT is saved: `false` | ||
@@ -176,5 +177,9 @@ If you wish to configure the `headerName`, `headerPrefix`, `tokenName`, `tokenGetter` function, or `noJwtError` boolean, you can pass a config object when `AuthHttp` is injected. | ||
## Contributing | ||
Pull requests are welcome! | ||
## Development | ||
To extend or contribute to this library, first clone the repo. A gulp task is set up for transpiling the TypeScript file to ES5. Just run `gulp` and changes will be watched. | ||
Use `npm run dev` to compile and watch for changes. | ||
@@ -181,0 +186,0 @@ ## What is Auth0? |
@@ -5,2 +5,13 @@ import {Injectable, Injector} from 'angular2/angular2'; | ||
// Avoid TS error "cannot find name escape" | ||
declare var escape; | ||
export interface IAuthConfig { | ||
headerName: string; | ||
headerPrefix: string; | ||
tokenName: string; | ||
tokenGetter: any; | ||
noJwtError: boolean; | ||
} | ||
/** | ||
@@ -26,3 +37,5 @@ * Sets up the authentication configuration. | ||
this.tokenGetter = this.config.tokenGetter || (() => localStorage.getItem(this.tokenName)); | ||
} | ||
getConfig() { | ||
return { | ||
@@ -46,3 +59,3 @@ headerName: this.headerName, | ||
private _config: AuthConfig; | ||
private _config: IAuthConfig; | ||
public tokenStream: Observable<string>; | ||
@@ -52,3 +65,3 @@ http: Http; | ||
constructor(config?:Object) { | ||
this._config = new AuthConfig(config); | ||
this._config = new AuthConfig(config).getConfig(); | ||
var injector = Injector.resolveAndCreate([HTTP_PROVIDERS]); | ||
@@ -69,3 +82,6 @@ this.http = injector.get(Http); | ||
url: url, | ||
body: body | ||
body: body, | ||
headers: null, | ||
search: null, | ||
merge: null | ||
})); | ||
@@ -83,3 +99,5 @@ } | ||
body: body, | ||
headers: authHeader | ||
headers: authHeader, | ||
search: null, | ||
merge: null | ||
})); | ||
@@ -168,3 +186,3 @@ | ||
public isTokenExpired(token:string, offsetSeconds:number) { | ||
public isTokenExpired(token:string, offsetSeconds?:number) { | ||
var date = this.getTokenExpirationDate(token); | ||
@@ -186,12 +204,12 @@ offsetSeconds = offsetSeconds || 0; | ||
export function tokenNotExpired(tokenName:string, jwt:string) { | ||
export function tokenNotExpired(tokenName?:string, jwt?:string) { | ||
var tokenName = tokenName || 'id_token'; | ||
var authToken:string = tokenName || 'id_token'; | ||
var token:string; | ||
if(token) { | ||
if(jwt) { | ||
token = jwt; | ||
} | ||
else { | ||
token = localStorage.getItem(tokenName); | ||
token = localStorage.getItem(authToken); | ||
} | ||
@@ -198,0 +216,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
32105
1
1
13
465
207
1
- Removedangular2@2.0.0-alpha.45
- Removedangular2@2.0.0-alpha.45(transitive)
- Removedes6-promise@3.3.1(transitive)
- Removedreflect-metadata@0.1.2(transitive)
- Removedzone.js@0.5.8(transitive)