@sneko/ionic-appauth
Version:
Intergration for OpenId/AppAuth-JS into Ionic V3/4/5
130 lines (129 loc) • 6.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IonicAuthorizationRequestHandler = exports.AUTHORIZATION_RESPONSE_KEY = void 0;
var tslib_1 = require("tslib");
var appauth_1 = require("@openid/appauth");
/** key for authorization request. */
var authorizationRequestKey = function (handle) {
return handle + "_appauth_authorization_request";
};
/** key in local storage which represents the current authorization request. */
var AUTHORIZATION_REQUEST_HANDLE_KEY = 'appauth_current_authorization_request';
exports.AUTHORIZATION_RESPONSE_KEY = "auth_response";
var IonicAuthorizationRequestHandler = /** @class */ (function (_super) {
tslib_1.__extends(IonicAuthorizationRequestHandler, _super);
function IonicAuthorizationRequestHandler(browser, storage, utils, generateRandom) {
if (utils === void 0) { utils = new appauth_1.BasicQueryStringUtils(); }
if (generateRandom === void 0) { generateRandom = new appauth_1.DefaultCrypto(); }
var _this = _super.call(this, utils, generateRandom) || this;
_this.browser = browser;
_this.storage = storage;
_this.generateRandom = generateRandom;
return _this;
}
IonicAuthorizationRequestHandler.prototype.performAuthorizationRequest = function (configuration, request) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var handle, _a, _b, _c, _d, _e, url, returnedUrl;
return tslib_1.__generator(this, function (_f) {
switch (_f.label) {
case 0:
handle = this.generateRandom.generateRandom(10);
this.storage.setItem(AUTHORIZATION_REQUEST_HANDLE_KEY, handle);
_b = (_a = this.storage).setItem;
_c = [authorizationRequestKey(handle)];
_e = (_d = JSON).stringify;
return [4 /*yield*/, request.toJson()];
case 1:
_b.apply(_a, _c.concat([_e.apply(_d, [_f.sent()])]));
url = this.buildRequestUrl(configuration, request);
return [4 /*yield*/, this.browser.showWindow(url, request.redirectUri)];
case 2:
returnedUrl = _f.sent();
if (!(returnedUrl != undefined)) return [3 /*break*/, 4];
return [4 /*yield*/, this.storage.setItem(exports.AUTHORIZATION_RESPONSE_KEY, url)];
case 3:
_f.sent();
this.completeAuthorizationRequestIfPossible();
_f.label = 4;
case 4: return [2 /*return*/];
}
});
});
};
IonicAuthorizationRequestHandler.prototype.completeAuthorizationRequest = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var handle, request, _a, queryParams, _b, state, error;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0: return [4 /*yield*/, this.storage.getItem(AUTHORIZATION_REQUEST_HANDLE_KEY)];
case 1:
handle = _c.sent();
if (!handle) {
throw new Error("Handle Not Available");
}
_a = this.getAuthorizationRequest;
return [4 /*yield*/, this.storage.getItem(authorizationRequestKey(handle))];
case 2:
request = _a.apply(this, [_c.sent()]);
_b = this.getQueryParams;
return [4 /*yield*/, this.storage.getItem(exports.AUTHORIZATION_RESPONSE_KEY)];
case 3:
queryParams = _b.apply(this, [_c.sent()]);
this.removeItemsFromStorage(handle);
state = queryParams['state'];
error = queryParams['error'];
if (state !== request.state) {
throw new Error("State Does Not Match");
}
return [2 /*return*/, {
request: request,
response: (!error) ? this.getAuthorizationResponse(queryParams) : undefined,
error: (error) ? this.getAuthorizationError(queryParams) : undefined
}];
}
});
});
};
IonicAuthorizationRequestHandler.prototype.getAuthorizationRequest = function (authRequest) {
if (authRequest == null) {
throw new Error("No Auth Request Available");
}
return new appauth_1.AuthorizationRequest(JSON.parse(authRequest));
};
IonicAuthorizationRequestHandler.prototype.getAuthorizationError = function (queryParams) {
var authorizationErrorJSON = {
error: queryParams['error'],
error_description: queryParams['error_description'],
error_uri: undefined,
state: queryParams['state']
};
return new appauth_1.AuthorizationError(authorizationErrorJSON);
};
IonicAuthorizationRequestHandler.prototype.getAuthorizationResponse = function (queryParams) {
var authorizationResponseJSON = {
code: queryParams['code'],
state: queryParams['state']
};
return new appauth_1.AuthorizationResponse(authorizationResponseJSON);
};
IonicAuthorizationRequestHandler.prototype.removeItemsFromStorage = function (handle) {
this.storage.removeItem(AUTHORIZATION_REQUEST_HANDLE_KEY);
this.storage.removeItem(authorizationRequestKey(handle));
this.storage.removeItem(exports.AUTHORIZATION_RESPONSE_KEY);
};
IonicAuthorizationRequestHandler.prototype.getQueryParams = function (authResponse) {
if (authResponse != null) {
var querySide = authResponse.split('#')[0];
var parts = querySide.split('?');
if (parts.length !== 2)
throw new Error("Invalid auth response string");
var hash = parts[1];
return this.utils.parseQueryString(hash);
}
else {
return {};
}
};
return IonicAuthorizationRequestHandler;
}(appauth_1.AuthorizationRequestHandler));
exports.IonicAuthorizationRequestHandler = IonicAuthorizationRequestHandler;