@obelisk/client
Advanced tools
Comparing version 2.6.1 to 2.7.0
@@ -44,2 +44,3 @@ import { Observable, Observer } from 'rxjs'; | ||
prompt?: 'none' | 'login'; | ||
offline_access?: boolean; | ||
}): string; | ||
@@ -62,2 +63,3 @@ private storeClientCredentials; | ||
prompt?: 'none' | 'login'; | ||
offline_access?: boolean; | ||
}): void; | ||
@@ -64,0 +66,0 @@ /** |
@@ -91,2 +91,5 @@ "use strict"; | ||
params += '&scope=' + encodeURIComponent('openid'); | ||
if (loginOptions && loginOptions.offline_access) { | ||
params += encodeURIComponent(' offline_access'); | ||
} | ||
params += '&response_mode=fragment'; | ||
@@ -259,3 +262,3 @@ switch (this._options.flow) { | ||
const recentlyLoggedIn = this.isLoggedIn(); | ||
if (this._tokens.pat === undefined && recentlyLoggedIn) { | ||
if (this._tokens.pat === undefined && recentlyLoggedIn && !this._storage.get('logInfo').offline_token) { | ||
util_1.Logger.debug('No PAT and loggedIn in storage: Try to log in silently', 'AUTHN'); | ||
@@ -322,49 +325,74 @@ this.login({ prompt: 'none' }); | ||
} | ||
try { | ||
const authResponse = new auth_1.TokenResponse(url, this._options.flow); | ||
window.location.hash = ''; | ||
// Load in oauth state object if it is present | ||
this._oauth = this._storage.get('oauth', true); | ||
if (this._options.flow === 'implicit') { | ||
util_1.Logger.debug('Implicit flow', 'AUTHN'); | ||
const pat = new auth_1.Token(authResponse.access_token); | ||
const idtok = new auth_1.Token(authResponse.id_token); | ||
// check nonces | ||
let storeTokens = (resp, hasState, offlineLoginHandling) => { | ||
const authResponse = resp.response; | ||
const pat = new auth_1.Token(authResponse.access_token); | ||
const patRefresh = new auth_1.Token(authResponse.refresh_token); | ||
const idtok = new auth_1.Token(authResponse.id_token); | ||
// check nonces | ||
if (!offlineLoginHandling) { | ||
if (!this.isNonceValid(pat.getParsedToken().nonce)) { | ||
console.log('[IoT-CLIENT] Invalid nonce, clearing token'); | ||
this.clearTokens(); | ||
this.authOver$.next(); | ||
return rxjs_1.of(false); | ||
} | ||
if (!this.isNonceValid(patRefresh.getParsedToken().nonce)) { | ||
console.log('[IoT-CLIENT] Invalid nonce, clearing token'); | ||
this.clearTokens(); | ||
this.authOver$.next(); | ||
return rxjs_1.of(false); | ||
} | ||
if (!this.isNonceValid(idtok.getParsedToken().nonce)) { | ||
console.log('[IoT-CLIENT] Invalid nonce, clearing token'); | ||
this.clearTokens(); | ||
this.authOver$.next(); | ||
return rxjs_1.of(false); | ||
} | ||
// store in memory | ||
this._tokens.pat = pat; | ||
this._tokens.idtoken = idtok; | ||
} | ||
// store in memory | ||
this._tokens.pat = pat; | ||
this._tokens.patRefresh = patRefresh; | ||
this._tokens.idtoken = idtok; | ||
// If scope includes offline_access and refresh_expires_in=== 0 (store in localstorage to skip login next time) | ||
if (!offlineLoginHandling && authResponse.scope.split(' ').includes('offline_access') && authResponse.refresh_expires_in === 0) { | ||
this._storage.add('logInfo', { authenticated: true, expires: pat.getExpiresAt(), offline_token: patRefresh.getToken() }); | ||
} | ||
else { | ||
// store logged in + expiration | ||
this._storage.add('logInfo', { authenticated: true, expires: pat.getExpiresAt() }); | ||
this.authOver$.next(); | ||
return rxjs_1.of(true); | ||
} | ||
else if (this._options.flow === 'standard') { | ||
util_1.Logger.debug('Standard flow', 'AUTHN'); | ||
const tokenUrl = this._uma2Config.token_endpoint; | ||
const headers = { 'Content-Type': 'application/x-www-form-urlencoded' }; | ||
const hasState = authResponse.state || null; | ||
let params = `code=${authResponse.code}&grant_type=authorization_code`; | ||
let redUri = window.location.origin + window.location.pathname; | ||
// if (!this.isStateValid(authResponse.state!)) { | ||
// console.log('[IoT-CLIENT] Invalid state, clearing token') | ||
// this.clearTokens(); | ||
// this.authOver$.next(); | ||
// return of(false); | ||
// } | ||
params += '&client_id=' + encodeURIComponent(this._options.clientId); | ||
params += '&redirect_uri=' + redUri; | ||
return ajax_1.ajax.post(tokenUrl, params, headers).pipe(operators_1.flatMap(resp => { | ||
const authResponse = resp.response; | ||
// this.scheduleTokenRefresh(pat, patRefresh); | ||
this.authOver$.next(); | ||
if (hasState) { | ||
// If modern browser, insert querystring without reload | ||
if (history.pushState) { | ||
const newurl = window.location.protocol + "//" + window.location.host + window.location.pathname + decodeURIComponent(escape(atob(decodeURIComponent(hasState)))); | ||
window.history.pushState({ path: newurl }, '', newurl); | ||
} | ||
} | ||
return rxjs_1.of(true); | ||
}; | ||
try { | ||
/** OFFLINE TOKEN FOUND: remembered you */ | ||
const logInfo = this._storage.get('logInfo'); | ||
if (logInfo && logInfo.offline_token) { | ||
// console.log('OFFLINE TOKEN FOUND'); | ||
let offline_token = logInfo.offline_token; | ||
console.log(offline_token); | ||
const url = this._uma2Config.token_endpoint; | ||
const clientId = this._options.clientId; | ||
const headers = { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}; | ||
const params = `client_id=${clientId}&grant_type=refresh_token&refresh_token=${offline_token}`; | ||
return ajax_1.ajax.post(url, params, headers).pipe(operators_1.flatMap(resp => storeTokens(resp, null, true))); | ||
} | ||
else { | ||
const authResponse = new auth_1.TokenResponse(url, this._options.flow); | ||
window.location.hash = ''; | ||
// Load in oauth state object if it is present | ||
this._oauth = this._storage.get('oauth', true); | ||
if (this._options.flow === 'implicit') { | ||
util_1.Logger.debug('Implicit flow', 'AUTHN'); | ||
const pat = new auth_1.Token(authResponse.access_token); | ||
const patRefresh = new auth_1.Token(authResponse.refresh_token); | ||
const idtok = new auth_1.Token(authResponse.id_token); | ||
@@ -375,15 +403,7 @@ // check nonces | ||
this.clearTokens(); | ||
this.authOver$.next(); | ||
return rxjs_1.of(false); | ||
} | ||
if (!this.isNonceValid(patRefresh.getParsedToken().nonce)) { | ||
console.log('[IoT-CLIENT] Invalid nonce, clearing token'); | ||
this.clearTokens(); | ||
this.authOver$.next(); | ||
return rxjs_1.of(false); | ||
} | ||
if (!this.isNonceValid(idtok.getParsedToken().nonce)) { | ||
console.log('[IoT-CLIENT] Invalid nonce, clearing token'); | ||
this.clearTokens(); | ||
this.authOver$.next(); | ||
return rxjs_1.of(false); | ||
@@ -393,22 +413,24 @@ } | ||
this._tokens.pat = pat; | ||
this._tokens.patRefresh = patRefresh; | ||
this._tokens.idtoken = idtok; | ||
// store logged in + expiration | ||
this._storage.add('logInfo', { authenticated: true, expires: pat.getExpiresAt() }); | ||
// this.scheduleTokenRefresh(pat, patRefresh); | ||
this.authOver$.next(); | ||
if (hasState) { | ||
// If modern browser, insert querystring without reload | ||
if (history.pushState) { | ||
const newurl = window.location.protocol + "//" + window.location.host + window.location.pathname + decodeURIComponent(escape(atob(decodeURIComponent(hasState)))); | ||
window.history.pushState({ path: newurl }, '', newurl); | ||
} | ||
} | ||
return rxjs_1.of(true); | ||
})); | ||
} | ||
else if (this._options.flow === 'standard') { | ||
util_1.Logger.debug('Standard flow', 'AUTHN'); | ||
const tokenUrl = this._uma2Config.token_endpoint; | ||
const headers = { 'Content-Type': 'application/x-www-form-urlencoded' }; | ||
const hasState = authResponse.state || null; | ||
let params = `code=${authResponse.code}&grant_type=authorization_code`; | ||
let redUri = window.location.origin + window.location.pathname; | ||
params += '&client_id=' + encodeURIComponent(this._options.clientId); | ||
params += '&redirect_uri=' + redUri; | ||
return ajax_1.ajax.post(tokenUrl, params, headers).pipe(operators_1.flatMap(resp => storeTokens(resp, hasState, false))); | ||
} | ||
else { | ||
this.authOver$.next(); | ||
return rxjs_1.of(false); | ||
} | ||
} | ||
else { | ||
this.authOver$.next(); | ||
return rxjs_1.of(false); | ||
} | ||
} | ||
@@ -415,0 +437,0 @@ catch (err) { |
{ | ||
"name": "@obelisk/client", | ||
"version": "2.6.1", | ||
"version": "2.7.0", | ||
"description": "Typescript client to interact with Obelisk on a higher level than the regular ReST API calls.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
109618
2874