azure-adal-angular2
Version:
Azure Active Directory OAuth2 module for Angular 2
179 lines • 6.54 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = require('@angular/core');
var http_1 = require("@angular/http");
var adal_service_1 = require("./adal.service");
var rxjs_1 = require("rxjs");
var AdalHttpService = (function () {
/**
* Service constructor
*
* @param http
* @param adal
*/
function AdalHttpService(http, adalService) {
this.http = http;
this.adalService = adalService;
}
/**
* Function calls GET request
*
* @param uri
* @param options
*
* @returns {Observable<any>}
*/
AdalHttpService.prototype.get = function (uri, options) {
return this._call(uri, Object.assign(new http_1.RequestOptions({ method: http_1.RequestMethod.Get }), options));
};
/**
* Function calls POST request
* @param uri
* @param body
* @param options
*
* @returns {Observable<any>}
*/
AdalHttpService.prototype.post = function (uri, body, options) {
return this._call(uri, Object.assign(new http_1.RequestOptions({ method: http_1.RequestMethod.Post, body: body }), options));
};
/**
* Function calls DELETE request
*
* @param uri
* @param options
*
* @returns {Observable<any>}
*/
AdalHttpService.prototype.delete = function (uri, options) {
return this._call(uri, Object.assign(new http_1.RequestOptions({ method: http_1.RequestMethod.Delete }), options));
};
/**
* Function calls PATCH request
*
* @param uri
* @param body
* @param options
*
* @returns {Observable<any>}
*/
AdalHttpService.prototype.patch = function (uri, body, options) {
return this._call(uri, Object.assign(new http_1.RequestOptions({ method: http_1.RequestMethod.Patch, body: body }), options));
};
/**
* Function calls PUT request
*
* @param uri
* @param body
* @param options
*
* @returns {Observable<any>}
*/
AdalHttpService.prototype.put = function (uri, body, options) {
return this._call(uri, Object.assign(new http_1.RequestOptions({ method: http_1.RequestMethod.Put, body: body }), options));
};
/**
* Function calls HEAD request
*
* @param uri
* @param options
*
* @returns {Observable<any>}
*/
AdalHttpService.prototype.head = function (uri, options) {
return this._call(uri, Object.assign(new http_1.RequestOptions({ method: http_1.RequestMethod.Head }), options));
};
/**
* Function calls appropriate http methods
*
* @param uri
* @param options
*
* @private
*/
AdalHttpService.prototype._call = function (uri, options) {
var _this = this;
// build request options
var requestOptions = new http_1.RequestOptions({ method: options.method });
if (options.search != null) {
requestOptions.search = new http_1.URLSearchParams(options.search.toString());
}
if (options.headers != null) {
requestOptions.headers = new http_1.Headers(options.headers.toJSON());
}
if (options.body != null) {
requestOptions.body = options.body;
}
// get adal resource for this endpoint
var resource = this.adalService.getResourceForEndpoint(uri);
// check if we have an adal resource
if (resource) {
// check if we are authenicated
if (this.adalService.userInfo.isAuthenticated) {
return this.adalService.acquireToken(resource).flatMap(function (token) {
// check if we have already some headers
if (requestOptions.headers === null) {
requestOptions.headers = new http_1.Headers();
}
// add authentication token in headers
requestOptions.headers.append('Authorization', 'Bearer ' + token);
return _this.http.request(uri, requestOptions).map(_this._extractData).catch(_this._handleError);
});
}
else {
return rxjs_1.Observable.throw(new Error("User Not Authenticated."));
}
}
else {
return this.http.request(uri, requestOptions).map(this._extractData).catch(this._handleError);
}
};
/**
* Function to parse response data
*
* @param res
*
* @returns {{}}
*
* @private
*/
AdalHttpService.prototype._extractData = function (res) {
// check status
if (res.status < 200 || res.status >= 300) {
throw new Error('Bad response status: ' + res.status);
}
var body = {};
//if there is some content, parse it
if (res.status != 204) {
body = res.json();
}
return body;
};
/**
*
* @param error
* @returns {ErrorObservable}
* @private
*/
AdalHttpService.prototype._handleError = function (error) {
// In a real world app, we might send the error to remote logging infrastructure
var errMsg = error.message || 'Server error';
console.error(JSON.stringify(error)); // log to console instead
return rxjs_1.Observable.throw(error);
};
AdalHttpService = __decorate([
core_1.Injectable(),
__metadata('design:paramtypes', [http_1.Http, adal_service_1.AdalService])
], AdalHttpService);
return AdalHttpService;
}());
exports.AdalHttpService = AdalHttpService;
//# sourceMappingURL=adal-http.service.js.map