@obelisk/auth
Advanced tools
Comparing version 1.0.0 to 1.0.4
@@ -1,19 +0,42 @@ | ||
import { ObeliskConfig, Tokens } from "./types"; | ||
export declare class ObeliskAuth { | ||
private config; | ||
private tokens; | ||
constructor(config: ObeliskConfig); | ||
import { ObeliskConfig, Tokens } from './types'; | ||
export interface ObeliskAuth { | ||
/** | ||
* Returns the config map for this client | ||
*/ | ||
getConfig(): ObeliskConfig; | ||
/** | ||
* Returns the Tokens object, which is a holder for all tokens | ||
*/ | ||
getTokens(): Tokens; | ||
/** | ||
* Clear all tokens from the Tokens holder object, which effectively logs the user out. | ||
*/ | ||
clearTokens(): void; | ||
private saveCodeVerifier; | ||
private loadCodeVerifier; | ||
/** | ||
* Checks the Tokens holder object, if not empty, the user is assumed to be logged in. | ||
* This can prove to be a wrong assumption when contacting the server APIs. | ||
*/ | ||
loggedIn(): boolean; | ||
/** | ||
* Returns the login url as a promise. Use this url to login. | ||
* @param state A state paramters that will be returned to the registered redirectUri after logging in. | ||
*/ | ||
getLoginUrl(state?: string): Promise<string>; | ||
/** | ||
* Handler for detecting and exchanging a authorization code parameter in the querystring of the uri. | ||
* It will use it to retrieve the actual token. | ||
* **Register this to a location href change listener to complete login** | ||
* | ||
* @param event Location change url (should have a 'url' property of the new url) | ||
* @param history Instance of the history object, defaults to window.history. | ||
*/ | ||
handleCodeExchange<T extends { | ||
url: string; | ||
}>(event: T): void; | ||
private setConfigDefaults; | ||
private getCodeChallenge; | ||
private digestMessage; | ||
private captureTokens; | ||
} | ||
export interface AuthEvent { | ||
type: AuthEventType; | ||
} | ||
export declare enum AuthEventType { | ||
READY = 0 | ||
} |
export * from './auth'; | ||
export * from './types'; | ||
export { ObeliskAuthClient } from './auth-client'; |
@@ -0,1 +1,6 @@ | ||
var AuthEventType; | ||
(function (AuthEventType) { | ||
AuthEventType[AuthEventType["READY"] = 0] = "READY"; | ||
})(AuthEventType || (AuthEventType = {})); | ||
/*! ***************************************************************************** | ||
@@ -261,23 +266,39 @@ Copyright (c) Microsoft Corporation. All rights reserved. | ||
var ObeliskAuth = /** @class */ (function () { | ||
function ObeliskAuth(config) { | ||
var ObeliskAuthClient = /** @class */ (function () { | ||
function ObeliskAuthClient(config, eventHandler) { | ||
this.config = this.setConfigDefaults(config); | ||
this.tokens = {}; | ||
this.eventHandler = eventHandler; | ||
} | ||
ObeliskAuth.prototype.getConfig = function () { | ||
/** | ||
* @inheritdoc | ||
*/ | ||
ObeliskAuthClient.prototype.getConfig = function () { | ||
return this.config; | ||
}; | ||
ObeliskAuth.prototype.getTokens = function () { | ||
/** | ||
* @inheritdoc | ||
*/ | ||
ObeliskAuthClient.prototype.getTokens = function () { | ||
return this.tokens; | ||
}; | ||
ObeliskAuth.prototype.clearTokens = function () { | ||
/** | ||
* @inheritdoc | ||
*/ | ||
ObeliskAuthClient.prototype.clearTokens = function () { | ||
this.tokens = {}; | ||
}; | ||
ObeliskAuth.prototype.saveCodeVerifier = function (verifier) { | ||
sessionStorage.setItem('obeliskCodeVerifier', verifier); | ||
/** | ||
* @inheritdoc | ||
*/ | ||
ObeliskAuthClient.prototype.loggedIn = function () { | ||
return (this.tokens != null && | ||
this.tokens.accessToken != null && | ||
this.tokens.idToken != null && | ||
this.tokens.idTokenString != null); | ||
}; | ||
ObeliskAuth.prototype.loadCodeVerifier = function () { | ||
return sessionStorage.getItem('obeliskCodeVerifier'); | ||
}; | ||
ObeliskAuth.prototype.getLoginUrl = function (state) { | ||
/** | ||
* @inheritdoc | ||
*/ | ||
ObeliskAuthClient.prototype.getLoginUrl = function (state) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -299,5 +320,7 @@ var queryFields, _a, _b, query; | ||
_a.code_challenge_method = 'S256', | ||
_a.state = state || (Date.now() + ''), | ||
_a.state = state || Date.now() + '', | ||
_a); | ||
query = Object.entries(queryFields).map(function (entry) { return entry[0] + '=' + entry[1]; }).join('&'); | ||
query = Object.entries(queryFields) | ||
.map(function (entry) { return entry[0] + '=' + entry[1]; }) | ||
.join('&'); | ||
return [2 /*return*/, Promise.resolve(this.config.host + '/auth?' + query)]; | ||
@@ -308,7 +331,10 @@ } | ||
}; | ||
ObeliskAuth.prototype.handleCodeExchange = function (event) { | ||
/** | ||
* @inheritdoc | ||
*/ | ||
ObeliskAuthClient.prototype.handleCodeExchange = function (event) { | ||
var _this = this; | ||
var url = event.url; | ||
if (url && url.indexOf('?') !== -1) { | ||
var params = url.split('?'); | ||
if (url && url.indexOf('#') !== -1) { | ||
var params = url.split('#'); | ||
var query_1 = {}; | ||
@@ -322,3 +348,3 @@ if (params.length === 2) { | ||
if ('code' in query_1) { | ||
query_1.client_id = "1"; | ||
query_1.client_id = '1'; | ||
query_1.grant_type = 'authorization_code'; | ||
@@ -330,6 +356,6 @@ query_1.code_verifier = this.loadCodeVerifier(); | ||
headers: { | ||
'Accept': 'application/json', | ||
'Content-type': 'application/json' | ||
Accept: 'application/json', | ||
'Content-type': 'application/json', | ||
}, | ||
method: 'POST' | ||
method: 'POST', | ||
}) | ||
@@ -339,8 +365,6 @@ .then(function (response) { return response.json(); }) | ||
_this.tokens = _this.captureTokens(data); | ||
// console.log(this.tokens); | ||
var uri = location.href; | ||
if (uri.indexOf('?') !== -1) { | ||
uri = uri.split('?')[0]; | ||
} | ||
window.history.replaceState({}, document.title, uri); | ||
location.hash = ''; | ||
}) | ||
.then(function (_) { | ||
_this.eventHandler({ type: AuthEventType.READY }); | ||
}); | ||
@@ -351,3 +375,9 @@ } | ||
}; | ||
ObeliskAuth.prototype.setConfigDefaults = function (config) { | ||
ObeliskAuthClient.prototype.saveCodeVerifier = function (verifier) { | ||
sessionStorage.setItem('obeliskCodeVerifier', verifier); | ||
}; | ||
ObeliskAuthClient.prototype.loadCodeVerifier = function () { | ||
return sessionStorage.getItem('obeliskCodeVerifier'); | ||
}; | ||
ObeliskAuthClient.prototype.setConfigDefaults = function (config) { | ||
if (!config.redirectUri) { | ||
@@ -358,3 +388,3 @@ config.redirectUri = 'http://localhost:4200'; | ||
}; | ||
ObeliskAuth.prototype.getCodeChallenge = function () { | ||
ObeliskAuthClient.prototype.getCodeChallenge = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -379,3 +409,3 @@ var cr, randomOctets, codeVerifier, codeChallenge, _a; | ||
}; | ||
ObeliskAuth.prototype.digestMessage = function (message) { | ||
ObeliskAuthClient.prototype.digestMessage = function (message) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -395,15 +425,16 @@ var msgUint8, hashBuffer; | ||
}; | ||
ObeliskAuth.prototype.captureTokens = function (tokenResponse) { | ||
var idToken = JSON.parse(atob(tokenResponse.id_token.split('\.')[1])); | ||
ObeliskAuthClient.prototype.captureTokens = function (tokenResponse) { | ||
var myIdToken = JSON.parse(atob(tokenResponse.id_token.split('.')[1])); | ||
var res = { | ||
idTokenString: tokenResponse.id_token, | ||
accessToken: tokenResponse.token, | ||
'idToken': idToken | ||
idToken: myIdToken, | ||
expiresAt: Date.now() + tokenResponse.expires_in * 1000, | ||
}; | ||
return res; | ||
}; | ||
return ObeliskAuth; | ||
return ObeliskAuthClient; | ||
}()); | ||
export { ObeliskAuth }; | ||
export { AuthEventType, ObeliskAuthClient }; | ||
//# sourceMappingURL=index.es.js.map |
@@ -5,2 +5,6 @@ 'use strict'; | ||
(function (AuthEventType) { | ||
AuthEventType[AuthEventType["READY"] = 0] = "READY"; | ||
})(exports.AuthEventType || (exports.AuthEventType = {})); | ||
/*! ***************************************************************************** | ||
@@ -266,23 +270,39 @@ Copyright (c) Microsoft Corporation. All rights reserved. | ||
var ObeliskAuth = /** @class */ (function () { | ||
function ObeliskAuth(config) { | ||
var ObeliskAuthClient = /** @class */ (function () { | ||
function ObeliskAuthClient(config, eventHandler) { | ||
this.config = this.setConfigDefaults(config); | ||
this.tokens = {}; | ||
this.eventHandler = eventHandler; | ||
} | ||
ObeliskAuth.prototype.getConfig = function () { | ||
/** | ||
* @inheritdoc | ||
*/ | ||
ObeliskAuthClient.prototype.getConfig = function () { | ||
return this.config; | ||
}; | ||
ObeliskAuth.prototype.getTokens = function () { | ||
/** | ||
* @inheritdoc | ||
*/ | ||
ObeliskAuthClient.prototype.getTokens = function () { | ||
return this.tokens; | ||
}; | ||
ObeliskAuth.prototype.clearTokens = function () { | ||
/** | ||
* @inheritdoc | ||
*/ | ||
ObeliskAuthClient.prototype.clearTokens = function () { | ||
this.tokens = {}; | ||
}; | ||
ObeliskAuth.prototype.saveCodeVerifier = function (verifier) { | ||
sessionStorage.setItem('obeliskCodeVerifier', verifier); | ||
/** | ||
* @inheritdoc | ||
*/ | ||
ObeliskAuthClient.prototype.loggedIn = function () { | ||
return (this.tokens != null && | ||
this.tokens.accessToken != null && | ||
this.tokens.idToken != null && | ||
this.tokens.idTokenString != null); | ||
}; | ||
ObeliskAuth.prototype.loadCodeVerifier = function () { | ||
return sessionStorage.getItem('obeliskCodeVerifier'); | ||
}; | ||
ObeliskAuth.prototype.getLoginUrl = function (state) { | ||
/** | ||
* @inheritdoc | ||
*/ | ||
ObeliskAuthClient.prototype.getLoginUrl = function (state) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -304,5 +324,7 @@ var queryFields, _a, _b, query; | ||
_a.code_challenge_method = 'S256', | ||
_a.state = state || (Date.now() + ''), | ||
_a.state = state || Date.now() + '', | ||
_a); | ||
query = Object.entries(queryFields).map(function (entry) { return entry[0] + '=' + entry[1]; }).join('&'); | ||
query = Object.entries(queryFields) | ||
.map(function (entry) { return entry[0] + '=' + entry[1]; }) | ||
.join('&'); | ||
return [2 /*return*/, Promise.resolve(this.config.host + '/auth?' + query)]; | ||
@@ -313,7 +335,10 @@ } | ||
}; | ||
ObeliskAuth.prototype.handleCodeExchange = function (event) { | ||
/** | ||
* @inheritdoc | ||
*/ | ||
ObeliskAuthClient.prototype.handleCodeExchange = function (event) { | ||
var _this = this; | ||
var url = event.url; | ||
if (url && url.indexOf('?') !== -1) { | ||
var params = url.split('?'); | ||
if (url && url.indexOf('#') !== -1) { | ||
var params = url.split('#'); | ||
var query_1 = {}; | ||
@@ -327,3 +352,3 @@ if (params.length === 2) { | ||
if ('code' in query_1) { | ||
query_1.client_id = "1"; | ||
query_1.client_id = '1'; | ||
query_1.grant_type = 'authorization_code'; | ||
@@ -335,6 +360,6 @@ query_1.code_verifier = this.loadCodeVerifier(); | ||
headers: { | ||
'Accept': 'application/json', | ||
'Content-type': 'application/json' | ||
Accept: 'application/json', | ||
'Content-type': 'application/json', | ||
}, | ||
method: 'POST' | ||
method: 'POST', | ||
}) | ||
@@ -344,8 +369,6 @@ .then(function (response) { return response.json(); }) | ||
_this.tokens = _this.captureTokens(data); | ||
// console.log(this.tokens); | ||
var uri = location.href; | ||
if (uri.indexOf('?') !== -1) { | ||
uri = uri.split('?')[0]; | ||
} | ||
window.history.replaceState({}, document.title, uri); | ||
location.hash = ''; | ||
}) | ||
.then(function (_) { | ||
_this.eventHandler({ type: exports.AuthEventType.READY }); | ||
}); | ||
@@ -356,3 +379,9 @@ } | ||
}; | ||
ObeliskAuth.prototype.setConfigDefaults = function (config) { | ||
ObeliskAuthClient.prototype.saveCodeVerifier = function (verifier) { | ||
sessionStorage.setItem('obeliskCodeVerifier', verifier); | ||
}; | ||
ObeliskAuthClient.prototype.loadCodeVerifier = function () { | ||
return sessionStorage.getItem('obeliskCodeVerifier'); | ||
}; | ||
ObeliskAuthClient.prototype.setConfigDefaults = function (config) { | ||
if (!config.redirectUri) { | ||
@@ -363,3 +392,3 @@ config.redirectUri = 'http://localhost:4200'; | ||
}; | ||
ObeliskAuth.prototype.getCodeChallenge = function () { | ||
ObeliskAuthClient.prototype.getCodeChallenge = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -384,3 +413,3 @@ var cr, randomOctets, codeVerifier, codeChallenge, _a; | ||
}; | ||
ObeliskAuth.prototype.digestMessage = function (message) { | ||
ObeliskAuthClient.prototype.digestMessage = function (message) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -400,15 +429,16 @@ var msgUint8, hashBuffer; | ||
}; | ||
ObeliskAuth.prototype.captureTokens = function (tokenResponse) { | ||
var idToken = JSON.parse(atob(tokenResponse.id_token.split('\.')[1])); | ||
ObeliskAuthClient.prototype.captureTokens = function (tokenResponse) { | ||
var myIdToken = JSON.parse(atob(tokenResponse.id_token.split('.')[1])); | ||
var res = { | ||
idTokenString: tokenResponse.id_token, | ||
accessToken: tokenResponse.token, | ||
'idToken': idToken | ||
idToken: myIdToken, | ||
expiresAt: Date.now() + tokenResponse.expires_in * 1000, | ||
}; | ||
return res; | ||
}; | ||
return ObeliskAuth; | ||
return ObeliskAuthClient; | ||
}()); | ||
exports.ObeliskAuth = ObeliskAuth; | ||
exports.ObeliskAuthClient = ObeliskAuthClient; | ||
//# sourceMappingURL=index.js.map |
@@ -10,2 +10,3 @@ export interface ObeliskConfig { | ||
accessToken?: any; | ||
expiresAt?: number; | ||
} |
{ | ||
"name": "@obelisk/auth", | ||
"version": "1.0.0", | ||
"version": "1.0.4", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
Sorry, the diff of this file is not supported yet
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
126713
11
909